|
43 | 43 |
|
44 | 44 | ## Result
|
45 | 45 |
|
46 |
| - ```python |
| 46 | + ```json |
47 | 47 | {result}
|
48 | 48 | ```
|
49 | 49 | """
|
50 | 50 | )
|
51 | 51 |
|
52 |
| -TOOL_CALL_FUNCTION_TEMPLATE = inspect.cleandoc( |
| 52 | +TOOL_CALL_FUNCTION_ARGS_TEMPLATE = inspect.cleandoc( |
| 53 | + """ |
| 54 | + # Tool call: {name} |
| 55 | + |
| 56 | + ## Arguments |
| 57 | + |
| 58 | + ```json |
| 59 | + {args} |
| 60 | + ``` |
| 61 | + """ |
| 62 | +) |
| 63 | +TOOL_CALL_FUNCTION_RESULT_TEMPLATE = inspect.cleandoc( |
53 | 64 | """
|
54 | 65 | # Tool call: {name}
|
55 | 66 |
|
@@ -209,21 +220,39 @@ async def on_tool_call_done(self, tool_call: ToolCall) -> None:
|
209 | 220 |
|
210 | 221 | # code interpreter is run as a single call, so we can publish a result artifact
|
211 | 222 | if tool_call.type == "code_interpreter":
|
| 223 | + # images = [] |
| 224 | + # for output in tool_call.code_interpreter.outputs: |
| 225 | + # if output.type == "image": |
| 226 | + # image_path = download_temp_file(output.image.file_id) |
| 227 | + # images.append(image_path) |
| 228 | + |
212 | 229 | markdown = TOOL_CALL_CODE_INTERPRETER_TEMPLATE.format(
|
213 | 230 | code=tool_call.code_interpreter.input,
|
214 |
| - result=tool_call.code_interpreter.outputs, |
| 231 | + result=json.dumps( |
| 232 | + [ |
| 233 | + o.model_dump(mode="json") |
| 234 | + for o in tool_call.code_interpreter.outputs |
| 235 | + ], |
| 236 | + indent=2, |
| 237 | + ), |
215 | 238 | )
|
216 |
| - # low level artifact call because we need to provide the task run ID manually |
217 |
| - return await client.create_artifact( |
218 |
| - artifact=ArtifactRequest( |
219 |
| - type="markdown", |
220 |
| - key="result", |
221 |
| - description="Code interpreter result", |
222 |
| - task_run_id=task_run.id, |
223 |
| - flow_run_id=task_run.flow_run_id, |
224 |
| - data=markdown, |
225 |
| - ) |
| 239 | + elif tool_call.type == "function": |
| 240 | + markdown = TOOL_CALL_FUNCTION_ARGS_TEMPLATE.format( |
| 241 | + name=tool_call.function.name, |
| 242 | + args=tool_call.function.arguments, |
| 243 | + ) |
| 244 | + |
| 245 | + # low level artifact call because we need to provide the task run ID manually |
| 246 | + return await client.create_artifact( |
| 247 | + artifact=ArtifactRequest( |
| 248 | + type="markdown", |
| 249 | + key="result", |
| 250 | + description="Code interpreter result", |
| 251 | + task_run_id=task_run.id, |
| 252 | + flow_run_id=task_run.flow_run_id, |
| 253 | + data=markdown, |
226 | 254 | )
|
| 255 | + ) |
227 | 256 |
|
228 | 257 |
|
229 | 258 | def talk_to_human(message: str, get_response: bool = True) -> str:
|
@@ -349,7 +378,7 @@ async def modified_fn(
|
349 | 378 | except Exception:
|
350 | 379 | pass
|
351 | 380 | await create_markdown_artifact(
|
352 |
| - markdown=TOOL_CALL_FUNCTION_TEMPLATE.format( |
| 381 | + markdown=TOOL_CALL_FUNCTION_RESULT_TEMPLATE.format( |
353 | 382 | name=tool.function.name,
|
354 | 383 | description=tool.function.description or "(none provided)",
|
355 | 384 | args=passed_args,
|
|
0 commit comments