-
Notifications
You must be signed in to change notification settings - Fork 15
/
examples.py
229 lines (183 loc) · 7.4 KB
/
examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import openai
import streamlit as st
def _submit_feedback(user_response, emoji=None):
st.toast(f"Feedback submitted: {user_response}", icon=emoji)
return user_response.update({"some metadata": 123})
def chatbot_thumbs_app(streamlit_feedback, debug=False):
st.title("💬 Chatbot")
with st.sidebar:
openai_api_key = st.text_input(
"OpenAI API Key",
key="chatbot_api_key",
type="password",
value=st.secrets.get("OPENAI_API_KEY"),
)
if "messages" not in st.session_state:
st.session_state["messages"] = [
{"role": "assistant", "content": "How can I help you?"}
]
messages = st.session_state.messages
for n, msg in enumerate(messages):
st.chat_message(msg["role"]).write(msg["content"])
if msg["role"] == "assistant" and n > 1:
feedback_key = f"feedback_{int(n/2)}"
if feedback_key not in st.session_state:
st.session_state[feedback_key] = None
streamlit_feedback(
feedback_type="thumbs",
optional_text_label="Please provide extra information",
on_submit=_submit_feedback,
key=feedback_key,
)
if prompt := st.chat_input():
messages.append({"role": "user", "content": prompt})
st.chat_message("user").write(prompt)
if debug:
st.session_state["response"] = "dummy response"
else:
if not openai_api_key:
st.info("Please add your OpenAI API key to continue.")
st.stop()
else:
openai.api_key = openai_api_key
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo", messages=messages
)
st.session_state["response"] = response.choices[0].message.content
with st.chat_message("assistant"):
messages.append(
{"role": "assistant", "content": st.session_state["response"]}
)
st.write(st.session_state["response"])
st.rerun()
def single_prediction_faces_app(streamlit_feedback, debug=False):
st.title("LLM User Feedback with Trubrics")
if "response" not in st.session_state:
st.session_state["response"] = ""
if "feedback_key" not in st.session_state:
st.session_state.feedback_key = 0
with st.sidebar:
openai_api_key = st.text_input(
"OpenAI API Key",
key="chatbot_api_key",
type="password",
value=st.secrets.get("OPENAI_API_KEY"),
)
if not openai_api_key:
st.info("Please add your OpenAI API key to continue.")
st.stop()
else:
openai.api_key = openai_api_key
prompt = st.text_area(
label="Prompt",
label_visibility="collapsed",
placeholder="What would you like to know?",
)
button = st.button(f"Ask text-davinci-002")
if button:
if debug:
st.session_state["response"] = "dummy response: " + prompt.strip()
else:
st.session_state["response"] = openai.Completion.create(
model="text-davinci-002", prompt=prompt, temperature=0.5, max_tokens=200
)
st.session_state["response"] = (
st.session_state["response"].choices[0].text.replace("\n", "")
)
st.session_state.feedback_key += 1 # overwrite feedback component
if st.session_state["response"]:
st.markdown(f"#### :violet[{st.session_state['response']}]")
streamlit_feedback(
feedback_type="faces",
optional_text_label="Please provide extra information",
align="flex-start",
on_submit=_submit_feedback,
key=f"feedback_{st.session_state.feedback_key}",
)
def basic_app(streamlit_feedback, debug):
st.title("Component demo")
if "feedback_key" not in st.session_state:
st.session_state.feedback_key = 0
st.button("Random button")
if st.button("Refresh feedback component"):
st.session_state.feedback_key += 1 # overwrite feedback component
multiline = st.toggle("Multiline", value=False)
if multiline:
feedback = streamlit_feedback(
feedback_type="faces",
# on_submit=_submit_feedback,
key=f"feedback_{st.session_state.feedback_key}",
optional_text_label="Please provide some more information",
max_text_length=500,
args=["✅"],
)
else:
feedback = streamlit_feedback(
feedback_type="faces",
# on_submit=_submit_feedback,
key=f"feedback_{st.session_state.feedback_key}",
optional_text_label="Please provide some more information",
args=["✅"],
)
if feedback:
st.write(":orange[Component output:]")
st.write(feedback)
def bare_bones_app(streamlit_feedback, debug):
feedback = streamlit_feedback(feedback_type="faces", on_submit=_submit_feedback)
if feedback:
st.write(":orange[Component output:]")
st.write(feedback)
def streaming_chatbot(streamlit_feedback, debug):
st.title("💬 Streaming Chatbot")
openai.api_key = st.secrets["OPENAI_API_KEY"]
if "openai_model" not in st.session_state:
st.session_state["openai_model"] = "gpt-3.5-turbo"
if "messages" not in st.session_state:
st.session_state.messages = []
if "feedback_key" not in st.session_state:
st.session_state.feedback_key = 0
feedback_kwargs = {
"feedback_type": "thumbs",
"optional_text_label": "Please provide extra information",
"on_submit": _submit_feedback,
}
for n, msg in enumerate(st.session_state.messages):
st.chat_message(msg["role"]).write(msg["content"])
if msg["role"] == "assistant" and n > 1:
feedback_key = f"feedback_{int(n/2)}"
if feedback_key not in st.session_state:
st.session_state[feedback_key] = None
disable_with_score = (
st.session_state[feedback_key].get("score")
if st.session_state[feedback_key]
else None
)
streamlit_feedback(
**feedback_kwargs,
key=feedback_key,
disable_with_score=disable_with_score,
)
if prompt := st.chat_input("What is up?"):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
with st.chat_message("assistant"):
message_placeholder = st.empty()
full_response = ""
for response in openai.ChatCompletion.create(
model=st.session_state["openai_model"],
messages=[
{"role": m["role"], "content": m["content"]}
for m in st.session_state.messages
],
stream=True,
):
full_response += response.choices[0].delta.get("content", "")
message_placeholder.markdown(full_response + "▌")
message_placeholder.markdown(full_response)
st.session_state.messages.append(
{"role": "assistant", "content": full_response}
)
streamlit_feedback(
**feedback_kwargs, key=f"feedback_{int(len(st.session_state.messages)/2)}"
)