-
Notifications
You must be signed in to change notification settings - Fork 509
input/output guardrail decorator #1039
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
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
660be3b
input/output guardrail decorator #1003
michi-okahata 299db87
changed span_kinds name, confirmed no existing semconv for guardrails
michi-okahata 291a373
ruff fixes
michi-okahata 9f5989c
Merge branch 'main' into guardrail_decorator
michi-okahata 5e11268
ruff fix 2
michi-okahata 7eb13ed
combined input/output into a guardrail decorator with parameter 'spec'
michi-okahata d98daea
quick fix to comment
michi-okahata 9c58cdf
Merge branch 'main' into guardrail_decorator
dot-agi 695750d
fix init
michi-okahata bb13199
guardrail examples
michi-okahata fef633d
add guardrail docs
michi-okahata b4a8c81
ruff fix
michi-okahata 2998198
pc fix
michi-okahata 1a6445e
fix agentops init in example notebook
dot-agi 5949af1
ruff again
dot-agi 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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| { | ||
| "cells": [ | ||
| { | ||
| "cell_type": "markdown", | ||
| "id": "f68ce4af", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "# OpenAI Agents Guardrails Demonstration\n", | ||
| "\n", | ||
| "This notebook demonstrates guardrails using the Agents SDK and how one can observe them using the AgentOps platform." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "10bcf29b", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "# Install required packages\n", | ||
| "%pip install agentops\n", | ||
| "%pip install openai-agents\n", | ||
| "%pip install dotenv pydantic" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "a3be4e68", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "# Import dependencies\n", | ||
| "from pydantic import BaseModel\n", | ||
| "from agents import (\n", | ||
| " Agent,\n", | ||
| " GuardrailFunctionOutput,\n", | ||
| " InputGuardrailTripwireTriggered,\n", | ||
| " RunContextWrapper,\n", | ||
| " Runner,\n", | ||
| " TResponseInputItem,\n", | ||
| " input_guardrail,\n", | ||
| ")" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "2d0dddb6", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "# Load API keys\n", | ||
| "import os\n", | ||
| "from dotenv import load_dotenv\n", | ||
| "\n", | ||
| "load_dotenv()\n", | ||
| "\n", | ||
| "os.environ[\"AGENTOPS_API_KEY\"] = os.getenv(\"AGENTOPS_API_KEY\", \"your_api_key_here\")\n", | ||
| "os.environ[\"OPENAI_API_KEY\"] = os.getenv(\"OPENAI_API_KEY\", \"your_openai_api_key_here\")" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "114e216b", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "# Initialize agentops and import the guardrail decorator\n", | ||
| "import agentops\n", | ||
| "from agentops import guardrail\n", | ||
| "\n", | ||
| "agentops.init(api_key=os.environ[\"AGENTOPS_API_KEY\"], tags=[\"agentops-example\"], auto_start_session=False)\n", | ||
| "tracer = agentops.start_trace(trace_name=\"OpenAI Agents Guardrail Example\")" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "0bf8b54d", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "# OpenAI Agents SDK guardrail example with agentops guardrails decorator for observability\n", | ||
| "class MathHomeworkOutput(BaseModel):\n", | ||
| " is_math_homework: bool\n", | ||
| " reasoning: str\n", | ||
| "\n", | ||
| "\n", | ||
| "guardrail_agent = Agent(\n", | ||
| " name=\"Guardrail check\",\n", | ||
| " instructions=\"Check if the user is asking you to do their math homework.\",\n", | ||
| " output_type=MathHomeworkOutput,\n", | ||
| ")\n", | ||
| "\n", | ||
| "\n", | ||
| "@input_guardrail\n", | ||
| "@guardrail(spec=\"input\") # Specify guardrail type as input or output\n", | ||
| "async def math_guardrail(\n", | ||
| " ctx: RunContextWrapper[None], agent: Agent, input: str | list[TResponseInputItem]\n", | ||
| ") -> GuardrailFunctionOutput:\n", | ||
| " result = await Runner.run(guardrail_agent, input, context=ctx.context)\n", | ||
| "\n", | ||
| " return GuardrailFunctionOutput(\n", | ||
| " output_info=result.final_output,\n", | ||
| " tripwire_triggered=result.final_output.is_math_homework,\n", | ||
| " )\n", | ||
| "\n", | ||
| "\n", | ||
| "agent = Agent(\n", | ||
| " name=\"Customer support agent\",\n", | ||
| " instructions=\"You are a customer support agent. You help customers with their questions.\",\n", | ||
| " input_guardrails=[math_guardrail],\n", | ||
| ")\n", | ||
| "\n", | ||
| "\n", | ||
| "async def main():\n", | ||
| " # This should trip the guardrail\n", | ||
| " try:\n", | ||
| " await Runner.run(agent, \"Hello, can you help me solve for x: 2x + 3 = 11?\")\n", | ||
| " print(\"Guardrail didn't trip - this is unexpected\")\n", | ||
| "\n", | ||
| " except InputGuardrailTripwireTriggered:\n", | ||
| " print(\"Math homework guardrail tripped\")\n", | ||
| "\n", | ||
| "\n", | ||
| "await main()" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "id": "63bf8e09", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "agentops.end_trace(tracer, end_state=\"Success\")" | ||
| ] | ||
| } | ||
| ], | ||
| "metadata": { | ||
| "kernelspec": { | ||
| "display_name": "agentops (3.11.11)", | ||
| "language": "python", | ||
| "name": "python3" | ||
| }, | ||
| "language_info": { | ||
| "codemirror_mode": { | ||
| "name": "ipython", | ||
| "version": 3 | ||
| }, | ||
| "file_extension": ".py", | ||
| "mimetype": "text/x-python", | ||
| "name": "python", | ||
| "nbconvert_exporter": "python", | ||
| "pygments_lexer": "ipython3", | ||
| "version": "3.11.11" | ||
| } | ||
| }, | ||
| "nbformat": 4, | ||
| "nbformat_minor": 5 | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.