mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-08-17 21:25:46 +02:00
code review
This commit is contained in:
@@ -6,7 +6,7 @@ Welcome to the LangGraph how-to guides! These guides provide practical, step-by-
|
||||
|
||||
The core guides show how to address common needs when building out AI workflows, with special focus placed on [ReAct](https://arxiv.org/abs/2210.03629)-style agents with [tool calling](https://python.langchain.com/docs/modules/model_io/chat/function_calling/).
|
||||
|
||||
- [ReAct agent](create-react-agent.ipynb): How to create a simple ReAct agent
|
||||
- [ReAct agent](create-react-agent.ipynb): How to create a tool-calling agent that **Re**asons and **Act**s to accomplish tasks
|
||||
- [Persistence](persistence.ipynb): How to give your graph "memory" and resilience by saving and loading state
|
||||
- [Time travel](time-travel.ipynb): How to navigate and manipulate graph state history once it's persisted
|
||||
- [Async execution](async.ipynb): How to run nodes asynchronously for improved performance
|
||||
|
||||
+172
-262
@@ -5,7 +5,7 @@
|
||||
"id": "992c4695-ec4f-428d-bd05-fb3b5fbd70f4",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# How to: create a ReAct agent"
|
||||
"# How to create a ReAct agent"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -16,7 +16,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%%capture --no-stderr\n",
|
||||
"%pip install -U langgraph langchain-community langchain-openai tavily-python"
|
||||
"%pip install -U langgraph langchain-openai"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -37,8 +37,7 @@
|
||||
"name": "stdin",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"OPENAI_API_KEY: ········\n",
|
||||
"TAVILY_API_KEY: ········\n"
|
||||
"OPENAI_API_KEY: ········\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -48,12 +47,11 @@
|
||||
"\n",
|
||||
"\n",
|
||||
"def _set_env(var: str):\n",
|
||||
" # if not os.environ.get(var):\n",
|
||||
" os.environ[var] = getpass.getpass(f\"{var}: \")\n",
|
||||
" if not os.environ.get(var):\n",
|
||||
" os.environ[var] = getpass.getpass(f\"{var}: \")\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"_set_env(\"OPENAI_API_KEY\")\n",
|
||||
"_set_env(\"TAVILY_API_KEY\")\n",
|
||||
"\n",
|
||||
"# Recommended\n",
|
||||
"_set_env(\"LANGCHAIN_API_KEY\")\n",
|
||||
@@ -66,7 +64,7 @@
|
||||
"id": "03c0f089-070c-4cd4-87e0-6c51f2477b82",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## How to: create a simple ReAct agent"
|
||||
"## How to create a simple ReAct agent"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -74,12 +72,12 @@
|
||||
"id": "f1c8e137-e6e6-4f9f-bd1b-77359a1c4ec5",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Let's create a simple ReAct agent app that can search the web. The app consists of an agent (LLM) and tools. As we interact with the app, we will first call the agent (LLM) to decide if we should use tools. Then we will run a loop: \n",
|
||||
"Let's create a simple ReAct agent app that can check the weather. The app consists of an agent (LLM) and tools. As we interact with the app, we will first call the agent (LLM) to decide if we should use tools. Then we will run a loop: \n",
|
||||
"\n",
|
||||
"1. If the agent said to take an action (i.e. call tool), we'll run the tools and pass the results back to the agent\n",
|
||||
"2. If the agent did not ask to run tools, we will finish (respond to the user)\n",
|
||||
"\n",
|
||||
"In our example we'll use `ChatOpenAI` as our agent and [Tavily Search API](https://tavily.com/) as our tool."
|
||||
"In our example we'll use `ChatOpenAI` as our agent and a custom tool that returns pre-defined values for weather in two cities (NYC & SF)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -93,51 +91,30 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "47726059-e6ea-4c5e-babe-aa5e6bad739f",
|
||||
"id": "029c838a-c6e7-4679-9a8b-953703fe3041",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_openai import ChatOpenAI"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "ea3a8657-ecb3-42f9-8b94-cd2bbca68055",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"model = ChatOpenAI(model=\"gpt-4o\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "98934efd-1b28-4475-af26-b4d55c8eae18",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_community.tools.tavily_search import TavilySearchResults"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"id": "c69aac7b-0053-4c28-8524-1d5d58451a87",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"tavily_search_tool = TavilySearchResults(max_results=1)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"id": "e132bd2c-a98a-46e2-b0a8-02ec4dec4f58",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"tools = [tavily_search_tool]"
|
||||
"from typing import Literal\n",
|
||||
"\n",
|
||||
"from langchain_core.tools import tool\n",
|
||||
"from langchain_openai import ChatOpenAI\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"model = ChatOpenAI(model=\"gpt-4o\", temperature=0)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"@tool\n",
|
||||
"def get_weather(city: Literal[\"nyc\", \"sf\"]):\n",
|
||||
" \"\"\"Use this to get weather information.\"\"\"\n",
|
||||
" if city == \"nyc\":\n",
|
||||
" return \"It might be cloudy in nyc\"\n",
|
||||
" elif city == \"sf\":\n",
|
||||
" return \"It's always sunny in sf\"\n",
|
||||
" else:\n",
|
||||
" raise AssertionError(\"Unknown city\")\n",
|
||||
"\n",
|
||||
"tools = [get_weather]"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -150,38 +127,19 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"id": "11fa1a0f-b99e-4b39-9827-b191b601e79c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langgraph.prebuilt import create_react_agent"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"id": "fe4bc2a0-075d-4042-8bfb-738cbf1f98de",
|
||||
"execution_count": 4,
|
||||
"id": "3d2df9e7-4c2c-494a-9249-5a2ee32eca3e",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langgraph.prebuilt import create_react_agent\n",
|
||||
"graph = create_react_agent(model, tools=tools)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"id": "65256d47-2fab-416e-a9d3-4fac25951c0a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from IPython.display import display, Image"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"id": "7dc12855-2cb4-4f64-80cf-ef3297e6eb96",
|
||||
"execution_count": 5,
|
||||
"id": "fa16de4c-aac0-4ff4-ab69-60d399f75423",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -196,12 +154,14 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from IPython.display import display, Image\n",
|
||||
"\n",
|
||||
"display(Image(graph.get_graph().draw_mermaid_png()))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"execution_count": 6,
|
||||
"id": "16636975-5f2d-4dc7-ab8e-d0bea0830a28",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -225,7 +185,17 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"execution_count": 7,
|
||||
"id": "46aa3f8f-067d-4178-99f8-806071ea78dd",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_core.tools import Tool"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"id": "9ffff6c3-a4f5-47c9-b51d-97caaee85cd6",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -238,27 +208,17 @@
|
||||
"what is the weather in sf\n",
|
||||
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
|
||||
"Tool Calls:\n",
|
||||
" tavily_search_results_json (call_3mZvRQf2INoE9kHCtMD7wYXf)\n",
|
||||
" Call ID: call_3mZvRQf2INoE9kHCtMD7wYXf\n",
|
||||
" get_weather (call_g6w9lHn3fxYo2ABE3Ihhprbr)\n",
|
||||
" Call ID: call_g6w9lHn3fxYo2ABE3Ihhprbr\n",
|
||||
" Args:\n",
|
||||
" query: current weather in San Francisco\n",
|
||||
" city: sf\n",
|
||||
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
|
||||
"Name: tavily_search_results_json\n",
|
||||
"Name: get_weather\n",
|
||||
"\n",
|
||||
"[{\"url\": \"https://www.weatherapi.com/\", \"content\": \"{'location': {'name': 'San Francisco', 'region': 'California', 'country': 'United States of America', 'lat': 37.78, 'lon': -122.42, 'tz_id': 'America/Los_Angeles', 'localtime_epoch': 1718290752, 'localtime': '2024-06-13 7:59'}, 'current': {'last_updated_epoch': 1718289900, 'last_updated': '2024-06-13 07:45', 'temp_c': 14.4, 'temp_f': 57.9, 'is_day': 1, 'condition': {'text': 'Partly cloudy', 'icon': '//cdn.weatherapi.com/weather/64x64/day/116.png', 'code': 1003}, 'wind_mph': 2.2, 'wind_kph': 3.6, 'wind_degree': 10, 'wind_dir': 'N', 'pressure_mb': 1013.0, 'pressure_in': 29.92, 'precip_mm': 0.01, 'precip_in': 0.0, 'humidity': 84, 'cloud': 75, 'feelslike_c': 13.4, 'feelslike_f': 56.0, 'windchill_c': 10.1, 'windchill_f': 50.1, 'heatindex_c': 11.7, 'heatindex_f': 53.1, 'dewpoint_c': 8.8, 'dewpoint_f': 47.9, 'vis_km': 16.0, 'vis_miles': 9.0, 'uv': 3.0, 'gust_mph': 12.7, 'gust_kph': 20.4}}\"}]\n",
|
||||
"It's always sunny in sf\n",
|
||||
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
|
||||
"\n",
|
||||
"The current weather in San Francisco is as follows:\n",
|
||||
"- **Condition:** Partly cloudy\n",
|
||||
"- **Temperature:** 14.4°C (57.9°F)\n",
|
||||
"- **Feels Like:** 13.4°C (56.0°F)\n",
|
||||
"- **Wind:** 2.2 mph (3.6 kph) from the North\n",
|
||||
"- **Humidity:** 84%\n",
|
||||
"- **Pressure:** 1013.0 mb\n",
|
||||
"- **Visibility:** 16.0 km (9.0 miles)\n",
|
||||
"- **UV Index:** 3\n",
|
||||
"\n",
|
||||
"\n"
|
||||
"The weather in San Francisco is currently sunny.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -277,7 +237,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"execution_count": 9,
|
||||
"id": "187479f9-32fa-4611-9487-cf816ba2e147",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -290,7 +250,7 @@
|
||||
"who built you?\n",
|
||||
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
|
||||
"\n",
|
||||
"I was developed by OpenAI, a research organization focused on artificial intelligence. OpenAI creates and maintains AI models like me to assist with a wide range of tasks and provide information.\n"
|
||||
"I was created by OpenAI, a research organization focused on developing and advancing artificial intelligence technology.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -304,7 +264,7 @@
|
||||
"id": "82b06aa3-6414-48e6-ba11-07bf065a316a",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## How to: add system prompt to `create_react_agent`"
|
||||
"## How to add system prompt to `create_react_agent`"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -319,19 +279,8 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"id": "2396d3ac-88f8-4a40-9e97-442173ba843c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"system_prompt = \"You are a helpful bot named Fred.\"\n",
|
||||
"graph = create_react_agent(model, tools, messages_modifier=system_prompt)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"id": "50d989d5-424e-447e-b70e-931ead21e663",
|
||||
"execution_count": 10,
|
||||
"id": "23ea0a31-3e6b-433b-a832-036655579ebe",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -342,24 +291,25 @@
|
||||
"\n",
|
||||
"What's your name? And what's the weather in SF?\n",
|
||||
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
|
||||
"\n",
|
||||
"My name is Fred. Let me check the current weather in San Francisco for you.\n",
|
||||
"Tool Calls:\n",
|
||||
" tavily_search_results_json (call_UuiAgBOpGLekVLNJxwjsTzGY)\n",
|
||||
" Call ID: call_UuiAgBOpGLekVLNJxwjsTzGY\n",
|
||||
" get_weather (call_PGwTiytTVAAvNKWp4nznPi4s)\n",
|
||||
" Call ID: call_PGwTiytTVAAvNKWp4nznPi4s\n",
|
||||
" Args:\n",
|
||||
" query: current weather in San Francisco\n",
|
||||
" city: sf\n",
|
||||
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
|
||||
"Name: tavily_search_results_json\n",
|
||||
"Name: get_weather\n",
|
||||
"\n",
|
||||
"[{\"url\": \"https://www.weatherapi.com/\", \"content\": \"{'location': {'name': 'San Francisco', 'region': 'California', 'country': 'United States of America', 'lat': 37.78, 'lon': -122.42, 'tz_id': 'America/Los_Angeles', 'localtime_epoch': 1718290752, 'localtime': '2024-06-13 7:59'}, 'current': {'last_updated_epoch': 1718289900, 'last_updated': '2024-06-13 07:45', 'temp_c': 14.4, 'temp_f': 57.9, 'is_day': 1, 'condition': {'text': 'Partly cloudy', 'icon': '//cdn.weatherapi.com/weather/64x64/day/116.png', 'code': 1003}, 'wind_mph': 2.2, 'wind_kph': 3.6, 'wind_degree': 10, 'wind_dir': 'N', 'pressure_mb': 1013.0, 'pressure_in': 29.92, 'precip_mm': 0.01, 'precip_in': 0.0, 'humidity': 84, 'cloud': 75, 'feelslike_c': 13.4, 'feelslike_f': 56.0, 'windchill_c': 10.1, 'windchill_f': 50.1, 'heatindex_c': 11.7, 'heatindex_f': 53.1, 'dewpoint_c': 8.8, 'dewpoint_f': 47.9, 'vis_km': 16.0, 'vis_miles': 9.0, 'uv': 3.0, 'gust_mph': 12.7, 'gust_kph': 20.4}}\"}]\n",
|
||||
"It's always sunny in sf\n",
|
||||
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
|
||||
"\n",
|
||||
"My name is Fred. The current weather in San Francisco is partly cloudy with a temperature of approximately 14.4°C (57.9°F). The wind is blowing from the north at about 2.2 mph (3.6 kph), and the humidity is around 84%.\n"
|
||||
"My name is Fred. The weather in San Francisco is currently sunny.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"system_prompt = \"You are a helpful bot named Fred.\"\n",
|
||||
"graph = create_react_agent(model, tools, messages_modifier=system_prompt)\n",
|
||||
"\n",
|
||||
"inputs = {\"messages\": [(\"user\", \"What's your name? And what's the weather in SF?\")]}\n",
|
||||
"print_stream(graph.stream(inputs, stream_mode=\"values\"))"
|
||||
]
|
||||
@@ -374,29 +324,8 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"id": "894b9d53-cd99-465c-b183-988f797d6b5b",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_core.prompts import ChatPromptTemplate\n",
|
||||
"prompt = ChatPromptTemplate.from_messages([\n",
|
||||
" (\"system\", \"You are a helpful bot named Fred.\"),\n",
|
||||
" (\"placeholder\", \"{messages}\"),\n",
|
||||
" (\"user\", \"Remember, always be polite!\"),\n",
|
||||
"])\n",
|
||||
"\n",
|
||||
"def modify_messages(messages: list):\n",
|
||||
" # You can do more complex modifications here\n",
|
||||
" return prompt.invoke({\"messages\": messages})\n",
|
||||
"\n",
|
||||
"graph = create_react_agent(model, tools, messages_modifier=modify_messages)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"id": "8602282f-72ec-405f-b014-e1e319f73d33",
|
||||
"execution_count": 11,
|
||||
"id": "23c40ab3-c574-47be-a80f-654e9035cd6d",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -405,43 +334,38 @@
|
||||
"text": [
|
||||
"================================\u001b[1m Human Message \u001b[0m=================================\n",
|
||||
"\n",
|
||||
"What's your name? And what's the weather in SF?\n",
|
||||
"What's my name? And what's the weather in SF?\n",
|
||||
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
|
||||
"\n",
|
||||
"Hello! My name is Fred. Let me check the current weather in San Francisco for you.\n",
|
||||
"Tool Calls:\n",
|
||||
" tavily_search_results_json (call_QOco9uwxOZaoh9WaSwoMc3eA)\n",
|
||||
" Call ID: call_QOco9uwxOZaoh9WaSwoMc3eA\n",
|
||||
" get_weather (call_scpzIjdK3l411zcEn2T00Xm0)\n",
|
||||
" Call ID: call_scpzIjdK3l411zcEn2T00Xm0\n",
|
||||
" Args:\n",
|
||||
" query: current weather in San Francisco\n",
|
||||
" city: sf\n",
|
||||
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
|
||||
"Name: tavily_search_results_json\n",
|
||||
"Name: get_weather\n",
|
||||
"\n",
|
||||
"HTTPError('502 Server Error: Bad Gateway for url: https://api.tavily.com/search')\n",
|
||||
"It's always sunny in sf\n",
|
||||
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
|
||||
"\n",
|
||||
"Of course! I always strive to be polite and helpful. Let's try to find out the current weather in San Francisco again.\n",
|
||||
"Tool Calls:\n",
|
||||
" tavily_search_results_json (call_G6x1cxoXQbRBPZ8i18X40j0d)\n",
|
||||
" Call ID: call_G6x1cxoXQbRBPZ8i18X40j0d\n",
|
||||
" Args:\n",
|
||||
" query: current weather in San Francisco\n",
|
||||
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
|
||||
"Name: tavily_search_results_json\n",
|
||||
"\n",
|
||||
"[{\"url\": \"https://www.weatherapi.com/\", \"content\": \"{'location': {'name': 'San Francisco', 'region': 'California', 'country': 'United States of America', 'lat': 37.78, 'lon': -122.42, 'tz_id': 'America/Los_Angeles', 'localtime_epoch': 1718290752, 'localtime': '2024-06-13 7:59'}, 'current': {'last_updated_epoch': 1718289900, 'last_updated': '2024-06-13 07:45', 'temp_c': 14.4, 'temp_f': 57.9, 'is_day': 1, 'condition': {'text': 'Partly cloudy', 'icon': '//cdn.weatherapi.com/weather/64x64/day/116.png', 'code': 1003}, 'wind_mph': 2.2, 'wind_kph': 3.6, 'wind_degree': 10, 'wind_dir': 'N', 'pressure_mb': 1013.0, 'pressure_in': 29.92, 'precip_mm': 0.01, 'precip_in': 0.0, 'humidity': 84, 'cloud': 75, 'feelslike_c': 13.4, 'feelslike_f': 56.0, 'windchill_c': 10.1, 'windchill_f': 50.1, 'heatindex_c': 11.7, 'heatindex_f': 53.1, 'dewpoint_c': 8.8, 'dewpoint_f': 47.9, 'vis_km': 16.0, 'vis_miles': 9.0, 'uv': 3.0, 'gust_mph': 12.7, 'gust_kph': 20.4}}\"}]\n",
|
||||
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
|
||||
"\n",
|
||||
"Of course! I always strive to be polite and helpful.\n",
|
||||
"\n",
|
||||
"Currently, in San Francisco, it is partly cloudy with a temperature of approximately 14.4°C (57.9°F). The wind is blowing from the north at around 2.2 mph (3.6 kph). The humidity level is quite high at 84%, and visibility is good at 16 km (9 miles).\n",
|
||||
"\n",
|
||||
"Is there anything else you would like to know?\n"
|
||||
"Your name is Joe. The weather in San Francisco is always sunny.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"inputs = {\"messages\": [(\"user\", \"What's your name? And what's the weather in SF?\")]}\n",
|
||||
"from langchain_core.prompts import ChatPromptTemplate\n",
|
||||
"prompt = ChatPromptTemplate.from_messages([\n",
|
||||
" (\"system\", \"You are a helpful bot named Fred.\"),\n",
|
||||
" (\"user\", \"My name is Joe\"),\n",
|
||||
" (\"placeholder\", \"{messages}\"),\n",
|
||||
"])\n",
|
||||
"\n",
|
||||
"def modify_messages(messages: list):\n",
|
||||
" # You can do more complex modifications here\n",
|
||||
" return prompt.invoke({\"messages\": messages})\n",
|
||||
"\n",
|
||||
"graph = create_react_agent(model, tools, messages_modifier=modify_messages)\n",
|
||||
"\n",
|
||||
"inputs = {\"messages\": [(\"user\", \"What's my name? And what's the weather in SF?\")]}\n",
|
||||
"print_stream(graph.stream(inputs, stream_mode=\"values\"))"
|
||||
]
|
||||
},
|
||||
@@ -450,7 +374,7 @@
|
||||
"id": "0af418b8-bdd3-4e10-a552-9d8d2a409a57",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## How to: add memory to `create_react_agent`"
|
||||
"## How to add memory to `create_react_agent`"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -463,30 +387,8 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"id": "dfd72a3c-3ff9-4bc3-ab51-f3c8424e7932",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langgraph.checkpoint import MemorySaver\n",
|
||||
"graph = create_react_agent(model, tools, checkpointer=MemorySaver())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"id": "3db05867-1ced-402c-b739-72afda037e7f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"config = {\"configurable\": {\"thread_id\": \"1\"}}\n",
|
||||
"inputs = {\"messages\": [(\"user\", \"What's the weather in SF?\")]}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 21,
|
||||
"id": "fcf1f692-b437-4f8d-9a75-94a018e90a7c",
|
||||
"execution_count": 12,
|
||||
"id": "c43d071a-a91e-4646-a035-b3fed9cd9f0c",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -495,24 +397,30 @@
|
||||
"text": [
|
||||
"================================\u001b[1m Human Message \u001b[0m=================================\n",
|
||||
"\n",
|
||||
"What's the weather in SF?\n",
|
||||
"What's the weather in NYC?\n",
|
||||
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
|
||||
"Tool Calls:\n",
|
||||
" tavily_search_results_json (call_SQua8Xc6fLjtJlS8CvaCxH2U)\n",
|
||||
" Call ID: call_SQua8Xc6fLjtJlS8CvaCxH2U\n",
|
||||
" get_weather (call_C5yEOD1GhlVPX9Gc5nnqgUXF)\n",
|
||||
" Call ID: call_C5yEOD1GhlVPX9Gc5nnqgUXF\n",
|
||||
" Args:\n",
|
||||
" query: current weather in San Francisco\n",
|
||||
" city: nyc\n",
|
||||
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
|
||||
"Name: tavily_search_results_json\n",
|
||||
"Name: get_weather\n",
|
||||
"\n",
|
||||
"[{\"url\": \"https://www.weatherapi.com/\", \"content\": \"{'location': {'name': 'San Francisco', 'region': 'California', 'country': 'United States of America', 'lat': 37.78, 'lon': -122.42, 'tz_id': 'America/Los_Angeles', 'localtime_epoch': 1718290938, 'localtime': '2024-06-13 8:02'}, 'current': {'last_updated_epoch': 1718290800, 'last_updated': '2024-06-13 08:00', 'temp_c': 14.4, 'temp_f': 57.9, 'is_day': 1, 'condition': {'text': 'Partly cloudy', 'icon': '//cdn.weatherapi.com/weather/64x64/day/116.png', 'code': 1003}, 'wind_mph': 2.2, 'wind_kph': 3.6, 'wind_degree': 10, 'wind_dir': 'N', 'pressure_mb': 1013.0, 'pressure_in': 29.92, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 84, 'cloud': 75, 'feelslike_c': 13.4, 'feelslike_f': 56.2, 'windchill_c': 11.1, 'windchill_f': 52.0, 'heatindex_c': 12.5, 'heatindex_f': 54.6, 'dewpoint_c': 8.8, 'dewpoint_f': 47.9, 'vis_km': 16.0, 'vis_miles': 9.0, 'uv': 4.0, 'gust_mph': 11.3, 'gust_kph': 18.1}}\"}]\n",
|
||||
"It might be cloudy in nyc\n",
|
||||
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
|
||||
"\n",
|
||||
"The current weather in San Francisco is partly cloudy with a temperature of 14.4°C (57.9°F). The wind is blowing from the north at 2.2 mph (3.6 kph). Humidity is at 84%, and visibility is 16 km (9 miles). The UV index is 4.\n"
|
||||
"The weather in NYC might be cloudy.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from langgraph.checkpoint import MemorySaver\n",
|
||||
"graph = create_react_agent(model, tools, checkpointer=MemorySaver())\n",
|
||||
"\n",
|
||||
"config = {\"configurable\": {\"thread_id\": \"1\"}}\n",
|
||||
"inputs = {\"messages\": [(\"user\", \"What's the weather in NYC?\")]}\n",
|
||||
"\n",
|
||||
"print_stream(graph.stream(inputs, config=config, stream_mode=\"values\"))"
|
||||
]
|
||||
},
|
||||
@@ -526,7 +434,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 22,
|
||||
"execution_count": 13,
|
||||
"id": "b509417b-70f3-4736-b838-5d1729ed759e",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -536,25 +444,54 @@
|
||||
"text": [
|
||||
"================================\u001b[1m Human Message \u001b[0m=================================\n",
|
||||
"\n",
|
||||
"How about NYC?\n",
|
||||
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
|
||||
"Tool Calls:\n",
|
||||
" tavily_search_results_json (call_s3cYMxsn11IfPxEMsxMp9Ycb)\n",
|
||||
" Call ID: call_s3cYMxsn11IfPxEMsxMp9Ycb\n",
|
||||
" Args:\n",
|
||||
" query: current weather in New York City\n",
|
||||
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
|
||||
"Name: tavily_search_results_json\n",
|
||||
"\n",
|
||||
"[{\"url\": \"https://www.weatherapi.com/\", \"content\": \"{'location': {'name': 'New York', 'region': 'New York', 'country': 'United States of America', 'lat': 40.71, 'lon': -74.01, 'tz_id': 'America/New_York', 'localtime_epoch': 1718290946, 'localtime': '2024-06-13 11:02'}, 'current': {'last_updated_epoch': 1718290800, 'last_updated': '2024-06-13 11:00', 'temp_c': 23.9, 'temp_f': 75.0, 'is_day': 1, 'condition': {'text': 'Sunny', 'icon': '//cdn.weatherapi.com/weather/64x64/day/113.png', 'code': 1000}, 'wind_mph': 2.2, 'wind_kph': 3.6, 'wind_degree': 10, 'wind_dir': 'N', 'pressure_mb': 1018.0, 'pressure_in': 30.05, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 55, 'cloud': 0, 'feelslike_c': 24.9, 'feelslike_f': 76.7, 'windchill_c': 26.1, 'windchill_f': 78.9, 'heatindex_c': 26.2, 'heatindex_f': 79.2, 'dewpoint_c': 12.5, 'dewpoint_f': 54.6, 'vis_km': 16.0, 'vis_miles': 9.0, 'uv': 7.0, 'gust_mph': 7.2, 'gust_kph': 11.6}}\"}]\n",
|
||||
"What's it known for?\n",
|
||||
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
|
||||
"\n",
|
||||
"The current weather in New York City is sunny with a temperature of 23.9°C (75.0°F). The wind is blowing from the north at 2.2 mph (3.6 kph). Humidity is at 55%, and visibility is 16 km (9 miles). The UV index is 7.\n"
|
||||
"New York City (NYC) is known for many things, including:\n",
|
||||
"\n",
|
||||
"1. **Landmarks and Attractions**: \n",
|
||||
" - **Statue of Liberty**: A symbol of freedom and democracy.\n",
|
||||
" - **Times Square**: Known for its bright lights, Broadway theaters, and bustling atmosphere.\n",
|
||||
" - **Central Park**: A large urban park offering various recreational activities.\n",
|
||||
" - **Empire State Building**: An iconic skyscraper with an observation deck offering panoramic views of the city.\n",
|
||||
" - **Brooklyn Bridge**: A historic bridge connecting Manhattan and Brooklyn.\n",
|
||||
"\n",
|
||||
"2. **Cultural Diversity**: NYC is a melting pot of cultures, languages, and cuisines, making it one of the most diverse cities in the world.\n",
|
||||
"\n",
|
||||
"3. **Arts and Entertainment**: \n",
|
||||
" - **Broadway**: Renowned for its world-class theater productions.\n",
|
||||
" - **Museums**: Such as the Metropolitan Museum of Art, the Museum of Modern Art (MoMA), and the American Museum of Natural History.\n",
|
||||
" - **Music and Nightlife**: A vibrant scene with numerous music venues, bars, and clubs.\n",
|
||||
"\n",
|
||||
"4. **Financial Hub**: \n",
|
||||
" - **Wall Street**: The financial district is home to the New York Stock Exchange and numerous financial institutions.\n",
|
||||
"\n",
|
||||
"5. **Fashion and Shopping**: \n",
|
||||
" - **Fifth Avenue**: Known for its high-end shopping.\n",
|
||||
" - **Fashion Week**: One of the major fashion events held twice a year.\n",
|
||||
"\n",
|
||||
"6. **Cuisine**: \n",
|
||||
" - **Diverse Food Scene**: From street food like hot dogs and pretzels to fine dining and international cuisines.\n",
|
||||
" - **Famous Foods**: New York-style pizza, bagels, and cheesecake.\n",
|
||||
"\n",
|
||||
"7. **Media and Publishing**: \n",
|
||||
" - Home to major media companies, newspapers like The New York Times, and numerous publishing houses.\n",
|
||||
"\n",
|
||||
"8. **Sports**: \n",
|
||||
" - Home to several major sports teams, including the New York Yankees (baseball), New York Mets (baseball), New York Knicks (basketball), Brooklyn Nets (basketball), New York Giants (football), and New York Jets (football).\n",
|
||||
"\n",
|
||||
"9. **Education and Research**: \n",
|
||||
" - Prestigious institutions like Columbia University, New York University (NYU), and Rockefeller University.\n",
|
||||
"\n",
|
||||
"10. **Public Transportation**: \n",
|
||||
" - An extensive subway system, buses, and taxis that make getting around the city convenient.\n",
|
||||
"\n",
|
||||
"NYC is a city that never sleeps, offering endless opportunities for exploration and experiences.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"inputs = {\"messages\": [(\"user\", \"How about NYC?\")]}\n",
|
||||
"inputs = {\"messages\": [(\"user\", \"What's it known for?\")]}\n",
|
||||
"print_stream(graph.stream(inputs, config=config, stream_mode=\"values\"))"
|
||||
]
|
||||
},
|
||||
@@ -568,7 +505,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 23,
|
||||
"execution_count": 14,
|
||||
"id": "1bb09dbe-bbba-4e8f-b44c-b18bd2ed96ff",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -578,25 +515,15 @@
|
||||
"text": [
|
||||
"================================\u001b[1m Human Message \u001b[0m=================================\n",
|
||||
"\n",
|
||||
"How about NYC?\n",
|
||||
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
|
||||
"Tool Calls:\n",
|
||||
" tavily_search_results_json (call_uXGI87Qw09CHNiDgYBgOrhoJ)\n",
|
||||
" Call ID: call_uXGI87Qw09CHNiDgYBgOrhoJ\n",
|
||||
" Args:\n",
|
||||
" query: current news in NYC\n",
|
||||
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
|
||||
"Name: tavily_search_results_json\n",
|
||||
"\n",
|
||||
"[{\"url\": \"https://abc7ny.com/\", \"content\": \"New York's source for breaking news, weather and live video. Covering NYC, New Jersey, Long Island and all of the greater New York City area.\"}]\n",
|
||||
"What's it known for?\n",
|
||||
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
|
||||
"\n",
|
||||
"For the latest news in NYC, you can visit [ABC7 New York](https://abc7ny.com/). They provide updates on breaking news, weather, and live video coverage for New York City, New Jersey, Long Island, and the greater NYC area.\n"
|
||||
"Could you please specify what \"it\" refers to? Are you asking about a specific city, person, object, or something else?\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"inputs = {\"messages\": [(\"user\", \"How about NYC?\")]}\n",
|
||||
"inputs = {\"messages\": [(\"user\", \"What's it known for?\")]}\n",
|
||||
"print_stream(graph.stream(inputs, config={\"configurable\": {\"thread_id\": 2}}, stream_mode=\"values\"))"
|
||||
]
|
||||
},
|
||||
@@ -605,7 +532,7 @@
|
||||
"id": "42e730aa-8fac-48a5-9c67-b51393665469",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## How to: add human-in-the-loop to `create_react_agent`"
|
||||
"## How to add human-in-the-loop to `create_react_agent`"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -618,23 +545,8 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 24,
|
||||
"id": "fe93def9-6ba4-4c89-9e84-1ff607dbfc8c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"graph = create_react_agent(\n",
|
||||
" model, tools, interrupt_before=[\"tools\"], checkpointer=MemorySaver()\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"config = {\"configurable\": {\"thread_id\": \"42\"}}\n",
|
||||
"inputs = {\"messages\": [(\"user\", \"What's the weather in SF?\")]}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 25,
|
||||
"id": "4d934d6a-6550-453b-8c23-fbb8b4656da6",
|
||||
"execution_count": 15,
|
||||
"id": "8cd2cbcc-8a1f-443e-9cc9-dc18cd0ceac6",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
@@ -646,20 +558,27 @@
|
||||
"What's the weather in SF?\n",
|
||||
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
|
||||
"Tool Calls:\n",
|
||||
" tavily_search_results_json (call_Wg4AZqacqqUHHmQfjOjudAyP)\n",
|
||||
" Call ID: call_Wg4AZqacqqUHHmQfjOjudAyP\n",
|
||||
" get_weather (call_I7B9YW4ENth7QXYzDpiIoLCE)\n",
|
||||
" Call ID: call_I7B9YW4ENth7QXYzDpiIoLCE\n",
|
||||
" Args:\n",
|
||||
" query: current weather in San Francisco\n"
|
||||
" city: sf\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"graph = create_react_agent(\n",
|
||||
" model, tools, interrupt_before=[\"tools\"], checkpointer=MemorySaver()\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"config = {\"configurable\": {\"thread_id\": \"42\"}}\n",
|
||||
"inputs = {\"messages\": [(\"user\", \"What's the weather in SF?\")]}\n",
|
||||
"\n",
|
||||
"print_stream(graph.stream(inputs, config, stream_mode=\"values\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 26,
|
||||
"execution_count": 16,
|
||||
"id": "493d4963-b27b-46ef-8474-1527b08c1c03",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -678,7 +597,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 27,
|
||||
"execution_count": 17,
|
||||
"id": "7ad01f3a-e838-4230-ab07-2f850bb7e7cb",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -687,21 +606,12 @@
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
|
||||
"Name: tavily_search_results_json\n",
|
||||
"Name: get_weather\n",
|
||||
"\n",
|
||||
"[{\"url\": \"https://www.weatherapi.com/\", \"content\": \"{'location': {'name': 'San Francisco', 'region': 'California', 'country': 'United States of America', 'lat': 37.78, 'lon': -122.42, 'tz_id': 'America/Los_Angeles', 'localtime_epoch': 1718290938, 'localtime': '2024-06-13 8:02'}, 'current': {'last_updated_epoch': 1718290800, 'last_updated': '2024-06-13 08:00', 'temp_c': 14.4, 'temp_f': 57.9, 'is_day': 1, 'condition': {'text': 'Partly cloudy', 'icon': '//cdn.weatherapi.com/weather/64x64/day/116.png', 'code': 1003}, 'wind_mph': 2.2, 'wind_kph': 3.6, 'wind_degree': 10, 'wind_dir': 'N', 'pressure_mb': 1013.0, 'pressure_in': 29.92, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 84, 'cloud': 75, 'feelslike_c': 13.4, 'feelslike_f': 56.2, 'windchill_c': 11.1, 'windchill_f': 52.0, 'heatindex_c': 12.5, 'heatindex_f': 54.6, 'dewpoint_c': 8.8, 'dewpoint_f': 47.9, 'vis_km': 16.0, 'vis_miles': 9.0, 'uv': 4.0, 'gust_mph': 11.3, 'gust_kph': 18.1}}\"}]\n",
|
||||
"It's always sunny in sf\n",
|
||||
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
|
||||
"\n",
|
||||
"The current weather in San Francisco is:\n",
|
||||
"\n",
|
||||
"- **Temperature**: 14.4°C (57.9°F)\n",
|
||||
"- **Condition**: Partly cloudy\n",
|
||||
"- **Wind**: 2.2 mph (3.6 kph) from the north\n",
|
||||
"- **Humidity**: 84%\n",
|
||||
"- **Visibility**: 16 km (9 miles)\n",
|
||||
"- **UV Index**: 4\n",
|
||||
"\n",
|
||||
"\n"
|
||||
"The weather in San Francisco is currently sunny.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user