-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathagent.py
More file actions
276 lines (227 loc) · 9.93 KB
/
agent.py
File metadata and controls
276 lines (227 loc) · 9.93 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# The example extends the following example from the OpenAI Agent SDK:
# https://github.com/openai/openai-agents-python/blob/main/examples/customer_service/main.py
from __future__ import annotations as _annotations
import asyncio
import random
import uuid
import sys
import traceback
from pydantic import BaseModel
try:
from agents import (
Agent,
HandoffOutputItem,
ItemHelpers,
MessageOutputItem,
RunContextWrapper,
Runner,
ToolCallItem,
ToolCallOutputItem,
TResponseInputItem,
function_tool,
handoff,
trace,
)
from agents.extensions.handoff_prompt import RECOMMENDED_PROMPT_PREFIX
from agents.mcp import MCPServerStreamableHttp
except ImportError as e:
print(f"❌ Failed to import required modules: {e}")
print(
"💡 Make sure to install the OpenAI Agent SDK: pip install openai-agents>=0.2.3"
)
sys.exit(1)
# Custom prompt prefix for tool use evaluation
TOOL_USE_EVAL_PROMPT_PREFIX = """
After using any tool, you should use the tool_use_evaluator from quotient_mcp to evaluate the effectiveness and accuracy of the tool usage.
"""
### CONTEXT
class AirlineAgentContext(BaseModel):
passenger_name: str | None = None
confirmation_number: str | None = None
seat_number: str | None = None
flight_number: str | None = None
### TOOLS
@function_tool(
name_override="faq_lookup_tool",
description_override="Lookup frequently asked questions.",
)
async def faq_lookup_tool(question: str) -> str:
if "bag" in question or "baggage" in question:
return (
"You are allowed to bring one bag on the plane. "
"It must be under 50 pounds and 22 inches x 14 inches x 9 inches."
)
elif "seats" in question or "plane" in question:
return (
"There are 120 seats on the plane. "
"There are 22 business class seats and 98 economy seats. "
"Exit rows are rows 4 and 16. "
"Rows 5-8 are Economy Plus, with extra legroom. "
)
elif "wifi" in question:
return "We have free wifi on the plane, join Airline-Wifi"
return "I'm sorry, I don't know the answer to that question."
@function_tool
async def update_seat(
context: RunContextWrapper[AirlineAgentContext],
confirmation_number: str,
new_seat: str,
) -> str:
"""
Update the seat for a given confirmation number.
Args:
confirmation_number: The confirmation number for the flight.
new_seat: The new seat to update to.
"""
# Update the context based on the customer's input
context.context.confirmation_number = confirmation_number
context.context.seat_number = new_seat
# Ensure that the flight number has been set by the incoming handoff
assert context.context.flight_number is not None, "Flight number is required"
return f"Updated seat to {new_seat} for confirmation number {confirmation_number}"
### HOOKS
async def on_seat_booking_handoff(
context: RunContextWrapper[AirlineAgentContext],
) -> None:
flight_number = f"FLT-{random.randint(100, 999)}"
context.context.flight_number = flight_number
### QUOTIENT MCP SETUP
async def setup_quotient_mcp():
"""Set up the custom Quotient MCP server connection."""
quotient_mcp = MCPServerStreamableHttp(
params={
"url": "https://mcp.quotientai.co/mcp/",
},
name="Quotient MCP Server",
)
return quotient_mcp
### AGENTS
async def create_agents_with_mcp():
"""Create agents with MCP server integration."""
# Set up Quotient MCP server
quotient_mcp = await setup_quotient_mcp()
await quotient_mcp.connect()
print("✅ MCP server connected")
faq_agent = Agent[AirlineAgentContext](
name="FAQ Agent",
handoff_description="A helpful agent that can answer questions about the airline and access external data through Quotient MCP.",
instructions=f"""{RECOMMENDED_PROMPT_PREFIX}
{TOOL_USE_EVAL_PROMPT_PREFIX}
You are an FAQ agent with access to advanced tools through the Quotient MCP server.
If you are speaking to a customer, you probably were transferred to from the triage agent.
Use the following routine to support the customer:
# Routine
1. Identify the last question asked by the customer.
2. First, try using the MCP tools if they might be relevant to the question.
3. Use the faq lookup tool for standard airline questions.
4. If you cannot answer the question, transfer back to the triage agent.""",
tools=[faq_lookup_tool],
mcp_servers=[quotient_mcp],
)
seat_booking_agent = Agent[AirlineAgentContext](
name="Seat Booking Agent",
handoff_description="A helpful agent that can update a seat on a flight and access external systems through Quotient MCP.",
instructions=f"""{RECOMMENDED_PROMPT_PREFIX}
{TOOL_USE_EVAL_PROMPT_PREFIX}
You are a seat booking agent with access to advanced tools through the Quotient MCP server.
If you are speaking to a customer, you probably were transferred to from the triage agent.
Use the following routine to support the customer:
# Routine
1. Ask for their confirmation number.
2. Ask the customer what their desired seat number is.
3. Use MCP tools if needed to check additional information.
4. Use the update seat tool to update the seat on the flight.
If the customer asks a question that is not related to the routine, transfer back to the triage agent.""",
tools=[update_seat],
mcp_servers=[quotient_mcp],
)
triage_agent = Agent[AirlineAgentContext](
name="Triage Agent",
handoff_description="A triage agent that can delegate a customer's request to the appropriate agent and access external data through Quotient MCP.",
instructions=(
f"{RECOMMENDED_PROMPT_PREFIX} "
f"{TOOL_USE_EVAL_PROMPT_PREFIX} "
"You are a helpful triaging agent with access to the Quotient MCP server tools. "
"You can use your tools to delegate questions to other appropriate agents. "
"You can also use MCP tools to help answer questions or gather information before making handoffs."
),
handoffs=[
faq_agent,
handoff(agent=seat_booking_agent, on_handoff=on_seat_booking_handoff),
],
mcp_servers=[quotient_mcp],
)
faq_agent.handoffs.append(triage_agent)
seat_booking_agent.handoffs.append(triage_agent)
return triage_agent, faq_agent, seat_booking_agent, quotient_mcp
### RUN
async def main():
print("🚀 Setting up agents with Quotient MCP integration...")
quotient_mcp = None
try:
# Create agents with MCP integration
triage_agent, faq_agent, seat_booking_agent, quotient_mcp = (
await create_agents_with_mcp()
)
current_agent: Agent[AirlineAgentContext] = triage_agent
input_items: list[TResponseInputItem] = []
context = AirlineAgentContext()
# Generate conversation ID for tracing
conversation_id = uuid.uuid4().hex[:16]
print(
"💬 You can now ask questions and the agents will use both built-in tools and your MCP tools."
)
print("📝 Try asking about flights, or anything your MCP server can help with!")
print("-" * 60)
while True:
try:
user_input = input("Enter your message (or 'quit' to exit): ")
if user_input.lower() in ["quit", "exit", "q"]:
print("👋 Goodbye!")
break
with trace("Customer service with MCP", group_id=conversation_id):
input_items.append({"content": user_input, "role": "user"})
result = await Runner.run(
current_agent, input_items, context=context
)
for new_item in result.new_items:
agent_name = new_item.agent.name
if isinstance(new_item, MessageOutputItem):
print(
f"🤖 {agent_name}: {ItemHelpers.text_message_output(new_item)}"
)
elif isinstance(new_item, HandoffOutputItem):
print(
f"🔄 Handed off from {new_item.source_agent.name} to {new_item.target_agent.name}"
)
elif isinstance(new_item, ToolCallItem):
print(f"🔧 {agent_name}: Calling a tool")
elif isinstance(new_item, ToolCallOutputItem):
print(
f"📋 {agent_name}: Tool call output: {new_item.output}"
)
else:
print(
f"ℹ️ {agent_name}: Skipping item: {new_item.__class__.__name__}"
)
input_items = result.to_input_list()
current_agent = result.last_agent
except KeyboardInterrupt:
print("\n👋 Goodbye!")
break
except Exception as e:
print(f"❌ Error during conversation: {e}")
print("🔄 Continuing...")
except Exception as e:
print(f"❌ Failed to set up MCP connection: {e}")
print("💡 Make sure your MCP server at https://mcp.quotientai.co is accessible")
print("💡 Check if you need authentication headers or the URL is correct")
finally:
# Clean up MCP server connection if it exists
if quotient_mcp is not None:
try:
await quotient_mcp.cleanup()
except Exception:
pass # Ignore cleanup errors
if __name__ == "__main__":
asyncio.run(main())