-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathchat_buttons_alt.py
213 lines (160 loc) · 6.94 KB
/
chat_buttons_alt.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
import json
import logging
import streamlit as st # 1.34.0
import time
import tiktoken
import os
import sys
from dotenv import load_dotenv, find_dotenv
from datetime import datetime
from streamlit_float import *
st.set_page_config(page_title="Streamlit Chat Interface Improvement",
page_icon="🤩")
# initialize float feature/capability
float_init()
from openai import OpenAI # 1.30.1
# from openai import AzureOpenAI # 1.30.1
logger = logging.getLogger()
logging.basicConfig(encoding="UTF-8", level=logging.INFO)
st.title("🤩 Improved Streamlit Chat UI")
# Secrets to be stored in /.streamlit/secrets.toml
# OPENAI_API_ENDPOINT = "https://xxx.openai.azure.com/"
# OPENAI_API_KEY = "efpgishhn2kwlnk9928avd6vrh28wkdj" (this is a fake key 😉)
# To be used with standard OpenAI API
# client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
# Openai local configuration
load_dotenv(find_dotenv())
try:
env_openai_key = os.getenv("OPENAI_API_KEY_A")
except:
sys.stdout.write("No local variables containing OpenAI API credentials found.")
client = OpenAI(api_key = env_openai_key)
# # To be used with standard Azure OpenAI API
# client = AzureOpenAI(
# azure_endpoint=st.secrets["OPENAI_API_ENDPOINT"],
# api_key=st.secrets["OPENAI_API_KEY"],
# api_version="2024-02-15-preview",
# )
# This function logs the last question and answer in the chat messages
def log_feedback(icon):
# We display a nice toast
st.toast("Thanks for your feedback!", icon="👌")
# We retrieve the last question and answer
last_messages = json.dumps(st.session_state["messages"][-2:])
# We record the timestamp
activity = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + ": "
# And include the messages
activity += "positive" if icon == "👍" else "negative"
activity += ": " + last_messages
# And log everything
logger.info(activity)
# Model Choice - Name to be adapter to your deployment
if "openai_model" not in st.session_state:
st.session_state["openai_model"] = "gpt-3.5-turbo"
# Adapted from https://docs.streamlit.io/develop/tutorials/llms/build-conversational-apps
if "messages" not in st.session_state:
st.session_state["messages"] = []
user_avatar = "👩💻"
assistant_avatar = "🤖"
# In case of rerun of the last question, we remove the last answer from st.session_state["messages"]
if "rerun" in st.session_state and st.session_state["rerun"]:
st.session_state["messages"].pop(-1)
# We rebuild the previous conversation stored in st.session_state["messages"] with the corresponding emojis
for message in st.session_state["messages"]:
with st.chat_message(
message["role"],
avatar=assistant_avatar if message["role"] == "assistant" else user_avatar,
):
st.markdown(message["content"])
# A chat input will add the corresponding prompt to the st.session_state["messages"]
if prompt := st.chat_input("How can I help you?"):
st.session_state["messages"].append({"role": "user", "content": prompt})
# and display it in the chat history
with st.chat_message("user", avatar=user_avatar):
st.markdown(prompt)
# If the prompt is initialized or if the user is asking for a rerun, we
# launch the chat completion by the LLM
if prompt or ("rerun" in st.session_state and st.session_state["rerun"]):
with st.chat_message("assistant", avatar=assistant_avatar):
stream = client.chat.completions.create(
model=st.session_state["openai_model"],
messages=[
{"role": m["role"], "content": m["content"]}
for m in st.session_state["messages"]
],
stream=True,
max_tokens=300, # Limited to 300 tokens for demo purposes
)
response = st.write_stream(stream)
st.session_state["messages"].append({"role": "assistant", "content": response})
# In case this is a rerun, we set the "rerun" state back to False
if "rerun" in st.session_state and st.session_state["rerun"]:
st.session_state["rerun"] = False
st.write("")
st.write("")
# If there is at least one message in the chat, we display the options
if len(st.session_state["messages"]) > 0:
# action_buttons_container = st.container()
# with action_buttons_container:
# float_parent("bottom: 6.9rem;background-color: var(--default-backgroundColor); padding-top: 0.9rem;")
# We set the space between the icons thanks to a share of 100
# cols_dimensions = [7, 19.4, 19.3, 9, 8.6, 8.6, 28.1]
# cols_dimensions = [7, 19.4, 19.3, 9, 8.6, 8.6, 28.1]
con1 = st.container()
con2 = st.container()
# col0, col1, col2, col3, col4, col5, col6 = action_buttons_container.columns(cols_dimensions)
with con1:
float_parent("bottom: 6.9rem;background-color: var(--default-backgroundColor); padding-top: 0.9rem;")
# Converts the list of messages into a JSON Bytes format
json_messages = json.dumps(st.session_state["messages"]).encode("utf-8")
# And the corresponding Download button
st.download_button(
label="📥 Save chat!",
data=json_messages,
file_name="chat_conversation.json",
mime="application/json",
)
with con2:
css_selector = float_parent("margin-left: 8rem;bottom: 6.9rem;background-color: var(--default-backgroundColor); padding-top: 0.9rem;")
# We set the message back to 0 and rerun the app
# (this part could probably be improved with the cache option)
if st.button("Clear Chat 🧹"):
st.session_state["messages"] = []
st.rerun()
# with col3:
# icon = "🔁"
# if st.button(icon):
# st.session_state["rerun"] = True
# st.rerun()
# with col4:
# icon = "👍"
# # The button will trigger the logging function
# if st.button(icon):
# log_feedback(icon)
# with col5:
# icon = "👎"
# # The button will trigger the logging function
# if st.button(icon):
# log_feedback(icon)
# with col6:
# # We initiate a tokenizer
# enc = tiktoken.get_encoding("cl100k_base")
# # We encode the messages
# tokenized_full_text = enc.encode(
# " ".join([item["content"] for item in st.session_state["messages"]])
# )
# # And display the corresponding number of tokens
# label = f"💬 {len(tokenized_full_text)} tokens"
# st.link_button(label, "https://platform.openai.com/tokenizer")
else:
# At the first run of a session, we temporarly display a message
if "disclaimer" not in st.session_state:
with st.empty():
for seconds in range(3):
st.warning(
" You can click on 👍 or 👎 to provide feedback regarding the quality of responses.",
icon="💡",
)
time.sleep(1)
st.write("")
st.session_state["disclaimer"] = True