Skip to content

Commit 85cc4e6

Browse files
Embedded apps for chat tutorials (#1218)
* Add chat tutorial app sources * Update tutorial-chat-feedback.py * Update chat app sources
1 parent f04fc75 commit 85cc4e6

File tree

3 files changed

+193
-0
lines changed

3 files changed

+193
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lorem
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import streamlit as st
2+
import time
3+
4+
5+
def chat_stream(prompt):
6+
response = f'You said, "{prompt}" ...interesting.'
7+
for char in response:
8+
yield char
9+
time.sleep(0.02)
10+
11+
12+
def save_feedback(index):
13+
st.session_state.history[index]["feedback"] = st.session_state[f"feedback_{index}"]
14+
15+
16+
if "history" not in st.session_state:
17+
st.session_state.history = []
18+
19+
for i, message in enumerate(st.session_state.history):
20+
with st.chat_message(message["role"]):
21+
st.write(message["content"])
22+
if message["role"] == "assistant":
23+
feedback = message.get("feedback", None)
24+
st.session_state[f"feedback_{i}"] = feedback
25+
st.feedback(
26+
"thumbs",
27+
key=f"feedback_{i}",
28+
disabled=feedback is not None,
29+
on_change=save_feedback,
30+
args=[i],
31+
)
32+
33+
if prompt := st.chat_input("Say something"):
34+
with st.chat_message("user"):
35+
st.write(prompt)
36+
st.session_state.history.append({"role": "user", "content": prompt})
37+
with st.chat_message("assistant"):
38+
response = st.write_stream(chat_stream(prompt))
39+
st.feedback(
40+
"thumbs",
41+
key=f"feedback_{len(st.session_state.history)}",
42+
on_change=save_feedback,
43+
args=[len(st.session_state.history)],
44+
)
45+
st.session_state.history.append({"role": "assistant", "content": response})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
import streamlit as st
2+
import lorem
3+
from random import randint
4+
import time
5+
6+
if "stage" not in st.session_state:
7+
st.session_state.stage = "user"
8+
st.session_state.history = []
9+
st.session_state.pending = None
10+
st.session_state.validation = {}
11+
12+
13+
def chat_stream():
14+
for i in range(randint(3, 9)):
15+
yield lorem.sentence() + " "
16+
time.sleep(0.2)
17+
18+
19+
def validate(response):
20+
response_sentences = response.split(". ")
21+
response_sentences = [
22+
sentence.strip(". ") + "."
23+
for sentence in response_sentences
24+
if sentence.strip(". ") != ""
25+
]
26+
validation_list = [
27+
True if sentence.count(" ") > 4 else False for sentence in response_sentences
28+
]
29+
return response_sentences, validation_list
30+
31+
32+
def add_highlights(response_sentences, validation_list, bg="red", text="red"):
33+
return [
34+
f":{text}[:{bg}-background[" + sentence + "]]" if not is_valid else sentence
35+
for sentence, is_valid in zip(response_sentences, validation_list)
36+
]
37+
38+
39+
for message in st.session_state.history:
40+
with st.chat_message(message["role"]):
41+
st.write(message["content"])
42+
43+
if st.session_state.stage == "user":
44+
if user_input := st.chat_input("Enter a prompt"):
45+
st.session_state.history.append({"role": "user", "content": user_input})
46+
with st.chat_message("user"):
47+
st.write(user_input)
48+
with st.chat_message("assistant"):
49+
response = st.write_stream(chat_stream())
50+
st.session_state.pending = response
51+
st.session_state.stage = "validate"
52+
st.rerun()
53+
54+
elif st.session_state.stage == "validate":
55+
st.chat_input("Accept, correct, or rewrite the answer above.", disabled=True)
56+
response_sentences, validation_list = validate(st.session_state.pending)
57+
highlighted_sentences = add_highlights(response_sentences, validation_list)
58+
with st.chat_message("assistant"):
59+
st.markdown(" ".join(highlighted_sentences))
60+
st.divider()
61+
cols = st.columns(3)
62+
if cols[0].button(
63+
"Correct errors", type="primary", disabled=all(validation_list)
64+
):
65+
st.session_state.validation = {
66+
"sentences": response_sentences,
67+
"valid": validation_list,
68+
}
69+
st.session_state.stage = "correct"
70+
st.rerun()
71+
if cols[1].button("Accept"):
72+
st.session_state.history.append(
73+
{"role": "assistant", "content": st.session_state.pending}
74+
)
75+
st.session_state.pending = None
76+
st.session_state.validation = {}
77+
st.session_state.stage = "user"
78+
st.rerun()
79+
if cols[2].button("Rewrite answer", type="tertiary"):
80+
st.session_state.stage = "rewrite"
81+
st.rerun()
82+
83+
elif st.session_state.stage == "correct":
84+
st.chat_input("Accept, correct, or rewrite the answer above.", disabled=True)
85+
response_sentences = st.session_state.validation["sentences"]
86+
validation_list = st.session_state.validation["valid"]
87+
highlighted_sentences = add_highlights(
88+
response_sentences, validation_list, "gray", "gray"
89+
)
90+
if not all(validation_list):
91+
focus = validation_list.index(False)
92+
highlighted_sentences[focus] = ":red[:red" + highlighted_sentences[focus][11:]
93+
else:
94+
focus = None
95+
with st.chat_message("assistant"):
96+
st.markdown(" ".join(highlighted_sentences))
97+
st.divider()
98+
if focus is not None:
99+
new_sentence = st.text_input(
100+
"Replacement text:", value=response_sentences[focus]
101+
)
102+
cols = st.columns(2)
103+
if cols[0].button(
104+
"Update", type="primary", disabled=len(new_sentence.strip()) < 1
105+
):
106+
st.session_state.validation["sentences"][focus] = (
107+
new_sentence.strip(". ") + "."
108+
)
109+
st.session_state.validation["valid"][focus] = True
110+
st.session_state.pending = " ".join(
111+
st.session_state.validation["sentences"]
112+
)
113+
st.rerun()
114+
if cols[1].button("Remove"):
115+
st.session_state.validation["sentences"].pop(focus)
116+
st.session_state.validation["valid"].pop(focus)
117+
st.session_state.pending = " ".join(
118+
st.session_state.validation["sentences"]
119+
)
120+
st.rerun()
121+
else:
122+
cols = st.columns(2)
123+
if cols[0].button("Accept", type="primary"):
124+
st.session_state.history.append(
125+
{"role": "assistant", "content": st.session_state.pending}
126+
)
127+
st.session_state.pending = None
128+
st.session_state.validation = {}
129+
st.session_state.stage = "user"
130+
st.rerun()
131+
if cols[1].button("Re-validate"):
132+
st.session_state.validation = {}
133+
st.session_state.stage = "validate"
134+
st.rerun()
135+
136+
elif st.session_state.stage == "rewrite":
137+
st.chat_input("Accept, correct, or rewrite the answer above.", disabled=True)
138+
with st.chat_message("assistant"):
139+
new = st.text_area("Rewrite the answer", value=st.session_state.pending)
140+
if st.button(
141+
"Update", type="primary", disabled=new is None or new.strip(". ") == ""
142+
):
143+
st.session_state.history.append({"role": "assistant", "content": new})
144+
st.session_state.pending = None
145+
st.session_state.validation = {}
146+
st.session_state.stage = "user"
147+
st.rerun()

0 commit comments

Comments
 (0)