-
Notifications
You must be signed in to change notification settings - Fork 3k
feat: Add on_stream to agents as tools #2169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
seratch
wants to merge
6
commits into
main
Choose a base branch
from
agent-as-tool-streaming
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+753
−22
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f161dd3
feat: Add on_stream to agents as tools
seratch 4ce48a1
fix mypy errors
seratch 7f1672a
Enrich the event properties
seratch 94d2239
improve the concurrency of event handling
seratch dbcb6e3
fix
seratch a8104bf
get rid of getattr
seratch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import asyncio | ||
|
|
||
| from agents import Agent, AgentToolStreamEvent, ModelSettings, Runner, function_tool, trace | ||
|
|
||
|
|
||
| @function_tool( | ||
| name_override="billing_status_checker", | ||
| description_override="Answer questions about customer billing status.", | ||
| ) | ||
| def billing_status_checker(customer_id: str | None = None, question: str = "") -> str: | ||
| """Return a canned billing answer or a fallback when the question is unrelated.""" | ||
| normalized = question.lower() | ||
| if "bill" in normalized or "billing" in normalized: | ||
| return f"This customer (ID: {customer_id})'s bill is $100" | ||
| return "I can only answer questions about billing." | ||
|
|
||
|
|
||
| def handle_stream(event: AgentToolStreamEvent) -> None: | ||
| """Print streaming events emitted by the nested billing agent.""" | ||
| stream = event["event"] | ||
| tool_call = event.get("tool_call") | ||
| tool_call_info = tool_call.call_id if tool_call is not None else "unknown" | ||
| print(f"[stream] agent={event['agent'].name} call={tool_call_info} type={stream.type} {stream}") | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| with trace("Agents as tools streaming example"): | ||
| billing_agent = Agent( | ||
| name="Billing Agent", | ||
| instructions="You are a billing agent that answers billing questions.", | ||
| model_settings=ModelSettings(tool_choice="required"), | ||
| tools=[billing_status_checker], | ||
| ) | ||
|
|
||
| billing_agent_tool = billing_agent.as_tool( | ||
| tool_name="billing_agent", | ||
| tool_description="You are a billing agent that answers billing questions.", | ||
| on_stream=handle_stream, | ||
| ) | ||
|
|
||
| main_agent = Agent( | ||
| name="Customer Support Agent", | ||
| instructions=( | ||
| "You are a customer support agent. Always call the billing agent to answer billing " | ||
| "questions and return the billing agent response to the user." | ||
| ), | ||
| tools=[billing_agent_tool], | ||
| ) | ||
|
|
||
| result = await Runner.run( | ||
| main_agent, | ||
| "Hello, my customer ID is ABC123. How much is my bill for this month?", | ||
| ) | ||
|
|
||
| print(f"\nFinal response:\n{result.final_output}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can be kinda bad since
on_streamwill block you from going to the next event. fine for now, but we should consider a queue based pattern where we write to a queue from here and the consumer reads from the queue, and we aren't blockingThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good call; actually i made these executions async in the TS SDK. so will make this more efficient and consistent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
improved by 94d2239