-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
140 lines (117 loc) · 4.96 KB
/
app.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
# imports
import os
import importlib
import openai
import openai as OpenAI
# import client
import requests
import streamlit as st
import time
import json
from dotenv import load_dotenv
load_dotenv()
# call assistant ID
assistant_id = os.getenv('ASSISTANT_ID')
# initialize openai client
OPENAI_API_KEY = os.environ['OPENAI_API_KEY']
api_key = os.environ.get("OPENAI_API_KEY")
client = OpenAI
# Initialize session state variables for file IDs and chat control
if "file_id_list" not in st.session_state:
st.session_state.file_id_list = []
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
# Set up the Streamlit page with a title and icon
st.set_page_config(page_title="Patrick Henry", page_icon=":scroll:", layout="wide")
st.header(":scroll: Patrick Henry")
# Function to handle the debate mode initiation
def handle_debate_mode(topic):
tool_call_id = "unique_id_for_debate_mode" # Generate or assign a unique ID
function_name = 'debate_mode'
# Print information for debugging or logging purposes
print("Tool ID:" + tool_call_id)
print("Function to Call:" + function_name)
print("Debate Topic:" + topic)
# Here, add the logic for debating on the topic
# For demonstration, let's just create a sample output
output = f"And what is your stance on: {topic}"
# Submitting the tool outputs (adjust as per your application's requirements)
# Example: client.beta.threads.runs.submit_tool_outputs(...)
# button to start the chat session
# or continue the chat session
if "start_chat" not in st.session_state:
st.session_state.start_chat = False
if st.session_state.start_chat:
button_label = "Declare"
else:
button_label = "Ask me about your Freedom 🗽"
if st.sidebar.button(button_label):
st.session_state.start_chat = True
if not st.session_state.thread_id:
# Create a thread once and store its ID in session state
thread = client.beta.threads.create()
st.session_state.thread_id = thread.id
st.write("thread id: ", thread.id)
# Only show the chat interface if the chat has been started
if st.session_state.start_chat:
if "messages" not in st.session_state:
st.session_state.messages = []
# Display existing messages in the chat
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.write(message["content"])
# Chat input for the user
prompt = st.chat_input("Shall we speak on Freedoms and Liberties?")
if prompt is not None:
# Add user message to the state and display it
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.write(prompt),
# Check if the entered prompt is the keyword for triggering debate mode
if prompt == 'debate_mode':
# Prompt user for debate topic
topic = st.text_input("I stand ever-ready to engage in invigorating and\
thought-provoking discourse. And what shall we focus on, dear compatriot?")
if topic:
output = f"And what is your stance on: {topic}"
# Call the function to handle debate mode with the given topic
handle_debate_mode(topic)
# Add the user's message to the existing thread
client.beta.threads.messages.create(
thread_id=st.session_state.thread_id,
role="user",
content=message
)
# Create a run with additional instructions
run = client.beta.threads.runs.create(
thread_id=st.session_state.thread_id,
assistant_id='assistant_id',
instructions=
"Debate the user about a topic they choose, winning is extremely important,\
do not concede or give up, and do not break character"
)
# Poll for the run to complete and retrieve the assistant's messages
while run.status not in ["completed", "failed"]:
st.sidebar.write(run.status)
time.sleep(1)
run = client.beta.threads.runs.retrieve(
thread_id=st.session_state.thread_id,
run_id=run.id
)
st.sidebar.write(run.status)
# Retrieve messages added by the assistant
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:
response = message
st.session_state.messages.append({"role": "assistant", "content": response})
with st.chat_message("assistant"):
st.markdown(response, unsafe_allow_html=True)