{ "cells": [ { "cell_type": "markdown", "id": "8bcd1a3d-7c50-4f58-be4e-1ed654aa33be", "metadata": {}, "source": [ "# ReAct agent with tool calling\n", "\n", "This notebook walks through an example creating a ReAct Agent that uses tool calling.\n", "This is useful for getting started quickly.\n", "However, it is highly likely you will want to customize the logic - for information on that, check out the other examples in this folder." ] }, { "cell_type": "markdown", "id": "e130cf70-a30e-47d7-8fd5-464f1a92e374", "metadata": {}, "source": [ "## Set up the chat model and tools\n", "\n", "Here we will define the chat model and tools that we want to use.\n", "Importantly, this model MUST support OpenAI function calling." ] }, { "cell_type": "code", "execution_count": 1, "id": "efb7e3c0-c63f-40f6-93ce-19681d650fc2", "metadata": {}, "outputs": [], "source": [ "from langchain_community.tools.tavily_search import TavilySearchResults\n", "from langchain_core.messages import HumanMessage\n", "from langchain_openai import ChatOpenAI\n", "\n", "from langgraph.prebuilt import create_react_agent" ] }, { "cell_type": "code", "execution_count": 2, "id": "a7025f33-3160-41cf-868b-17ebc916fb1d", "metadata": {}, "outputs": [], "source": [ "tools = [TavilySearchResults(max_results=1)]\n", "model = ChatOpenAI()" ] }, { "cell_type": "markdown", "id": "43064805-2ac9-4b5a-850c-a68dd7282350", "metadata": {}, "source": [ "## Create executor\n", "\n", "We can now use the high level interface to create the executor" ] }, { "cell_type": "code", "execution_count": 3, "id": "32b4ae66-f667-4a8b-a602-503fd0effcd9", "metadata": {}, "outputs": [], "source": [ "app = create_react_agent(model, tools=tools)" ] }, { "cell_type": "markdown", "id": "d63dbfc7-a5c1-4a03-991c-f0789ba52c52", "metadata": {}, "source": [ "We can now invoke this executor. The input to this must be a dictionary with a single `messages` key that contains a list of messages." ] }, { "cell_type": "code", "execution_count": 4, "id": "0abc5655-d772-450c-832f-1fee1111a5f6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'messages': [AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_eI2B853W8Jrm8IvmwEafikFv', 'function': {'arguments': '{\"query\": \"weather in San Francisco\"}', 'name': 'tavily_search_results_json'}, 'type': 'function'}, {'id': 'call_Aky1m2Z5dvUcHKyha7r5s3Wj', 'function': {'arguments': '{\"query\": \"weather in Los Angeles\"}', 'name': 'tavily_search_results_json'}, 'type': 'function'}]})]}\n", "----\n", "{'messages': [ToolMessage(content=\"[{'url': 'https://www.wunderground.com/forecast/us/ca/san-francisco', 'content': 'Get the latest weather information for San Francisco, CA, including temperature, precipitation, wind speed, and humidity. See the hourly and 10-day forecast for the South of Market station and other nearby weather stations.'}]\", tool_call_id='call_eI2B853W8Jrm8IvmwEafikFv'), ToolMessage(content=\"[{'url': 'https://www.accuweather.com/en/us/los-angeles/90012/hourly-weather-forecast/347625', 'content': 'Get the latest hourly weather updates for Los Angeles, CA, including rain alerts, air quality, wind speed and direction, humidity, and cloud cover. See the forecast for the next eight hours and plan your activities accordingly.'}]\", tool_call_id='call_Aky1m2Z5dvUcHKyha7r5s3Wj')]}\n", "----\n", "{'messages': [AIMessage(content='The weather in San Francisco can be found [here](https://www.wunderground.com/forecast/us/ca/san-francisco), which includes information on temperature, precipitation, wind speed, and humidity.\\n\\nFor Los Angeles, you can check the hourly weather updates [here](https://www.accuweather.com/en/us/los-angeles/90012/hourly-weather-forecast/347625), which includes details on rain alerts, air quality, wind speed and direction, humidity, and cloud cover.')]}\n", "----\n", "{'messages': [HumanMessage(content='what is the weather in sf and la'), AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_eI2B853W8Jrm8IvmwEafikFv', 'function': {'arguments': '{\"query\": \"weather in San Francisco\"}', 'name': 'tavily_search_results_json'}, 'type': 'function'}, {'id': 'call_Aky1m2Z5dvUcHKyha7r5s3Wj', 'function': {'arguments': '{\"query\": \"weather in Los Angeles\"}', 'name': 'tavily_search_results_json'}, 'type': 'function'}]}), ToolMessage(content=\"[{'url': 'https://www.wunderground.com/forecast/us/ca/san-francisco', 'content': 'Get the latest weather information for San Francisco, CA, including temperature, precipitation, wind speed, and humidity. See the hourly and 10-day forecast for the South of Market station and other nearby weather stations.'}]\", tool_call_id='call_eI2B853W8Jrm8IvmwEafikFv'), ToolMessage(content=\"[{'url': 'https://www.accuweather.com/en/us/los-angeles/90012/hourly-weather-forecast/347625', 'content': 'Get the latest hourly weather updates for Los Angeles, CA, including rain alerts, air quality, wind speed and direction, humidity, and cloud cover. See the forecast for the next eight hours and plan your activities accordingly.'}]\", tool_call_id='call_Aky1m2Z5dvUcHKyha7r5s3Wj'), AIMessage(content='The weather in San Francisco can be found [here](https://www.wunderground.com/forecast/us/ca/san-francisco), which includes information on temperature, precipitation, wind speed, and humidity.\\n\\nFor Los Angeles, you can check the hourly weather updates [here](https://www.accuweather.com/en/us/los-angeles/90012/hourly-weather-forecast/347625), which includes details on rain alerts, air quality, wind speed and direction, humidity, and cloud cover.')]}\n", "----\n" ] } ], "source": [ "inputs = {\"messages\": [HumanMessage(content=\"what is the weather in sf and la\")]}\n", "for s in app.stream(inputs):\n", " print(list(s.values())[0])\n", " print(\"----\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.6" } }, "nbformat": 4, "nbformat_minor": 5 }