Spaces:
Sleeping
Sleeping
pyatasya
commited on
Commit
·
fd8ca06
1
Parent(s):
5c9ea55
added check for tool calls
Browse files- 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 |
-
|
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 |
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
|