-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Example for streaming guardrails #505
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
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
|
||
from openai.types.responses import ResponseTextDeltaEvent | ||
from pydantic import BaseModel, Field | ||
|
||
from agents import Agent, Runner | ||
|
||
""" | ||
This example shows how to use guardrails as the model is streaming. Output guardrails run after the | ||
final output has been generated; this example runs guardails every N tokens, allowing for early | ||
termination if bad output is detected. | ||
|
||
The expected output is that you'll see a bunch of tokens stream in, then the guardrail will trigger | ||
and stop the streaming. | ||
""" | ||
|
||
|
||
agent = Agent( | ||
name="Assistant", | ||
instructions=( | ||
"You are a helpful assistant. You ALWAYS write long responses, making sure to be verbose " | ||
"and detailed." | ||
), | ||
) | ||
|
||
|
||
class GuardrailOutput(BaseModel): | ||
reasoning: str = Field( | ||
description="Reasoning about whether the response could be understood by a ten year old." | ||
) | ||
is_readable_by_ten_year_old: bool = Field( | ||
description="Whether the response is understandable by a ten year old." | ||
) | ||
|
||
|
||
guardrail_agent = Agent( | ||
name="Checker", | ||
instructions=( | ||
"You will be given a question and a response. Your goal is to judge whether the response " | ||
"is simple enough to be understood by a ten year old." | ||
), | ||
output_type=GuardrailOutput, | ||
model="gpt-4o-mini", | ||
) | ||
|
||
|
||
async def check_guardrail(text: str) -> GuardrailOutput: | ||
result = await Runner.run(guardrail_agent, text) | ||
return result.final_output_as(GuardrailOutput) | ||
|
||
|
||
async def main(): | ||
question = "What is a black hole, and how does it behave?" | ||
result = Runner.run_streamed(agent, question) | ||
current_text = "" | ||
|
||
# We will check the guardrail every N characters | ||
next_guardrail_check_len = 300 | ||
guardrail_task = None | ||
|
||
async for event in result.stream_events(): | ||
if event.type == "raw_response_event" and isinstance(event.data, ResponseTextDeltaEvent): | ||
print(event.data.delta, end="", flush=True) | ||
current_text += event.data.delta | ||
|
||
# Check if it's time to run the guardrail check | ||
# Note that we don't run the guardrail check if there's already a task running. An | ||
# alternate implementation is to have N guardrails running, or cancel the previous | ||
# one. | ||
if len(current_text) >= next_guardrail_check_len and not guardrail_task: | ||
print("Running guardrail check") | ||
guardrail_task = asyncio.create_task(check_guardrail(current_text)) | ||
next_guardrail_check_len += 300 | ||
|
||
# Every iteration of the loop, check if the guardrail has been triggered | ||
if guardrail_task and guardrail_task.done(): | ||
guardrail_result = guardrail_task.result() | ||
if not guardrail_result.is_readable_by_ten_year_old: | ||
print("\n\n================\n\n") | ||
print(f"Guardrail triggered. Reasoning:\n{guardrail_result.reasoning}") | ||
break | ||
|
||
# Do one final check on the final output | ||
guardrail_result = await check_guardrail(current_text) | ||
if not guardrail_result.is_readable_by_ten_year_old: | ||
print("\n\n================\n\n") | ||
print(f"Guardrail triggered. Reasoning:\n{guardrail_result.reasoning}") | ||
|
||
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.