Skip to content

Commit c89f3fa

Browse files
committed
Improve artifact production
1 parent afdcffb commit c89f3fa

File tree

1 file changed

+43
-14
lines changed

1 file changed

+43
-14
lines changed

src/control_flow/agent.py

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,24 @@
4343
4444
## Result
4545
46-
```python
46+
```json
4747
{result}
4848
```
4949
"""
5050
)
5151

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(
5364
"""
5465
# Tool call: {name}
5566
@@ -209,21 +220,39 @@ async def on_tool_call_done(self, tool_call: ToolCall) -> None:
209220

210221
# code interpreter is run as a single call, so we can publish a result artifact
211222
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+
212229
markdown = TOOL_CALL_CODE_INTERPRETER_TEMPLATE.format(
213230
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+
),
215238
)
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,
226254
)
255+
)
227256

228257

229258
def talk_to_human(message: str, get_response: bool = True) -> str:
@@ -349,7 +378,7 @@ async def modified_fn(
349378
except Exception:
350379
pass
351380
await create_markdown_artifact(
352-
markdown=TOOL_CALL_FUNCTION_TEMPLATE.format(
381+
markdown=TOOL_CALL_FUNCTION_RESULT_TEMPLATE.format(
353382
name=tool.function.name,
354383
description=tool.function.description or "(none provided)",
355384
args=passed_args,

0 commit comments

Comments
 (0)