pyatasya commited on
Commit
fd8ca06
·
1 Parent(s): 5c9ea55

added check for tool calls

Browse files
Files changed (1) hide show
  1. openai_utils.py +37 -32
openai_utils.py CHANGED
@@ -88,38 +88,43 @@ def wait_for_response(thread, run):
88
  print(f'{run_status.status.capitalize()}... Please wait.')
89
  time.sleep(1.5) # Wait before checking again
90
  elif run_status.status == 'requires_action':
91
- outputs = {}
92
- for tool_call in run_status.required_action.submit_tool_outputs.tool_calls:
93
- if tool_call.function.name =="getListOfCompanies":
94
- try:
95
- args = json.loads(tool_call.function.arguments)
96
- print(f"Processing tool_call {tool_call.id}. Calling 'getListOfCompanies with args: {args}" )
97
- res = getListOfCompanies(args['query'])
98
- # matches = res['matches']
99
- # sorted_matches = sorted(matches, key=lambda x: x['score'], reverse=True)
100
- # output = ""
101
- # for m in sorted_matches:
102
- # if 'summary' in m.metadata:
103
- # try:
104
- # data = json.loads(m["metadata"]["summary"])
105
- # output = output + f"company name: {m.metadata['company_name']}, description: {data['Summary']}\n"
106
- # except Exception as e:
107
- # output = output + f"company name: {m.metadata['company_name']}, description: {m.metadata['summary']}\n"
108
- if len(res)>0:
109
- outputs[tool_call.id] = res
110
- except Exception as e:
111
- print(f"Error calling tools, {str(e)}")
112
- traceback.print_exc()
113
-
114
- tool_outputs=[{"tool_call_id": k, "output": v} for (k,v) in outputs.items()]
115
- print(f"Finished tools calling: {tool_outputs}")
116
- run = st.session_state.openai_client.beta.threads.runs.submit_tool_outputs(
117
- thread_id=thread.id,
118
- run_id=run.id,
119
- tool_outputs=tool_outputs,
120
- )
121
- print(run_status.required_action)
122
- #return run_status
 
 
 
 
 
123
  else:
124
  print(f"Run status: {run_status.status}")
125
  return run_status
 
88
  print(f'{run_status.status.capitalize()}... Please wait.')
89
  time.sleep(1.5) # Wait before checking again
90
  elif run_status.status == 'requires_action':
91
+ required_action = run_status.required_action
92
+ if required_action.type == 'submit_tool_outputs':
93
+ outputs = {}
94
+ for tool_call in required_action.submit_tool_outputs.tool_calls:
95
+ if tool_call.function.name =="getListOfCompanies":
96
+ try:
97
+ args = json.loads(tool_call.function.arguments)
98
+ print(f"Processing tool_call {tool_call.id}. Calling 'getListOfCompanies with args: {args}" )
99
+ res = getListOfCompanies(args['query'])
100
+ # matches = res['matches']
101
+ # sorted_matches = sorted(matches, key=lambda x: x['score'], reverse=True)
102
+ # output = ""
103
+ # for m in sorted_matches:
104
+ # if 'summary' in m.metadata:
105
+ # try:
106
+ # data = json.loads(m["metadata"]["summary"])
107
+ # output = output + f"company name: {m.metadata['company_name']}, description: {data['Summary']}\n"
108
+ # except Exception as e:
109
+ # output = output + f"company name: {m.metadata['company_name']}, description: {m.metadata['summary']}\n"
110
+ if len(res)>0:
111
+ outputs[tool_call.id] = res
112
+ except Exception as e:
113
+ print(f"Error calling tools, {str(e)}")
114
+ traceback.print_exc()
115
+
116
+ tool_outputs=[{"tool_call_id": k, "output": v} for (k,v) in outputs.items()]
117
+ print(f"Finished tools calling: {tool_outputs}")
118
+ run = st.session_state.openai_client.beta.threads.runs.submit_tool_outputs(
119
+ thread_id=thread.id,
120
+ run_id=run.id,
121
+ tool_outputs=tool_outputs,
122
+ )
123
+ print(run_status.required_action)
124
+ #return run_status
125
+ else:
126
+ print(f"Unknown required action: {required_action.type}")
127
+ return run_status
128
  else:
129
  print(f"Run status: {run_status.status}")
130
  return run_status