14 lines
626 B
Python
14 lines
626 B
Python
from agents import Agent, Runner, SQLiteSession
|
|
from openai.types.responses import ResponseTextDeltaEvent
|
|
|
|
async def stream_response(agent: Agent, prompt: str, session: SQLiteSession):
|
|
"""Stream agent response and update the UI."""
|
|
|
|
try:
|
|
result = Runner.run_streamed(agent, input=prompt, session=session)
|
|
async for event in result.stream_events():
|
|
if event.type == "raw_response_event" and isinstance(event.data, ResponseTextDeltaEvent):
|
|
yield event.data.delta # <-- sends the next piece of response text
|
|
except Exception as e:
|
|
yield f"⚠️🖨️ Error: {e}"
|