Files
langgraph/examples/chat_agent_executor_with_function_calling/high-level.ipynb
T
4a5206c93e add notebook for get update state (#180)
* add notebook for get update state

* cr

* Fix bad merge

* Update notebook

* Update

* Fix unbound local bug

* Fix duplicate history entries

* Update notebook with time travel section

* Update notebook

* cr

* cr

---------

Co-authored-by: Nuno Campos <nuno@langchain.dev>
2024-03-15 14:35:01 -07:00

4.8 KiB

Chat Executor: with function calling

This notebook walks through an example creating a chat executor that uses function 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_openai import ChatOpenAI
from langchain_community.tools.tavily_search import TavilySearchResults
from langgraph.prebuilt import chat_agent_executor
from langchain_core.messages import HumanMessage
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 = chat_agent_executor.create_function_calling_executor(model, tools)

We can now invoke this executor. The input to this must be a dictionary with a single messsages key that contains a list of messages.

In [4]:
inputs = {"messages": [HumanMessage(content="what is the weather in sf")]}
for s in app.stream(inputs):
    print(list(s.values())[0])
    print("----")
{'messages': [AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{"query":"weather in San Francisco"}', 'name': 'tavily_search_results_json'}})]}
----
{'messages': [FunctionMessage(content="[{'url': 'https://www.accuweather.com/en/us/san-francisco/94103/weather-forecast/347629', 'content': 'Get the current and future weather conditions for San Francisco, CA, including temperature, precipitation, wind, air quality and more. See the hourly and 10-day outlook, radar maps, alerts and allergy information.'}]", name='tavily_search_results_json')]}
----
{'messages': [AIMessage(content='You can check the current and future weather conditions for San Francisco, CA on [AccuWeather](https://www.accuweather.com/en/us/san-francisco/94103/weather-forecast/347629).')]}
----
{'messages': [HumanMessage(content='what is the weather in sf'), AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{"query":"weather in San Francisco"}', 'name': 'tavily_search_results_json'}}), FunctionMessage(content="[{'url': 'https://www.accuweather.com/en/us/san-francisco/94103/weather-forecast/347629', 'content': 'Get the current and future weather conditions for San Francisco, CA, including temperature, precipitation, wind, air quality and more. See the hourly and 10-day outlook, radar maps, alerts and allergy information.'}]", name='tavily_search_results_json'), AIMessage(content='You can check the current and future weather conditions for San Francisco, CA on [AccuWeather](https://www.accuweather.com/en/us/san-francisco/94103/weather-forecast/347629).')]}
----
In [ ]: