hanoch.rahimi@gmail commited on
Commit
7c5594b
·
1 Parent(s): 45a7d81
Files changed (3) hide show
  1. app.py +5 -2
  2. openai_utils.py +20 -7
  3. utils.py +4 -3
app.py CHANGED
@@ -178,8 +178,10 @@ def run_query(query, report_type, top_k , regions, countries, is_debug, index_na
178
  if not messages is None:
179
  with content_container:
180
  for message in list(messages)[::-1]:
181
- with st.chat_message(name = message.role):
182
- st.write(message.content[0].text.value)
 
 
183
  # if message.role == "assistant":
184
  # with st.chat_message(name = message.role, avatar = st.image("resources/raized_logo.png")):
185
  # st.write(message.content[0].text.value)
@@ -324,6 +326,7 @@ if utils.check_password():
324
  st.markdown("<script language='javascript'>console.log('scrolling');</script>", unsafe_allow_html=True)
325
 
326
  if st.sidebar.button("New Conversation") or "messages" not in st.session_state:
 
327
  st.session_state.new_conversation = True
328
  st.session_state.messages = [{"role":"system", "content":"Hello. I'm your startups discovery assistant."}]
329
 
 
178
  if not messages is None:
179
  with content_container:
180
  for message in list(messages)[::-1]:
181
+ if hasattr(message, 'role'):
182
+ print(f"\n-----\nMessage: {message}\n")
183
+ with st.chat_message(name = message.role):
184
+ st.write(message.content[0].text.value)
185
  # if message.role == "assistant":
186
  # with st.chat_message(name = message.role, avatar = st.image("resources/raized_logo.png")):
187
  # st.write(message.content[0].text.value)
 
326
  st.markdown("<script language='javascript'>console.log('scrolling');</script>", unsafe_allow_html=True)
327
 
328
  if st.sidebar.button("New Conversation") or "messages" not in st.session_state:
329
+ st.session_state.assistant_thread = st.session_state.openai_client.beta.threads.create()
330
  st.session_state.new_conversation = True
331
  st.session_state.messages = [{"role":"system", "content":"Hello. I'm your startups discovery assistant."}]
332
 
openai_utils.py CHANGED
@@ -20,14 +20,19 @@ def getListOfCompanies(query, filters = {}):
20
  descriptions = "\n".join([f"Description of company \"{res['name']}\": {res['data']['Summary']}.\n" for res in st.session_state.db_search_results[:20] if 'Summary' in res['data']])
21
  return descriptions
22
 
 
 
 
23
  def wait_for_response(thread, run):
24
- while True:
 
 
25
  # Retrieve the run status
26
  run_status = st.session_state.openai_client.beta.threads.runs.retrieve(
27
  thread_id=thread.id,
28
  run_id=run.id
29
  )
30
-
31
  # Check and print the step details
32
  run_steps = st.session_state.openai_client.beta.threads.runs.steps.list(
33
  thread_id=thread.id,
@@ -84,14 +89,22 @@ def wait_for_response(thread, run):
84
  run_id=run.id,
85
  tool_outputs=tool_outputs,
86
  )
87
- print(run_status.required_action)
88
  #return run_status
89
  else:
90
- print(f"Unknown required action: {required_action.type}")
91
- return run_status
92
  else:
93
- print(f"Run status: {run_status.status}")
94
- return run_status
 
 
 
 
 
 
 
 
95
 
96
  def call_assistant(query, engine="gpt-3.5-turbo"): #, temp=0, top_p=1.0, max_tokens=4048):
97
  try:
 
20
  descriptions = "\n".join([f"Description of company \"{res['name']}\": {res['data']['Summary']}.\n" for res in st.session_state.db_search_results[:20] if 'Summary' in res['data']])
21
  return descriptions
22
 
23
+ def report_error(txt):
24
+ print(f"\nEEEEEEEEEEEEE\n{txt}")
25
+
26
  def wait_for_response(thread, run):
27
+ timeout = 60 #timeout in seconds
28
+ started = time.time()
29
+ while True and time.time()-started<timeout:
30
  # Retrieve the run status
31
  run_status = st.session_state.openai_client.beta.threads.runs.retrieve(
32
  thread_id=thread.id,
33
  run_id=run.id
34
  )
35
+ print(f"Run status: {run_status.status}")
36
  # Check and print the step details
37
  run_steps = st.session_state.openai_client.beta.threads.runs.steps.list(
38
  thread_id=thread.id,
 
89
  run_id=run.id,
90
  tool_outputs=tool_outputs,
91
  )
92
+ print(f"Required action {run_status.required_action}")
93
  #return run_status
94
  else:
95
+ report_error(f"Unknown required action type: {required_action}")
96
+ break
97
  else:
98
+ report_error(f"Unhandled Run status: {run_status.status}\n\nError: {run_status.last_error}\n")
99
+ break
100
+ if time.time()-started>timeout:
101
+ report_error(f"Wait for response timeout after {timeout}")
102
+ report_error(f"Flow not completed")
103
+ messages = st.session_state.openai_client.beta.threads.messages.list(
104
+ thread_id=thread.id
105
+ )
106
+ return messages
107
+
108
 
109
  def call_assistant(query, engine="gpt-3.5-turbo"): #, temp=0, top_p=1.0, max_tokens=4048):
110
  try:
utils.py CHANGED
@@ -10,7 +10,6 @@ import psycopg2
10
  from psycopg2 import extras
11
  import streamlit as st
12
 
13
- print(f"openai-key{os.getenv('OPENAI_API_KEY')}")
14
  # gcp_client = secretmanager.SecretManagerServiceClient()
15
  # response = gcp_client.access_secret_version(request={"name": version.name})
16
 
@@ -21,7 +20,6 @@ def get_variable(name):
21
  res = st.secrets[name]
22
  except Exception as e:
23
  pass
24
- print(f"Got var {name}: {res}")
25
  return res
26
 
27
  OPENAI_API_KEY = get_variable("OPENAI_API_KEY") # app.pinecone.io
@@ -107,7 +105,10 @@ def search_index(query, top_k, regions, countries, retriever, index_namespace):
107
  data = {"Summary": match["metadata"]["summary"]}
108
  if 'summary' in match['metadata']:
109
  try:
110
- data = json.loads(match["metadata"]["summary"])
 
 
 
111
  except Exception as e:
112
  pass
113
  answer['data'] = data
 
10
  from psycopg2 import extras
11
  import streamlit as st
12
 
 
13
  # gcp_client = secretmanager.SecretManagerServiceClient()
14
  # response = gcp_client.access_secret_version(request={"name": version.name})
15
 
 
20
  res = st.secrets[name]
21
  except Exception as e:
22
  pass
 
23
  return res
24
 
25
  OPENAI_API_KEY = get_variable("OPENAI_API_KEY") # app.pinecone.io
 
105
  data = {"Summary": match["metadata"]["summary"]}
106
  if 'summary' in match['metadata']:
107
  try:
108
+ txt_data = match["metadata"]["summary"]
109
+ if txt_data.startswith('```json') and txt_data.endswith('```'):
110
+ txt_data = txt_data[7:-3]
111
+ data = json.loads(txt_data)
112
  except Exception as e:
113
  pass
114
  answer['data'] = data