Streamování se sémantickými událostmi

Working with the OpenAI Responses API

James Chapman

AI Curriculum Manager, DataCamp

Proč streamovat?

Streamování vypnuto

Working with the OpenAI Responses API

Proč streamovat?

Streamování zapnuto

Working with the OpenAI Responses API

Sémantické události

  • Strukturované aktualizace popisující aktuální dění
Working with the OpenAI Responses API

Sémantické události

  • Strukturované aktualizace popisující aktuální dění
Typ události Popis
response.created Model začal generovat
response.output_text.delta Částečná aktualizace textu
response.output_text.done Textový blok dokončen
response.function_call.arguments.delta Streamování argumentů nástroje
response.completed Celá odpověď je dokončena
Working with the OpenAI Responses API

Příklad: Základní streamování textu

prompt = "Explain the seasons of the year concisely to a child."

with client.responses.create(model="gpt-5.4-mini", input=prompt, stream=True) as stream:
current_text = ""
for event in stream: if event.type == "response.output_text.delta":
current_text += event.delta print(current_text)
Working with the OpenAI Responses API

Příklad: Základní streamování textu

Streamování textu

Working with the OpenAI Responses API

Příklad: Zpracování více událostí

prompt = "Explain how a neural network learns concisely for a child."

with client.responses.create(model="gpt-5.4-mini", input=prompt, stream=True) as stream:

for event in stream: if event.type == "response.created":
print("Response started...\n")
elif event.type == "response.output_text.done":
print("\n\n--- Text block finished ---\n")
elif event.type == "response.completed":
print(f"\nFull response:\n{current_text}")
Working with the OpenAI Responses API

Příklad: Zpracování více událostí

Více událostí

Working with the OpenAI Responses API

Příklad: Streamování událostí nástrojů

convert_currency() -> str:

  • date_time: str
  • from_timezone: str
  • to_timezone: str
tools = [
    {
        "type": "function",
        "name": "convert_currency",
        ...
     }
]

frankfurter.png

Working with the OpenAI Responses API

Příklad: Streamování událostí nástrojů

prompt = "How much is 120 euros in British pounds using the current exchange rate?"

with client.responses.create(model="gpt-5.4-mini", input=prompt, tools=tools, stream=True) as stream:
current_args = ""
for event in stream: if event.type == "response.function_call_arguments.delta":
current_args += event.delta print("Streaming args:", current_args)
elif event.type == "response.function_call_arguments.done":
print("\nFinal arguments:", event.arguments)
elif event.type == "response.completed":
print("\n--- Completed ---")
Working with the OpenAI Responses API

Příklad: Streamování událostí nástrojů

Streamování událostí nástrojů

Working with the OpenAI Responses API

Shrnutí

Sémantické události v ChatGPT

1 Sémantické události použité v ChatGPT
Working with the OpenAI Responses API

Pojďme procvičovat!

Working with the OpenAI Responses API

Preparing Video For Download...