-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
215 lines (162 loc) ยท 6.35 KB
/
app.py
File metadata and controls
215 lines (162 loc) ยท 6.35 KB
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
import json
import time
import streamlit as st
import plotly.graph_objects as go
from openai import OpenAI
import toml
#######################################
# PREREQUISITES
#######################################
# Load custom secrets
try:
with open("api_secrets.toml", "r") as f:
secrets = toml.load(f)
except FileNotFoundError:
secrets = {
"openai": {
"api_key": st.secrets["OPENAI_API_KEY"],
"assistant_id": st.secrets["OPENAI_ASSISTANT_ID"]
}
}
#current is 0.9v continue..
st.set_page_config(
page_title="Wanderlust",
page_icon="๐บ๏ธ",
layout="wide",
initial_sidebar_state="collapsed",
)
api_key_secret = secrets["openai"]["api_key"]
# Setting OpenAI API key and Assistant ID
client = OpenAI(api_key=api_key_secret)
# Use the assistant_id from the custom secrets
assistant_id = secrets["openai"]["assistant_id"]
assistant_state = "assistant"
thread_state = "thread"
conversation_state = "conversation"
last_openai_run_state = "last_openai_run"
user_msg_input_key = "input_user_msg"
#######################################
# SESSION STATE SETUP
#######################################
# ์ฌ์ด๋๋ฐ ์ค์
with st.sidebar:
st.title("TVCF ๊ณ ๊ฐ์ผํฐ")
# assistant_id์ thread_id ์ด๊ธฐํ
if 'assistant_id' not in st.session_state:
st.session_state.assistant_id = assistant_id
if 'thread_id' not in st.session_state:
st.session_state.thread_id = None
if 'thread_messages' not in st.session_state:
st.session_state.thread_messages = []
if 'user_message' not in st.session_state:
st.session_state.user_message = ""
# Assistant ID ์์ฑ ๋ฒํผ ์๋ ๋ฒ์
# if st.button("์๋ก์ด Assistant ID ์์ฑ"):
# assistant = client.beta.assistants.create(
# name="Math Tutor",
# instructions="A personal math tutor that answers math problems. Write and execute code to answer math questions.",
# tools=[{"type": "code_interpreter"}],
# model="gpt-4-1106-preview"
# )
# st.session_state.assistant_id = assistant.id
# st.success(f"Assistant ID created: {assistant.id}")
# # Thread ID ์์ฑ ๋ฒํผ ์๋ ๋ฒ์
# if st.button("์๋ก์ด Thread ID ์์ฑ"):
# thread = client.beta.threads.create()
# st.session_state.thread_id = thread.id
# st.success(f"Thread ID created: {thread.id}")
thread_id = st.text_input("Thread ID")
thread_btn = st.button("Create a new thread")
if thread_btn:
thread = client.beta.threads.create()
thread_id = thread.id
st.subheader(f"Thread ID: {thread_id}" , divider="rainbow")
st.info("You can use this thread ID to continue the conversation later.")
# ๋ฉ์ธ ์ฑํ
์์ญ ์ค์
if assistant_state not in st.session_state or st.session_state[assistant_state] is None:
st.session_state[assistant_state] = client.beta.assistants.retrieve(assistant_id)
if thread_state not in st.session_state or st.session_state[thread_state] is None:
st.session_state[thread_state] = client.beta.threads.create()
st.session_state.thread_id = st.session_state[thread_state].id
if conversation_state not in st.session_state:
st.session_state[conversation_state] = []
if last_openai_run_state not in st.session_state:
st.session_state[last_openai_run_state] = None
def get_assistant_id():
return st.session_state.assistant_id
def get_thread_id():
if st.session_state.thread_id is None:
st.session_state.thread_id = client.beta.threads.create().id
return st.session_state.thread_id
def get_run_id():
return st.session_state[last_openai_run_state].id
def on_text_input(status_placeholder):
"""Callback method for any chat_input value change"""
if st.session_state[user_msg_input_key] == "":
return
client.beta.threads.messages.create(
thread_id=get_thread_id(),
role="user",
content=st.session_state[user_msg_input_key],
)
st.session_state[last_openai_run_state] = client.beta.threads.runs.create(
assistant_id=get_assistant_id(),
thread_id=get_thread_id(),
)
completed = False
# Polling
with status_placeholder.status("Computing Assistant answer") as status_container:
st.write(f"Launching run {get_run_id()}")
while not completed:
run = client.beta.threads.runs.retrieve(
thread_id=get_thread_id(),
run_id=get_run_id(),
)
if run.status == "requires_action":
tools_output = []
st.write(f"Will submit {tools_output}")
client.beta.threads.runs.submit_tool_outputs(
thread_id=get_thread_id(),
run_id=get_run_id(),
tool_outputs=tools_output,
)
if run.status == "completed":
st.write(f"Completed run {get_run_id()}")
status_container.update(label="Assistant is done", state="complete")
completed = True
else:
time.sleep(0.1)
messages = client.beta.threads.messages.list(get_thread_id()).data
st.session_state[conversation_state] = [
(m.role, m.content[0].text.value.replace("ใ", "").replace("โ sourceใ", ""))
for m in messages
]
def on_reset_thread():
client.beta.threads.delete(get_thread_id())
st.session_state[thread_state] = client.beta.threads.create()
st.session_state.thread_id = st.session_state[thread_state].id
st.session_state[conversation_state] = []
st.session_state[last_openai_run_state] = None
#######################################
# SIDEBAR
#######################################
# with st.sidebar:
# st.header("Debug")
# st.write(st.session_state.to_dict())
# st.button("Reset Thread", on_click=on_reset_thread)
#######################################
# MAIN
#######################################
st.title("TVCF ๊ณ ๊ฐ์ผํฐ")
st.subheader(':blue[๋ฌด์์ด๋ ๋ฌผ์ด๋ณด์ธ์!] :sunglasses:')
with st.container():
for role, message in reversed(st.session_state[conversation_state]):
with st.chat_message(role):
st.write(message)
status_placeholder = st.empty()
st.chat_input(
placeholder="Ask your question here",
key=user_msg_input_key,
on_submit=on_text_input,
args=(status_placeholder,),
)