Closed as not planned
Description
Question
This is my code to trans agent sdk events to fastapi response, but something wrong with tool_call(tool_call_item) and tool_call_output(tool_call_output_item), the frontend parse it error, is there something wrong about ny code? Or I need other way to do this?
@app.post("/v1/chat/completions")
async def chat_completions(body: ChatCompletionsBody) -> StreamingResponse:
print(body)
msg_id = str(uuid.uuid4())
created = int(time.time())
system_fingerprint = "fp_3d5141a69a_prod0225"
async def generate():
result = Runner.run_streamed(
search_agent,
body.messages,
run_config=RunConfig(model_provider=CUSTOM_MODEL_PROVIDER),
)
async for event in result.stream_events():
if event.type == "raw_response_event" and isinstance(event.data, ResponseTextDeltaEvent):
yield "data: " + json.dumps({
"id": msg_id,
"object": "chat.completion.chunk",
"created": created,
"model": body.model,
"system_fingerprint": system_fingerprint,
"choices": [{
"index": 0,
"delta": {"content": event.data.delta},
"finish_reason": None
}]
}) + "\n\n"
elif event.type == "agent_updated_stream_event":
pass
elif event.type == "run_item_stream_event":
raw_item = event.item.raw_item
if event.item.type == "tool_call_item":
if isinstance(raw_item, ResponseFunctionToolCall):
yield "data: " + json.dumps({
"id": msg_id,
"object": "chat.completion.chunk",
"created": created,
"model": body.model,
"system_fingerprint": system_fingerprint,
"choices": [{
"index": 0,
"delta": {
"tool_calls": [{
"index": 0,
"id": raw_item.call_id,
"function": {
"name": raw_item.name,
"arguments": raw_item.arguments
},
"type": "function"
}]
},
"finish_reason": None
}]
}) + "\n\n"
elif event.item.type == "tool_call_output_item":
yield "data: " + json.dumps({
"id": msg_id,
"object": "chat.completion.chunk",
"created": created,
"model": body.model,
"system_fingerprint": system_fingerprint,
"choices": [{
"index": 0,
"delta": {"content": raw_item['output']},
"finish_reason": None
}]
}) + "\n\n"
# finish
yield "data: " + json.dumps({
"id": msg_id,
"object": "chat.completion.chunk",
"created": created,
"model": body.model,
"system_fingerprint": system_fingerprint,
"choices": [{"index": 0, "delta": {"content": ""}, "logprobs": None, "finish_reason": "stop"}]
}) + "\n\n"
yield "data: [DONE]\n\n"
return StreamingResponse(
generate(),
media_type="text/event-stream"
)