Skip to content

Commit dedafa3

Browse files
Full working custom tool call!
1 parent 67fe090 commit dedafa3

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

routers/chat.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
ThreadMessageCreated, ThreadMessageDelta, ThreadRunCompleted,
1111
ThreadRunRequiresAction, ThreadRunStepCreated, ThreadRunStepDelta
1212
)
13+
from openai.types.beta.threads.run_submit_tool_outputs_params import ToolOutput
1314
from openai.types.beta.threads.run import RequiredAction
1415
from fastapi.responses import StreamingResponse
1516
from fastapi import APIRouter, Depends, Form, HTTPException
1617
from pydantic import BaseModel
18+
1719
import json
1820

1921
from utils.weather import get_weather
@@ -39,14 +41,22 @@ class ToolCallOutputs(BaseModel):
3941
async def post_tool_outputs(client: AsyncOpenAI, data: dict, thread_id: str):
4042
"""
4143
data is expected to be something like
42-
4344
{
44-
"tool_outputs": {"location": "City", "temperature": 70, "conditions": "Sunny"},
45+
"tool_outputs": {
46+
"output": {"location": "City", "temperature": 70, "conditions": "Sunny"},
47+
"tool_call_id": "call_123"
48+
},
4549
"runId": "some-run-id",
4650
}
4751
"""
4852
try:
49-
outputs_list = [data["tool_outputs"]]
53+
outputs_list = [
54+
ToolOutput(
55+
output=data["tool_outputs"]["output"],
56+
tool_call_id=data["tool_outputs"]["tool_call_id"]
57+
)
58+
]
59+
5060

5161
stream_manager = client.beta.threads.runs.submit_tool_outputs_stream(
5262
thread_id=thread_id,
@@ -227,10 +237,13 @@ async def event_generator():
227237
logger.info(f"Weather output: {weather_output}")
228238

229239
data_for_tool = {
230-
"tool_outputs": weather_output,
231-
"runId": event.data.id,
240+
"tool_outputs": {
241+
"output": str(weather_output),
242+
"tool_call_id": tool_call.id
243+
},
244+
"runId": run_requires_action_event.data.id,
232245
}
233-
246+
234247
# Afterwards, create a fresh stream_manager for the next iteration
235248
new_stream_manager: AsyncAssistantStreamManager = await post_tool_outputs(
236249
client,

0 commit comments

Comments
 (0)