Files
langgraph/examples/chat_agent_executor_with_function_calling/high-level-tools.ipynb
T
2024-09-05 12:03:55 -04:00

6.5 KiB

ReAct agent with tool calling

This notebook walks through an example creating a ReAct Agent that uses tool calling. This is useful for getting started quickly. However, it is highly likely you will want to customize the logic - for information on that, check out the other examples in this folder.

Set up the chat model and tools

Here we will define the chat model and tools that we want to use. Importantly, this model MUST support OpenAI function calling.

In [1]:
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_core.messages import HumanMessage
from langchain_openai import ChatOpenAI

from langgraph.prebuilt import create_react_agent
In [2]:
tools = [TavilySearchResults(max_results=1)]
model = ChatOpenAI()

Create executor

We can now use the high level interface to create the executor

In [3]:
app = create_react_agent(model, tools=tools)

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.

In [4]:
inputs = {"messages": [HumanMessage(content="what is the weather in sf and la")]}
for s in app.stream(inputs):
    print(list(s.values())[0])
    print("----")
{'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'}]})]}
----
{'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')]}
----
{'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.')]}
----
{'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.')]}
----