-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
207 lines (137 loc) · 7.25 KB
/
main.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
import streamlit as st
import os
import openai
import time
from datetime import datetime
import json
from schema import Agent
from schema import LlmModelType, get_completion_from_messages
from schema import TwoAgentsSettings
from utility.file_import_export import create_download_link
def main():
# set_api_key("e78bc29cdc5b72d0760c84e57078786c")
# audio = generate(
# text="Hi! My name is Bella, nice to meet you!",
# voice="Bella",
# model='eleven_monolingual_v1'
# )
#st.audio(data=audio)
openai.api_key=st.secrets["OPENAI_API_KEY"]
aiInstructions = """
Conduct a casual conversation with a construction worker named {{User.FirstName}} in order to determine how well he/she can identify potential hazards on a construction scenario described in the “Jobsite Scenario” section of the content enclosed in ###Content below. He has already read a description of this scenario.
Ask him up to 3 questions in order to determine if he can identify the potential hazards in this scenario. Your questions should be only on this scenario.
Ask him for clarification if you don’t understand his answer. Don’t provide any feedback to him after each question. Just acknowledge his answer and move on.
Use the material described in the “Sample Fatality Investigation Report” section of the content enclosed in ###Content below to formulate your questions. Don’t make any explicit references to the fatality report.
Don’t allow the conversation to deviate from the topic of this scenario. Don’t number your questions.
#IF {{previousSessions}} == true
Base your conversation on the previous conversation and evaluation of those conversations enclosed in <pre> tags. It should naturally flow from what was covered before. Always ask one question at the time.
<pre>
{{Simulation.PreviousSessions}}
</pre>
#ENDIF
###Content
{{AI.ReferenceContent}}
###
"""
st.subheader("Surge9 AI Playground")
#write aiInstructions to file
with open("aiInstructions.txt", "w") as text_file:
text_file.write(aiInstructions)
with open("aiInstructions.txt", "r") as text_file:
aiInstructions = text_file.read()
st.write(aiInstructions)
def twoAgentTab():
st.subheader('Two Agent Conversation@')
settings = TwoAgentsSettings()
with st.sidebar:
uploaded_file = st.file_uploader("Import settings")
if uploaded_file is not None:
bytes_data = uploaded_file.read()
settings = TwoAgentsSettings.parse_raw(bytes_data)
# UI for agents
for agent in [settings.agent1 , settings.agent2]:
with st.expander(agent.title):
agent.title = st.text_input('Title', agent.title , key=agent.title)
agent.role = st.text_area('Role Description', agent.role, height=400)
if agent.first_message:
agent.first_message = st.text_area('First Message', agent.first_message, height=100)
#UI of other settings
settings.temperature = st.slider("Temperature", 0.0 ,1.0 ,settings.temperature)
model_names = [enum.value for enum in LlmModelType]
model_name = st.selectbox('Model', model_names, index=model_names.index(settings.llm_model_type.value))
selected_model = LlmModelType(model_name)
settings.number_of_turns = st.number_input("Number of exchanges" , settings.number_of_turns , 10)
start = st.button("Start")
settings.llm_model_type = selected_model
download_settings = create_download_link(settings.json(), 'settings.json', 'Click here to download settings')
st.markdown(download_settings, unsafe_allow_html=True)
messages = [
{'role':'system', 'content': settings.agent2.role},
{'role':'user', 'content': settings.agent1.first_message},
]
if start:
total_cost = 0
total_seconds = 0
st.write(f"**{settings.agent1.title}**")
st.write(messages[1]["content"])
for i in range(1, settings.number_of_turns):
st.markdown(f"**{settings.agent1.title if i%2 == 0 else settings.agent2.title}**")
#st.write(messages)
start_time = time.time()
with st.spinner('...'):
try:
response , usage = get_completion_from_messages(messages, temperature=settings.temperature , model=selected_model)
except openai.error.Timeout as e:
st.error(f"OpenAI API request timed out: {e}")
break
except openai.error.APIError as e:
st.error(f"OpenAI API returned an API Error: {e}")
break
except openai.error.APIConnectionError as e:
st.error(f"OpenAI API request failed to connect: {e}")
break
except openai.error.InvalidRequestError as e:
st.error(f"OpenAI API request was invalid: {e}")
break
except openai.error.AuthenticationError as e:
st.error(f"OpenAI API request was not authorized: {e}")
break
except openai.error.PermissionError as e:
st.error(f"OpenAI API request was not permitted: {e}")
break
except openai.error.RateLimitError as e:
st.error(f"OpenAI API request exceeded rate limit: {e}")
break
except Exception as e:
st.error(f"OpenAI API: {e}")
break
end_time = time.time()
execution_time = end_time - start_time
total_seconds += execution_time
st.write(response)
cost = selected_model.cost(usage)
total_cost += cost
st.write(f'*{round(execution_time, 2)} sec , {round(cost, 2)} cents*')
messages.append({'role':'assistant' if i%2 == 0 else "user" , 'content' : response })
#switch roles
new_messages = [{'role' :'system' , 'content' : settings.agent2.role if i%2 == 0 else settings.agent1.role}]
for j in range(1,len(messages)):
new_message = {'role':'user' if i%2 == 0 else "assistant" , 'content' : messages[j]['content'] }
new_messages.append(new_message)
messages = new_messages
#total cost and time
minutes, seconds = divmod(total_seconds, 60)
time_format = f"{minutes:.0f}:{seconds:02.0f}"
st.write(f'**total cost : {round(total_cost,2)} cents** in *{time_format} seconds*')
#download
download_str = ""
for i in range(1,len(messages)):
role = settings.agent2.title if i%2 == 0 else settings.agent1.title
download_str += f'''{role} :
{messages[i]['content']}
'''
filename = 'two agent' + datetime.now().strftime("%Y-%m-%d %H:%M:%S") + '.txt'
download_link = create_download_link(download_str, filename, 'Click here to download the conversation')
st.markdown(download_link, unsafe_allow_html=True)
if __name__ == '__main__':
main()