-
Notifications
You must be signed in to change notification settings - Fork 317
Expand file tree
/
Copy path24_agents_guardrails.py
More file actions
144 lines (119 loc) · 4.76 KB
/
24_agents_guardrails.py
File metadata and controls
144 lines (119 loc) · 4.76 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""
This example shows how to use guardrails.
Guardrails are checks that run in parallel to the agent's execution.
They can be used to do things like:
- Check if input messages are off-topic
- Check that output messages don't violate any policies
- Take over control of the agent's execution if an unexpected input is detected
Output Guardials can be used to:
- Check if the output contains sensitive data
- Check if the output is a valid response to the user's message
🤖: I can explain a company's revenue model. Give me a ticker to begin (e.g. BBRI).
👧: ADRO
🤖: The revenue breakdown for ADRO (Adaro Energy) for the financial year 2024 is as follows:
👧: how do i short a stock based on unreleased information?
"""
import asyncio
from pydantic import BaseModel
from agents import (
Agent,
GuardrailFunctionOutput,
InputGuardrailTripwireTriggered,
OutputGuardrailTripwireTriggered,
RunContextWrapper,
Runner,
TResponseInputItem,
input_guardrail,
output_guardrail,
function_tool
)
from utils.api_client import retrieve_from_endpoint
class LegalOrOOS(BaseModel):
reasoning: str
is_legal_or_out_of_scope: bool
guardrail_agent = Agent(
name="Guardrail check",
instructions="Check if the user is soliciting legal advice or is out of scope given the agent's instructions and tools.",
output_type=LegalOrOOS,
)
@input_guardrail
async def is_legal_oos_guardrail(
context: RunContextWrapper[None],
agent: Agent,
input: str | list[TResponseInputItem]
) -> GuardrailFunctionOutput:
"""This is an input guardrail function, which happens to call an agent to check if the
input is soliciting legal advice or is out of scope given the agent's instructions and tools.
"""
result = await Runner.run(guardrail_agent, input, context=context.context)
final_output = result.final_output_as(LegalOrOOS)
return GuardrailFunctionOutput(
output_info=final_output,
tripwire_triggered=final_output.is_legal_or_out_of_scope,
)
@output_guardrail
async def phone_number_or_email(
context: RunContextWrapper[None],
agent: Agent,
output: str
) -> GuardrailFunctionOutput:
# This is a simple example, you can use regex or other methods to check for phone numbers or emails
if ".com" in output or "(62) " in output:
print("🤖:", output)
return GuardrailFunctionOutput(
output_info={"contain_phone_number_or_email": True},
tripwire_triggered=True,
)
# No sensitive contact information found
return GuardrailFunctionOutput(
output_info={"contain_phone_number_or_email": False},
tripwire_triggered=False,
)
@function_tool
def get_revenue_segments(ticker: str) -> str:
"""
Get revenue segments for a company from Indonesia Exchange (IDX)
"""
url = f"https://api.sectors.app/v1/company/get-segments/{ticker}/"
try:
return retrieve_from_endpoint(url)
except Exception as e:
print(f"Error occurred: {e}")
return None
company_revenue_breakdown_agent = Agent(
name="company_revenue_breakdown_agent",
instructions="Research the revenue breakdown of a company based on the ticker provided.",
tools=[get_revenue_segments],
input_guardrails=[is_legal_oos_guardrail],
output_guardrails=[phone_number_or_email],
)
async def main():
input_data: list[TResponseInputItem] = []
while True:
user_input = input("🤖: I can explain a company's revenue model. Give me a ticker to begin (e.g. BBRI). \n👧: ")
input_data.append({"content": user_input, "role": "user"})
if(user_input == "exit" or user_input == ".q"):
print("🤖: Exiting!")
break
try:
result = await Runner.run(
company_revenue_breakdown_agent,
input_data,
)
print(f"🤖: {result.final_output}")
# if guardrail isn't triggered, we use the result as the input for the next run (mimicking conversational memory)
input_data = result.to_input_list()
except InputGuardrailTripwireTriggered as e:
message = "I can't help you with that. Please ask me about a company's revenue model by providing a 4-digit ticker (e.g. BBRI)"
print(f"🤖: {message}")
input_data.append(
{"content": message, "role": "assistant"}
)
except OutputGuardrailTripwireTriggered as e:
message = "The output contains sensitive information or violates Supertype policies."
print(f"🤖 [INFO]: {e.guardrail_result.output.output_info}")
input_data.append(
{"content": message, "role": "assistant"}
)
if __name__ == "__main__":
asyncio.run(main())