mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-09-13 13:17:52 +02:00
docs: update multi-agent multi-turn how-to (#3241)
This commit is contained in:
-1
File diff suppressed because one or more lines are too long
+1
File diff suppressed because one or more lines are too long
@@ -88,8 +88,8 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%%capture --no-stderr\n",
|
||||
"%pip install -U langgraph langchain-anthropic"
|
||||
"# %%capture --no-stderr\n",
|
||||
"# %pip install -U langgraph langchain-anthropic"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -99,7 +99,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"name": "stdin",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"ANTHROPIC_API_KEY: ········\n"
|
||||
@@ -212,11 +212,13 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": 6,
|
||||
"id": "aa4bdbff-9461-46cc-aee9-8a22d3c3d9ec",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import uuid\n",
|
||||
"\n",
|
||||
"from langchain_core.messages import AIMessage\n",
|
||||
"from langchain_anthropic import ChatAnthropic\n",
|
||||
"from langgraph.prebuilt import create_react_agent\n",
|
||||
@@ -273,11 +275,14 @@
|
||||
"checkpointer = MemorySaver()\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def string_to_uuid(input_string):\n",
|
||||
" return str(uuid.uuid5(uuid.NAMESPACE_URL, input_string))\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"@entrypoint(checkpointer=checkpointer)\n",
|
||||
"def multi_turn_graph(messages, previous):\n",
|
||||
" previous = previous or []\n",
|
||||
" messages = add_messages(previous, messages)\n",
|
||||
"\n",
|
||||
" call_active_agent = call_travel_advisor\n",
|
||||
" while True:\n",
|
||||
" agent_messages = call_active_agent(messages).result()\n",
|
||||
@@ -292,7 +297,17 @@
|
||||
" ai_msg = next(m for m in reversed(agent_messages) if isinstance(m, AIMessage))\n",
|
||||
" if not ai_msg.tool_calls:\n",
|
||||
" user_input = interrupt(value=\"Ready for user input.\")\n",
|
||||
" messages = add_messages(messages, [{\"role\": \"user\", \"content\": user_input}])\n",
|
||||
" # Add user input as a human message\n",
|
||||
" # NOTE: we generate unique ID for the human message based on its content\n",
|
||||
" # it's important, since on subsequent invocations previous user input (interrupt) values\n",
|
||||
" # will be looked up again and we will attempt to add them again here\n",
|
||||
" # `add_messages` deduplicates messages based on the ID, ensuring correct message history\n",
|
||||
" human_message = {\n",
|
||||
" \"role\": \"user\",\n",
|
||||
" \"content\": user_input,\n",
|
||||
" \"id\": string_to_uuid(user_input),\n",
|
||||
" }\n",
|
||||
" messages = add_messages(messages, [human_message])\n",
|
||||
" continue\n",
|
||||
"\n",
|
||||
" tool_call = ai_msg.tool_calls[-1]\n",
|
||||
@@ -318,8 +333,8 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "161e0cf1-d13a-4026-8f89-bdab67d1ad4d",
|
||||
"execution_count": 7,
|
||||
"id": "2b6fde57-86e3-440e-a7bf-f1e9b5ed9ff2",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -329,71 +344,69 @@
|
||||
"\n",
|
||||
"--- Conversation Turn 1 ---\n",
|
||||
"\n",
|
||||
"User: {'role': 'user', 'content': 'i wanna go somewhere warm in the caribbean'}\n",
|
||||
"User: {'role': 'user', 'content': 'i wanna go somewhere warm in the caribbean', 'id': 'f48d82a7-7efa-43f5-ad4c-541758c95f61'}\n",
|
||||
"\n",
|
||||
"call_travel_advisor: Based on the recommendations, Aruba would be an excellent choice for your Caribbean getaway! Aruba is known for its perfect warm weather year-round, with consistent temperatures around 82°F (28°C) and very little rainfall. The island offers:\n",
|
||||
"call_travel_advisor: Based on the recommendations, Aruba would be an excellent choice for your Caribbean getaway! Known as \"One Happy Island,\" Aruba offers:\n",
|
||||
"- Year-round warm weather with consistent temperatures around 82°F (28°C)\n",
|
||||
"- Beautiful white sand beaches like Eagle Beach and Palm Beach\n",
|
||||
"- Crystal clear waters perfect for swimming and snorkeling\n",
|
||||
"- Minimal rainfall and location outside the hurricane belt\n",
|
||||
"- Rich culture blending Dutch and Caribbean influences\n",
|
||||
"- Various activities from water sports to desert-like landscape exploration\n",
|
||||
"- Excellent dining and shopping options\n",
|
||||
"\n",
|
||||
"1. Beautiful white-sand beaches like Eagle Beach and Palm Beach\n",
|
||||
"2. Crystal clear waters perfect for swimming and snorkeling\n",
|
||||
"3. Constant cooling trade winds that make the warm weather comfortable\n",
|
||||
"4. A mix of luxury resorts and boutique hotels\n",
|
||||
"5. Diverse activities from water sports to desert-like terrain exploration\n",
|
||||
"6. Great dining and nightlife options\n",
|
||||
"7. Safe and tourist-friendly environment\n",
|
||||
"\n",
|
||||
"Would you like me to connect you with our hotel advisor to help you find the perfect place to stay in Aruba?\n",
|
||||
"Would you like me to help you find suitable accommodations in Aruba? I can transfer you to our hotel advisor who can recommend specific hotels based on your preferences.\n",
|
||||
"\n",
|
||||
"--- Conversation Turn 2 ---\n",
|
||||
"\n",
|
||||
"User: Command(resume='could you recommend a nice hotel in one of the areas and tell me which area it is.')\n",
|
||||
"\n",
|
||||
"call_hotel_advisor: Based on the recommendations, I can highlight two excellent options in different areas:\n",
|
||||
"call_hotel_advisor: I can recommend two excellent options in different areas:\n",
|
||||
"\n",
|
||||
"1. The Ritz-Carlton, Aruba - Located in Palm Beach\n",
|
||||
"- Part of the high-rise hotel district\n",
|
||||
"- Luxury beachfront resort with full-service spa\n",
|
||||
"- Multiple restaurants and a casino\n",
|
||||
"- Perfect for those who want to be in the heart of the action\n",
|
||||
"- Close to shopping, dining, and nightlife\n",
|
||||
"- Luxury beachfront resort\n",
|
||||
"- Located in the vibrant Palm Beach area, known for its lively atmosphere\n",
|
||||
"- Close to restaurants, shopping, and nightlife\n",
|
||||
"- Perfect for those who want a more active vacation with plenty of amenities nearby\n",
|
||||
"\n",
|
||||
"2. Bucuti & Tara Beach Resort - Located in Eagle Beach\n",
|
||||
"- Adults-only boutique resort\n",
|
||||
"- Located on Eagle Beach, voted one of the best beaches in the world\n",
|
||||
"- More serene and romantic atmosphere\n",
|
||||
"- Perfect for couples and those seeking a quieter vacation\n",
|
||||
"- Known for its excellent service and sustainability practices\n",
|
||||
"- Situated on the quieter Eagle Beach\n",
|
||||
"- Known for its romantic atmosphere and excellent service\n",
|
||||
"- Ideal for couples seeking a more peaceful, intimate setting\n",
|
||||
"\n",
|
||||
"Would you like more specific information about either of these properties or would you like to explore other options in either area?\n",
|
||||
"Would you like more specific information about either of these properties or their locations?\n",
|
||||
"\n",
|
||||
"--- Conversation Turn 3 ---\n",
|
||||
"\n",
|
||||
"User: Command(resume='i like the first one. could you recommend something to do near the hotel?')\n",
|
||||
"\n",
|
||||
"call_travel_advisor: Near the Ritz-Carlton in Palm Beach, you can enjoy several fantastic activities:\n",
|
||||
"call_travel_advisor: Near The Ritz-Carlton in Palm Beach, here are some popular activities you can enjoy:\n",
|
||||
"\n",
|
||||
"1. Paseo Herencia Mall - A beautiful outdoor shopping and entertainment center just a short walk away\n",
|
||||
"2. High-Rise Beach Strip - Perfect for beach walks and water sports\n",
|
||||
"3. Bubali Bird Sanctuary - A nature preserve where you can spot local wildlife\n",
|
||||
"4. The Butterfly Farm - A unique attraction featuring hundreds of exotic butterflies\n",
|
||||
"5. Palm Beach Plaza Mall - Great for shopping and dining\n",
|
||||
"6. Various water sports operators offering:\n",
|
||||
" - Jet skiing\n",
|
||||
" - Parasailing\n",
|
||||
" - Snorkeling trips\n",
|
||||
" - Sunset sailing cruises\n",
|
||||
"1. Palm Beach Strip - Take a walk along this bustling strip filled with restaurants, shops, and bars\n",
|
||||
"2. Visit the Bubali Bird Sanctuary - Just a short distance away\n",
|
||||
"3. Try your luck at the Stellaris Casino - Located right in The Ritz-Carlton\n",
|
||||
"4. Water Sports at Palm Beach - Right in front of the hotel you can:\n",
|
||||
" - Go parasailing\n",
|
||||
" - Try jet skiing\n",
|
||||
" - Take a sunset sailing cruise\n",
|
||||
"5. Visit the Palm Beach Plaza Mall - High-end shopping just a short walk away\n",
|
||||
"6. Enjoy dinner at Madame Janette's - One of Aruba's most famous restaurants nearby\n",
|
||||
"\n",
|
||||
"Additionally, the hotel concierge can arrange most activities directly for you. Would you like more specific information about any of these activities?\n"
|
||||
"Would you like more specific information about any of these activities or other suggestions in the area?\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import uuid\n",
|
||||
"\n",
|
||||
"thread_config = {\"configurable\": {\"thread_id\": uuid.uuid4()}}\n",
|
||||
"\n",
|
||||
"inputs = [\n",
|
||||
" # 1st round of conversation,\n",
|
||||
" {\"role\": \"user\", \"content\": \"i wanna go somewhere warm in the caribbean\"},\n",
|
||||
" {\n",
|
||||
" \"role\": \"user\",\n",
|
||||
" \"content\": \"i wanna go somewhere warm in the caribbean\",\n",
|
||||
" \"id\": str(uuid.uuid4()),\n",
|
||||
" },\n",
|
||||
" # Since we're using `interrupt`, we'll need to resume using the Command primitive.\n",
|
||||
" # 2nd round of conversation,\n",
|
||||
" Command(\n",
|
||||
|
||||
Reference in New Issue
Block a user