Skip to content

Commit d7405ea

Browse files
Made a start on code interpreter handling
1 parent e2d8e01 commit d7405ea

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

routers/chat.py

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import json
2020

21-
from utils.weather import get_weather
21+
from utils.custom_functions import get_weather
2222
from utils.sse import sse_format
2323

2424
logger: logging.Logger = logging.getLogger("uvicorn.error")
@@ -164,16 +164,40 @@ async def handle_assistant_stream(
164164
)
165165

166166
if isinstance(event, ThreadRunStepDelta) and event.data.delta.step_details.type == "tool_calls":
167-
if event.data.delta.step_details.tool_calls[0].function.name:
168-
yield sse_format(
169-
f"toolDelta{step_counter}",
170-
event.data.delta.step_details.tool_calls[0].function.name + "<br>"
171-
)
172-
elif event.data.delta.step_details.tool_calls[0].function.arguments:
173-
yield sse_format(
174-
f"toolDelta{step_counter}",
175-
event.data.delta.step_details.tool_calls[0].function.arguments
176-
)
167+
tool_call = event.data.delta.step_details.tool_calls[0]
168+
169+
# Handle function tool calls
170+
if tool_call.type == "function":
171+
if tool_call.function.name:
172+
yield sse_format(
173+
f"toolDelta{step_counter}",
174+
tool_call.function.name + "<br>"
175+
)
176+
elif tool_call.function.arguments:
177+
yield sse_format(
178+
f"toolDelta{step_counter}",
179+
tool_call.function.arguments
180+
)
181+
182+
# Handle code interpreter tool calls
183+
elif tool_call.type == "code_interpreter":
184+
if tool_call.code_interpreter.input:
185+
yield sse_format(
186+
f"toolDelta{step_counter}",
187+
f"{tool_call.code_interpreter.input}"
188+
)
189+
if tool_call.code_interpreter.outputs:
190+
for output in tool_call.code_interpreter.outputs:
191+
if output.type == "logs":
192+
yield sse_format(
193+
f"toolDelta{step_counter}",
194+
f"{output.logs}"
195+
)
196+
elif output.type == "image":
197+
yield sse_format(
198+
f"toolDelta{step_counter}",
199+
f"{output.image.file_id}"
200+
)
177201

178202
# If the assistant run requires an action (a tool call), break and handle it
179203
if isinstance(event, ThreadRunRequiresAction):
@@ -218,11 +242,12 @@ async def event_generator():
218242
# If the assistant still needs a tool call, do it and then re-stream
219243
if required_action and required_action.submit_tool_outputs:
220244
for tool_call in required_action.submit_tool_outputs.tool_calls:
221-
yield (
222-
f"event: toolCallCreated\n"
223-
f"data: {templates.get_template('components/assistant-step.html').render(
224-
step_type='toolCall', stream_name=f'toolDelta{step_counter}'
225-
).replace('\n', '')}\n\n"
245+
yield sse_format(
246+
"toolCallCreated",
247+
templates.get_template('components/assistant-step.html').render(
248+
step_type='toolCall',
249+
stream_name=f'toolDelta{step_counter}'
250+
)
226251
)
227252

228253
if tool_call.type == "function" and tool_call.function.name == "get_weather":
File renamed without changes.

0 commit comments

Comments
 (0)