bstraehle commited on
Commit
4a90510
1 Parent(s): 766f004

Update assistants.py

Browse files
Files changed (1) hide show
  1. assistants.py +34 -34
assistants.py CHANGED
@@ -42,7 +42,7 @@ tools = {
42
  "tavily_search_tool": tavily_search_tool,
43
  }
44
 
45
- def create_assistant(openai_client):
46
  assistant = openai_client.beta.assistants.create(
47
  name="Python Coding Assistant",
48
  instructions=(
@@ -63,17 +63,17 @@ def create_assistant(openai_client):
63
 
64
  return assistant
65
 
66
- def load_assistant(openai_client):
67
  assistant = openai_client.beta.assistants.retrieve(assistant_id)
68
  show_json("assistant", assistant)
69
  return assistant
70
 
71
- def create_thread(openai_client):
72
  thread = openai_client.beta.threads.create()
73
  show_json("thread", thread)
74
  return thread
75
 
76
- def create_message(openai_client, thread, msg):
77
  message = openai_client.beta.threads.messages.create(
78
  role="user",
79
  thread_id=thread.id,
@@ -83,7 +83,7 @@ def create_message(openai_client, thread, msg):
83
  show_json("message", message)
84
  return message
85
 
86
- def create_run(openai_client, assistant, thread):
87
  run = openai_client.beta.threads.runs.create(
88
  assistant_id=assistant.id,
89
  thread_id=thread.id,
@@ -93,7 +93,7 @@ def create_run(openai_client, assistant, thread):
93
  show_json("run", run)
94
  return run
95
 
96
- def wait_on_run(openai_client, thread, run):
97
  while run.status == "queued" or run.status == "in_progress":
98
  run = openai_client.beta.threads.runs.retrieve(
99
  thread_id=thread.id,
@@ -109,7 +109,7 @@ def wait_on_run(openai_client, thread, run):
109
 
110
  return run
111
 
112
- def get_run_steps(openai_client, thread, run):
113
  run_steps = openai_client.beta.threads.runs.steps.list(
114
  thread_id=thread.id,
115
  run_id=run.id,
@@ -149,34 +149,10 @@ def execute_tool_calls(run_steps):
149
 
150
  return tool_call_ids, tool_call_results
151
 
152
- def get_messages(openai_client, thread):
153
- messages = openai_client.beta.threads.messages.list(
154
- thread_id=thread.id
155
- )
156
-
157
- show_json("messages", messages)
158
- return messages
159
-
160
- def extract_content_values(data):
161
- text_values, image_values = [], []
162
-
163
- for item in data.data:
164
- for content in item.content:
165
- # TODO: Handle other file types
166
- if content.type == "text":
167
- text_value = content.text.value
168
- text_values.append(text_value)
169
- if content.type == "image_file":
170
- image_value = content.image_file.file_id
171
- image_values.append(image_value)
172
-
173
- return text_values, image_values
174
-
175
  def recurse_execute_tool_calls(thread, run, run_steps, iteration):
176
  tool_call_ids, tool_call_results = execute_tool_calls(run_steps)
177
 
178
  if len(tool_call_ids) > iteration:
179
- # https://platform.openai.com/docs/api-reference/runs/submitToolOutputs
180
  tool_output = {}
181
 
182
  try:
@@ -190,15 +166,39 @@ def recurse_execute_tool_calls(thread, run, run_steps, iteration):
190
  "output": tool_call_results[iteration]
191
  }
192
 
 
193
  run = openai_client.beta.threads.runs.submit_tool_outputs(
194
  thread_id=thread.id,
195
  run_id=run.id,
196
  tool_outputs=[tool_output]
197
  )
198
 
199
- run = wait_on_run(openai_client, thread, run)
200
- run_steps = get_run_steps(openai_client, thread, run)
201
 
202
  recurse_execute_tool_calls(thread, run, run_steps, iteration + 1)
203
  else:
204
- return
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  "tavily_search_tool": tavily_search_tool,
43
  }
44
 
45
+ def create_assistant():
46
  assistant = openai_client.beta.assistants.create(
47
  name="Python Coding Assistant",
48
  instructions=(
 
63
 
64
  return assistant
65
 
66
+ def load_assistant():
67
  assistant = openai_client.beta.assistants.retrieve(assistant_id)
68
  show_json("assistant", assistant)
69
  return assistant
70
 
71
+ def create_thread():
72
  thread = openai_client.beta.threads.create()
73
  show_json("thread", thread)
74
  return thread
75
 
76
+ def create_message(thread, msg):
77
  message = openai_client.beta.threads.messages.create(
78
  role="user",
79
  thread_id=thread.id,
 
83
  show_json("message", message)
84
  return message
85
 
86
+ def create_run(assistant, thread):
87
  run = openai_client.beta.threads.runs.create(
88
  assistant_id=assistant.id,
89
  thread_id=thread.id,
 
93
  show_json("run", run)
94
  return run
95
 
96
+ def wait_on_run(thread, run):
97
  while run.status == "queued" or run.status == "in_progress":
98
  run = openai_client.beta.threads.runs.retrieve(
99
  thread_id=thread.id,
 
109
 
110
  return run
111
 
112
+ def get_run_steps(thread, run):
113
  run_steps = openai_client.beta.threads.runs.steps.list(
114
  thread_id=thread.id,
115
  run_id=run.id,
 
149
 
150
  return tool_call_ids, tool_call_results
151
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
  def recurse_execute_tool_calls(thread, run, run_steps, iteration):
153
  tool_call_ids, tool_call_results = execute_tool_calls(run_steps)
154
 
155
  if len(tool_call_ids) > iteration:
 
156
  tool_output = {}
157
 
158
  try:
 
166
  "output": tool_call_results[iteration]
167
  }
168
 
169
+ # https://platform.openai.com/docs/api-reference/runs/submitToolOutputs
170
  run = openai_client.beta.threads.runs.submit_tool_outputs(
171
  thread_id=thread.id,
172
  run_id=run.id,
173
  tool_outputs=[tool_output]
174
  )
175
 
176
+ run = wait_on_run(thread, run)
177
+ run_steps = get_run_steps(thread, run)
178
 
179
  recurse_execute_tool_calls(thread, run, run_steps, iteration + 1)
180
  else:
181
+ return
182
+
183
+ def get_messages(thread):
184
+ messages = openai_client.beta.threads.messages.list(
185
+ thread_id=thread.id
186
+ )
187
+
188
+ show_json("messages", messages)
189
+ return messages
190
+
191
+ def extract_content_values(data):
192
+ text_values, image_values = [], []
193
+
194
+ for item in data.data:
195
+ for content in item.content:
196
+ # TODO: Handle other file types
197
+ if content.type == "text":
198
+ text_value = content.text.value
199
+ text_values.append(text_value)
200
+ if content.type == "image_file":
201
+ image_value = content.image_file.file_id
202
+ image_values.append(image_value)
203
+
204
+ return text_values, image_values