{"repo": "857743949/langchian-learn", "files": {"docs/docs/tutorials/agents.ipynb": {"d330e5f184e8a474da628fdba53f26e10799ab7b": {"t": 1785745489.7476451, "n": 59293, "h": "7bb8517c0174b7d1", "c": "{\n \"cells\": [\n {\n \"cell_type\": \"raw\",\n \"id\": \"17546ebb\",\n \"metadata\": {\n \"vscode\": {\n \"languageId\": \"raw\"\n },\n \"id\": \"17546ebb\"\n },\n \"source\": [\n \"---\\n\",\n \"keywords: [agent, agents]\\n\",\n \"---\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"1df78a71\",\n \"metadata\": {\n \"id\": \"1df78a71\"\n },\n \"source\": [\n \"# Build an Agent\\n\",\n \"\\n\",\n \"By themselves, language models can't take actions - they just output text.\\n\",\n \"A big use case for LangChain is creating **agents**.\\n\",\n \"[Agents](/docs/concepts/agents) are systems that use [LLMs](/docs/concepts/chat_models) as reasoning engines to determine which actions to take and the inputs necessary to perform the action.\\n\",\n \"After executing actions, the results can be fed back into the LLM to determine whether more actions are needed, or whether it is okay to finish. This is often achieved via [tool-calling](/docs/concepts/tool_calling).\\n\",\n \"\\n\",\n \"In this tutorial we will build an agent that can interact with a search engine. You will be able to ask this agent questions, watch it call the search tool, and have conversations with it.\\n\",\n \"\\n\",\n \"## End-to-end agent\\n\",\n \"\\n\",\n \"The code snippet below represents a fully functional agent that uses an LLM to decide which tools to use. It is equipped with a generic search tool. It has conversational memory - meaning that it can be used as a multi-turn chatbot.\\n\",\n \"\\n\",\n \"In the rest of the guide, we will walk through the individual components and what each part does - but if you want to just grab some code and get started, feel free to use this!\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"86d9386b-442f-4cd6-a78f-57c88249d3f0\",\n \"metadata\": {\n \"id\": \"86d9386b-442f-4cd6-a78f-57c88249d3f0\"\n },\n \"outputs\": [],\n \"source\": [\n \"# Import relevant functionality\\n\",\n \"from langchain_anthropic import ChatAnthropic\\n\",\n \"from langchain_community.tools.tavily_search import TavilySearchResults\\n\",\n \"from langchain_core.messages import HumanMessage\\n\",\n \"from langgraph.checkpoint.memory import MemorySaver\\n\",\n \"from langgraph.prebuilt import create_react_agent\\n\",\n \"\\n\",\n \"# Create the agent\\n\",\n \"memory = MemorySaver()\\n\",\n \"model = ChatAnthropic(model_name=\\\"claude-3-sonnet-20240229\\\")\\n\",\n \"search = TavilySearchResults(max_results=2)\\n\",\n \"tools = [search]\\n\",\n \"agent_executor = create_react_agent(model, tools, checkpointer=memory)\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"7ab50503-d09f-4ff4-9080-5afe297ccc38\",\n \"metadata\": {\n \"id\": \"7ab50503-d09f-4ff4-9080-5afe297ccc38\",\n \"outputId\": \"7982e162-f5ac-4791-f543-c31dba474313\"\n },\n \"outputs\": [\n {\n \"name\": \"stdout\",\n \"output_type\": \"stream\",\n \"text\": [\n \"================================\\u001b[1m Human Message \\u001b[0m=================================\\n\",\n \"\\n\",\n \"hi im bob! and i live in sf\\n\",\n \"==================================\\u001b[1m Ai Message \\u001b[0m==================================\\n\",\n \"\\n\",\n \"Hello Bob! Since you didn't ask a specific question, I don't need to use any tools right now. I'm an AI assistant created by Anthropic to be helpful, honest, and harmless. Feel free to ask me anything and I'll do my best to provide a useful response or look up information using my capabilities.\\n\"\n ]\n }\n ],\n \"source\": [\n \"# Use the agent\\n\",\n \"config = {\\\"configurable\\\": {\\\"thread_id\\\": \\\"abc123\\\"}}\\n\",\n \"for step in agent_executor.stream(\\n\",\n \" {\\\"messages\\\": [HumanMessage(content=\\\"hi im bob! and i live in sf\\\")]},\\n\",\n \" config,\\n\",\n \" stream_mode=\\\"values\\\",\\n\",\n \"):\\n\",\n \" step[\\\"messages\\\"][-1].pretty_print()\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"fcadb699-3787-4028-a5f6-e5605c8118d4\",\n \"metadata\": {\n \"id\": \"fcadb699-3787-4028-a5f6-e5605c8118d4\",\n \"outputId\": \"145c6b6b-c511-4d0b-f230-bd43a347fe15\"\n },\n \"outputs\": [\n {\n \"name\": \"stdout\",\n \"output_type\": \"stream\",\n \"text\": [\n \"================================\\u001b[1m Human Message \\u001b[0m=================================\\n\",\n \"\\n\",\n \"whats the weather where I live?\\n\",\n \"==================================\\u001b[1m Ai Message \\u001b[0m==================================\\n\",\n \"\\n\",\n \"[{'text': 'To get the current weather for your location in San Francisco, I can use the tavily_search_results_json tool:', 'type': 'text'}, {'id': 'toolu_01AKa2MErG1CU3zRiGsvpBud', 'input': {'query': 'san francisco weather'}, 'name': 'tavily_search_results_json', 'type': 'tool_use'}]\\n\",\n \"Tool Calls:\\n\",\n \" tavily_search_results_json (toolu_01AKa2MErG1CU3zRiGsvpBud)\\n\",\n \" Call ID: toolu_01AKa2MErG1CU3zRiGsvpBud\\n\",\n \" Args:\\n\",\n \" query: san francisco weather\\n\",\n \"=================================\\u001b[1m Tool Message \\u001b[0m=================================\\n\",\n \"Name: tavily_search_results_json\\n\",\n \"\\n\",\n \"[{\\\"url\\\": \\\"https://www.weatherapi.com/\\\", \\\"content\\\": \\\"{'location': {'name': 'San Francisco', 'region': 'California', 'country': 'United States of America', 'lat': 37.775, 'lon': -122.4183, 'tz_id': 'America/Los_Angeles', 'localtime_epoch': 1739994486, 'localtime': '2025-02-19 11:48'}, 'current': {'last_updated_epoch': 1739994300, 'last_updated': '2025-02-19 11:45', 'temp_c': 13.3, 'temp_f': 55.9, 'is_day': 1, 'condition': {'text': 'Light rain', 'icon': '//cdn.weatherapi.com/weather/64x64/day/296.png', 'code': 1183}, 'wind_mph': 5.8, 'wind_kph': 9.4, 'wind_degree': 195, 'wind_dir': 'SSW', 'pressure_mb': 1023.0, 'pressure_in': 30.2, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 87, 'cloud': 100, 'feelslike_c': 12.7, 'feelslike_f': 54.8, 'windchill_c': 9.1, 'windchill_f': 48.4, 'heatindex_c': 10.2, 'heatindex_f': 50.3, 'dewpoint_c': 9.8, 'dewpoint_f': 49.7, 'vis_km': 4.0, 'vis_miles': 2.0, 'uv': 1.4, 'gust_mph': 8.9, 'gust_kph': 14.4}}\\\"}, {\\\"url\\\": \\\"https://world-weather.info/forecast/usa/san_francisco/february-2025/\\\", \\\"content\\\": \\\"Weather in San Francisco in February 2025 (California) - Detailed Weather Forecast for a Month Weather World Weather in San Francisco Weather in San Francisco in February 2025 San Francisco Weather Forecast for February 2025, is based on previous years' statistical data. +59°+50° +59°+52° +59°+50° +61°+52° +59°+50° +61°+50° +61°+52° +63°+52° +61°+52° +61°+50° +61°+50° +61°+50° +59°+50° +59°+50° +61°+50° +61°+52° +59°+50° +59°+48° +57°+48° +59°+50° +59°+48° +59°+50° +57°+46° +61°+50° +61°+50° +59°+50° +59°+48° +59°+50° Extended weather forecast in San Francisco HourlyWeek10-Day14-Day30-DayYear Weather in large and nearby cities Weather in Washington, D.C.+41° Sacramento+55° Pleasanton+55° Redwood City+55° San Leandro+55° San Mateo+54° San Rafael+52° San Ramon+52° South San Francisco+54° Vallejo+50° Palo Alto+55° Pacifica+55° Berkeley+54° Castro Valley+55° Concord+52° Daly City+54° Noverd+52° Sign Hill+54° world's temperature today day day Temperature units\\\"}]\\n\",\n \"==================================\\u001b[1m Ai Message \\u001b[0m==================================\\n\",\n \"\\n\",\n \"The search results provide the current weather conditions and forecast for San Francisco. According to the data from WeatherAPI, the current temperature in San Francisco is around 55°F (13°C) with light rain and winds around 6 mph. The extended forecast shows temperatures ranging from the upper 40s to low 60s Fahrenheit over the next few weeks.\\n\",\n \"\\n\",\n \"So in summary, it's a cool, rainy day currently in San Francisco where you live, Bob. Let me know if you need any other details about the weather there!\\n\"\n ]\n }\n ],\n \"source\": [\n \"for step in agent_executor.stream(\\n\",\n \" {\\\"messages\\\": [HumanMessage(content=\\\"whats the weather where I live?\\\")]},\\n\",\n \" config,\\n\",\n \" stream_mode=\\\"values\\\",\\n\",\n \"):\\n\",\n \" step[\\\"messages\\\"][-1].pretty_print()\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"f4c03f40-1328-412d-8a48-1db0cd481b77\",\n \"metadata\": {\n \"id\": \"f4c03f40-1328-412d-8a48-1db0cd481b77\"\n },\n \"source\": [\n \"## Setup\\n\",\n \"\\n\",\n \"### Jupyter Notebook\\n\",\n \"\\n\",\n \"This guide (and most of the other guides in the documentation) uses [Jupyter notebooks](https://jupyter.org/) and assumes the reader is as well. Jupyter notebooks are perfect interactive environments for learning how to work with LLM systems because oftentimes things can go wrong (unexpected output, API down, etc), and observing these cases is a great way to better understand building with LLMs.\\n\",\n \"\\n\",\n \"This and other tutorials are perhaps most conveniently run in a Jupyter notebook. See [here](https://jupyter.org/install) for instructions on how to install.\\n\",\n \"\\n\",\n \"### Installation\\n\",\n \"\\n\",\n \"To install LangChain run:\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 1,\n \"id\": \"60bb3eb1\",\n \"metadata\": {\n \"collapsed\": true,\n \"id\": \"60bb3eb1\",\n \"outputId\": \"17f3c524-fa33-4ac7-e6ac-6786c9c86093\",\n \"colab\": {\n \"base_uri\": \"https://localhost:8080/\"\n }\n },\n \"outputs\": [\n {\n \"output_type\": \"stream\",\n \"name\": \"stdout\",\n \"text\": [\n \"Collecting langchain-community\\n\",\n \" Downloading langchain_community-0.3.24-py3-none-any.whl.metadata (2.5 kB)\\n\",\n \"Collecting langgraph\\n\",\n \" Downloading langgraph-0.4.8-py3-none-any.whl.metadata (6.8 kB)\\n\",\n \"Collecting langchain-anthropic\\n\",\n \" Downloading langchain_anthropic-0.3.15-py3-none-any.whl.metadata (1.9 kB)\\n\",\n \"Collecting tavily-python\\n\",\n \" Downloading tavily_python-0.7.3-py3-none-any.whl.metadata (7.0 kB)\\n\",\n \"Collecting langgraph-checkpoint-sqlite\\n\",\n \" Downloading langgraph_checkpoint_sqlite-2.0.10-py3-none-any.whl.metadata (2.7 kB)\\n\",\n \"Requirement already satisfied: langchain-core<1.0.0,>=0.3.59 in /usr/local/lib/python3.11/dist-packages (from langchain-community) (0.3.63)\\n\",\n \"Requirement already satisfied: langchain<1.0.0,>=0.3.25 in /usr/local/lib/python3.11/dist-packages (from langchain-community) (0.3.25)\\n\",\n \"Requirement already satisfied: SQLAlchemy<3,>=1.4 in /usr/local/lib/python3.11/dist-packages (from langchain-community) (2.0.41)\\n\",\n \"Requirement already satisfied: requests<3,>=2 in /usr/local/lib/python3.11/dist-packages (from langchain-community) (2.32.3)\\n\",\n \"Requirement already satisfied: PyYAML>=5.3 in /usr/local/lib/python3.11/dist-packages (from langchain-community) (6.0.2)\\n\",\n \"Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in /usr/local/lib/python3.11/dist-packages (from langchain-community) (3.11.15)\\n\",\n \"Requirement already satisfied: tenacity!=8.4.0,<10,>=8.1.0 in /usr/local/lib/python3.11/dist-packages (from langchain-community) (9.1.2)\\n\",\n \"Collecting dataclasses-json<0.7,>=0.5.7 (from langchain-community)\\n\",\n \" Downloading dataclasses_json-0.6.7-py3-none-any.whl.metadata (25 kB)\\n\",\n \"Collecting pydantic-settings<3.0.0,>=2.4.0 (from langchain-community)\\n\",\n \" Downloading pydantic_settings-2.9.1-py3-none-any.whl.metadata (3.8 kB)\\n\",\n \"Requirement already satisfied: langsmith<0.4,>=0.1.125 in /usr/local/lib/python3.11/dist-packages (from langchain-community) (0.3.43)\\n\",\n \"Collecting httpx-sse<1.0.0,>=0.4.0 (from langchain-community)\\n\",\n \" Downloading httpx_sse-0.4.0-py3-none-any.whl.metadata (9.0 kB)\\n\",\n \"Requirement already satisfied: numpy>=1.26.2 in /usr/local/lib/python3.11/dist-packages (from langchain-community) (2.0.2)\\n\",\n \"Collecting langgraph-checkpoint>=2.0.26 (from langgraph)\\n\",\n \" Downloading langgraph_checkpoint-2.0.26-py3-none-any.whl.metadata (4.6 kB)\\n\",\n \"Collecting langgraph-prebuilt>=0.2.0 (from langgraph)\\n\",\n \" Downloading langgraph_prebuilt-0.2.2-py3-none-any.whl.metadata (4.5 kB)\\n\",\n \"Collecting langgraph-sdk>=0.1.42 (from langgraph)\\n\",\n \" Downloading langgraph_sdk-0.1.70-py3-none-any.whl.metadata (1.5 kB)\\n\",\n \"Requirement already satisfied: pydantic>=2.7.4 in /usr/local/lib/python3.11/dist-packages (from langgraph) (2.11.5)\\n\",\n \"Requirement already satisfied: xxhash>=3.5.0 in /usr/local/lib/python3.11/dist-packages (from langgraph) (3.5.0)\\n\",\n \"Collecting anthropic<1,>=0.52.0 (from langchain-anthropic)\\n\",\n \" Downloading anthropic-0.52.2-py3-none-any.whl.metadata (25 kB)\\n\",\n \"Requirement already satisfied: tiktoken>=0.5.1 in /usr/local/lib/python3.11/dist-packages (from tavily-python) (0.9.0)\\n\",\n \"Requirement already satisfied: httpx in /usr/local/lib/python3.11/dist-packages (from tavily-python) (0.28.1)\\n\",\n \"Collecting aiosqlite>=0.20 (from langgraph-checkpoint-sqlite)\\n\",\n \" Downloading aiosqlite-0.21.0-py3-none-any.whl.metadata (4.3 kB)\\n\",\n \"Collecting sqlite-vec>=0.1.6 (from langgraph-checkpoint-sqlite)\\n\",\n \" Downloading sqlite_vec-0.1.6-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux1_x86_64.whl.metadata (198 bytes)\\n\",\n \"Requirement already satisfied: aiohappyeyeballs>=2.3.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (2.6.1)\\n\",\n \"Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.11/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (1.3.2)\\n\",\n \"Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (25.3.0)\\n\",\n \"Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.11/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (1.6.0)\\n\",\n \"Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.11/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (6.4.4)\\n\",\n \"Requirement already satisfied: propcache>=0.2.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (0.3.1)\\n\",\n \"Requirement already satisfied: yarl<2.0,>=1.17.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (1.20.0)\\n\",\n \"Requirement already satisfied: typing_extensions>=4.0 in /usr/local/lib/python3.11/dist-packages (from aiosqlite>=0.20->langgraph-checkpoint-sqlite) (4.13.2)\\n\",\n \"Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.11/dist-packages (from anthropic<1,>=0.52.0->langchain-anthropic) (4.9.0)\\n\",\n \"Requirement already satisfied: distro<2,>=1.7.0 in /usr/local/lib/python3.11/dist-packages (from anthropic<1,>=0.52.0->langchain-anthropic) (1.9.0)\\n\",\n \"Requirement already satisfied: jiter<1,>=0.4.0 in /usr/local/lib/python3.11/dist-packages (from anthropic<1,>=0.52.0->langchain-anthropic) (0.10.0)\\n\",\n \"Requirement already satisfied: sniffio in /usr/local/lib/python3.11/dist-packages (from anthropic<1,>=0.52.0->langchain-anthropic) (1.3.1)\\n\",\n \"Collecting marshmallow<4.0.0,>=3.18.0 (from dataclasses-json<0.7,>=0.5.7->langchain-community)\\n\",\n \" Downloading marshmallow-3.26.1-py3-none-any.whl.metadata (7.3 kB)\\n\",\n \"Collecting typing-inspect<1,>=0.4.0 (from dataclasses-json<0.7,>=0.5.7->langchain-community)\\n\",\n \" Downloading typing_inspect-0.9.0-py3-none-any.whl.metadata (1.5 kB)\\n\",\n \"Requirement already satisfied: certifi in /usr/local/lib/python3.11/dist-packages (from httpx->tavily-python) (2025.4.26)\\n\",\n \"Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.11/dist-packages (from httpx->tavily-python) (1.0.9)\\n\",\n \"Requirement already satisfied: idna in /usr/local/lib/python3.11/dist-packages (from httpx->tavily-python) (3.10)\\n\",\n \"Requirement already satisfied: h11>=0.16 in /usr/local/lib/python3.11/dist-packages (from httpcore==1.*->httpx->tavily-python) (0.16.0)\\n\",\n \"Requirement already satisfied: langchain-text-splitters<1.0.0,>=0.3.8 in /usr/local/lib/python3.11/dist-packages (from langchain<1.0.0,>=0.3.25->langchain-community) (0.3.8)\\n\",\n \"Requirement already satisfied: jsonpatch<2.0,>=1.33 in /usr/local/lib/python3.11/dist-packages (from langchain-core<1.0.0,>=0.3.59->langchain-community) (1.33)\\n\",\n \"Requirement already satisfied: packaging<25,>=23.2 in /usr/local/lib/python3.11/dist-packages (from langchain-core<1.0.0,>=0.3.59->langchain-community) (24.2)\\n\",\n \"Collecting ormsgpack<2.0.0,>=1.8.0 (from langgraph-checkpoint>=2.0.26->langgraph)\\n\",\n \" Downloading ormsgpack-1.10.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (43 kB)\\n\",\n \"\\u001b[2K \\u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\u001b[0m \\u001b[32m43.7/43.7 kB\\u001b[0m \\u001b[31m2.9 MB/s\\u001b[0m eta \\u001b[36m0:00:00\\u001b[0m\\n\",\n \"\\u001b[?25hRequirement already satisfied: orjson>=3.10.1 in /usr/local/lib/python3.11/dist-packages (from langgraph-sdk>=0.1.42->langgraph) (3.10.18)\\n\",\n \"Requirement already satisfied: requests-toolbelt<2.0.0,>=1.0.0 in /usr/local/lib/python3.11/dist-packages (from langsmith<0.4,>=0.1.125->langchain-community) (1.0.0)\\n\",\n \"Requirement already satisfied: zstandard<0.24.0,>=0.23.0 in /usr/local/lib/python3.11/dist-packages (from langsmith<0.4,>=0.1.125->langchain-community) (0.23.0)\\n\",\n \"Requirement already satisfied: annotated-types>=0.6.0 in /usr/local/lib/python3.11/dist-packages (from pydantic>=2.7.4->langgraph) (0.7.0)\\n\",\n \"Requirement already satisfied: pydantic-core==2.33.2 in /usr/local/lib/python3.11/dist-packages (from pydantic>=2.7.4->langgraph) (2.33.2)\\n\",\n \"Requirement already satisfied: typing-inspection>=0.4.0 in /usr/local/lib/python3.11/dist-packages (from pydantic>=2.7.4->langgraph) (0.4.1)\\n\",\n \"Collecting python-dotenv>=0.21.0 (from pydantic-settings<3.0.0,>=2.4.0->langchain-community)\\n\",\n \" Downloading python_dotenv-1.1.0-py3-none-any.whl.metadata (24 kB)\\n\",\n \"Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/dist-packages (from requests<3,>=2->langchain-community) (3.4.2)\\n\",\n \"Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.11/dist-packages (from requests<3,>=2->langchain-community) (2.4.0)\\n\",\n \"Requirement already satisfied: greenlet>=1 in /usr/local/lib/python3.11/dist-packages (from SQLAlchemy<3,>=1.4->langchain-community) (3.2.2)\\n\",\n \"Requirement already satisfied: regex>=2022.1.18 in /usr/local/lib/python3.11/dist-packages (from tiktoken>=0.5.1->tavily-python) (2024.11.6)\\n\",\n \"Requirement already satisfied: jsonpointer>=1.9 in /usr/local/lib/python3.11/dist-packages (from jsonpatch<2.0,>=1.33->langchain-core<1.0.0,>=0.3.59->langchain-community) (3.0.0)\\n\",\n \"Collecting mypy-extensions>=0.3.0 (from typing-inspect<1,>=0.4.0->dataclasses-json<0.7,>=0.5.7->langchain-community)\\n\",\n \" Downloading mypy_extensions-1.1.0-py3-none-any.whl.metadata (1.1 kB)\\n\",\n \"Downloading langchain_community-0.3.24-py3-none-any.whl (2.5 MB)\\n\",\n \"\\u001b[2K \\u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\u001b[0m \\u001b[32m2.5/2.5 MB\\u001b[0m \\u001b[31m33.7 MB/s\\u001b[0m eta \\u001b[36m0:00:00\\u001b[0m\\n\",\n \"\\u001b[?25hDownloading langgraph-0.4.8-py3-none-any.whl (152 kB)\\n\",\n \"\\u001b[2K \\u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\u001b[0m \\u001b[32m152.4/152.4 kB\\u001b[0m \\u001b[31m9.5 MB/s\\u001b[0m eta \\u001b[36m0:00:00\\u001b[0m\\n\",\n \"\\u001b[?25hDownloading langchain_anthropic-0.3.15-py3-none-any.whl (28 kB)\\n\",\n \"Downloading tavily_python-0.7.3-py3-none-any.whl (15 kB)\\n\",\n \"Downloading langgraph_checkpoint_sqlite-2.0.10-py3-none-any.whl (30 kB)\\n\",\n \"Downloading aiosqlite-0.21.0-py3-none-any.whl (15 kB)\\n\",\n \"Downloading anthropic-0.52.2-py3-none-any.whl (286 kB)\\n\",\n \"\\u001b[2K \\u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\u001b[0m \\u001b[32m286.3/286.3 kB\\u001b[0m \\u001b[31m19.4 MB/s\\u001b[0m eta \\u001b[36m0:00:00\\u001b[0m\\n\",\n \"\\u001b[?25hDownloading dataclasses_json-0.6.7-py3-none-any.whl (28 kB)\\n\",\n \"Downloading httpx_sse-0.4.0-py3-none-any.whl (7.8 kB)\\n\",\n \"Downloading langgraph_checkpoint-2.0.26-py3-none-any.whl (44 kB)\\n\",\n \"\\u001b[2K \\u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\u001b[0m \\u001b[32m44.2/44.2 kB\\u001b[0m \\u001b[31m2.8 MB/s\\u001b[0m eta \\u001b[36m0:00:00\\u001b[0m\\n\",\n \"\\u001b[?25hDownloading langgraph_prebuilt-0.2.2-py3-none-any.whl (23 kB)\\n\",\n \"Downloading langgraph_sdk-0.1.70-py3-none-any.whl (49 kB)\\n\",\n \"\\u001b[2K \\u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\u001b[0m \\u001b[32m50.0/50.0 kB\\u001b[0m \\u001b[31m3.5 MB/s\\u001b[0m eta \\u001b[36m0:00:00\\u001b[0m\\n\",\n \"\\u001b[?25h\\u001b[33mWARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProtocolError('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))': /packages/b6/5f/d6d641b490fd3ec2c4c13b4244d68deea3a1b970a97be64f34fb5504ff72/pydantic_settings-2.9.1-py3-none-any.whl\\u001b[0m\\u001b[33m\\n\",\n \"\\u001b[0mDownloading pydantic_settings-2.9.1-py3-none-any.whl (44 kB)\\n\",\n \"\\u001b[2K \\u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\u001b[0m \\u001b[32m44.4/44.4 kB\\u001b[0m \\u001b[31m2.8 MB/s\\u001b[0m eta \\u001b[36m0:00:00\\u001b[0m\\n\",\n \"\\u001b[?25hDownloading sqlite_vec-0.1.6-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux1_x86_64.whl (151 kB)\\n\",\n \"\\u001b[2K \\u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\u001b[0m \\u001b[32m151.6/151.6 kB\\u001b[0m \\u001b[31m8.6 MB/s\\u001b[0m eta \\u001b[36m0:00:00\\u001b[0m\\n\",\n \"\\u001b[?25hDownloading marshmallow-3.26.1-py3-none-any.whl (50 kB)\\n\",\n \"\\u001b[2K \\u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\u001b[0m \\u001b[32m50.9/50.9 kB\\u001b[0m \\u001b[31m3.5 MB/s\\u001b[0m eta \\u001b[36m0:00:00\\u001b[0m\\n\",\n \"\\u001b[?25hDownloading ormsgpack-1.10.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (216 kB)\\n\",\n \"\\u001b[2K \\u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\u001b[0m \\u001b[32m216.5/216.5 kB\\u001b[0m \\u001b[31m8.0 MB/s\\u001b[0m eta \\u001b[36m0:00:00\\u001b[0m\\n\",\n \"\\u001b[?25hDownloading python_dotenv-1.1.0-py3-none-any.whl (20 kB)\\n\",\n \"Downloading typing_inspect-0.9.0-py3-none-any.whl (8.8 kB)\\n\",\n \"Downloading mypy_extensions-1.1.0-py3-none-any.whl (5.0 kB)\\n\",\n \"Installing collected packages: sqlite-vec, python-dotenv, ormsgpack, mypy-extensions, marshmallow, httpx-sse, aiosqlite, typing-inspect, tavily-python, pydantic-settings, langgraph-sdk, dataclasses-json, anthropic, langgraph-checkpoint, langchain-anthropic, langgraph-prebuilt, langgraph-checkpoint-sqlite, langgraph, langchain-community\\n\",\n \"Successfully installed aiosqlite-0.21.0 anthropic-0.52.2 dataclasses-json-0.6.7 httpx-sse-0.4.0 langchain-anthropic-0.3.15 langchain-community-0.3.24 langgraph-0.4.8 langgraph-checkpoint-2.0.26 langgraph-checkpoint-sqlite-2.0.10 langgraph-prebuilt-0.2.2 langgraph-sdk-0.1.70 marshmallow-3.26.1 mypy-extensions-1.1.0 ormsgpack-1.10.0 pydantic-settings-2.9.1 python-dotenv-1.1.0 sqlite-vec-0.1.6 tavily-python-0.7.3 typing-inspect-0.9.0\\n\"\n ]\n }\n ],\n \"source\": [\n \"%pip install -U langchain-community langgraph langchain-anthropic tavily-python langgraph-checkpoint-sqlite\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"2ee337ae\",\n \"metadata\": {\n \"id\": \"2ee337ae\"\n },\n \"source\": [\n \"For more details, see our [Installation guide](/docs/how_to/installation).\\n\",\n \"\\n\",\n \"### LangSmith\\n\",\n \"\\n\",\n \"Many of the applications you build with LangChain will contain multiple steps with multiple invocations of LLM calls.\\n\",\n \"As these applications get more and more complex, it becomes crucial to be able to inspect what exactly is going on inside your chain or agent.\\n\",\n \"The best way to do this is with [LangSmith](https://smith.langchain.com).\\n\",\n \"\\n\",\n \"After you sign up at the link above, make sure to set your environment variables to start logging traces:\\n\",\n \"\\n\",\n \"```shell\\n\",\n \"export LANGSMITH_TRACING=\\\"true\\\"\\n\",\n \"export LANGSMITH_API_KEY=\\\"...\\\"\\n\",\n \"```\\n\",\n \"\\n\",\n \"Or, if in a notebook, you can set them with:\\n\",\n \"\\n\",\n \"```python\\n\",\n \"import getpass\\n\",\n \"import os\\n\",\n \"\\n\",\n \"os.environ[\\\"LANGSMITH_TRACING\\\"] = \\\"true\\\"\\n\",\n \"os.environ[\\\"LANGSMITH_API_KEY\\\"] = getpass.getpass()\\n\",\n \"```\\n\",\n \"\\n\",\n \"### Tavily\\n\",\n \"\\n\",\n \"We will be using [Tavily](/docs/integrations/tools/tavily_search) (a search engine) as a tool.\\n\",\n \"In order to use it, you will need to get and set an API key:\\n\",\n \"\\n\",\n \"```bash\\n\",\n \"export TAVILY_API_KEY=\\\"...\\\"\\n\",\n \"```\\n\",\n \"\\n\",\n \"Or, if in a notebook, you can set it with:\\n\",\n \"\\n\",\n \"```python\\n\",\n \"import getpass\\n\",\n \"import os\\n\",\n \"\\n\",\n \"os.environ[\\\"TAVILY_API_KEY\\\"] = getpass.getpass()\\n\",\n \"```\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"c335d1bf\",\n \"metadata\": {\n \"id\": \"c335d1bf\"\n },\n \"source\": [\n \"## Define tools\\n\",\n \"\\n\",\n \"We first need to create the tools we want to use. Our main tool of choice will be [Tavily](/docs/integrations/tools/tavily_search) - a search engine. We have a built-in tool in LangChain to easily use Tavily search engine as tool.\\n\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"source\": [\n \"import getpass\\n\",\n \"import os\\n\",\n \"\\n\",\n \"os.environ[\\\"LANGSMITH_TRACING\\\"] = \\\"true\\\"\\n\",\n \"os.environ[\\\"LANGSMITH_API_KEY\\\"] = \\\"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\\\"\\n\",\n \"os.environ['LANGSMITH_ENDPOINT'] = \\\"https://api.smith.langchain.com\\\"\\n\",\n \"os.environ['LANGSMITH_PROJECT'] = \\\"lang-chain-agent-learn\\\"\"\n ],\n \"metadata\": {\n \"id\": \"gE_sd8t6m-S1\"\n },\n \"id\": \"gE_sd8t6m-S1\",\n \"execution_count\": 2,\n \"outputs\": []\n },\n {\n \"cell_type\": \"code\",\n \"source\": [\n \"import getpass\\n\",\n \"import os\\n\",\n \"\\n\",\n \"os.environ[\\\"TAVILY_API_KEY\\\"] = \\\"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\\\"\"\n ],\n \"metadata\": {\n \"id\": \"e3IFYVwXnXCU\"\n },\n \"id\": \"e3IFYVwXnXCU\",\n \"execution_count\": 3,\n \"outputs\": []\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 4,\n \"id\": \"002e23b1-fdf9-46e9-82d9-f467abdd3f35\",\n \"metadata\": {\n \"id\": \"002e23b1-fdf9-46e9-82d9-f467abdd3f35\",\n \"outputId\": \"dbab9cde-07de-4459-8e6b-fe42e2093e98\",\n \"colab\": {\n \"base_uri\": \"https://localhost:8080/\"\n }\n },\n \"outputs\": [\n {\n \"output_type\": \"stream\",\n \"name\": \"stdout\",\n \"text\": [\n \"[{'title': 'Wednesday, June 4, 2025. San Francisco, CA - Weather Forecast', 'url': 'https://weathershogun.com/weather/usa/ca/san-francisco/480/june/2025-06-04', 'content': 'San Francisco, California Weather: Wednesday, June 4, 2025. Cloudy weather, overcast skies with clouds. Day 66°. Night 55°. Precipitation 20 %.', 'score': 0.94989765}, {'title': 'Weather in San Francisco in April 2025 (California)', 'url': 'https://world-weather.info/forecast/usa/san_francisco/april-2025/', 'content': '* [6 +63°+52°](https://world-weather.info/forecast/usa/san_francisco/14days/#2025-04-06)\\\\n* [7 +59°+52°](https://world-weather.info/forecast/usa/san_francisco/14days/#2025-04-07)\\\\n* [8 +63°+50°](https://world-weather.info/forecast/usa/san_francisco/14days/#2025-04-08)\\\\n* [9 +66°+50°](https://world-weather.info/forecast/usa/san_francisco/14days/#2025-04-09)\\\\n* [10 +68°+52°](https://world-weather.info/forecast/usa/san_francisco/14days/#2025-04-10) [...] +63°+54° [...] * [16 +61°+52°](https://world-weather.info/forecast/usa/san_francisco/14days/#2025-04-16)\\\\n* 17\\\\n \\\\n +61°+52°\\\\n \\\\n* 18\\\\n \\\\n +63°+54°\\\\n \\\\n* 19\\\\n \\\\n +63°+54°\\\\n \\\\n* 20\\\\n \\\\n +63°+54°\\\\n \\\\n* 21\\\\n \\\\n +64°+54°\\\\n \\\\n* 22\\\\n \\\\n +63°+54°\\\\n \\\\n* 23\\\\n \\\\n +63°+54°\\\\n \\\\n* 24\\\\n \\\\n +63°+54°\\\\n \\\\n* 25\\\\n \\\\n +63°+54°\\\\n \\\\n* 26\\\\n \\\\n +64°+54°\\\\n \\\\n* 27\\\\n \\\\n +64°+54°\\\\n \\\\n* 28\\\\n \\\\n +64°+54°\\\\n \\\\n* 29\\\\n \\\\n +63°+54°\\\\n \\\\n* 30', 'score': 0.92392945}]\\n\"\n ]\n }\n ],\n \"source\": [\n \"from langchain_community.tools.tavily_search import TavilySearchResults\\n\",\n \"\\n\",\n \"search = TavilySearchResults(max_results=2)\\n\",\n \"search_results = search.invoke(\\\"what is the weather in SF\\\")\\n\",\n \"print(search_results)\\n\",\n \"# If we want, we can create other tools.\\n\",\n \"# Once we have all the tools we want, we can put them in a list that we will reference later.\\n\",\n \"tools = [search]\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"e00068b0\",\n \"metadata\": {\n \"id\": \"e00068b0\"\n },\n \"source\": [\n \"## Using Language Models\\n\",\n \"\\n\",\n \"Next, let's learn how to use a language model to call tools. LangChain supports many different language models that you can use interchangably - select the one you want to use below!\\n\",\n \"\\n\",\n \"import ChatModelTabs from \\\"@theme/ChatModelTabs\\\";\\n\",\n \"\\n\",\n \"\\n\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"source\": [\n \"pip install -qU langchain-deepseek\"\n ],\n \"metadata\": {\n \"collapsed\": true,\n \"id\": \"a-natkPWZDHS\",\n \"outputId\": \"f7c56b72-99d2-4d8d-df75-c7e2cf8f9ddf\",\n \"colab\": {\n \"base_uri\": \"https://localhost:8080/\"\n }\n },\n \"id\": \"a-natkPWZDHS\",\n \"execution_count\": 5,\n \"outputs\": [\n {\n \"output_type\": \"stream\",\n \"name\": \"stdout\",\n \"text\": [\n \"\\u001b[?25l \\u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\u001b[0m \\u001b[32m0.0/64.5 kB\\u001b[0m \\u001b[31m?\\u001b[0m eta \\u001b[36m-:--:--\\u001b[0m\\r\\u001b[2K \\u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\u001b[0m \\u001b[32m64.5/64.5 kB\\u001b[0m \\u001b[31m1.6 MB/s\\u001b[0m eta \\u001b[36m0:00:00\\u001b[0m\\n\",\n \"\\u001b[?25h\"\n ]\n }\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"642ed8bf\",\n \"metadata\": {\n \"id\": \"642ed8bf\"\n },\n \"source\": [\n \"You can call the language model by passing in a list of messages. By default, the response is a `content` string.\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"source\": [\n \"import os\\n\",\n \"import getpass\\n\",\n \"os.environ[\\\"DEEPSEEK_API_KEY\\\"] = getpass.getpass()\\n\",\n \"\\n\",\n \"from langchain.chat_models import init_chat_model\\n\",\n \"\\n\",\n \"model = init_chat_model(\\\"deepseek-chat\\\", model_provider=\\\"deepseek\\\")\"\n ],\n \"metadata\": {\n \"id\": \"VrBV8g9ngiEy\",\n \"outputId\": \"dde5c59f-8c62-4b38-d7dc-726c90e4d64f\",\n \"colab\": {\n \"base_uri\": \"https://localhost:8080/\"\n }\n },\n \"id\": \"VrBV8g9ngiEy\",\n \"execution_count\": 6,\n \"outputs\": [\n {\n \"name\": \"stdout\",\n \"output_type\": \"stream\",\n \"text\": [\n \"··········\\n\"\n ]\n }\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 7,\n \"id\": \"c96c960b\",\n \"metadata\": {\n \"id\": \"c96c960b\",\n \"outputId\": \"4934c3b4-7b10-4fb3-e293-ae9c33f74b00\",\n \"colab\": {\n \"base_uri\": \"https://localhost:8080/\",\n \"height\": 35\n }\n },\n \"outputs\": [\n {\n \"output_type\": \"execute_result\",\n \"data\": {\n \"text/plain\": [\n \"'Hello! 👋 How can I help you today? 😊'\"\n ],\n \"application/vnd.google.colaboratory.intrinsic+json\": {\n \"type\": \"string\"\n }\n },\n \"metadata\": {},\n \"execution_count\": 7\n }\n ],\n \"source\": [\n \"from langchain_core.messages import HumanMessage\\n\",\n \"\\n\",\n \"response = model.invoke([HumanMessage(content=\\\"hi!\\\")])\\n\",\n \"response.content\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"47bf8210\",\n \"metadata\": {\n \"id\": \"47bf8210\"\n },\n \"source\": [\n \"We can now see what it is like to enable this model to do tool calling. In order to enable that we use `.bind_tools` to give the language model knowledge of these tools\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 13,\n \"id\": \"ba692a74\",\n \"metadata\": {\n \"id\": \"ba692a74\"\n },\n \"outputs\": [],\n \"source\": [\n \"model_with_tools = model.bind_tools(tools)\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"fd920b69\",\n \"metadata\": {\n \"id\": \"fd920b69\"\n },\n \"source\": [\n \"We can now call the model. Let's first call it with a normal message, and see how it responds. We can look at both the `content` field as well as the `tool_calls` field.\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 14,\n \"id\": \"b6a7e925\",\n \"metadata\": {\n \"id\": \"b6a7e925\",\n \"outputId\": \"0a603cbf-05c2-4cc0-c902-4dff1c0cdf39\",\n \"colab\": {\n \"base_uri\": \"https://localhost:8080/\"\n }\n },\n \"outputs\": [\n {\n \"output_type\": \"stream\",\n \"name\": \"stdout\",\n \"text\": [\n \"ContentString: Hello! How can I assist you today? 😊\\n\",\n \"ToolCalls: []\\n\"\n ]\n }\n ],\n \"source\": [\n \"response = model_with_tools.invoke([HumanMessage(content=\\\"Hi!\\\")])\\n\",\n \"\\n\",\n \"print(f\\\"ContentString: {response.content}\\\")\\n\",\n \"print(f\\\"ToolCalls: {response.tool_calls}\\\")\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"e8c81e76\",\n \"metadata\": {\n \"id\": \"e8c81e76\"\n },\n \"source\": [\n \"Now, let's try calling it with some input that would expect a tool to be called.\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 15,\n \"id\": \"688b465d\",\n \"metadata\": {\n \"id\": \"688b465d\",\n \"outputId\": \"2af21076-cd7e-4440-c7fe-1d346bf4062f\",\n \"colab\": {\n \"base_uri\": \"https://localhost:8080/\"\n }\n },\n \"outputs\": [\n {\n \"output_type\": \"stream\",\n \"name\": \"stdout\",\n \"text\": [\n \"ContentString: \\n\",\n \"ToolCalls: [{'name': 'tavily_search_results_json', 'args': {'query': 'current weather in San Francisco'}, 'id': 'call_0_f1fe428a-2db2-45e0-955e-f4b8b8629bd7', 'type': 'tool_call'}]\\n\"\n ]\n }\n ],\n \"source\": [\n \"response = model_with_tools.invoke([HumanMessage(content=\\\"What's the weather in SF?\\\")])\\n\",\n \"\\n\",\n \"print(f\\\"ContentString: {response.content}\\\")\\n\",\n \"print(f\\\"ToolCalls: {response.tool_calls}\\\")\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"83c4bcd3\",\n \"metadata\": {\n \"id\": \"83c4bcd3\"\n },\n \"source\": [\n \"We can see that there's now no text content, but there is a tool call! It wants us to call the Tavily Search tool.\\n\",\n \"\\n\",\n \"This isn't calling that tool yet - it's just telling us to. In order to actually call it, we'll want to create our agent.\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"40ccec80\",\n \"metadata\": {\n \"id\": \"40ccec80\"\n },\n \"source\": [\n \"## Create the agent\\n\",\n \"\\n\",\n \"Now that we have defined the tools and the LLM, we can create the agent. We will be using [LangGraph](/docs/concepts/architecture/#langgraph) to construct the agent.\\n\",\n \"Currently, we are using a high level interface to construct the agent, but the nice thing about LangGraph is that this high-level interface is backed by a low-level, highly controllable API in case you want to modify the agent logic.\\n\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"f8014c9d\",\n \"metadata\": {\n \"id\": \"f8014c9d\"\n },\n \"source\": [\n \"Now, we can initialize the agent with the LLM and the tools.\\n\",\n \"\\n\",\n \"Note that we are passing in the `model`, not `model_with_tools`. That is because `create_react_agent` will call `.bind_tools` for us under the hood.\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 8,\n \"id\": \"89cf72b4-6046-4b47-8f27-5522d8cb8036\",\n \"metadata\": {\n \"id\": \"89cf72b4-6046-4b47-8f27-5522d8cb8036\"\n },\n \"outputs\": [],\n \"source\": [\n \"from langgraph.prebuilt import create_react_agent\\n\",\n \"\\n\",\n \"agent_executor = create_react_agent(model, tools)\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"e4df0e06\",\n \"metadata\": {\n \"id\": \"e4df0e06\"\n },\n \"source\": [\n \"## Run the agent\\n\",\n \"\\n\",\n \"We can now run the agent with a few queries! Note that for now, these are all **stateless** queries (it won't remember previous interactions). Note that the agent will return the **final** state at the end of the interaction (which includes any inputs, we will see later on how to get only the outputs).\\n\",\n \"\\n\",\n \"First up, let's see how it responds when there's no need to call a tool:\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 17,\n \"id\": \"114ba50d\",\n \"metadata\": {\n \"id\": \"114ba50d\",\n \"outputId\": \"1dff3fea-9699-40b8-cc0c-4b9d50558e05\",\n \"colab\": {\n \"base_uri\": \"https://localhost:8080/\"\n }\n },\n \"outputs\": [\n {\n \"output_type\": \"execute_result\",\n \"data\": {\n \"text/plain\": [\n \"[HumanMessage(content='hi!', additional_kwargs={}, response_metadata={}, id='1fff40fc-8257-452e-8f33-88fb7cd578c4'),\\n\",\n \" AIMessage(content='Hello! How can I assist you today? 😊', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 11, 'prompt_tokens': 146, 'total_tokens': 157, 'completion_tokens_details': None, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 128}, 'prompt_cache_hit_tokens': 128, 'prompt_cache_miss_tokens': 18}, 'model_name': 'deepseek-chat', 'system_fingerprint': 'fp_8802369eaa_prod0425fp8', 'id': 'da174002-368e-4df8-bb99-9b1ac8b79b4a', 'service_tier': None, 'finish_reason': 'stop', 'logprobs': None}, id='run--3b584362-d359-4a29-aece-3368d2727b9b-0', usage_metadata={'input_tokens': 146, 'output_tokens': 11, 'total_tokens': 157, 'input_token_details': {'cache_read': 128}, 'output_token_details': {}})]\"\n ]\n },\n \"metadata\": {},\n \"execution_count\": 17\n }\n ],\n \"source\": [\n \"response = agent_executor.invoke({\\\"messages\\\": [HumanMessage(content=\\\"hi!\\\")]})\\n\",\n \"\\n\",\n \"response[\\\"messages\\\"]\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"71493a42\",\n \"metadata\": {\n \"id\": \"71493a42\"\n },\n \"source\": [\n \"In order to see exactly what is happening under the hood (and to make sure it's not calling a tool) we can take a look at the [LangSmith trace](https://smith.langchain.com/public/28311faa-e135-4d6a-ab6b-caecf6482aaa/r)\\n\",\n \"\\n\",\n \"Let's now try it out on an example where it should be invoking the tool\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 9,\n \"id\": \"77c2f769\",\n \"metadata\": {\n \"id\": \"77c2f769\",\n \"outputId\": \"3d123990-fce3-4075-dce5-6b582f52b5fa\",\n \"colab\": {\n \"base_uri\": \"https://localhost:8080/\"\n }\n },\n \"outputs\": [\n {\n \"output_type\": \"execute_result\",\n \"data\": {\n \"text/plain\": [\n \"[HumanMessage(content='whats the weather in sf?', additional_kwargs={}, response_metadata={}, id='2be30ac4-49ca-4cbd-a48b-03d196c293ac'),\\n\",\n \" AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_0_57411449-29e7-4e88-a24b-8fe7665bcbf0', 'function': {'arguments': '{\\\"query\\\":\\\"current weather in San Francisco\\\"}', 'name': 'tavily_search_results_json'}, 'type': 'function', 'index': 0}], 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 27, 'prompt_tokens': 151, 'total_tokens': 178, 'completion_tokens_details': None, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 128}, 'prompt_cache_hit_tokens': 128, 'prompt_cache_miss_tokens': 23}, 'model_name': 'deepseek-chat', 'system_fingerprint': 'fp_8802369eaa_prod0425fp8', 'id': '76e7e751-d161-4e5b-a702-09aefc53e9a5', 'service_tier': None, 'finish_reason': 'tool_calls', 'logprobs': None}, id='run--2753ca76-75e2-4c9e-85c2-1a8b1a47d18a-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'current weather in San Francisco'}, 'id': 'call_0_57411449-29e7-4e88-a24b-8fe7665bcbf0', 'type': 'tool_call'}], usage_metadata={'input_tokens': 151, 'output_tokens': 27, 'total_tokens': 178, 'input_token_details': {'cache_read': 128}, 'output_token_details': {}}),\\n\",\n \" ToolMessage(content='[{\\\"title\\\": \\\"Weather in San Francisco in April 2025 (California)\\\", \\\"url\\\": \\\"https://world-weather.info/forecast/usa/san_francisco/april-2025/\\\", \\\"content\\\": \\\"Detailed ⚡ San Francisco Weather Forecast for April 2025 – day/night 🌡️ temperatures, precipitations – World-Weather.info.\\\", \\\"score\\\": 0.75027883}, {\\\"title\\\": \\\"San Francisco Bay Area Weather April 6, 2025 - YouTube\\\", \\\"url\\\": \\\"https://www.youtube.com/watch?v=Hzy0GjJHZMg\\\", \\\"content\\\": \\\"San Francisco Bay Area Weather April 6, 2025. 396 views · 1 month ago ...more. KRON 4. 153K. Subscribe. 9. Share. Save. Report\\\", \\\"score\\\": 0.67321366}]', name='tavily_search_results_json', id='02e43dd6-4d9c-4b26-b9d0-085cdd117e0d', tool_call_id='call_0_57411449-29e7-4e88-a24b-8fe7665bcbf0', artifact={'query': 'current weather in San Francisco', 'follow_up_questions': None, 'answer': None, 'images': [], 'results': [{'url': 'https://world-weather.info/forecast/usa/san_francisco/april-2025/', 'title': 'Weather in San Francisco in April 2025 (California)', 'content': 'Detailed ⚡ San Francisco Weather Forecast for April 2025 – day/night 🌡️ temperatures, precipitations – World-Weather.info.', 'score': 0.75027883, 'raw_content': None}, {'url': 'https://www.youtube.com/watch?v=Hzy0GjJHZMg', 'title': 'San Francisco Bay Area Weather April 6, 2025 - YouTube', 'content': 'San Francisco Bay Area Weather April 6, 2025. 396 views · 1 month ago ...more. KRON 4. 153K. Subscribe. 9. Share. Save. Report', 'score': 0.67321366, 'raw_content': None}], 'response_time': 6.36}),\\n\",\n \" AIMessage(content=\\\"I couldn't retrieve the current weather for San Francisco. You can check the latest weather updates on websites like [Weather.com](https://weather.com) or [AccuWeather](https://www.accuweather.com). Let me know if you'd like me to try again or assist with something else!\\\", additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 62, 'prompt_tokens': 370, 'total_tokens': 432, 'completion_tokens_details': None, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 128}, 'prompt_cache_hit_tokens': 128, 'prompt_cache_miss_tokens': 242}, 'model_name': 'deepseek-chat', 'system_fingerprint': 'fp_8802369eaa_prod0425fp8', 'id': '9d21095c-0424-4a49-a6da-cff7a30f2570', 'service_tier': None, 'finish_reason': 'stop', 'logprobs': None}, id='run--3f06bef6-78a8-4810-b647-7d0bb4e07928-0', usage_metadata={'input_tokens': 370, 'output_tokens': 62, 'total_tokens': 432, 'input_token_details': {'cache_read': 128}, 'output_token_details': {}})]\"\n ]\n },\n \"metadata\": {},\n \"execution_count\": 9\n }\n ],\n \"source\": [\n \"response = agent_executor.invoke(\\n\",\n \" {\\\"messages\\\": [HumanMessage(content=\\\"whats the weather in sf?\\\")]}\\n\",\n \")\\n\",\n \"response[\\\"messages\\\"]\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"c174f838\",\n \"metadata\": {\n \"id\": \"c174f838\"\n },\n \"source\": [\n \"We can check out the [LangSmith trace](https://smith.langchain.com/public/f520839d-cd4d-4495-8764-e32b548e235d/r) to make sure it's calling the search tool effectively.\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"8f6ca7e4\",\n \"metadata\": {\n \"id\": \"8f6ca7e4\"\n },\n \"source\": [\n \"## Streaming Messages\\n\",\n \"\\n\",\n \"We've seen how the agent can be called with `.invoke` to get a final response. If the agent executes multiple steps, this may take a while. To show intermediate progress, we can stream back messages as they occur.\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 10,\n \"id\": \"bd93812b-2350-4d7f-9643-34c753503754\",\n \"metadata\": {\n \"id\": \"bd93812b-2350-4d7f-9643-34c753503754\",\n \"outputId\": \"4669f0e5-c797-4fa8-9f20-68203838de8c\",\n \"colab\": {\n \"base_uri\": \"https://localhost:8080/\"\n }\n },\n \"outputs\": [\n {\n \"output_type\": \"stream\",\n \"name\": \"stdout\",\n \"text\": [\n \"================================\\u001b[1m Human Message \\u001b[0m=================================\\n\",\n \"\\n\",\n \"whats the weather in sf?\\n\",\n \"==================================\\u001b[1m Ai Message \\u001b[0m==================================\\n\",\n \"Tool Calls:\\n\",\n \" tavily_search_results_json (call_0_3a600c03-5f7a-4038-b89a-0714ec205c7e)\\n\",\n \" Call ID: call_0_3a600c03-5f7a-4038-b89a-0714ec205c7e\\n\",\n \" Args:\\n\",\n \" query: current weather in San Francisco\\n\",\n \"=================================\\u001b[1m Tool Message \\u001b[0m=================================\\n\",\n \"Name: tavily_search_results_json\\n\",\n \"\\n\",\n \"[{\\\"title\\\": \\\"Weather in San Francisco, April 6\\\", \\\"url\\\": \\\"https://world-weather.info/forecast/usa/san_francisco/06-april/\\\", \\\"content\\\": \\\"Detailed ⚡ Weather Forecast for April 6 in San Francisco, California ... April 06, 2025. Night. +50°. 5.8. 30.2. 84%. Morning. +48°. 4.3. 30.2. 85%. Day. +61°.\\\", \\\"score\\\": 0.90791017}, {\\\"title\\\": \\\"April 2025 Weather History in San Francisco California, United States\\\", \\\"url\\\": \\\"https://weatherspark.com/h/m/557/2025/4/Historical-Weather-in-April-2025-in-San-Francisco-California-United-States\\\", \\\"content\\\": \\\"April 2025 Weather History in San Francisco California, United States ; San Francisco Temperature History April 2025 · 40°F · 40°F · 45°F · 45°F\\\", \\\"score\\\": 0.82288796}]\\n\",\n \"==================================\\u001b[1m Ai Message \\u001b[0m==================================\\n\",\n \"\\n\",\n \"The current weather in San Francisco is approximately 50°F (10°C) at night, with a high of around 61°F (16°C) during the day. Humidity levels are around 84-85%. For more detailed forecasts, you can check [this link](https://world-weather.info/forecast/usa/san_francisco/06-april/).\\n\"\n ]\n }\n ],\n \"source\": [\n \"for step in agent_executor.stream(\\n\",\n \" {\\\"messages\\\": [HumanMessage(content=\\\"whats the weather in sf?\\\")]},\\n\",\n \" stream_mode=\\\"values\\\",\\n\",\n \"):\\n\",\n \" step[\\\"messages\\\"][-1].pretty_print()\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"c72b3043\",\n \"metadata\": {\n \"id\": \"c72b3043\"\n },\n \"source\": [\n \"## Streaming tokens\\n\",\n \"\\n\",\n \"In addition to streaming back messages, it is also useful to stream back tokens.\\n\",\n \"We can do this by specifying `stream_mode=\\\"messages\\\"`.\\n\",\n \"\\n\",\n \"\\n\",\n \"::: note\\n\",\n \"\\n\",\n \"Below we use `message.text()`, which requires `langchain-core>=0.3.37`.\\n\",\n \"\\n\",\n \":::\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 11,\n \"id\": \"63198158-380e-43a3-a2ad-d4288949c1d4\",\n \"metadata\": {\n \"id\": \"63198158-380e-43a3-a2ad-d4288949c1d4\",\n \"outputId\": \"e7dafc65-f7ff-4dd0-f67b-461b343ae91b\",\n \"colab\": {\n \"base_uri\": \"https://localhost:8080/\"\n }\n },\n \"outputs\": [\n {\n \"output_type\": \"stream\",\n \"name\": \"stdout\",\n \"text\": [\n \"The| current| weather| in| San| Francisco| is| as| follows|:\\n\",\n \"\\n\",\n \"|-| **|Night|**:| Around| |50|°|F| (|10|°|C|),| with| |84|%| humidity|.\\n\",\n \"|-| **|Morning|**:| Approximately| |48|°|F| (|9|°|C|),| with| |85|%| humidity|.\\n\",\n \"|-| **|Day|**:| Expected| to| reach| |61|°|F| (|16|°|C|).\\n\",\n \"\\n\",\n \"|For| more| detailed| forecasts|,| you| can| check| [|World| Weather| Info|](|https|://|world|-|weather|.info|/|fore|cast|/|usa|/s|an|_f|ran|cis|co|/|06|-ap|ril|/)| or| [|Weather|Spark|](|https|://|weather|sp|ark|.com|/h|/m|/|557|/|202|5|/|4|/|Historical|-|Weather|-in|-A|pril|-|202|5|-in|-S|an|-Fran|cis|co|-Cal|ifornia|-|United|-St|ates|).|\"\n ]\n }\n ],\n \"source\": [\n \"for step, metadata in agent_executor.stream(\\n\",\n \" {\\\"messages\\\": [HumanMessage(content=\\\"whats the weather in sf?\\\")]},\\n\",\n \" stream_mode=\\\"messages\\\",\\n\",\n \"):\\n\",\n \" if metadata[\\\"langgraph_node\\\"] == \\\"agent\\\" and (text := step.text()):\\n\",\n \" print(text, end=\\\"|\\\")\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"022cbc8a\",\n \"metadata\": {\n \"id\": \"022cbc8a\"\n },\n \"source\": [\n \"## Adding in memory\\n\",\n \"\\n\",\n \"As mentioned earlier, this agent is stateless. This means it does not remember previous interactions. To give it memory we need to pass in a checkpointer. When passing in a checkpointer, we also have to pass in a `thread_id` when invoking the agent (so it knows which thread/conversation to resume from).\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 12,\n \"id\": \"c4073e35\",\n \"metadata\": {\n \"id\": \"c4073e35\"\n },\n \"outputs\": [],\n \"source\": [\n \"from langgraph.checkpoint.memory import MemorySaver\\n\",\n \"\\n\",\n \"memory = MemorySaver()\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 13,\n \"id\": \"e64a944e-f9ac-43cf-903c-d3d28d765377\",\n \"metadata\": {\n \"id\": \"e64a944e-f9ac-43cf-903c-d3d28d765377\"\n },\n \"outputs\": [],\n \"source\": [\n \"agent_executor = create_react_agent(model, tools, checkpointer=memory)\\n\",\n \"\\n\",\n \"config = {\\\"configurable\\\": {\\\"thread_id\\\": \\\"abc123\\\"}}\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 14,\n \"id\": \"a13462d0-2d02-4474-921e-15a1ba1fa274\",\n \"metadata\": {\n \"id\": \"a13462d0-2d02-4474-921e-15a1ba1fa274\",\n \"outputId\": \"84eecb2a-cfb7-4e43-9b3e-6a76ae206508\",\n \"colab\": {\n \"base_uri\": \"https://localhost:8080/\"\n }\n },\n \"outputs\": [\n {\n \"output_type\": \"stream\",\n \"name\": \"stdout\",\n \"text\": [\n \"{'agent': {'messages': [AIMessage(content='Hi Bob! How can I assist you today? 😊', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 12, 'prompt_tokens': 148, 'total_tokens': 160, 'completion_tokens_details': None, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 128}, 'prompt_cache_hit_tokens': 128, 'prompt_cache_miss_tokens': 20}, 'model_name': 'deepseek-chat', 'system_fingerprint': 'fp_8802369eaa_prod0425fp8', 'id': '2903417e-c6f6-4e1a-927c-4399ad15beb8', 'service_tier': None, 'finish_reason': 'stop', 'logprobs': None}, id='run--bc0a7245-9a7d-4cd8-a81d-b55cb3d9fb85-0', usage_metadata={'input_tokens': 148, 'output_tokens': 12, 'total_tokens': 160, 'input_token_details': {'cache_read': 128}, 'output_token_details': {}})]}}\\n\",\n \"----\\n\"\n ]\n }\n ],\n \"source\": [\n \"for chunk in agent_executor.stream(\\n\",\n \" {\\\"messages\\\": [HumanMessage(content=\\\"hi im bob!\\\")]}, config\\n\",\n \"):\\n\",\n \" print(chunk)\\n\",\n \" print(\\\"----\\\")\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": 15,\n \"id\": \"56d8028b-5dbc-40b2-86f5-ed60631d86a3\",\n \"metadata\": {\n \"id\": \"56d8028b-5dbc-40b2-86f5-ed60631d86a3\",\n \"outputId\": \"d7dbdd18-6599-4cab-9c30-58f4ed7c23c4\",\n \"colab\": {\n \"base_uri\": \"https://localhost:8080/\"\n }\n },\n \"outputs\": [\n {\n \"output_type\": \"stream\",\n \"name\": \"stdout\",\n \"text\": [\n \"{'agent': {'messages': [AIMessage(content=\\\"Your name is Bob! Unless you'd like me to call you something else? 😊\\\", additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 18, 'prompt_tokens': 168, 'total_tokens': 186, 'completion_tokens_details': None, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 128}, 'prompt_cache_hit_tokens': 128, 'prompt_cache_miss_tokens': 40}, 'model_name': 'deepseek-chat', 'system_fingerprint': 'fp_8802369eaa_prod0425fp8', 'id': 'd1b04bd7-fd78-4e51-a1cd-b014c33c9b8d', 'service_tier': None, 'finish_reason': 'stop', 'logprobs': None}, id='run--ca5d1c28-26ba-488c-b97b-5491d893ad71-0', usage_metadata={'input_tokens': 168, 'output_tokens': 18, 'total_tokens': 186, 'input_token_details': {'cache_read': 128}, 'output_token_details': {}})]}}\\n\",\n \"----\\n\"\n ]\n }\n ],\n \"source\": [\n \"for chunk in agent_executor.stream(\\n\",\n \" {\\\"messages\\\": [HumanMessage(content=\\\"whats my name?\\\")]}, config\\n\",\n \"):\\n\",\n \" print(chunk)\\n\",\n \" print(\\\"----\\\")\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"bda99754-0a11-4447-b408-e8db8f2e3517\",\n \"metadata\": {\n \"id\": \"bda99754-0a11-4447-b408-e8db8f2e3517\"\n },\n \"source\": [\n \"Example [LangSmith trace](https://smith.langchain.com/public/fa73960b-0f7d-4910-b73d-757a12f33b2b/r)\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"ae908088\",\n \"metadata\": {\n \"id\": \"ae908088\"\n },\n \"source\": [\n \"If you want to start a new conversation, all you have to do is change the `thread_id` used\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"24460239\",\n \"metadata\": {\n \"id\": \"24460239\",\n \"outputId\": \"d57212f8-c1c3-4839-c7b6-5997cb9ab90d\"\n },\n \"outputs\": [\n {\n \"name\": \"stdout\",\n \"output_type\": \"stream\",\n \"text\": [\n \"{'agent': {'messages': [AIMessage(content=\\\"I'm afraid I don't actually know your name. As an AI assistant without personal information about you, I don't have a specific name associated with our conversation.\\\", response_metadata={'id': 'msg_01NoaXNNYZKSoBncPcLkdcbo', 'model': 'claude-3-sonnet-20240229', 'stop_reason': 'end_turn', 'stop_sequence': None, 'usage': {'input_tokens': 267, 'output_tokens': 36}}, id='run-c9f7df3d-525a-4d8f-bbcf-a5b4a5d2e4b0-0', usage_metadata={'input_tokens': 267, 'output_tokens': 36, 'total_tokens': 303})]}}\\n\",\n \"----\\n\"\n ]\n }\n ],\n \"source\": [\n \"config = {\\\"configurable\\\": {\\\"thread_id\\\": \\\"xyz123\\\"}}\\n\",\n \"for chunk in agent_executor.stream(\\n\",\n \" {\\\"messages\\\": [HumanMessage(content=\\\"whats my name?\\\")]}, config\\n\",\n \"):\\n\",\n \" print(chunk)\\n\",\n \" print(\\\"----\\\")\"\n ]\n },\n {\n \"cell_type\": \"markdown\",\n \"id\": \"c029798f\",\n \"metadata\": {\n \"id\": \"c029798f\"\n },\n \"source\": [\n \"## Conclusion\\n\",\n \"\\n\",\n \"That's a wrap! In this quick start we covered how to create a simple agent.\\n\",\n \"We've then shown how to stream back a response - not only with the intermediate steps, but also tokens!\\n\",\n \"We've also added in memory so you can have a conversation with them.\\n\",\n \"Agents are a complex topic with lots to learn!\\n\",\n \"\\n\",\n \"For more information on Agents, please check out the [LangGraph](/docs/concepts/architecture/#langgraph) documentation. This has it's own set of concepts, tutorials, and how-to guides.\"\n ]\n },\n {\n \"cell_type\": \"code\",\n \"execution_count\": null,\n \"id\": \"e3ec3244\",\n \"metadata\": {\n \"id\": \"e3ec3244\"\n },\n \"outputs\": [],\n \"source\": []\n }\n ],\n \"metadata\": {\n \"kernelspec\": {\n \"display_name\": \"Python 3 (ipykernel)\",\n \"language\": \"python\",\n \"name\": \"python3\"\n },\n \"language_info\": {\n \"codemirror_mode\": {\n \"name\": \"ipython\",\n \"version\": 3\n },\n \"file_extension\": \".py\",\n \"mimetype\": \"text/x-python\",\n \"name\": \"python\",\n \"nbconvert_exporter\": \"python\",\n \"pygments_lexer\": \"ipython3\",\n \"version\": \"3.10.4\"\n },\n \"colab\": {\n \"provenance\": []\n }\n },\n \"nbformat\": 4,\n \"nbformat_minor\": 5\n}"}}}}