bstraehle commited on
Commit
7f972c9
1 Parent(s): eabf47d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -28
app.py CHANGED
@@ -144,7 +144,7 @@ def create_issues_repairs_agent(client):
144
 
145
  def create_thread(client):
146
  thread = client.beta.threads.create()
147
- show_json("thread", thread)
148
 
149
  return thread
150
 
@@ -154,7 +154,7 @@ def create_message(client, thread, msg):
154
  thread_id=thread.id,
155
  content=msg,
156
  )
157
- show_json("message", message)
158
 
159
  return message
160
 
@@ -163,7 +163,7 @@ def create_run(client, assistant, thread):
163
  assistant_id=assistant.id,
164
  thread_id=thread.id,
165
  )
166
- show_json("run", run)
167
 
168
  return run
169
 
@@ -174,10 +174,20 @@ def wait_on_run(client, thread, run):
174
  run_id=run.id,
175
  )
176
  time.sleep(0.25)
177
- show_json("run", run)
178
 
179
  return run
180
 
 
 
 
 
 
 
 
 
 
 
181
  def execute_tool_call(tool_call):
182
  name = tool_call.function.name
183
  args = json.loads(tool_call.function.arguments)
@@ -186,25 +196,16 @@ def execute_tool_call(tool_call):
186
 
187
  tools[name](**args)
188
 
189
- def list_run_steps(client, thread, run):
190
- run_steps = client.beta.threads.runs.steps.list(
191
- thread_id=thread.id,
192
- run_id=run.id,
193
- order="asc",
194
- )
195
-
196
- for step in run_steps.data:
197
  step_details = step.step_details
198
  show_json("step_details", step_details)
199
 
200
- if not hasattr(step_details, "tool_calls"):
201
- continue
202
-
203
- for tool_call in step_details.tool_calls:
204
- execute_tool_call(tool_call)
205
 
206
- return run_steps
207
-
208
  def list_messages(client, thread):
209
  messages = client.beta.threads.messages.list(
210
  thread_id=thread.id
@@ -218,7 +219,7 @@ def extract_content_values(data):
218
 
219
  for item in data.data:
220
  for content in item.content:
221
- if content.type == 'text':
222
  content_values.append(content.text.value)
223
 
224
  return content_values
@@ -276,24 +277,23 @@ def chat(message, history, openai_api_key):
276
  run = create_run(client, assistant, thread)
277
  run = wait_on_run(client, thread, run)
278
 
279
- list_run_steps(client, thread, run)
280
 
 
 
281
  messages = list_messages(client, thread)
282
 
283
- return extract_content_values(messages)[0]
 
 
284
 
285
  gr.ChatInterface(
286
  chat,
287
  chatbot=gr.Chatbot(),
288
- textbox=gr.Textbox(placeholder="Type here", container=False, scale=7),
289
  title="Multi-Agent Orchestration",
290
  description="Demo using hand-off pattern: triage agent, sales agent, and issues & repairs agent",
291
  clear_btn=None,
292
  retry_btn=None,
293
  undo_btn=None,
294
- #examples=[["Generate the first 10 Fibbonaci numbers with code.", "sk-<BringYourOwn>"]],
295
- #cache_examples=False,
296
- #additional_inputs=[
297
- # gr.Textbox("sk-", label="OpenAI API Key", type = "password"),
298
- #],
299
  ).launch()
 
144
 
145
  def create_thread(client):
146
  thread = client.beta.threads.create()
147
+ #show_json("thread", thread)
148
 
149
  return thread
150
 
 
154
  thread_id=thread.id,
155
  content=msg,
156
  )
157
+ #show_json("message", message)
158
 
159
  return message
160
 
 
163
  assistant_id=assistant.id,
164
  thread_id=thread.id,
165
  )
166
+ #show_json("run", run)
167
 
168
  return run
169
 
 
174
  run_id=run.id,
175
  )
176
  time.sleep(0.25)
177
+ #show_json("run", run)
178
 
179
  return run
180
 
181
+ def list_steps(client, thread, run):
182
+ steps = client.beta.threads.runs.steps.list(
183
+ thread_id=thread.id,
184
+ run_id=run.id,
185
+ order="asc",
186
+ )
187
+ show_json("steps", steps)
188
+
189
+ return steps
190
+
191
  def execute_tool_call(tool_call):
192
  name = tool_call.function.name
193
  args = json.loads(tool_call.function.arguments)
 
196
 
197
  tools[name](**args)
198
 
199
+ def execute_tool_calls(steps):
200
+ for step in steps.data:
 
 
 
 
 
 
201
  step_details = step.step_details
202
  show_json("step_details", step_details)
203
 
204
+ if hasattr(step_details, "tool_calls"):
205
+ for tool_call in step_details.tool_calls:
206
+ execute_tool_call(tool_call)
207
+ show_json("tool_call", tool_call)
 
208
 
 
 
209
  def list_messages(client, thread):
210
  messages = client.beta.threads.messages.list(
211
  thread_id=thread.id
 
219
 
220
  for item in data.data:
221
  for content in item.content:
222
+ if content.type == "text":
223
  content_values.append(content.text.value)
224
 
225
  return content_values
 
277
  run = create_run(client, assistant, thread)
278
  run = wait_on_run(client, thread, run)
279
 
280
+ steps = list_steps(client, thread, run)
281
 
282
+ execute_tool_calls(steps)
283
+
284
  messages = list_messages(client, thread)
285
 
286
+ content_values = extract_content_values(messages)
287
+
288
+ return content_values[0]
289
 
290
  gr.ChatInterface(
291
  chat,
292
  chatbot=gr.Chatbot(),
293
+ textbox=gr.Textbox(placeholder="hi", container=False, scale=7),
294
  title="Multi-Agent Orchestration",
295
  description="Demo using hand-off pattern: triage agent, sales agent, and issues & repairs agent",
296
  clear_btn=None,
297
  retry_btn=None,
298
  undo_btn=None,
 
 
 
 
 
299
  ).launch()