|
18 | 18 |
|
19 | 19 | import json
|
20 | 20 |
|
21 |
| -from utils.weather import get_weather |
| 21 | +from utils.custom_functions import get_weather |
22 | 22 | from utils.sse import sse_format
|
23 | 23 |
|
24 | 24 | logger: logging.Logger = logging.getLogger("uvicorn.error")
|
@@ -164,16 +164,40 @@ async def handle_assistant_stream(
|
164 | 164 | )
|
165 | 165 |
|
166 | 166 | 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 | + ) |
177 | 201 |
|
178 | 202 | # If the assistant run requires an action (a tool call), break and handle it
|
179 | 203 | if isinstance(event, ThreadRunRequiresAction):
|
@@ -218,11 +242,12 @@ async def event_generator():
|
218 | 242 | # If the assistant still needs a tool call, do it and then re-stream
|
219 | 243 | if required_action and required_action.submit_tool_outputs:
|
220 | 244 | 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 | + ) |
226 | 251 | )
|
227 | 252 |
|
228 | 253 | if tool_call.type == "function" and tool_call.function.name == "get_weather":
|
|
0 commit comments