import json
TOOLS = [
{
"type": "function",
"name": "get_weather",
"description": "Get current weather for a city",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
}
]
def get_weather(city: str) -> dict:
... # your implementation
async def run_tool_turn(ws):
# Declare the tool and ask a question that needs it.
await ws.send(json.dumps({
"type": "session.update",
"session": {
"model": "higgs-realtime",
"tools": TOOLS,
"instructions": "Use the available tools when needed.",
},
}))
await ws.send(json.dumps({
"type": "conversation.item.create",
"item": {
"type": "message",
"role": "user",
"content": [
{"type": "input_text", "text": "What's the weather in Tokyo?"}
],
},
}))
await ws.send(json.dumps({"type": "response.create"}))
async for msg in ws:
event = json.loads(msg)
if event["type"] == "response.done":
# A response ending in a tool call carries the function_call item.
calls = [
item
for item in event["response"]["output"]
if item.get("type") == "function_call"
]
if not calls:
break # Final spoken reply finished.
call = calls[0]
result = get_weather(**json.loads(call["arguments"])) # 1. Execute.
await ws.send(json.dumps({
"type": "conversation.item.create", # 2. Return the result.
"item": {
"type": "function_call_output",
"call_id": call["call_id"],
"output": json.dumps(result),
},
}))
await ws.send(
json.dumps({"type": "response.create"})
) # 3. Continue.
elif event["type"] == "response.output_audio.delta":
... # Play audio as usual.