Glao feidhme fíor-ama
Ar an leathanach seo
Nuashonrú 2 Samhain, 2026 — Múnlaí fuaime. Tá roinnt múnlaí sa chúrsa seo curtha as feidhm: stopfaidh
tts-1,tts-1-hd, agusgpt-4o-mini-ttsag obair ar 6 Eanáir, 2027 (is égpt-realtime-2.1-minian t-ionadaí atá luaite, a deir treoir TTS OpenAI nach dtacaítear leis le/v1/audio/speech: aistrítear sintéis cainte chuig an Realtime API);gpt-realtime,gpt-4o-realtime, agus múnlaí fuaime oidhreachta ar 20 Eanáir, 2027 (gpt-realtime-2.1,gpt-realtime-2.1-mini,gpt-audio-1.5);whisper-1,gpt-4o-transcribe, agus/v1/audio/translationsar 26 Feabhra, 2027 (gpt-transcribe,gpt-live-transcribe,v1/realtime/translations). Maidir le guth lán-dúbailte, tá GPT-Live 1 ar fáil san API ón 10 Meán Fómhair, 2026.
Glao feidhme fíor-ama
Ligeann glao feidhme sa Realtime API do do ghníomhaire gutha gníomhartha concreideacha a dhéanamh le linn comhrá: bunachar sonraí a fhiosrú, ordú a chur, nó stádas a sheiceáil. Tá an mheicnic cosúil leis an Responses API, ach oiriúnaithe do shruth imeachta WebSocket.
Uirlisí a dhearbhú sa seisiún
Dearbhaíonn tú uirlisí in session.update, díreach mar a dhéanann tú le treoracha agus guth. Is scéima JSON é gach uirlis a chuireann síos ar a paraiméadair:
tools = [
{
"type": "function",
"name": "get_weather",
"description": "Get the current weather for a given city",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "Name of the city"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "Temperature unit"
}
},
"required": ["city"]
}
},
{
"type": "function",
"name": "book_appointment",
"description": "Book an appointment",
"parameters": {
"type": "object",
"properties": {
"date": {"type": "string", "description": "Date in YYYY-MM-DD format"},
"time": {"type": "string", "description": "Time in HH:MM format"},
"service": {"type": "string", "description": "Type of service"}
},
"required": ["date", "time", "service"]
}
}
]
await ws.send(json.dumps({
"type": "session.update",
"session": {
"type": "realtime",
"output_modalities": ["audio"],
"instructions": "You are a voice assistant. Use the available tools.",
"audio": {"output": {"voice": "marin"}},
"tools": tools,
"tool_choice": "auto"
}
}))
Sreabhadh an ghlao feidhme
Nuair a chinneann an múnla glao a chur ar uirlis, seo a leanas an seicheamh imeachtaí:
response.function_call_arguments.delta
Tagann argóintí feidhme trí shruthú, píosa ar phíosa.
response.function_call_arguments.done
Tá na hargóintí críochnaithe. Is féidir leat an fheidhm a fhorghníomhú anois.
conversation.item.create (tusa)
Seolann tú toradh na feidhme ar ais chuig an múnla.
response.create (tusa)
Iarrann tú ar an múnla leanúint lena fhreagra leis an toradh.
Láimhseálaí feidhme a chur i bhfeidhm
Seo sampla iomlán de láimhseáil glaotha feidhme:
import json
# Clárú na bhfeidhmeanna atá ar fáil
async def get_weather(city: str, unit: str = "celsius") -> dict:
# Glaoigh ar API fíor-ama aimsire
return {"city": city, "temperature": 22, "unit": unit, "condition": "sunny"}
async def book_appointment(date: str, time: str, service: str) -> dict:
# Loighic áirithinte
return {"confirmed": True, "date": date, "time": time, "reference": "RDV-2026-0042"}
FUNCTION_REGISTRY = {
"get_weather": get_weather,
"book_appointment": book_appointment,
}
async def handle_function_call(ws, event):
"""Déanann sé próiseáil ar ghlao feidhme agus filleann sé an toradh."""
response = event["response"]
for output in response.get("output", []):
if output["type"] != "function_call":
continue
func_name = output["name"]
call_id = output["call_id"]
arguments = json.loads(output["arguments"])
print(f"Calling function: {func_name}({arguments})")
# Forghníomhaigh an fheidhm
func = FUNCTION_REGISTRY.get(func_name)
if func:
result = await func(**arguments)
else:
result = {"error": f"Unknown function: {func_name}"}
# Seol an toradh ar ais chuig an múnla
await ws.send(json.dumps({
"type": "conversation.item.create",
"item": {
"type": "function_call_output",
"call_id": call_id,
"output": json.dumps(result)
}
}))
# Iarr ar an múnla leanúint leis an toradh
await ws.send(json.dumps({
"type": "response.create"
}))
Comhtháthú sa lúb imeachta
Comhtháthaíonn an láimhseálaí feidhme i do phríomhlúb:
async def event_loop(ws):
"""Príomhlúb próiseála imeachtaí."""
async for raw_message in ws:
event = json.loads(raw_message)
event_type = event["type"]
if event_type == "response.done":
response = event["response"]
# Seiceáil ar thug an múnla glao ar aon fheidhmeanna
has_function_calls = any(
o["type"] == "function_call"
for o in response.get("output", [])
)
if has_function_calls:
await handle_function_call(ws, event)
elif event_type == "response.output_audio.delta":
# Seinn fuaim na freagra
audio_bytes = base64.b64decode(event["delta"])
player.write(audio_bytes)
elif event_type == "error":
print(f"Error: {event['error']['message']}")
Glaotha feidhme iolracha
Féadfaidh an múnla cinneadh a dhéanamh glao a chur ar fheidhmeanna iolracha in aon fhreagra amháin. Is féidir leis an réimse output de response.done iontrálacha iolracha de chineál function_call a bheith ann. Próiseáil iad go léir sula nglaoíonn tú ar response.create.
Príomhphointí
- Dearbhaítear uirlisí in
session.updateag baint úsáide as scéima JSON caighdeánach - Tagann argóintí trí shruthú via
response.function_call_arguments.delta - Tar éis forghníomhaithe, seol an toradh ar ais via
conversation.item.createleis ancall_id - Glaoigh ar
response.createionas gur féidir leis an múnla a fhreagra gutha a fhoirmiú leis an toradh - Láimhseáil glaotha iolracha: is féidir leis an múnla glao a chur ar roinnt feidhmeanna ag an am céanna