|
| 1 | +import asyncio |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +from openai.types.responses import ResponseFunctionCallArgumentsDeltaEvent |
| 5 | + |
| 6 | +from agents import Agent, Runner, function_tool |
| 7 | + |
| 8 | + |
| 9 | +@function_tool |
| 10 | +def write_file(filename: str, content: str) -> str: |
| 11 | + """Write content to a file.""" |
| 12 | + return f"File {filename} written successfully" |
| 13 | + |
| 14 | + |
| 15 | +@function_tool |
| 16 | +def create_config(project_name: str, version: str, dependencies: list[str]) -> str: |
| 17 | + """Create a configuration file for a project.""" |
| 18 | + return f"Config for {project_name} v{version} created" |
| 19 | + |
| 20 | + |
| 21 | +async def main(): |
| 22 | + """ |
| 23 | + Demonstrates real-time streaming of function call arguments. |
| 24 | +
|
| 25 | + Function arguments are streamed incrementally as they are generated, |
| 26 | + providing immediate feedback during parameter generation. |
| 27 | + """ |
| 28 | + agent = Agent( |
| 29 | + name="CodeGenerator", |
| 30 | + instructions="You are a helpful coding assistant. Use the provided tools to create files and configurations.", |
| 31 | + tools=[write_file, create_config], |
| 32 | + ) |
| 33 | + |
| 34 | + print("🚀 Function Call Arguments Streaming Demo") |
| 35 | + |
| 36 | + result = Runner.run_streamed( |
| 37 | + agent, |
| 38 | + input="Create a Python web project called 'my-app' with FastAPI. Version 1.0.0, dependencies: fastapi, uvicorn" |
| 39 | + ) |
| 40 | + |
| 41 | + # Track function calls for detailed output |
| 42 | + function_calls: dict[Any, dict[str, Any]] = {} # call_id -> {name, arguments} |
| 43 | + current_active_call_id = None |
| 44 | + |
| 45 | + async for event in result.stream_events(): |
| 46 | + if event.type == "raw_response_event": |
| 47 | + # Function call started |
| 48 | + if event.data.type == "response.output_item.added": |
| 49 | + if getattr(event.data.item, "type", None) == "function_call": |
| 50 | + function_name = getattr(event.data.item, "name", "unknown") |
| 51 | + call_id = getattr(event.data.item, "call_id", "unknown") |
| 52 | + |
| 53 | + function_calls[call_id] = { |
| 54 | + 'name': function_name, |
| 55 | + 'arguments': "" |
| 56 | + } |
| 57 | + current_active_call_id = call_id |
| 58 | + print(f"\n📞 Function call streaming started: {function_name}()") |
| 59 | + print("📝 Arguments building...") |
| 60 | + |
| 61 | + # Real-time argument streaming |
| 62 | + elif isinstance(event.data, ResponseFunctionCallArgumentsDeltaEvent): |
| 63 | + if current_active_call_id and current_active_call_id in function_calls: |
| 64 | + function_calls[current_active_call_id]['arguments'] += event.data.delta |
| 65 | + print(f" + {event.data.delta}", end="", flush=True) |
| 66 | + |
| 67 | + # Function call completed |
| 68 | + elif event.data.type == "response.output_item.done": |
| 69 | + if hasattr(event.data.item, 'call_id'): |
| 70 | + call_id = getattr(event.data.item, "call_id", "unknown") |
| 71 | + if call_id in function_calls: |
| 72 | + function_info = function_calls[call_id] |
| 73 | + print(f"\n✅ Function call streaming completed: {function_info['name']}") |
| 74 | + print() |
| 75 | + if current_active_call_id == call_id: |
| 76 | + current_active_call_id = None |
| 77 | + |
| 78 | + print("Summary of all function calls:") |
| 79 | + for call_id, info in function_calls.items(): |
| 80 | + print(f" - #{call_id}: {info['name']}({info['arguments']})") |
| 81 | + |
| 82 | + print(f"\nResult: {result.final_output}") |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + asyncio.run(main()) |
0 commit comments