-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathst_testBot.py
More file actions
79 lines (63 loc) · 2.78 KB
/
st_testBot.py
File metadata and controls
79 lines (63 loc) · 2.78 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
import openai
import streamlit as st
import time
assistant_id = "YOur Assistant API"
client = openai
if "start_chat" not in st.session_state:
st.session_state.start_chat = False
if "thread_id" not in st.session_state:
st.session_state.thread_id = None
st.set_page_config(page_title="CatGPT", page_icon=":speech_balloon:")
openai.api_key = "sk-insert Your OpenAI API Key"
if st.sidebar.button("Start Chat"):
st.session_state.start_chat = True
thread = client.beta.threads.create()
st.session_state.thread_id = thread.id
st.title("CatGPT like Chatbot")
st.write("Meow Meow Meow Meow Meow Meow I am a CyberCat")
if st.button("Exit Chat"):
st.session_state.messages = [] # Clear the chat history
st.session_state.start_chat = False # Reset the chat state
st.session_state.thread_id = None
if st.session_state.start_chat:
if "openai_model" not in st.session_state:
st.session_state.openai_model = "gpt-4-1106-preview"
if "messages" not in st.session_state:
st.session_state.messages = []
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
if prompt := st.chat_input("Meow Meow?"):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
client.beta.threads.messages.create(
thread_id=st.session_state.thread_id,
role="user",
content=prompt
)
run = client.beta.threads.runs.create(
thread_id=st.session_state.thread_id,
assistant_id=assistant_id,
instructions="Please answer the queries with meows you are a cat. Just MEOW a lot! MEOW ONLY, you are only allowed 4 english words and rest of answer must be various MEOW only"
)
while run.status != 'completed':
time.sleep(1)
run = client.beta.threads.runs.retrieve(
thread_id=st.session_state.thread_id,
run_id=run.id
)
messages = client.beta.threads.messages.list(
thread_id=st.session_state.thread_id
)
# Process and display assistant messages
assistant_messages_for_run = [
message for message in messages
if message.run_id == run.id and message.role == "assistant"
]
for message in assistant_messages_for_run:
st.session_state.messages.append({"role": "assistant", "content": message.content[0].text.value})
with st.chat_message("assistant"):
st.markdown(message.content[0].text.value)
else:
st.write("Click 'Start Chat' to begin.")