forked from langchain-ai/langgraph-swarm-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomer_support.py
143 lines (118 loc) · 4.09 KB
/
customer_support.py
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
import datetime
from collections import defaultdict
from typing import Callable
from langchain_core.runnables import RunnableConfig
from langchain_openai import ChatOpenAI
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.prebuilt import create_react_agent
from langgraph_swarm import create_handoff_tool, create_swarm
model = ChatOpenAI(model="gpt-4o")
# Mock data for tools
RESERVATIONS = defaultdict(lambda: {"flight_info": {}, "hotel_info": {}})
TOMORROW = (datetime.date.today() + datetime.timedelta(days=1)).isoformat()
FLIGHTS = [
{
"departure_airport": "BOS",
"arrival_airport": "JFK",
"airline": "Jet Blue",
"date": TOMORROW,
"id": "1",
}
]
HOTELS = [
{
"location": "New York",
"name": "McKittrick Hotel",
"neighborhood": "Chelsea",
"id": "1",
}
]
# Flight tools
def search_flights(
departure_airport: str,
arrival_airport: str,
date: str,
) -> list[dict]:
"""Search flights.
Args:
departure_airport: 3-letter airport code for the departure airport. If unsure, use the biggest airport in the area
arrival_airport: 3-letter airport code for the arrival airport. If unsure, use the biggest airport in the area
date: YYYY-MM-DD date
"""
# return all flights for simplicity
return FLIGHTS
def book_flight(
flight_id: str,
config: RunnableConfig,
) -> str:
"""Book a flight."""
user_id = config["configurable"].get("user_id")
flight = [flight for flight in FLIGHTS if flight["id"] == flight_id][0]
RESERVATIONS[user_id]["flight_info"] = flight
return "Successfully booked flight"
# Hotel tools
def search_hotels(location: str) -> list[dict]:
"""Search hotels.
Args:
location: offical, legal city name (proper noun)
"""
# return all hotels for simplicity
return HOTELS
def book_hotel(
hotel_id: str,
config: RunnableConfig,
) -> str:
"""Book a hotel"""
user_id = config["configurable"].get("user_id")
hotel = [hotel for hotel in HOTELS if hotel["id"] == hotel_id][0]
RESERVATIONS[user_id]["hotel_info"] = hotel
return "Successfully booked hotel"
# Define handoff tools
transfer_to_hotel_assistant = create_handoff_tool(
agent_name="hotel_assistant",
description="Transfer user to the hotel-booking assistant that can search for and book hotels.",
)
transfer_to_flight_assistant = create_handoff_tool(
agent_name="flight_assistant",
description="Transfer user to the flight-booking assistant that can search for and book flights.",
)
# Define agent prompt
def make_prompt(base_system_prompt: str) -> Callable[[dict, RunnableConfig], list]:
def prompt(state: dict, config: RunnableConfig) -> list:
user_id = config["configurable"].get("user_id")
current_reservation = RESERVATIONS[user_id]
system_prompt = (
base_system_prompt
+ f"\n\nUser's active reservation: {current_reservation}"
+ f"Today is: {datetime.datetime.now()}"
)
return [{"role": "system", "content": system_prompt}] + state["messages"]
return prompt
# Define agents
flight_assistant = create_react_agent(
model,
[search_flights, book_flight, transfer_to_hotel_assistant],
prompt=make_prompt("You are a flight booking assistant"),
name="flight_assistant",
)
hotel_assistant = create_react_agent(
model,
[search_hotels, book_hotel, transfer_to_flight_assistant],
prompt=make_prompt("You are a hotel booking assistant"),
name="hotel_assistant",
)
# Compile and run!
checkpointer = InMemorySaver()
builder = create_swarm([flight_assistant, hotel_assistant], default_active_agent="flight_assistant")
# Important: compile the swarm with a checkpointer to remember
# previous interactions and last active agent
app = builder.compile(checkpointer=checkpointer)
# config = {"configurable": {"thread_id": "1", "user_id": "1"}}
# result = app.invoke({
# "messages": [
# {
# "role": "user",
# "content": "i am looking for a flight from boston to ny tomorrow"
# }
# ],
# }, config)