-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathagents_sdk.py
More file actions
93 lines (83 loc) · 2.95 KB
/
Copy pathagents_sdk.py
File metadata and controls
93 lines (83 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""Example: Basic async guardrail bundle using Agents SDK with GuardrailAgent."""
import asyncio
from contextlib import suppress
from agents import (
InputGuardrailTripwireTriggered,
OutputGuardrailTripwireTriggered,
Runner,
SQLiteSession,
)
from agents.run import RunConfig
from guardrails import GuardrailAgent
# Define your pipeline configuration
PIPELINE_CONFIG = {
"version": 1,
"pre_flight": {
"version": 1,
"guardrails": [
{
"name": "Moderation",
"config": {
"categories": ["hate", "violence", "self-harm"],
},
},
{"name": "Contains PII", "config": {"entities": ["US_SSN", "PHONE_NUMBER", "EMAIL_ADDRESS"]}},
],
},
"input": {
"version": 1,
"guardrails": [
{
"name": "Custom Prompt Check",
"config": {
"model": "gpt-4.1-mini-2025-04-14",
"confidence_threshold": 0.7,
"system_prompt_details": "Check if the text contains any math problems.",
},
},
],
},
"output": {
"version": 1,
"guardrails": [
{"name": "URL Filter", "config": {"url_allow_list": ["example.com"]}},
],
},
}
async def main() -> None:
"""Main input loop for the customer support agent with input/output guardrails."""
# Create a session for the agent to store the conversation history
session = SQLiteSession("guardrails-session")
# Create agent with guardrails automatically configured from pipeline configuration
AGENT = GuardrailAgent(
config=PIPELINE_CONFIG,
name="Customer support agent",
instructions="You are a customer support agent. You help customers with their questions.",
)
with suppress(KeyboardInterrupt, asyncio.CancelledError):
while True:
try:
user_input = input("Enter a message: ")
result = await Runner.run(
AGENT,
user_input,
run_config=RunConfig(tracing_disabled=True),
session=session,
)
print(f"Assistant: {result.final_output}")
except EOFError:
print("\nExiting.")
break
except InputGuardrailTripwireTriggered as exc:
print("🛑 Input guardrail triggered!")
print(exc.guardrail_result.guardrail.name)
print(exc.guardrail_result.output.output_info)
continue
except OutputGuardrailTripwireTriggered as exc:
print("🛑 Output guardrail triggered!")
print(exc.guardrail_result.guardrail.name)
print(exc.guardrail_result.output.output_info)
continue
if __name__ == "__main__":
asyncio.run(main())
print("\nExiting the program.")