From 95b333f474af0b1edb829c0afb37bc8669408217 Mon Sep 17 00:00:00 2001 From: Harrison Chase Date: Mon, 15 Jan 2024 15:30:35 -0800 Subject: [PATCH] stash --- README.md | 377 +++++++------- .../base.ipynb | 473 ++++++++++++++++-- .../dynamically-returning-directly.ipynb | 471 +++++++++++++---- .../force-calling-a-tool-first.ipynb | 356 ++++++++++--- .../human-in-the-loop.ipynb | 325 ++++++++++-- .../managing-agent-steps.ipynb | 340 ++++++++++--- .../respond-in-format.ipynb | 333 +++++++++--- langgraph/prebuilt/__init__.py | 4 +- langgraph/prebuilt/tool_executor.py | 22 +- 9 files changed, 2115 insertions(+), 586 deletions(-) diff --git a/README.md b/README.md index d3863b8d4..21fe41dfa 100644 --- a/README.md +++ b/README.md @@ -23,13 +23,13 @@ pip install langgraph ## Quick Start -Here we will go over an example of recreating the [`AgentExecutor`](https://python.langchain.com/docs/modules/agents/concepts#agentexecutor) class from LangChain. -The benefits of creating it with LangGraph is that it is more modifiable. +Here we will go over an example of creating a simple agent that uses chat models and function calling. +This agent will represent all state as a list of messages. -We will also want to install some LangChain packages, as well as [Tavily](https://app.tavily.com/sign-in) to use as an example tool. +We will need to install some LangChain packages, as well as [Tavily](https://app.tavily.com/sign-in) to use as an example tool. ```shell -pip install -U langchain langchain_openai langchainhub tavily-python +pip install -U langchain langchain_openai tavily-python ``` We also need to export some environment variables needed for our agent. @@ -47,31 +47,57 @@ export LANGCHAIN_API_KEY=ls__... export LANGCHAIN_ENDPOINT=https://api.langchain.plus ``` -### Define the LangChain Agent +### Set up the tools -This is the LangChain agent. -Crucially, this agent is just responsible for deciding what actions to take. -For more information on what is happening here, please see [this documentation](https://python.langchain.com/docs/modules/agents/quick_start). +We will first define the tools we want to use. +For this simple example, we will use a built-in search tool via Tavily. +However, it is really easy to create your own tools - see documentation [here](https://python.langchain.com/docs/modules/agents/tools/custom_tools) on how to do that. ```python -from langchain import hub -from langchain.agents import create_openai_functions_agent -from langchain_openai.chat_models import ChatOpenAI from langchain_community.tools.tavily_search import TavilySearchResults tools = [TavilySearchResults(max_results=1)] - -# Get the prompt to use - you can modify this! -prompt = hub.pull("hwchase17/openai-functions-agent") - -# Choose the LLM that will drive the agent -# We set streaming=True so that we can stream tokens (we will cover this more detail later on) -llm = ChatOpenAI(model="gpt-3.5-turbo-1106", streaming=True) - -# Construct the OpenAI Functions agent -agent_runnable = create_openai_functions_agent(llm, tools, prompt) ``` +We can now wrap these tools in a simple ToolExecutor. +This is a real simple class that takes in a ToolInvocation and calls that tool, returning the output. +A ToolInvocation is any class with `tool` and `tool_input` attribute. + +```python +from langgraph.prebuilt import ToolExecutor + +tool_executor = ToolExecutor(tools) +``` + +### Set up the model + +Now we need to load the chat model we want to use. +Importantly, this should satisfy two criteria: + +1. It should work with messages. We will represent all agent state in the form of messages, so it needs to be able to work well with them. +2. It should work with OpenAI function calling. This means it should either be an OpenAI model or a model that exposes a similar interface. + +Note: these model requirements are not requirements for using LangGraph - they are just requirements for this one example. + +```python +from langchain_openai import ChatOpenAI + +# We will set streaming=True so that we can stream tokens +# See the streaming section for more information on this. +model = ChatOpenAI(temperature=0, streaming=True) +``` + +After we've done this, we should make sure the model knows that it has these tools available to call. +We can do this by converting the LangChain tools into the format for OpenAI function calling, and then bind them to the model class. + +```python +from langchain.tools.render import format_tool_to_openai_function + +functions = [format_tool_to_openai_function(t) for t in tools] +model = model.bind_functions(functions) +``` + + ### Define the agent state The main type of graph in `langgraph` is the `StatefulGraph`. @@ -80,35 +106,18 @@ Each node then returns operations to update that state. These operations can either SET specific attributes on the state (e.g. overwrite the existing values) or ADD to the existing attribute. Whether to set or add is denoted by annotating the state object you construct the graph with. -The state for the traditional LangChain agent has a few attributes: - -1. `input`: This is the input string representing the main ask from the user, passed in as input. -2. `chat_history`: This is any previous conversation messages, also passed in as input. -3. `intermediate_steps`: This is list of actions and corresponding observations that the agent takes over time. This is updated each iteration of the agent. -4. `agent_outcome`: This is the response from the agent, either an AgentAction or AgentFinish. The AgentExecutor should finish when this is an AgentFinish, otherwise it should call the requested tools. - -Let's make these ideas concrete by create an agent state! +For this example, the state we will track will just be a list of messages. +We want each node to just add messages to that list. +Therefore, we will use a `TypedDict` with one key (`messages`) and annotate it so that the `messages` attribute is always added to. ```python -from typing import TypedDict, Annotated, Sequence, Union -from langchain_core.agents import AgentAction, AgentFinish -from langchain_core.messages import BaseMessage +from typing import TypedDict, Annotated, Sequence import operator +from langchain_core.messages import BaseMessage class AgentState(TypedDict): - # The input string - input: str - # The list of previous messages in the conversation - chat_history: Sequence[BaseMessage] - # The outcome of a given call to the agent - # Needs `None` as a valid type, since this is what this will start as - agent_outcome: Union[AgentAction, AgentFinish, None] - # List of actions and corresponding observations - # Here we annotate this with `operator.add` to indicate that operations to - # this state should be ADDED to the existing values (not overwrite it) - intermediate_steps: Annotated[list[tuple[AgentAction, str]], operator.add] - + messages: Annotated[Sequence[BaseMessage], operator.add] ``` ### Define the nodes @@ -133,36 +142,45 @@ The path that is taken is not known until that node is run (the LLM decides). Let's define the nodes, as well as a function to decide how what conditional edge to take. ```python -from langchain_core.agents import AgentFinish -from langgraph.prebuilt.tool_executor import ToolExecutor +from langgraph.prebuilt import ToolInvocation +import json +from langchain_core.messages import FunctionMessage -# This a helper class we have that is useful for running tools -# It takes in an agent action and calls that tool and returns the result -tool_executor = ToolExecutor(tools) - -# Define the agent -def run_agent(data): - agent_outcome = agent_runnable.invoke(data) - return {"agent_outcome": agent_outcome} - -# Define the function to execute tools -def execute_tools(data): - # Get the most recent agent_outcome - this is the key added in the `agent` above - agent_action = data['agent_outcome'] - output = tool_executor.invoke(agent_action) - return {"intermediate_steps": [(agent_action, str(output))]} - -# Define logic that will be used to determine which conditional edge to go down -def should_continue(data): - # If the agent outcome is an AgentFinish, then we return `exit` string - # This will be used when setting up the graph to define the flow - if isinstance(data['agent_outcome'], AgentFinish): +# Define the function that determines whether to continue or not +def should_continue(state): + messages = state['messages'] + last_message = messages[-1] + # If there is no function call, then we finish + if "function_call" not in last_message.additional_kwargs: return "end" - # Otherwise, an AgentAction is returned - # Here we return `continue` string - # This will be used when setting up the graph to define the flow + # Otherwise if there is, we continue else: return "continue" + +# Define the function that calls the model +def call_model(state): + messages = state['messages'] + response = model.invoke(messages) + # We return a list, because this will get added to the existing list + return {"messages": [response]} + +# Define the function to execute tools +def call_tool(state): + messages = state['messages'] + # Based on the continue condition + # we know the last message involves a function call + last_message = messages[-1] + # We construct an ToolInvocation from the function_call + action = ToolInvocation( + tool=last_message.additional_kwargs["function_call"]["name"], + tool_input=json.loads(last_message.additional_kwargs["function_call"]["arguments"]), + ) + # We call the tool_executor and get back a response + response = tool_executor.invoke(action) + # We use the response to create a FunctionMessage + function_message = FunctionMessage(content=str(response), name=action.tool) + # We return a list, because this will get added to the existing list + return {"messages": [function_message]} ``` ### Define the graph @@ -170,59 +188,65 @@ def should_continue(data): We can now put it all together and define the graph! ```python -from langgraph.graph import END, StateGraph - +from langgraph.graph import StateGraph, END # Define a new graph - workflow = StateGraph(AgentState) +workflow = StateGraph(AgentState) - # Define the two nodes we will cycle between - workflow.add_node("agent", run_agent) - workflow.add_node("action", execute_tools) +# Define the two nodes we will cycle between +workflow.add_node("agent", call_model) +workflow.add_node("action", call_tool) - # Set the entrypoint as `agent` - # This means that this node is the first one called - workflow.set_entry_point("agent") +# Set the entrypoint as `agent` +# This means that this node is the first one called +workflow.set_entry_point("agent") - # We now add a conditional edge - workflow.add_conditional_edges( - # First, we define the start node. We use `agent`. - # This means these are the edges taken after the `agent` node is called. - "agent", - # Next, we pass in the function that will determine which node is called next. - should_continue, - # Finally we pass in a mapping. - # The keys are strings, and the values are other nodes. - # END is a special node marking that the graph should finish. - # What will happen is we will call `should_continue`, and then the output of that - # will be matched against the keys in this mapping. - # Based on which one it matches, that node will then be called. - { - # If `tools`, then we call the tool node. - "continue": "action", - # Otherwise we finish. - "end": END - } - ) +# We now add a conditional edge +workflow.add_conditional_edges( + # First, we define the start node. We use `agent`. + # This means these are the edges taken after the `agent` node is called. + "agent", + # Next, we pass in the function that will determine which node is called next. + should_continue, + # Finally we pass in a mapping. + # The keys are strings, and the values are other nodes. + # END is a special node marking that the graph should finish. + # What will happen is we will call `should_continue`, and then the output of that + # will be matched against the keys in this mapping. + # Based on which one it matches, that node will then be called. + { + # If `tools`, then we call the tool node. + "continue": "action", + # Otherwise we finish. + "end": END + } +) - # We now add a normal edge from `tools` to `agent`. - # This means that after `tools` is called, `agent` node is called next. - workflow.add_edge('action', 'agent') +# We now add a normal edge from `tools` to `agent`. +# This means that after `tools` is called, `agent` node is called next. +workflow.add_edge('action', 'agent') - # Finally, we compile it! - # This compiles it into a LangChain Runnable, - # meaning you can use it as you would any other runnable - chain = workflow.compile() +# Finally, we compile it! +# This compiles it into a LangChain Runnable, +# meaning you can use it as you would any other runnable +app = workflow.compile() ``` ### Use it! We can now use it! -This now exposes the [same interface](https://python.langchain.com/docs/expression_language/) as all other LangChain runnables +This now exposes the [same interface](https://python.langchain.com/docs/expression_language/) as all other LangChain runnables. +This runnable accepts a list of messages. ```python -chain.invoke({"input": "what is the weather in sf"}) +from langchain_core.messages import HumanMessage + +inputs = {"messages": [HumanMessage(content="what is the weather in sf")]} +app.invoke(inputs) ``` +This may take a little bit - it's making a few calls behind the scenes. +In order to start seeing some intermediate results as they happen, we can use streaming - see below for more information on that. + ## Streaming LangGraph has support for several different types of streaming. @@ -232,9 +256,8 @@ LangGraph has support for several different types of streaming. One of the benefits of using LangGraph is that it is easy to stream output as it's produced by each node. ```python -for output in chain.stream( - {"input": "what is the weather in sf"} -): +inputs = {"messages": [HumanMessage(content="what is the weather in sf")]} +for output in app.stream(inputs): # stream() yields dictionaries with output keyed by node name for key, value in output.items(): print(f"Output from node '{key}':") @@ -246,25 +269,25 @@ for output in chain.stream( ``` Output from node 'agent': --- -{'agent_outcome': AgentActionMessageLog(tool='tavily_search_results_json', tool_input={'query': 'weather in San Francisco'}, log="\nInvoking: `tavily_search_results_json` with `{'query': 'weather in San Francisco'}`\n\n\n", message_log=[AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{"query":"weather in San Francisco"}', 'name': 'tavily_search_results_json'}})])} +{'messages': [AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\n "query": "weather in San Francisco"\n}', 'name': 'tavily_search_results_json'}})]} --- Output from node 'action': --- -{'intermediate_steps': [(AgentActionMessageLog(tool='tavily_search_results_json', tool_input={'query': 'weather in San Francisco'}, log="\nInvoking: `tavily_search_results_json` with `{'query': 'weather in San Francisco'}`\n\n\n", message_log=[AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{"query":"weather in San Francisco"}', 'name': 'tavily_search_results_json'}})]), "[{'url': 'https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States', 'content': 'January 2024 Weather History in San Francisco California, United States Daily Precipitation in January 2024 in San Francisco Observed Weather in January 2024 in San Francisco San Francisco Temperature History January 2024 Hourly Temperature in January 2024 in San Francisco Hours of Daylight and Twilight in January 2024 in San FranciscoThis report shows the past weather for San Francisco, providing a weather history for January 2024. It features all historical weather data series we have available, including the San Francisco temperature history for January 2024. You can drill down from year to month and even day level reports by clicking on the graphs.'}]")]} +{'messages': [FunctionMessage(content="[{'url': 'https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States', 'content': 'January 2024 Weather History in San Francisco California, United States Daily Precipitation in January 2024 in San Francisco Observed Weather in January 2024 in San Francisco San Francisco Temperature History January 2024 Hourly Temperature in January 2024 in San Francisco Hours of Daylight and Twilight in January 2024 in San FranciscoThis report shows the past weather for San Francisco, providing a weather history for January 2024. It features all historical weather data series we have available, including the San Francisco temperature history for January 2024. You can drill down from year to month and even day level reports by clicking on the graphs.'}]", name='tavily_search_results_json')]} --- Output from node 'agent': --- -{'agent_outcome': AgentFinish(return_values={'output': "It seems that I couldn't retrieve the current weather in San Francisco. However, you can easily check the current weather in San Francisco by using a weather website or app."}, log="It seems that I couldn't retrieve the current weather in San Francisco. However, you can easily check the current weather in San Francisco by using a weather website or app.")} +{'messages': [AIMessage(content="I couldn't find the current weather in San Francisco. However, you can visit [WeatherSpark](https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States) to check the historical weather data for January 2024 in San Francisco.")]} --- Output from node '__end__': --- -{'input': 'what is the weather in sf', 'chat_history': [], 'agent_outcome': AgentFinish(return_values={'output': "It seems that I couldn't retrieve the current weather in San Francisco. However, you can easily check the current weather in San Francisco by using a weather website or app."}, log="It seems that I couldn't retrieve the current weather in San Francisco. However, you can easily check the current weather in San Francisco by using a weather website or app."), 'intermediate_steps': [(AgentActionMessageLog(tool='tavily_search_results_json', tool_input={'query': 'weather in San Francisco'}, log="\nInvoking: `tavily_search_results_json` with `{'query': 'weather in San Francisco'}`\n\n\n", message_log=[AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{"query":"weather in San Francisco"}', 'name': 'tavily_search_results_json'}})]), "[{'url': 'https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States', 'content': 'January 2024 Weather History in San Francisco California, United States Daily Precipitation in January 2024 in San Francisco Observed Weather in January 2024 in San Francisco San Francisco Temperature History January 2024 Hourly Temperature in January 2024 in San Francisco Hours of Daylight and Twilight in January 2024 in San FranciscoThis report shows the past weather for San Francisco, providing a weather history for January 2024. It features all historical weather data series we have available, including the San Francisco temperature history for January 2024. You can drill down from year to month and even day level reports by clicking on the graphs.'}]")]} +{'messages': [HumanMessage(content='what is the weather in sf'), AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\n "query": "weather in San Francisco"\n}', 'name': 'tavily_search_results_json'}}), FunctionMessage(content="[{'url': 'https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States', 'content': 'January 2024 Weather History in San Francisco California, United States Daily Precipitation in January 2024 in San Francisco Observed Weather in January 2024 in San Francisco San Francisco Temperature History January 2024 Hourly Temperature in January 2024 in San Francisco Hours of Daylight and Twilight in January 2024 in San FranciscoThis report shows the past weather for San Francisco, providing a weather history for January 2024. It features all historical weather data series we have available, including the San Francisco temperature history for January 2024. You can drill down from year to month and even day level reports by clicking on the graphs.'}]", name='tavily_search_results_json'), AIMessage(content="I couldn't find the current weather in San Francisco. However, you can visit [WeatherSpark](https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States) to check the historical weather data for January 2024 in San Francisco.")]} --- ``` @@ -276,10 +299,8 @@ In this case only the "agent" node produces LLM tokens. In order for this to work properly, you must be using an LLM that supports streaming as well as have set it when constructing the LLM (e.g. `ChatOpenAI(model="gpt-3.5-turbo-1106", streaming=True)`) ```python -async for output in chain.astream_log( - {"input": "what is the weather in sf", "intermediate_steps": []}, - include_types=["llm"], -): +inputs = {"messages": [HumanMessage(content="what is the weather in sf")]} +async for output in app.astream_log(inputs, include_types=["llm"]): # astream_log() yields the requested logs (here LLMs) in JSONPatch format for op in output.ops: if op["path"] == "/streamed_output/-": @@ -294,76 +315,98 @@ async for output in chain.astream_log( ``` content='' additional_kwargs={'function_call': {'arguments': '', 'name': 'tavily_search_results_json'}} -content='' additional_kwargs={'function_call': {'arguments': '{"', 'name': ''}} +content='' additional_kwargs={'function_call': {'arguments': '{\n', 'name': ''}} +content='' additional_kwargs={'function_call': {'arguments': ' ', 'name': ''}} +content='' additional_kwargs={'function_call': {'arguments': ' "', 'name': ''}} content='' additional_kwargs={'function_call': {'arguments': 'query', 'name': ''}} -content='' additional_kwargs={'function_call': {'arguments': '":"', 'name': ''}} -content='' additional_kwargs={'function_call': {'arguments': 'current', 'name': ''}} -content='' additional_kwargs={'function_call': {'arguments': ' weather', 'name': ''}} +content='' additional_kwargs={'function_call': {'arguments': '":', 'name': ''}} +content='' additional_kwargs={'function_call': {'arguments': ' "', 'name': ''}} +content='' additional_kwargs={'function_call': {'arguments': 'weather', 'name': ''}} content='' additional_kwargs={'function_call': {'arguments': ' in', 'name': ''}} content='' additional_kwargs={'function_call': {'arguments': ' San', 'name': ''}} content='' additional_kwargs={'function_call': {'arguments': ' Francisco', 'name': ''}} -content='' additional_kwargs={'function_call': {'arguments': '"}', 'name': ''}} +content='' additional_kwargs={'function_call': {'arguments': '"\n', 'name': ''}} +content='' additional_kwargs={'function_call': {'arguments': '}', 'name': ''}} content='' content='' content='I' -content=' found' -content=' a' -content=' website' -content=' that' -content=' provides' -content=' detailed' -content=' weather' -content=' information' -content=' for' -content=' San' -content=' Francisco' -content='.' -content=' You' -content=' can' -content=' visit' -content=' the' -content=' following' -content=' link' -content=' for' +content="'m" +content=' sorry' +content=',' +content=' but' +content=' I' +content=' couldn' +content="'t" +content=' find' content=' the' content=' current' content=' weather' -content=' report' -content=':' -content=' [' -content='San' +content=' in' +content=' San' content=' Francisco' -content=' Weather' -content=' Report' +content='.' +content=' However' +content=',' +content=' you' +content=' can' +content=' check' +content=' the' +content=' historical' +content=' weather' +content=' data' +content=' for' +content=' January' +content=' ' +content='202' +content='4' +content=' in' +content=' San' +content=' Francisco' +content=' [' +content='here' content='](' content='https' content='://' -content='www' -content='.weather' -content='25' +content='we' +content='athers' +content='park' content='.com' -content='/n' -content='orth' -content='-' -content='amer' -content='ica' +content='/h' +content='/m' content='/' -content='usa' -content='/cal' -content='ifornia' -content='/s' +content='557' +content='/' +content='202' +content='4' +content='/' +content='1' +content='/H' +content='istorical' +content='-' +content='Weather' +content='-in' +content='-Jan' +content='uary' +content='-' +content='202' +content='4' +content='-in' +content='-S' content='an' -content='-fr' +content='-F' +content='r' content='anc' content='isco' -content=')' +content='-Cal' +content='ifornia' +content='-' +content='United' +content='-' +content='States' +content=').' content='' ``` - - - - ## When to Use When should you use this versus [LangChain Expression Language](https://python.langchain.com/docs/expression_language/)? diff --git a/examples/chat_executor_with_function_calling/base.ipynb b/examples/chat_executor_with_function_calling/base.ipynb index 8b6397135..cae3f6212 100644 --- a/examples/chat_executor_with_function_calling/base.ipynb +++ b/examples/chat_executor_with_function_calling/base.ipynb @@ -1,87 +1,244 @@ { "cells": [ + { + "cell_type": "markdown", + "id": "51466c8d-8ce4-4b3d-be4e-18fdbeda5f53", + "metadata": {}, + "source": [ + "# Chat Executor\n", + "\n", + "In this example we will build a chat executor that uses function calling from scratch." + ] + }, + { + "cell_type": "markdown", + "id": "7cbd446a-808f-4394-be92-d45ab818953c", + "metadata": {}, + "source": [ + "## Setup\n", + "\n", + "First we need to install the packages required" + ] + }, { "cell_type": "code", "execution_count": 1, - "id": "aa9f9110-ea74-43af-aa72-6b45518abd6e", + "id": "af4ce0ba-7596-4e5f-8bf8-0b0bd6e62833", + "metadata": {}, + "outputs": [], + "source": [ + "!pip install --quiet -U langchain langchain_openai tavily-python" + ] + }, + { + "cell_type": "markdown", + "id": "0abe11f4-62ed-4dc4-8875-3db21e260d1d", + "metadata": {}, + "source": [ + "Next, we need to set API keys for OpenAI (the LLM we will use) and Tavily (the search tool we will use)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c903a1cf-2977-4e2d-ad7d-8b3946821d89", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import getpass\n", + "\n", + "os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key:\")\n", + "os.environ[\"TAVILY_API_KEY\"] = getpass.getpass(\"Tavily API Key:\")" + ] + }, + { + "cell_type": "markdown", + "id": "f0ed46a8-effe-4596-b0e1-a6a29ee16f5c", + "metadata": {}, + "source": [ + "Optionally, we can set API key for [LangSmith tracing](https://smith.langchain.com/), which will give us best-in-class observability." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "95e25aec-7c9f-4a63-b143-225d0e9a79c3", + "metadata": {}, + "outputs": [], + "source": [ + "os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n", + "os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass(\"LangSmith API Key:\")" + ] + }, + { + "cell_type": "markdown", + "id": "21ac643b-cb06-4724-a80c-2862ba4773f1", + "metadata": {}, + "source": [ + "## Set up the tools\n", + "\n", + "We will first define the tools we want to use.\n", + "For this simple example, we will use a built-in search tool via Tavily.\n", + "However, it is really easy to create your own tools - see documentation [here](https://python.langchain.com/docs/modules/agents/tools/custom_tools) on how to do that.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "d7ef57dd-5d6e-4ad3-9377-a92201c1310e", "metadata": {}, "outputs": [], "source": [ - "from langchain import hub\n", - "from langchain_openai import ChatOpenAI\n", "from langchain_community.tools.tavily_search import TavilySearchResults\n", - "from langgraph.prebuilt.messages_executor import create_messages_executor\n", - "from langchain_core.messages import HumanMessage" + "\n", + "tools = [TavilySearchResults(max_results=1)]" + ] + }, + { + "cell_type": "markdown", + "id": "01885785-b71a-44d1-b1d6-7b5b14d53b58", + "metadata": {}, + "source": [ + "We can now wrap these tools in a simple ToolExecutor.\n", + "This is a real simple class that takes in a ToolInvocation and calls that tool, returning the output.\n", + "A ToolInvocation is any class with `tool` and `tool_input` attribute.\n" ] }, { "cell_type": "code", "execution_count": 2, - "id": "0f005b2d-ddc0-4d60-8af9-9eb3dbeb45b7", + "id": "5cf3331e-ccb3-41c8-aeb9-a840a94d41e7", "metadata": {}, "outputs": [], "source": [ - "tools = [TavilySearchResults(max_results=1)]\n", - "model = ChatOpenAI()" + "from langgraph.prebuilt import ToolExecutor\n", + "\n", + "tool_executor = ToolExecutor(tools)" + ] + }, + { + "cell_type": "markdown", + "id": "5497ed70-fce3-47f1-9cad-46f912bad6a5", + "metadata": {}, + "source": [ + "## Set up the model\n", + "\n", + "Now we need to load the chat model we want to use.\n", + "Importantly, this should satisfy two criteria:\n", + "\n", + "1. It should work with messages. We will represent all agent state in the form of messages, so it needs to be able to work well with them.\n", + "2. It should work with OpenAI function calling. This means it should either be an OpenAI model or a model that exposes a similar interface.\n", + "\n", + "Note: these model requirements are not requirements for using LangGraph - they are just requirements for this one example.\n" ] }, { "cell_type": "code", "execution_count": 3, - "id": "02831581-f1ba-4701-ba33-f0f68468907d", + "id": "892b54b9-75f0-4804-9ed0-88b5e5532989", "metadata": {}, "outputs": [], "source": [ - "from langchain.tools.render import format_tool_to_openai_function" + "from langchain_openai import ChatOpenAI\n", + "\n", + "# We will set streaming=True so that we can stream tokens\n", + "# See the streaming section for more information on this.\n", + "model = ChatOpenAI(temperature=0, streaming=True)" + ] + }, + { + "cell_type": "markdown", + "id": "a77995c0-bae2-4cee-a036-8688a90f05b9", + "metadata": {}, + "source": [ + "\n", + "After we've done this, we should make sure the model knows that it has these tools available to call.\n", + "We can do this by converting the LangChain tools into the format for OpenAI function calling, and then bind them to the model class.\n" ] }, { "cell_type": "code", "execution_count": 4, - "id": "a5c0ca12-4922-461c-b740-62ff99f8ae56", + "id": "cd3cbae5-d92c-4559-a4aa-44721b80d107", "metadata": {}, "outputs": [], "source": [ - "model = model.bind_functions([format_tool_to_openai_function(t) for t in tools])" + "from langchain.tools.render import format_tool_to_openai_function\n", + "\n", + "functions = [format_tool_to_openai_function(t) for t in tools]\n", + "model = model.bind_functions(functions)" + ] + }, + { + "cell_type": "markdown", + "id": "8e8b9211-93d0-4ad5-aa7a-9c09099c53ff", + "metadata": {}, + "source": [ + "## Define the agent state\n", + "\n", + "The main type of graph in `langgraph` is the `StatefulGraph`.\n", + "This graph is parameterized by a state object that it passes around to each node.\n", + "Each node then returns operations to update that state.\n", + "These operations can either SET specific attributes on the state (e.g. overwrite the existing values) or ADD to the existing attribute.\n", + "Whether to set or add is denoted by annotating the state object you construct the graph with.\n", + "\n", + "For this example, the state we will track will just be a list of messages.\n", + "We want each node to just add messages to that list.\n", + "Therefore, we will use a `TypedDict` with one key (`messages`) and annotate it so that the `messages` attribute is always added to.\n" ] }, { "cell_type": "code", "execution_count": 5, - "id": "57547622-ddd8-4179-aa4a-e1b69ca4523c", - "metadata": {}, - "outputs": [], - "source": [ - "from langgraph.prebuilt.tool_executor import ToolExecutor\n", - "tool_executor = ToolExecutor(tools)" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "id": "96141ebd-32af-4c9d-a0b0-f46482b8bb88", + "id": "ea793afa-2eab-4901-910d-6eed90cd6564", "metadata": {}, "outputs": [], "source": [ "from typing import TypedDict, Annotated, Sequence\n", "import operator\n", "from langchain_core.messages import BaseMessage\n", - "# We create the AgentState that we will pass around\n", - "# This simply involves a list of messages\n", - "# We want steps to return messages to append to the list\n", - "# So we annotate the messages attribute with operator.add\n", + "\n", + "\n", "class AgentState(TypedDict):\n", " messages: Annotated[Sequence[BaseMessage], operator.add]" ] }, + { + "cell_type": "markdown", + "id": "e03c5094-9297-4d19-a04e-3eedc75cefb4", + "metadata": {}, + "source": [ + "## Define the nodes\n", + "\n", + "We now need to define a few different nodes in our graph.\n", + "In `langgraph`, a node can be either a function or a [runnable](https://python.langchain.com/docs/expression_language/).\n", + "There are two main nodes we need for this:\n", + "\n", + "1. The agent: responsible for deciding what (if any) actions to take.\n", + "2. A function to invoke tools: if the agent decides to take an action, this node will then execute that action.\n", + "\n", + "We will also need to define some edges.\n", + "Some of these edges may be conditional.\n", + "The reason they are conditional is that based on the output of a node, one of several paths may be taken.\n", + "The path that is taken is not known until that node is run (the LLM decides).\n", + "\n", + "1. Conditional Edge: after the agent is called, we should either:\n", + " a. If the agent said to take an action, then the function to invoke tools should be called\n", + " b. If the agent said that it was finished, then it should finish\n", + "2. Normal Edge: after the tools are invoked, it should always go back to the agent to decide what to do next\n", + "\n", + "Let's define the nodes, as well as a function to decide how what conditional edge to take.\n" + ] + }, { "cell_type": "code", - "execution_count": 20, - "id": "f422dc99-672e-414f-88b3-1a6dd21b6882", + "execution_count": 6, + "id": "3b541bb9-900c-40d0-964d-7b5dfee30667", "metadata": {}, "outputs": [], "source": [ - "from langchain_core.agents import AgentAction\n", + "from langgraph.prebuilt import ToolInvocation\n", "import json\n", "from langchain_core.messages import FunctionMessage\n", "\n", @@ -109,25 +266,33 @@ " # Based on the continue condition\n", " # we know the last message involves a function call\n", " last_message = messages[-1]\n", - " # We construct an AgentAction from the function_call\n", - " action = AgentAction(\n", + " # We construct an ToolInvocation from the function_call\n", + " action = ToolInvocation(\n", " tool=last_message.additional_kwargs[\"function_call\"][\"name\"],\n", " tool_input=json.loads(last_message.additional_kwargs[\"function_call\"][\"arguments\"]),\n", - " log=\"\",\n", " )\n", " # We call the tool_executor and get back a response\n", " response = tool_executor.invoke(action)\n", " # We use the response to create a FunctionMessage\n", " function_message = FunctionMessage(content=str(response), name=action.tool)\n", " # We return a list, because this will get added to the existing list\n", - " return {\"messages\": [function_message]}\n", - "\n" + " return {\"messages\": [function_message]}" + ] + }, + { + "cell_type": "markdown", + "id": "ffd6e892-946c-4899-8cc0-7c9291c1f73b", + "metadata": {}, + "source": [ + "## Define the graph\n", + "\n", + "We can now put it all together and define the graph!" ] }, { "cell_type": "code", - "execution_count": 21, - "id": "1133ec83-7af9-4444-9f88-c793fbdce214", + "execution_count": 7, + "id": "813ae66c-3b58-4283-a02a-36da72a2ab90", "metadata": {}, "outputs": [], "source": [ @@ -174,38 +339,244 @@ "app = workflow.compile()" ] }, + { + "cell_type": "markdown", + "id": "547c3931-3dae-4281-ad4e-4b51305594d4", + "metadata": {}, + "source": [ + "## Use it!\n", + "\n", + "We can now use it!\n", + "This now exposes the [same interface](https://python.langchain.com/docs/expression_language/) as all other LangChain runnables." + ] + }, { "cell_type": "code", - "execution_count": 22, - "id": "289c648f-bfc6-464f-8df9-50be8b1b9e48", + "execution_count": 8, + "id": "8edb04b9-40b6-46f1-a7a8-4b2d8aba7752", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'messages': [HumanMessage(content='what is the weather in sf'),\n", + " AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n \"query\": \"weather in San Francisco\"\\n}', 'name': 'tavily_search_results_json'}}),\n", + " FunctionMessage(content=\"[{'url': 'https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States', 'content': 'January 2024 Weather History in San Francisco California, United States Daily Precipitation in January 2024 in San Francisco Observed Weather in January 2024 in San Francisco San Francisco Temperature History January 2024 Hourly Temperature in January 2024 in San Francisco Hours of Daylight and Twilight in January 2024 in San FranciscoThis report shows the past weather for San Francisco, providing a weather history for January 2024. It features all historical weather data series we have available, including the San Francisco temperature history for January 2024. You can drill down from year to month and even day level reports by clicking on the graphs.'}]\", name='tavily_search_results_json'),\n", + " AIMessage(content=\"I'm sorry, but I couldn't find the current weather in San Francisco. However, you can check the historical weather data for January 2024 in San Francisco [here](https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States).\")]}" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from langchain_core.messages import HumanMessage\n", + "\n", + "inputs = {\"messages\": [HumanMessage(content=\"what is the weather in sf\")]}\n", + "app.invoke(inputs)" + ] + }, + { + "cell_type": "markdown", + "id": "5a9e8155-70c5-4973-912c-dc55104b2acf", + "metadata": {}, + "source": [ + "This may take a little bit - it's making a few calls behind the scenes.\n", + "In order to start seeing some intermediate results as they happen, we can use streaming - see below for more information on that.\n", + "\n", + "## Streaming\n", + "\n", + "LangGraph has support for several different types of streaming.\n", + "\n", + "### Streaming Node Output\n", + "\n", + "One of the benefits of using LangGraph is that it is easy to stream output as it's produced by each node.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "f544977e-31f7-41f0-88c4-ec9c27b8cecb", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ + "Output from node 'agent':\n", + "---\n", "{'messages': [AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n \"query\": \"weather in San Francisco\"\\n}', 'name': 'tavily_search_results_json'}})]}\n", - "----\n", - "{'messages': [FunctionMessage(content=\"[{'url': 'https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States', 'content': 'January 2024 Weather History in San Francisco California, United States Daily Precipitation in January 2024 in San Francisco Observed Weather in January 2024 in San Francisco San Francisco Temperature History January 2024 Hourly Temperature in January 2024 in San Francisco Hours of Daylight and Twilight in January 2024 in San Francisco80 deg, E Cloud Cover Mostly Cloudy 18,000 ft Mostly Clear 4,000 ft Partly Cloudy 15,000 ft Raw: KSFO 121956Z 08006KT 10SM FEW040 SCT150 BKN180 11/07 A3028 RMK AO2 SLP254 T01110067 This report shows the past weather for San Francisco, providing a weather history for January 2024.'}]\", name='tavily_search_results_json')]}\n", - "----\n", - "{'messages': [AIMessage(content='The weather in San Francisco is currently not available. However, you can check the weather history for January 2024 in San Francisco on [weatherspark.com](https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States).')]}\n", - "----\n", - "{'messages': [HumanMessage(content='what is the weather in sf'), AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n \"query\": \"weather in San Francisco\"\\n}', 'name': 'tavily_search_results_json'}}), FunctionMessage(content=\"[{'url': 'https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States', 'content': 'January 2024 Weather History in San Francisco California, United States Daily Precipitation in January 2024 in San Francisco Observed Weather in January 2024 in San Francisco San Francisco Temperature History January 2024 Hourly Temperature in January 2024 in San Francisco Hours of Daylight and Twilight in January 2024 in San Francisco80 deg, E Cloud Cover Mostly Cloudy 18,000 ft Mostly Clear 4,000 ft Partly Cloudy 15,000 ft Raw: KSFO 121956Z 08006KT 10SM FEW040 SCT150 BKN180 11/07 A3028 RMK AO2 SLP254 T01110067 This report shows the past weather for San Francisco, providing a weather history for January 2024.'}]\", name='tavily_search_results_json'), AIMessage(content='The weather in San Francisco is currently not available. However, you can check the weather history for January 2024 in San Francisco on [weatherspark.com](https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States).')]}\n", - "----\n" + "\n", + "---\n", + "\n", + "Output from node 'action':\n", + "---\n", + "{'messages': [FunctionMessage(content=\"[{'url': 'https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States', 'content': 'January 2024 Weather History in San Francisco California, United States Daily Precipitation in January 2024 in San Francisco Observed Weather in January 2024 in San Francisco San Francisco Temperature History January 2024 Hourly Temperature in January 2024 in San Francisco Hours of Daylight and Twilight in January 2024 in San FranciscoThis report shows the past weather for San Francisco, providing a weather history for January 2024. It features all historical weather data series we have available, including the San Francisco temperature history for January 2024. You can drill down from year to month and even day level reports by clicking on the graphs.'}]\", name='tavily_search_results_json')]}\n", + "\n", + "---\n", + "\n", + "Output from node 'agent':\n", + "---\n", + "{'messages': [AIMessage(content=\"I'm sorry, but I couldn't find the current weather in San Francisco. However, you can check the historical weather data for January 2024 in San Francisco [here](https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States).\")]}\n", + "\n", + "---\n", + "\n", + "Output from node '__end__':\n", + "---\n", + "{'messages': [HumanMessage(content='what is the weather in sf'), AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n \"query\": \"weather in San Francisco\"\\n}', 'name': 'tavily_search_results_json'}}), FunctionMessage(content=\"[{'url': 'https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States', 'content': 'January 2024 Weather History in San Francisco California, United States Daily Precipitation in January 2024 in San Francisco Observed Weather in January 2024 in San Francisco San Francisco Temperature History January 2024 Hourly Temperature in January 2024 in San Francisco Hours of Daylight and Twilight in January 2024 in San FranciscoThis report shows the past weather for San Francisco, providing a weather history for January 2024. It features all historical weather data series we have available, including the San Francisco temperature history for January 2024. You can drill down from year to month and even day level reports by clicking on the graphs.'}]\", name='tavily_search_results_json'), AIMessage(content=\"I'm sorry, but I couldn't find the current weather in San Francisco. However, you can check the historical weather data for January 2024 in San Francisco [here](https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States).\")]}\n", + "\n", + "---\n", + "\n" ] } ], "source": [ "inputs = {\"messages\": [HumanMessage(content=\"what is the weather in sf\")]}\n", - "for s in app.stream(inputs):\n", - " print(list(s.values())[0])\n", - " print(\"----\")" + "for output in app.stream(inputs):\n", + " # stream() yields dictionaries with output keyed by node name\n", + " for key, value in output.items():\n", + " print(f\"Output from node '{key}':\")\n", + " print(\"---\")\n", + " print(value)\n", + " print(\"\\n---\\n\")" + ] + }, + { + "cell_type": "markdown", + "id": "2a1b56c5-bd61-4192-8bdb-458a1e9f0159", + "metadata": {}, + "source": [ + "### Streaming LLM Tokens\n", + "\n", + "You can also access the LLM tokens as they are produced by each node. \n", + "In this case only the \"agent\" node produces LLM tokens.\n", + "In order for this to work properly, you must be using an LLM that supports streaming as well as have set it when constructing the LLM (e.g. `ChatOpenAI(model=\"gpt-3.5-turbo-1106\", streaming=True)`)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "cfd140f0-a5a6-4697-8115-322242f197b5", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "content='' additional_kwargs={'function_call': {'arguments': '', 'name': 'tavily_search_results_json'}}\n", + "content='' additional_kwargs={'function_call': {'arguments': '{\\n', 'name': ''}}\n", + "content='' additional_kwargs={'function_call': {'arguments': ' ', 'name': ''}}\n", + "content='' additional_kwargs={'function_call': {'arguments': ' \"', 'name': ''}}\n", + "content='' additional_kwargs={'function_call': {'arguments': 'query', 'name': ''}}\n", + "content='' additional_kwargs={'function_call': {'arguments': '\":', 'name': ''}}\n", + "content='' additional_kwargs={'function_call': {'arguments': ' \"', 'name': ''}}\n", + "content='' additional_kwargs={'function_call': {'arguments': 'weather', 'name': ''}}\n", + "content='' additional_kwargs={'function_call': {'arguments': ' in', 'name': ''}}\n", + "content='' additional_kwargs={'function_call': {'arguments': ' San', 'name': ''}}\n", + "content='' additional_kwargs={'function_call': {'arguments': ' Francisco', 'name': ''}}\n", + "content='' additional_kwargs={'function_call': {'arguments': '\"\\n', 'name': ''}}\n", + "content='' additional_kwargs={'function_call': {'arguments': '}', 'name': ''}}\n", + "content=''\n", + "content=''\n", + "content='I'\n", + "content=\"'m\"\n", + "content=' sorry'\n", + "content=','\n", + "content=' but'\n", + "content=' I'\n", + "content=' couldn'\n", + "content=\"'t\"\n", + "content=' find'\n", + "content=' the'\n", + "content=' current'\n", + "content=' weather'\n", + "content=' in'\n", + "content=' San'\n", + "content=' Francisco'\n", + "content='.'\n", + "content=' However'\n", + "content=','\n", + "content=' you'\n", + "content=' can'\n", + "content=' check'\n", + "content=' the'\n", + "content=' historical'\n", + "content=' weather'\n", + "content=' data'\n", + "content=' for'\n", + "content=' January'\n", + "content=' '\n", + "content='202'\n", + "content='4'\n", + "content=' in'\n", + "content=' San'\n", + "content=' Francisco'\n", + "content=' ['\n", + "content='here'\n", + "content=']('\n", + "content='https'\n", + "content='://'\n", + "content='we'\n", + "content='athers'\n", + "content='park'\n", + "content='.com'\n", + "content='/h'\n", + "content='/m'\n", + "content='/'\n", + "content='557'\n", + "content='/'\n", + "content='202'\n", + "content='4'\n", + "content='/'\n", + "content='1'\n", + "content='/H'\n", + "content='istorical'\n", + "content='-'\n", + "content='Weather'\n", + "content='-in'\n", + "content='-Jan'\n", + "content='uary'\n", + "content='-'\n", + "content='202'\n", + "content='4'\n", + "content='-in'\n", + "content='-S'\n", + "content='an'\n", + "content='-F'\n", + "content='r'\n", + "content='anc'\n", + "content='isco'\n", + "content='-Cal'\n", + "content='ifornia'\n", + "content='-'\n", + "content='United'\n", + "content='-'\n", + "content='States'\n", + "content=').'\n", + "content=''\n" + ] + } + ], + "source": [ + "inputs = {\"messages\": [HumanMessage(content=\"what is the weather in sf\")]}\n", + "async for output in app.astream_log(inputs, include_types=[\"llm\"]):\n", + " # astream_log() yields the requested logs (here LLMs) in JSONPatch format\n", + " for op in output.ops:\n", + " if op[\"path\"] == \"/streamed_output/-\":\n", + " # this is the output from .stream()\n", + " ...\n", + " elif op[\"path\"].startswith(\"/logs/\") and op[\"path\"].endswith(\n", + " \"/streamed_output/-\"\n", + " ):\n", + " # because we chose to only include LLMs, these are LLM tokens\n", + " print(op[\"value\"])" ] }, { "cell_type": "code", "execution_count": null, - "id": "4cd4b39f-831a-4818-bf56-99cf301b0555", + "id": "08ae8246-11d5-40e1-8567-361e5bef8917", "metadata": {}, "outputs": [], "source": [] diff --git a/examples/chat_executor_with_function_calling/dynamically-returning-directly.ipynb b/examples/chat_executor_with_function_calling/dynamically-returning-directly.ipynb index 6822c442d..4830192c8 100644 --- a/examples/chat_executor_with_function_calling/dynamically-returning-directly.ipynb +++ b/examples/chat_executor_with_function_calling/dynamically-returning-directly.ipynb @@ -1,29 +1,106 @@ { "cells": [ { - "cell_type": "code", - "execution_count": 2, - "id": "aa9f9110-ea74-43af-aa72-6b45518abd6e", + "cell_type": "markdown", + "id": "51466c8d-8ce4-4b3d-be4e-18fdbeda5f53", "metadata": {}, - "outputs": [], "source": [ - "from langchain import hub\n", - "from langchain_openai import ChatOpenAI\n", - "from langchain_community.tools.tavily_search import TavilySearchResults\n", - "from langgraph.prebuilt.messages_executor import create_messages_executor\n", - "from langchain_core.messages import HumanMessage" + "# Dynamically Returning Directly\n", + "\n", + "In this example we will build a chat executor where the LLM can optionally decide to return the result of a tool call as the final answer. This is useful in cases where you have tools that can sometimes generate responses that are acceptable as final answers, and you want to use the LLM to determine when that is the case\n", + "\n", + "This examples builds off the base chat executor. It is highly recommended you learn about that executor before going through this notebook. You can find documentation for that example [here](./base.ipynb).\n", + "\n", + "Any modifications of that example are called below with **MODIFICATION**, so if you are looking for the differences you can just search for that." + ] + }, + { + "cell_type": "markdown", + "id": "7cbd446a-808f-4394-be92-d45ab818953c", + "metadata": {}, + "source": [ + "## Setup\n", + "\n", + "First we need to install the packages required" ] }, { "cell_type": "code", - "execution_count": 9, - "id": "75bbcaa8-b23a-409c-9745-08d3aca7c3cb", + "execution_count": 1, + "id": "af4ce0ba-7596-4e5f-8bf8-0b0bd6e62833", + "metadata": {}, + "outputs": [], + "source": [ + "!pip install --quiet -U langchain langchain_openai tavily-python" + ] + }, + { + "cell_type": "markdown", + "id": "0abe11f4-62ed-4dc4-8875-3db21e260d1d", + "metadata": {}, + "source": [ + "Next, we need to set API keys for OpenAI (the LLM we will use) and Tavily (the search tool we will use)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c903a1cf-2977-4e2d-ad7d-8b3946821d89", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import getpass\n", + "\n", + "os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key:\")\n", + "os.environ[\"TAVILY_API_KEY\"] = getpass.getpass(\"Tavily API Key:\")" + ] + }, + { + "cell_type": "markdown", + "id": "f0ed46a8-effe-4596-b0e1-a6a29ee16f5c", + "metadata": {}, + "source": [ + "Optionally, we can set API key for [LangSmith tracing](https://smith.langchain.com/), which will give us best-in-class observability." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "95e25aec-7c9f-4a63-b143-225d0e9a79c3", + "metadata": {}, + "outputs": [], + "source": [ + "os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n", + "os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass(\"LangSmith API Key:\")" + ] + }, + { + "cell_type": "markdown", + "id": "21ac643b-cb06-4724-a80c-2862ba4773f1", + "metadata": {}, + "source": [ + "## Set up the tools\n", + "\n", + "We will first define the tools we want to use.\n", + "For this simple example, we will use a built-in search tool via Tavily.\n", + "However, it is really easy to create your own tools - see documentation [here](https://python.langchain.com/docs/modules/agents/tools/custom_tools) on how to do that.\n", + "\n", + "**MODIFICATION**\n", + "\n", + "We overwrite the default schema of the input tool to have an additional parameter for returning directly." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "4a1b9990-3b11-4a51-bd51-76117afd38b9", "metadata": {}, "outputs": [], "source": [ "from langchain_core.pydantic_v1 import BaseModel, Field\n", "\n", - "class SearchTool(search_tool.args_schema):\n", + "class SearchTool(BaseModel):\n", " \"\"\"Look up things online, optionally returning directly\"\"\"\n", " query: str = Field(description=\"query to look up online\")\n", " return_direct: bool = Field(\n", @@ -34,85 +111,182 @@ }, { "cell_type": "code", - "execution_count": 10, - "id": "0c93c6e1-532b-472e-b9f7-d374b9d3c89e", + "execution_count": 4, + "id": "d7ef57dd-5d6e-4ad3-9377-a92201c1310e", "metadata": {}, "outputs": [], "source": [ - "search_tool = TavilySearchResults(max_results=1, args_schema=SearchTool)" + "from langchain_community.tools.tavily_search import TavilySearchResults\n", + "\n", + "search_tool = TavilySearchResults(max_results=1, args_schema=SearchTool)\n", + "tools = [search_tool]" + ] + }, + { + "cell_type": "markdown", + "id": "01885785-b71a-44d1-b1d6-7b5b14d53b58", + "metadata": {}, + "source": [ + "We can now wrap these tools in a simple ToolExecutor.\n", + "This is a real simple class that takes in a ToolInvocation and calls that tool, returning the output.\n", + "A ToolInvocation is any class with `tool` and `tool_input` attribute.\n" ] }, { "cell_type": "code", - "execution_count": 11, - "id": "0f005b2d-ddc0-4d60-8af9-9eb3dbeb45b7", + "execution_count": 5, + "id": "5cf3331e-ccb3-41c8-aeb9-a840a94d41e7", "metadata": {}, "outputs": [], "source": [ - "tools = [search_tool]\n", - "model = ChatOpenAI()" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "id": "02831581-f1ba-4701-ba33-f0f68468907d", - "metadata": {}, - "outputs": [], - "source": [ - "from langchain.tools.render import format_tool_to_openai_function" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "id": "a5c0ca12-4922-461c-b740-62ff99f8ae56", - "metadata": {}, - "outputs": [], - "source": [ - "model = model.bind_functions([format_tool_to_openai_function(t) for t in tools])" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "id": "57547622-ddd8-4179-aa4a-e1b69ca4523c", - "metadata": {}, - "outputs": [], - "source": [ - "from langgraph.prebuilt.tool_executor import ToolExecutor\n", + "from langgraph.prebuilt import ToolExecutor\n", + "\n", "tool_executor = ToolExecutor(tools)" ] }, + { + "cell_type": "markdown", + "id": "5497ed70-fce3-47f1-9cad-46f912bad6a5", + "metadata": {}, + "source": [ + "## Set up the model\n", + "\n", + "Now we need to load the chat model we want to use.\n", + "Importantly, this should satisfy two criteria:\n", + "\n", + "1. It should work with messages. We will represent all agent state in the form of messages, so it needs to be able to work well with them.\n", + "2. It should work with OpenAI function calling. This means it should either be an OpenAI model or a model that exposes a similar interface.\n", + "\n", + "Note: these model requirements are not requirements for using LangGraph - they are just requirements for this one example.\n" + ] + }, { "cell_type": "code", - "execution_count": 15, - "id": "96141ebd-32af-4c9d-a0b0-f46482b8bb88", + "execution_count": 6, + "id": "892b54b9-75f0-4804-9ed0-88b5e5532989", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain_openai import ChatOpenAI\n", + "\n", + "# We will set streaming=True so that we can stream tokens\n", + "# See the streaming section for more information on this.\n", + "model = ChatOpenAI(temperature=0, streaming=True)" + ] + }, + { + "cell_type": "markdown", + "id": "a77995c0-bae2-4cee-a036-8688a90f05b9", + "metadata": {}, + "source": [ + "\n", + "After we've done this, we should make sure the model knows that it has these tools available to call.\n", + "We can do this by converting the LangChain tools into the format for OpenAI function calling, and then bind them to the model class.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "cd3cbae5-d92c-4559-a4aa-44721b80d107", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain.tools.render import format_tool_to_openai_function\n", + "\n", + "functions = [format_tool_to_openai_function(t) for t in tools]\n", + "model = model.bind_functions(functions)" + ] + }, + { + "cell_type": "markdown", + "id": "8e8b9211-93d0-4ad5-aa7a-9c09099c53ff", + "metadata": {}, + "source": [ + "## Define the agent state\n", + "\n", + "The main type of graph in `langgraph` is the `StatefulGraph`.\n", + "This graph is parameterized by a state object that it passes around to each node.\n", + "Each node then returns operations to update that state.\n", + "These operations can either SET specific attributes on the state (e.g. overwrite the existing values) or ADD to the existing attribute.\n", + "Whether to set or add is denoted by annotating the state object you construct the graph with.\n", + "\n", + "For this example, the state we will track will just be a list of messages.\n", + "We want each node to just add messages to that list.\n", + "Therefore, we will use a `TypedDict` with one key (`messages`) and annotate it so that the `messages` attribute is always added to.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "ea793afa-2eab-4901-910d-6eed90cd6564", "metadata": {}, "outputs": [], "source": [ "from typing import TypedDict, Annotated, Sequence\n", "import operator\n", "from langchain_core.messages import BaseMessage\n", - "# We create the AgentState that we will pass around\n", - "# This simply involves a list of messages\n", - "# We want steps to return messages to append to the list\n", - "# So we annotate the messages attribute with operator.add\n", + "\n", + "\n", "class AgentState(TypedDict):\n", " messages: Annotated[Sequence[BaseMessage], operator.add]" ] }, + { + "cell_type": "markdown", + "id": "e03c5094-9297-4d19-a04e-3eedc75cefb4", + "metadata": {}, + "source": [ + "## Define the nodes\n", + "\n", + "We now need to define a few different nodes in our graph.\n", + "In `langgraph`, a node can be either a function or a [runnable](https://python.langchain.com/docs/expression_language/).\n", + "There are two main nodes we need for this:\n", + "\n", + "1. The agent: responsible for deciding what (if any) actions to take.\n", + "2. A function to invoke tools: if the agent decides to take an action, this node will then execute that action.\n", + "\n", + "We will also need to define some edges.\n", + "Some of these edges may be conditional.\n", + "The reason they are conditional is that based on the output of a node, one of several paths may be taken.\n", + "The path that is taken is not known until that node is run (the LLM decides).\n", + "\n", + "1. Conditional Edge: after the agent is called, we should either:\n", + " a. If the agent said to take an action, then the function to invoke tools should be called\n", + " b. If the agent said that it was finished, then it should finish\n", + "2. Normal Edge: after the tools are invoked, it should always go back to the agent to decide what to do next\n", + "\n", + "Let's define the nodes, as well as a function to decide how what conditional edge to take.\n" + ] + }, { "cell_type": "code", - "execution_count": 20, - "id": "f422dc99-672e-414f-88b3-1a6dd21b6882", + "execution_count": 9, + "id": "03308b6b-de72-4cdc-b6c6-47e654df340e", "metadata": {}, "outputs": [], "source": [ - "from langchain_core.agents import AgentAction\n", + "from langgraph.prebuilt import ToolInvocation\n", "import json\n", - "from langchain_core.messages import FunctionMessage\n", + "from langchain_core.messages import FunctionMessage" + ] + }, + { + "cell_type": "markdown", + "id": "50bf356c-2dbd-4f66-8fa3-133e9c2e371e", + "metadata": {}, + "source": [ + "**MODIFICATION**\n", "\n", + "We change the `should_continue` function to check whether return_direct was set to True" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "55e088b1-f3c8-4798-9ca8-5b0be961b49a", + "metadata": {}, + "outputs": [], + "source": [ "# Define the function that determines whether to continue or not\n", "def should_continue(state):\n", " messages = state['messages']\n", @@ -126,45 +300,83 @@ " if arguments.get(\"return_direct\", False):\n", " return \"final\"\n", " else:\n", - " return \"continue\"\n", - "\n", + " return \"continue\"" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "2b45da72-1afa-4cd7-9b7f-49a7c99cdb8a", + "metadata": {}, + "outputs": [], + "source": [ "# Define the function that calls the model\n", "def call_model(state):\n", " messages = state['messages']\n", " response = model.invoke(messages)\n", " # We return a list, because this will get added to the existing list\n", - " return {\"messages\": [response]}\n", + " return {\"messages\": [response]}" + ] + }, + { + "cell_type": "markdown", + "id": "8535a36c-3ced-401e-98b5-ec1d1b434bbc", + "metadata": {}, + "source": [ + "**MODIFICATION**\n", "\n", + "We change the tool calling to get rid of the `return_direct` parameter (not used in the actual tool call)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "dd876f5d-88d6-4f93-b1d0-f2f0b6f4d991", + "metadata": {}, + "outputs": [], + "source": [ "# Define the function to execute tools\n", "def call_tool(state):\n", " messages = state['messages']\n", " # Based on the continue condition\n", " # we know the last message involves a function call\n", " last_message = messages[-1]\n", + " # We construct an ToolInvocation from the function_call\n", " tool_name = last_message.additional_kwargs[\"function_call\"][\"name\"]\n", " arguments = json.loads(last_message.additional_kwargs[\"function_call\"][\"arguments\"])\n", " if tool_name == \"tavily_search_results_json\":\n", " if \"return_direct\" in arguments:\n", " del arguments[\"return_direct\"]\n", - " # We construct an AgentAction from the function_call\n", - " action = AgentAction(\n", + " action = ToolInvocation(\n", " tool=tool_name,\n", " tool_input=arguments,\n", - " log=\"\",\n", " )\n", " # We call the tool_executor and get back a response\n", " response = tool_executor.invoke(action)\n", " # We use the response to create a FunctionMessage\n", " function_message = FunctionMessage(content=str(response), name=action.tool)\n", " # We return a list, because this will get added to the existing list\n", - " return {\"messages\": [function_message]}\n", - "\n" + " return {\"messages\": [function_message]}" + ] + }, + { + "cell_type": "markdown", + "id": "ffd6e892-946c-4899-8cc0-7c9291c1f73b", + "metadata": {}, + "source": [ + "## Define the graph\n", + "\n", + "We can now put it all together and define the graph!\n", + "\n", + "**MODIFICATION**\n", + "\n", + "We add a separate node for any tool call where `return_direct=True`. The reason this is needed is that after this node we want to end, while after other tool calls we want to go back to the LLM. " ] }, { "cell_type": "code", - "execution_count": 21, - "id": "1133ec83-7af9-4444-9f88-c793fbdce214", + "execution_count": 13, + "id": "813ae66c-3b58-4283-a02a-36da72a2ab90", "metadata": {}, "outputs": [], "source": [ @@ -216,63 +428,114 @@ ] }, { - "cell_type": "code", - "execution_count": 22, - "id": "289c648f-bfc6-464f-8df9-50be8b1b9e48", + "cell_type": "markdown", + "id": "547c3931-3dae-4281-ad4e-4b51305594d4", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'messages': [AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n \"query\": \"weather in San Francisco\"\\n}', 'name': 'tavily_search_results_json'}})]}\n", - "----\n", - "{'messages': [FunctionMessage(content=\"[{'url': 'https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States', 'content': 'January 2024 Weather History in San Francisco California, United States Daily Precipitation in January 2024 in San Francisco Observed Weather in January 2024 in San Francisco San Francisco Temperature History January 2024 Hourly Temperature in January 2024 in San Francisco Hours of Daylight and Twilight in January 2024 in San Francisco80 deg, E Cloud Cover Mostly Cloudy 18,000 ft Mostly Clear 4,000 ft Partly Cloudy 15,000 ft Raw: KSFO 121956Z 08006KT 10SM FEW040 SCT150 BKN180 11/07 A3028 RMK AO2 SLP254 T01110067 This report shows the past weather for San Francisco, providing a weather history for January 2024.'}]\", name='tavily_search_results_json')]}\n", - "----\n", - "{'messages': [AIMessage(content='The current weather in San Francisco is not available. However, you can check the weather history for January 2024 in San Francisco [here](https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States).')]}\n", - "----\n", - "{'messages': [HumanMessage(content='what is the weather in sf'), AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n \"query\": \"weather in San Francisco\"\\n}', 'name': 'tavily_search_results_json'}}), FunctionMessage(content=\"[{'url': 'https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States', 'content': 'January 2024 Weather History in San Francisco California, United States Daily Precipitation in January 2024 in San Francisco Observed Weather in January 2024 in San Francisco San Francisco Temperature History January 2024 Hourly Temperature in January 2024 in San Francisco Hours of Daylight and Twilight in January 2024 in San Francisco80 deg, E Cloud Cover Mostly Cloudy 18,000 ft Mostly Clear 4,000 ft Partly Cloudy 15,000 ft Raw: KSFO 121956Z 08006KT 10SM FEW040 SCT150 BKN180 11/07 A3028 RMK AO2 SLP254 T01110067 This report shows the past weather for San Francisco, providing a weather history for January 2024.'}]\", name='tavily_search_results_json'), AIMessage(content='The current weather in San Francisco is not available. However, you can check the weather history for January 2024 in San Francisco [here](https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States).')]}\n", - "----\n" - ] - } - ], "source": [ - "inputs = {\"messages\": [HumanMessage(content=\"what is the weather in sf\")]}\n", - "for s in app.stream(inputs):\n", - " print(list(s.values())[0])\n", - " print(\"----\")" + "## Use it!\n", + "\n", + "We can now use it!\n", + "This now exposes the [same interface](https://python.langchain.com/docs/expression_language/) as all other LangChain runnables." ] }, { "cell_type": "code", - "execution_count": 27, - "id": "4cd4b39f-831a-4818-bf56-99cf301b0555", + "execution_count": 15, + "id": "f544977e-31f7-41f0-88c4-ec9c27b8cecb", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "{'messages': [AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n \"query\": \"weather in San Francisco\",\\n \"return_direct\": true\\n}', 'name': 'tavily_search_results_json'}})]}\n", - "----\n", - "{'messages': [FunctionMessage(content=\"[{'url': 'https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States', 'content': 'January 2024 Weather History in San Francisco California, United States Daily Precipitation in January 2024 in San Francisco Observed Weather in January 2024 in San Francisco San Francisco Temperature History January 2024 Hourly Temperature in January 2024 in San Francisco Hours of Daylight and Twilight in January 2024 in San Francisco80 deg, E Cloud Cover Mostly Cloudy 18,000 ft Mostly Clear 4,000 ft Partly Cloudy 15,000 ft Raw: KSFO 121956Z 08006KT 10SM FEW040 SCT150 BKN180 11/07 A3028 RMK AO2 SLP254 T01110067 This report shows the past weather for San Francisco, providing a weather history for January 2024.'}]\", name='tavily_search_results_json')]}\n", - "----\n", - "{'messages': [HumanMessage(content='what is the weather in sf? return this result directly by setting return_direct = True'), AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n \"query\": \"weather in San Francisco\",\\n \"return_direct\": true\\n}', 'name': 'tavily_search_results_json'}}), FunctionMessage(content=\"[{'url': 'https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States', 'content': 'January 2024 Weather History in San Francisco California, United States Daily Precipitation in January 2024 in San Francisco Observed Weather in January 2024 in San Francisco San Francisco Temperature History January 2024 Hourly Temperature in January 2024 in San Francisco Hours of Daylight and Twilight in January 2024 in San Francisco80 deg, E Cloud Cover Mostly Cloudy 18,000 ft Mostly Clear 4,000 ft Partly Cloudy 15,000 ft Raw: KSFO 121956Z 08006KT 10SM FEW040 SCT150 BKN180 11/07 A3028 RMK AO2 SLP254 T01110067 This report shows the past weather for San Francisco, providing a weather history for January 2024.'}]\", name='tavily_search_results_json')]}\n", - "----\n" + "Output from node 'agent':\n", + "---\n", + "{'messages': [AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n \"query\": \"weather in San Francisco\"\\n}', 'name': 'tavily_search_results_json'}})]}\n", + "\n", + "---\n", + "\n", + "Output from node 'action':\n", + "---\n", + "{'messages': [FunctionMessage(content=\"[{'url': 'https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States', 'content': 'January 2024 Weather History in San Francisco California, United States Daily Precipitation in January 2024 in San Francisco Observed Weather in January 2024 in San Francisco San Francisco Temperature History January 2024 Hourly Temperature in January 2024 in San Francisco Hours of Daylight and Twilight in January 2024 in San FranciscoThis report shows the past weather for San Francisco, providing a weather history for January 2024. It features all historical weather data series we have available, including the San Francisco temperature history for January 2024. You can drill down from year to month and even day level reports by clicking on the graphs.'}]\", name='tavily_search_results_json')]}\n", + "\n", + "---\n", + "\n", + "Output from node 'agent':\n", + "---\n", + "{'messages': [AIMessage(content=\"I'm sorry, but I couldn't find the current weather in San Francisco. However, you can check the historical weather data for January 2024 in San Francisco [here](https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States).\")]}\n", + "\n", + "---\n", + "\n", + "Output from node '__end__':\n", + "---\n", + "{'messages': [HumanMessage(content='what is the weather in sf'), AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n \"query\": \"weather in San Francisco\"\\n}', 'name': 'tavily_search_results_json'}}), FunctionMessage(content=\"[{'url': 'https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States', 'content': 'January 2024 Weather History in San Francisco California, United States Daily Precipitation in January 2024 in San Francisco Observed Weather in January 2024 in San Francisco San Francisco Temperature History January 2024 Hourly Temperature in January 2024 in San Francisco Hours of Daylight and Twilight in January 2024 in San FranciscoThis report shows the past weather for San Francisco, providing a weather history for January 2024. It features all historical weather data series we have available, including the San Francisco temperature history for January 2024. You can drill down from year to month and even day level reports by clicking on the graphs.'}]\", name='tavily_search_results_json'), AIMessage(content=\"I'm sorry, but I couldn't find the current weather in San Francisco. However, you can check the historical weather data for January 2024 in San Francisco [here](https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States).\")]}\n", + "\n", + "---\n", + "\n" ] } ], "source": [ + "from langchain_core.messages import HumanMessage\n", + "\n", + "inputs = {\"messages\": [HumanMessage(content=\"what is the weather in sf\")]}\n", + "for output in app.stream(inputs):\n", + " # stream() yields dictionaries with output keyed by node name\n", + " for key, value in output.items():\n", + " print(f\"Output from node '{key}':\")\n", + " print(\"---\")\n", + " print(value)\n", + " print(\"\\n---\\n\")" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "08ae8246-11d5-40e1-8567-361e5bef8917", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Output from node 'agent':\n", + "---\n", + "{'messages': [AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n \"query\": \"weather in San Francisco\",\\n \"return_direct\": true\\n}', 'name': 'tavily_search_results_json'}})]}\n", + "\n", + "---\n", + "\n", + "Output from node 'final':\n", + "---\n", + "{'messages': [FunctionMessage(content=\"[{'url': 'https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States', 'content': 'January 2024 Weather History in San Francisco California, United States Daily Precipitation in January 2024 in San Francisco Observed Weather in January 2024 in San Francisco San Francisco Temperature History January 2024 Hourly Temperature in January 2024 in San Francisco Hours of Daylight and Twilight in January 2024 in San FranciscoThis report shows the past weather for San Francisco, providing a weather history for January 2024. It features all historical weather data series we have available, including the San Francisco temperature history for January 2024. You can drill down from year to month and even day level reports by clicking on the graphs.'}]\", name='tavily_search_results_json')]}\n", + "\n", + "---\n", + "\n", + "Output from node '__end__':\n", + "---\n", + "{'messages': [HumanMessage(content='what is the weather in sf? return this result directly by setting return_direct = True'), AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n \"query\": \"weather in San Francisco\",\\n \"return_direct\": true\\n}', 'name': 'tavily_search_results_json'}}), FunctionMessage(content=\"[{'url': 'https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States', 'content': 'January 2024 Weather History in San Francisco California, United States Daily Precipitation in January 2024 in San Francisco Observed Weather in January 2024 in San Francisco San Francisco Temperature History January 2024 Hourly Temperature in January 2024 in San Francisco Hours of Daylight and Twilight in January 2024 in San FranciscoThis report shows the past weather for San Francisco, providing a weather history for January 2024. It features all historical weather data series we have available, including the San Francisco temperature history for January 2024. You can drill down from year to month and even day level reports by clicking on the graphs.'}]\", name='tavily_search_results_json')]}\n", + "\n", + "---\n", + "\n" + ] + } + ], + "source": [ + "from langchain_core.messages import HumanMessage\n", + "\n", "inputs = {\"messages\": [HumanMessage(content=\"what is the weather in sf? return this result directly by setting return_direct = True\")]}\n", - "for s in app.stream(inputs):\n", - " print(list(s.values())[0])\n", - " print(\"----\")" + "for output in app.stream(inputs):\n", + " # stream() yields dictionaries with output keyed by node name\n", + " for key, value in output.items():\n", + " print(f\"Output from node '{key}':\")\n", + " print(\"---\")\n", + " print(value)\n", + " print(\"\\n---\\n\")" ] }, { "cell_type": "code", "execution_count": null, - "id": "2d2e89d8-0e35-465c-8984-1aa37a132b03", + "id": "49ccc134-4abe-4982-8ecd-d70fc56a4d2d", "metadata": {}, "outputs": [], "source": [] diff --git a/examples/chat_executor_with_function_calling/force-calling-a-tool-first.ipynb b/examples/chat_executor_with_function_calling/force-calling-a-tool-first.ipynb index d4fb92bf5..f6457742a 100644 --- a/examples/chat_executor_with_function_calling/force-calling-a-tool-first.ipynb +++ b/examples/chat_executor_with_function_calling/force-calling-a-tool-first.ipynb @@ -1,87 +1,248 @@ { "cells": [ + { + "cell_type": "markdown", + "id": "51466c8d-8ce4-4b3d-be4e-18fdbeda5f53", + "metadata": {}, + "source": [ + "# Force Calling a Tool First\n", + "\n", + "In this example we will build a chat executor that always calls a certain tool first. In this example, we will create an agent with a search tool. However, at the start we will force the agent to call the search tool (and then let it do whatever it wants after). This is useful when you want to force agents to call particular tools, but still want flexibility of what happens after that.\n", + "\n", + "This examples builds off the base chat executor. It is highly recommended you learn about that executor before going through this notebook. You can find documentation for that example [here](./base.ipynb).\n", + "\n", + "Any modifications of that example are called below with **MODIFICATION**, so if you are looking for the differences you can just search for that." + ] + }, + { + "cell_type": "markdown", + "id": "7cbd446a-808f-4394-be92-d45ab818953c", + "metadata": {}, + "source": [ + "## Setup\n", + "\n", + "First we need to install the packages required" + ] + }, { "cell_type": "code", "execution_count": 1, - "id": "aa9f9110-ea74-43af-aa72-6b45518abd6e", + "id": "af4ce0ba-7596-4e5f-8bf8-0b0bd6e62833", + "metadata": {}, + "outputs": [], + "source": [ + "!pip install --quiet -U langchain langchain_openai tavily-python" + ] + }, + { + "cell_type": "markdown", + "id": "0abe11f4-62ed-4dc4-8875-3db21e260d1d", + "metadata": {}, + "source": [ + "Next, we need to set API keys for OpenAI (the LLM we will use) and Tavily (the search tool we will use)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c903a1cf-2977-4e2d-ad7d-8b3946821d89", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import getpass\n", + "\n", + "os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key:\")\n", + "os.environ[\"TAVILY_API_KEY\"] = getpass.getpass(\"Tavily API Key:\")" + ] + }, + { + "cell_type": "markdown", + "id": "f0ed46a8-effe-4596-b0e1-a6a29ee16f5c", + "metadata": {}, + "source": [ + "Optionally, we can set API key for [LangSmith tracing](https://smith.langchain.com/), which will give us best-in-class observability." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "95e25aec-7c9f-4a63-b143-225d0e9a79c3", + "metadata": {}, + "outputs": [], + "source": [ + "os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n", + "os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass(\"LangSmith API Key:\")" + ] + }, + { + "cell_type": "markdown", + "id": "21ac643b-cb06-4724-a80c-2862ba4773f1", + "metadata": {}, + "source": [ + "## Set up the tools\n", + "\n", + "We will first define the tools we want to use.\n", + "For this simple example, we will use a built-in search tool via Tavily.\n", + "However, it is really easy to create your own tools - see documentation [here](https://python.langchain.com/docs/modules/agents/tools/custom_tools) on how to do that.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "d7ef57dd-5d6e-4ad3-9377-a92201c1310e", "metadata": {}, "outputs": [], "source": [ - "from langchain import hub\n", - "from langchain_openai import ChatOpenAI\n", "from langchain_community.tools.tavily_search import TavilySearchResults\n", - "from langgraph.prebuilt.messages_executor import create_messages_executor\n", - "from langchain_core.messages import HumanMessage" + "\n", + "tools = [TavilySearchResults(max_results=1)]" + ] + }, + { + "cell_type": "markdown", + "id": "01885785-b71a-44d1-b1d6-7b5b14d53b58", + "metadata": {}, + "source": [ + "We can now wrap these tools in a simple ToolExecutor.\n", + "This is a real simple class that takes in a ToolInvocation and calls that tool, returning the output.\n", + "A ToolInvocation is any class with `tool` and `tool_input` attribute.\n" ] }, { "cell_type": "code", "execution_count": 2, - "id": "0f005b2d-ddc0-4d60-8af9-9eb3dbeb45b7", + "id": "5cf3331e-ccb3-41c8-aeb9-a840a94d41e7", "metadata": {}, "outputs": [], "source": [ - "tools = [TavilySearchResults(max_results=1)]\n", - "model = ChatOpenAI()" + "from langgraph.prebuilt import ToolExecutor\n", + "\n", + "tool_executor = ToolExecutor(tools)" + ] + }, + { + "cell_type": "markdown", + "id": "5497ed70-fce3-47f1-9cad-46f912bad6a5", + "metadata": {}, + "source": [ + "## Set up the model\n", + "\n", + "Now we need to load the chat model we want to use.\n", + "Importantly, this should satisfy two criteria:\n", + "\n", + "1. It should work with messages. We will represent all agent state in the form of messages, so it needs to be able to work well with them.\n", + "2. It should work with OpenAI function calling. This means it should either be an OpenAI model or a model that exposes a similar interface.\n", + "\n", + "Note: these model requirements are not requirements for using LangGraph - they are just requirements for this one example.\n" ] }, { "cell_type": "code", "execution_count": 3, - "id": "02831581-f1ba-4701-ba33-f0f68468907d", + "id": "892b54b9-75f0-4804-9ed0-88b5e5532989", "metadata": {}, "outputs": [], "source": [ - "from langchain.tools.render import format_tool_to_openai_function" + "from langchain_openai import ChatOpenAI\n", + "\n", + "# We will set streaming=True so that we can stream tokens\n", + "# See the streaming section for more information on this.\n", + "model = ChatOpenAI(temperature=0, streaming=True)" + ] + }, + { + "cell_type": "markdown", + "id": "a77995c0-bae2-4cee-a036-8688a90f05b9", + "metadata": {}, + "source": [ + "\n", + "After we've done this, we should make sure the model knows that it has these tools available to call.\n", + "We can do this by converting the LangChain tools into the format for OpenAI function calling, and then bind them to the model class.\n" ] }, { "cell_type": "code", "execution_count": 4, - "id": "a5c0ca12-4922-461c-b740-62ff99f8ae56", + "id": "cd3cbae5-d92c-4559-a4aa-44721b80d107", "metadata": {}, "outputs": [], "source": [ - "model = model.bind_functions([format_tool_to_openai_function(t) for t in tools])" + "from langchain.tools.render import format_tool_to_openai_function\n", + "\n", + "functions = [format_tool_to_openai_function(t) for t in tools]\n", + "model = model.bind_functions(functions)" + ] + }, + { + "cell_type": "markdown", + "id": "8e8b9211-93d0-4ad5-aa7a-9c09099c53ff", + "metadata": {}, + "source": [ + "## Define the agent state\n", + "\n", + "The main type of graph in `langgraph` is the `StatefulGraph`.\n", + "This graph is parameterized by a state object that it passes around to each node.\n", + "Each node then returns operations to update that state.\n", + "These operations can either SET specific attributes on the state (e.g. overwrite the existing values) or ADD to the existing attribute.\n", + "Whether to set or add is denoted by annotating the state object you construct the graph with.\n", + "\n", + "For this example, the state we will track will just be a list of messages.\n", + "We want each node to just add messages to that list.\n", + "Therefore, we will use a `TypedDict` with one key (`messages`) and annotate it so that the `messages` attribute is always added to.\n" ] }, { "cell_type": "code", "execution_count": 5, - "id": "57547622-ddd8-4179-aa4a-e1b69ca4523c", - "metadata": {}, - "outputs": [], - "source": [ - "from langgraph.prebuilt.tool_executor import ToolExecutor\n", - "tool_executor = ToolExecutor(tools)" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "id": "96141ebd-32af-4c9d-a0b0-f46482b8bb88", + "id": "ea793afa-2eab-4901-910d-6eed90cd6564", "metadata": {}, "outputs": [], "source": [ "from typing import TypedDict, Annotated, Sequence\n", "import operator\n", "from langchain_core.messages import BaseMessage\n", - "# We create the AgentState that we will pass around\n", - "# This simply involves a list of messages\n", - "# We want steps to return messages to append to the list\n", - "# So we annotate the messages attribute with operator.add\n", + "\n", + "\n", "class AgentState(TypedDict):\n", " messages: Annotated[Sequence[BaseMessage], operator.add]" ] }, + { + "cell_type": "markdown", + "id": "e03c5094-9297-4d19-a04e-3eedc75cefb4", + "metadata": {}, + "source": [ + "## Define the nodes\n", + "\n", + "We now need to define a few different nodes in our graph.\n", + "In `langgraph`, a node can be either a function or a [runnable](https://python.langchain.com/docs/expression_language/).\n", + "There are two main nodes we need for this:\n", + "\n", + "1. The agent: responsible for deciding what (if any) actions to take.\n", + "2. A function to invoke tools: if the agent decides to take an action, this node will then execute that action.\n", + "\n", + "We will also need to define some edges.\n", + "Some of these edges may be conditional.\n", + "The reason they are conditional is that based on the output of a node, one of several paths may be taken.\n", + "The path that is taken is not known until that node is run (the LLM decides).\n", + "\n", + "1. Conditional Edge: after the agent is called, we should either:\n", + " a. If the agent said to take an action, then the function to invoke tools should be called\n", + " b. If the agent said that it was finished, then it should finish\n", + "2. Normal Edge: after the tools are invoked, it should always go back to the agent to decide what to do next\n", + "\n", + "Let's define the nodes, as well as a function to decide how what conditional edge to take.\n" + ] + }, { "cell_type": "code", - "execution_count": 7, - "id": "f422dc99-672e-414f-88b3-1a6dd21b6882", + "execution_count": 6, + "id": "3b541bb9-900c-40d0-964d-7b5dfee30667", "metadata": {}, "outputs": [], "source": [ - "from langchain_core.agents import AgentAction\n", + "from langgraph.prebuilt import ToolInvocation\n", "import json\n", "from langchain_core.messages import FunctionMessage\n", "\n", @@ -109,46 +270,33 @@ " # Based on the continue condition\n", " # we know the last message involves a function call\n", " last_message = messages[-1]\n", - " # We construct an AgentAction from the function_call\n", - " action = AgentAction(\n", + " # We construct an ToolInvocation from the function_call\n", + " action = ToolInvocation(\n", " tool=last_message.additional_kwargs[\"function_call\"][\"name\"],\n", " tool_input=json.loads(last_message.additional_kwargs[\"function_call\"][\"arguments\"]),\n", - " log=\"\",\n", " )\n", " # We call the tool_executor and get back a response\n", " response = tool_executor.invoke(action)\n", " # We use the response to create a FunctionMessage\n", " function_message = FunctionMessage(content=str(response), name=action.tool)\n", " # We return a list, because this will get added to the existing list\n", - " return {\"messages\": [function_message]}\n", - "\n" + " return {\"messages\": [function_message]}" ] }, { - "cell_type": "code", - "execution_count": 9, - "id": "47bf79f1-652c-43dc-aeb3-0f0de4400539", + "cell_type": "markdown", + "id": "7c3e0ac2-0c89-4751-bc2c-f644654841d1", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'tavily_search_results_json'" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], "source": [ - "tools[0].name" + "**MODIFICATION**\n", + "\n", + "Here we create a node that returns an AIMessage with a tool call - we will use this at the start to force it call a tool" ] }, { "cell_type": "code", - "execution_count": 11, - "id": "ab62da37-9632-4dc8-833b-feb4c62bab37", + "execution_count": 7, + "id": "1bfd2b22-292a-4f4d-91a0-46bb704f5e38", "metadata": {}, "outputs": [], "source": [ @@ -158,22 +306,39 @@ "\n", "def first_model(state):\n", " human_input = state['messages'][-1].content\n", - " return {\"messages\": [AIMessage(\n", - " content=\"\", \n", - " additional_kwargs={\n", - " \"function_call\": {\n", - " \"name\": \"tavily_search_results_json\", \n", - " \"arguments\": json.dumps({\"query\": human_input})\n", - " }\n", - " }\n", - " )\n", - " ]}" + " return {\n", + " \"messages\": [\n", + " AIMessage(\n", + " content=\"\", \n", + " additional_kwargs={\n", + " \"function_call\": {\n", + " \"name\": \"tavily_search_results_json\", \n", + " \"arguments\": json.dumps({\"query\": human_input})\n", + " }\n", + " }\n", + " )\n", + " ]\n", + " }" + ] + }, + { + "cell_type": "markdown", + "id": "ffd6e892-946c-4899-8cc0-7c9291c1f73b", + "metadata": {}, + "source": [ + "## Define the graph\n", + "\n", + "We can now put it all together and define the graph!\n", + "\n", + "**MODIFICATION**\n", + "\n", + "We will define a `first_agent` node which we will set as the entrypoint." ] }, { "cell_type": "code", - "execution_count": 12, - "id": "1133ec83-7af9-4444-9f88-c793fbdce214", + "execution_count": 8, + "id": "813ae66c-3b58-4283-a02a-36da72a2ab90", "metadata": {}, "outputs": [], "source": [ @@ -226,38 +391,71 @@ "app = workflow.compile()" ] }, + { + "cell_type": "markdown", + "id": "547c3931-3dae-4281-ad4e-4b51305594d4", + "metadata": {}, + "source": [ + "## Use it!\n", + "\n", + "We can now use it!\n", + "This now exposes the [same interface](https://python.langchain.com/docs/expression_language/) as all other LangChain runnables." + ] + }, { "cell_type": "code", - "execution_count": 13, - "id": "289c648f-bfc6-464f-8df9-50be8b1b9e48", + "execution_count": 9, + "id": "8edb04b9-40b6-46f1-a7a8-4b2d8aba7752", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ + "Output from node 'first_agent':\n", + "---\n", "{'messages': [AIMessage(content='', additional_kwargs={'function_call': {'name': 'tavily_search_results_json', 'arguments': '{\"query\": \"what is the weather in sf\"}'}})]}\n", - "----\n", - "{'messages': [FunctionMessage(content=\"[{'url': 'https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States', 'content': 'January 2024 Weather History in San Francisco California, United States Daily Precipitation in January 2024 in San Francisco Observed Weather in January 2024 in San Francisco San Francisco Temperature History January 2024 Hourly Temperature in January 2024 in San Francisco Hours of Daylight and Twilight in January 2024 in San Francisco80 deg, E Cloud Cover Mostly Cloudy 18,000 ft Mostly Clear 4,000 ft Partly Cloudy 15,000 ft Raw: KSFO 121956Z 08006KT 10SM FEW040 SCT150 BKN180 11/07 A3028 RMK AO2 SLP254 T01110067 This report shows the past weather for San Francisco, providing a weather history for January 2024.'}]\", name='tavily_search_results_json')]}\n", - "----\n", - "{'messages': [AIMessage(content=\"I'm sorry, but I couldn't find the current weather in San Francisco. However, you can visit [this link](https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States) to see the historical weather data for January 2024 in San Francisco.\")]}\n", - "----\n", - "{'messages': [HumanMessage(content='what is the weather in sf'), AIMessage(content='', additional_kwargs={'function_call': {'name': 'tavily_search_results_json', 'arguments': '{\"query\": \"what is the weather in sf\"}'}}), FunctionMessage(content=\"[{'url': 'https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States', 'content': 'January 2024 Weather History in San Francisco California, United States Daily Precipitation in January 2024 in San Francisco Observed Weather in January 2024 in San Francisco San Francisco Temperature History January 2024 Hourly Temperature in January 2024 in San Francisco Hours of Daylight and Twilight in January 2024 in San Francisco80 deg, E Cloud Cover Mostly Cloudy 18,000 ft Mostly Clear 4,000 ft Partly Cloudy 15,000 ft Raw: KSFO 121956Z 08006KT 10SM FEW040 SCT150 BKN180 11/07 A3028 RMK AO2 SLP254 T01110067 This report shows the past weather for San Francisco, providing a weather history for January 2024.'}]\", name='tavily_search_results_json'), AIMessage(content=\"I'm sorry, but I couldn't find the current weather in San Francisco. However, you can visit [this link](https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States) to see the historical weather data for January 2024 in San Francisco.\")]}\n", - "----\n" + "\n", + "---\n", + "\n", + "Output from node 'action':\n", + "---\n", + "{'messages': [FunctionMessage(content=\"[{'url': 'https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States', 'content': 'January 2024 Weather History in San Francisco California, United States Daily Precipitation in January 2024 in San Francisco Observed Weather in January 2024 in San Francisco San Francisco Temperature History January 2024 Hourly Temperature in January 2024 in San Francisco Hours of Daylight and Twilight in January 2024 in San FranciscoJanuary 2024 Weather History in San Francisco California, United States. The data for this report comes from the San Francisco International Airport. ... frigid 15°F freezing 32°F very cold 45°F cold 55°F cool 65°F comfortable 75°F warm 85°F hot 95°F sweltering. The hourly reported temperature, color coded into bands. ...'}]\", name='tavily_search_results_json')]}\n", + "\n", + "---\n", + "\n", + "Output from node 'agent':\n", + "---\n", + "{'messages': [AIMessage(content=\"I'm sorry, but I couldn't find the current weather in San Francisco. However, you can check the historical weather data for January 2024 in San Francisco on this [website](https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States).\")]}\n", + "\n", + "---\n", + "\n", + "Output from node '__end__':\n", + "---\n", + "{'messages': [HumanMessage(content='what is the weather in sf'), AIMessage(content='', additional_kwargs={'function_call': {'name': 'tavily_search_results_json', 'arguments': '{\"query\": \"what is the weather in sf\"}'}}), FunctionMessage(content=\"[{'url': 'https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States', 'content': 'January 2024 Weather History in San Francisco California, United States Daily Precipitation in January 2024 in San Francisco Observed Weather in January 2024 in San Francisco San Francisco Temperature History January 2024 Hourly Temperature in January 2024 in San Francisco Hours of Daylight and Twilight in January 2024 in San FranciscoJanuary 2024 Weather History in San Francisco California, United States. The data for this report comes from the San Francisco International Airport. ... frigid 15°F freezing 32°F very cold 45°F cold 55°F cool 65°F comfortable 75°F warm 85°F hot 95°F sweltering. The hourly reported temperature, color coded into bands. ...'}]\", name='tavily_search_results_json'), AIMessage(content=\"I'm sorry, but I couldn't find the current weather in San Francisco. However, you can check the historical weather data for January 2024 in San Francisco on this [website](https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States).\")]}\n", + "\n", + "---\n", + "\n" ] } ], "source": [ + "from langchain_core.messages import HumanMessage\n", + "\n", "inputs = {\"messages\": [HumanMessage(content=\"what is the weather in sf\")]}\n", - "for s in app.stream(inputs):\n", - " print(list(s.values())[0])\n", - " print(\"----\")" + "for output in app.stream(inputs):\n", + " # stream() yields dictionaries with output keyed by node name\n", + " for key, value in output.items():\n", + " print(f\"Output from node '{key}':\")\n", + " print(\"---\")\n", + " print(value)\n", + " print(\"\\n---\\n\")" ] }, { "cell_type": "code", "execution_count": null, - "id": "4cd4b39f-831a-4818-bf56-99cf301b0555", + "id": "08ae8246-11d5-40e1-8567-361e5bef8917", "metadata": {}, "outputs": [], "source": [] diff --git a/examples/chat_executor_with_function_calling/human-in-the-loop.ipynb b/examples/chat_executor_with_function_calling/human-in-the-loop.ipynb index 5f6219809..2c4de7109 100644 --- a/examples/chat_executor_with_function_calling/human-in-the-loop.ipynb +++ b/examples/chat_executor_with_function_calling/human-in-the-loop.ipynb @@ -1,87 +1,248 @@ { "cells": [ + { + "cell_type": "markdown", + "id": "51466c8d-8ce4-4b3d-be4e-18fdbeda5f53", + "metadata": {}, + "source": [ + "# Chat Executor\n", + "\n", + "In this example we will build a chat executor that has a human in the loop. We will use the human to approve specific actions.\n", + "\n", + "This examples builds off the base chat executor. It is highly recommended you learn about that executor before going through this notebook. You can find documentation for that example [here](./base.ipynb).\n", + "\n", + "Any modifications of that example are called below with **MODIFICATION**, so if you are looking for the differences you can just search for that." + ] + }, + { + "cell_type": "markdown", + "id": "7cbd446a-808f-4394-be92-d45ab818953c", + "metadata": {}, + "source": [ + "## Setup\n", + "\n", + "First we need to install the packages required" + ] + }, { "cell_type": "code", "execution_count": 1, - "id": "aa9f9110-ea74-43af-aa72-6b45518abd6e", + "id": "af4ce0ba-7596-4e5f-8bf8-0b0bd6e62833", + "metadata": {}, + "outputs": [], + "source": [ + "!pip install --quiet -U langchain langchain_openai tavily-python" + ] + }, + { + "cell_type": "markdown", + "id": "0abe11f4-62ed-4dc4-8875-3db21e260d1d", + "metadata": {}, + "source": [ + "Next, we need to set API keys for OpenAI (the LLM we will use) and Tavily (the search tool we will use)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c903a1cf-2977-4e2d-ad7d-8b3946821d89", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import getpass\n", + "\n", + "os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key:\")\n", + "os.environ[\"TAVILY_API_KEY\"] = getpass.getpass(\"Tavily API Key:\")" + ] + }, + { + "cell_type": "markdown", + "id": "f0ed46a8-effe-4596-b0e1-a6a29ee16f5c", + "metadata": {}, + "source": [ + "Optionally, we can set API key for [LangSmith tracing](https://smith.langchain.com/), which will give us best-in-class observability." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "95e25aec-7c9f-4a63-b143-225d0e9a79c3", + "metadata": {}, + "outputs": [], + "source": [ + "os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n", + "os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass(\"LangSmith API Key:\")" + ] + }, + { + "cell_type": "markdown", + "id": "21ac643b-cb06-4724-a80c-2862ba4773f1", + "metadata": {}, + "source": [ + "## Set up the tools\n", + "\n", + "We will first define the tools we want to use.\n", + "For this simple example, we will use a built-in search tool via Tavily.\n", + "However, it is really easy to create your own tools - see documentation [here](https://python.langchain.com/docs/modules/agents/tools/custom_tools) on how to do that.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "d7ef57dd-5d6e-4ad3-9377-a92201c1310e", "metadata": {}, "outputs": [], "source": [ - "from langchain import hub\n", - "from langchain_openai import ChatOpenAI\n", "from langchain_community.tools.tavily_search import TavilySearchResults\n", - "from langgraph.prebuilt.messages_executor import create_messages_executor\n", - "from langchain_core.messages import HumanMessage" + "\n", + "tools = [TavilySearchResults(max_results=1)]" + ] + }, + { + "cell_type": "markdown", + "id": "01885785-b71a-44d1-b1d6-7b5b14d53b58", + "metadata": {}, + "source": [ + "We can now wrap these tools in a simple ToolExecutor.\n", + "This is a real simple class that takes in a ToolInvocation and calls that tool, returning the output.\n", + "A ToolInvocation is any class with `tool` and `tool_input` attribute.\n" ] }, { "cell_type": "code", "execution_count": 2, - "id": "0f005b2d-ddc0-4d60-8af9-9eb3dbeb45b7", + "id": "5cf3331e-ccb3-41c8-aeb9-a840a94d41e7", "metadata": {}, "outputs": [], "source": [ - "tools = [TavilySearchResults(max_results=1)]\n", - "model = ChatOpenAI()" + "from langgraph.prebuilt import ToolExecutor\n", + "\n", + "tool_executor = ToolExecutor(tools)" + ] + }, + { + "cell_type": "markdown", + "id": "5497ed70-fce3-47f1-9cad-46f912bad6a5", + "metadata": {}, + "source": [ + "## Set up the model\n", + "\n", + "Now we need to load the chat model we want to use.\n", + "Importantly, this should satisfy two criteria:\n", + "\n", + "1. It should work with messages. We will represent all agent state in the form of messages, so it needs to be able to work well with them.\n", + "2. It should work with OpenAI function calling. This means it should either be an OpenAI model or a model that exposes a similar interface.\n", + "\n", + "Note: these model requirements are not requirements for using LangGraph - they are just requirements for this one example.\n" ] }, { "cell_type": "code", "execution_count": 3, - "id": "02831581-f1ba-4701-ba33-f0f68468907d", + "id": "892b54b9-75f0-4804-9ed0-88b5e5532989", "metadata": {}, "outputs": [], "source": [ - "from langchain.tools.render import format_tool_to_openai_function" + "from langchain_openai import ChatOpenAI\n", + "\n", + "# We will set streaming=True so that we can stream tokens\n", + "# See the streaming section for more information on this.\n", + "model = ChatOpenAI(temperature=0, streaming=True)" + ] + }, + { + "cell_type": "markdown", + "id": "a77995c0-bae2-4cee-a036-8688a90f05b9", + "metadata": {}, + "source": [ + "\n", + "After we've done this, we should make sure the model knows that it has these tools available to call.\n", + "We can do this by converting the LangChain tools into the format for OpenAI function calling, and then bind them to the model class.\n" ] }, { "cell_type": "code", "execution_count": 4, - "id": "a5c0ca12-4922-461c-b740-62ff99f8ae56", + "id": "cd3cbae5-d92c-4559-a4aa-44721b80d107", "metadata": {}, "outputs": [], "source": [ - "model = model.bind_functions([format_tool_to_openai_function(t) for t in tools])" + "from langchain.tools.render import format_tool_to_openai_function\n", + "\n", + "functions = [format_tool_to_openai_function(t) for t in tools]\n", + "model = model.bind_functions(functions)" + ] + }, + { + "cell_type": "markdown", + "id": "8e8b9211-93d0-4ad5-aa7a-9c09099c53ff", + "metadata": {}, + "source": [ + "## Define the agent state\n", + "\n", + "The main type of graph in `langgraph` is the `StatefulGraph`.\n", + "This graph is parameterized by a state object that it passes around to each node.\n", + "Each node then returns operations to update that state.\n", + "These operations can either SET specific attributes on the state (e.g. overwrite the existing values) or ADD to the existing attribute.\n", + "Whether to set or add is denoted by annotating the state object you construct the graph with.\n", + "\n", + "For this example, the state we will track will just be a list of messages.\n", + "We want each node to just add messages to that list.\n", + "Therefore, we will use a `TypedDict` with one key (`messages`) and annotate it so that the `messages` attribute is always added to.\n" ] }, { "cell_type": "code", "execution_count": 5, - "id": "57547622-ddd8-4179-aa4a-e1b69ca4523c", - "metadata": {}, - "outputs": [], - "source": [ - "from langgraph.prebuilt.tool_executor import ToolExecutor\n", - "tool_executor = ToolExecutor(tools)" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "id": "96141ebd-32af-4c9d-a0b0-f46482b8bb88", + "id": "ea793afa-2eab-4901-910d-6eed90cd6564", "metadata": {}, "outputs": [], "source": [ "from typing import TypedDict, Annotated, Sequence\n", "import operator\n", "from langchain_core.messages import BaseMessage\n", - "# We create the AgentState that we will pass around\n", - "# This simply involves a list of messages\n", - "# We want steps to return messages to append to the list\n", - "# So we annotate the messages attribute with operator.add\n", + "\n", + "\n", "class AgentState(TypedDict):\n", " messages: Annotated[Sequence[BaseMessage], operator.add]" ] }, + { + "cell_type": "markdown", + "id": "e03c5094-9297-4d19-a04e-3eedc75cefb4", + "metadata": {}, + "source": [ + "## Define the nodes\n", + "\n", + "We now need to define a few different nodes in our graph.\n", + "In `langgraph`, a node can be either a function or a [runnable](https://python.langchain.com/docs/expression_language/).\n", + "There are two main nodes we need for this:\n", + "\n", + "1. The agent: responsible for deciding what (if any) actions to take.\n", + "2. A function to invoke tools: if the agent decides to take an action, this node will then execute that action.\n", + "\n", + "We will also need to define some edges.\n", + "Some of these edges may be conditional.\n", + "The reason they are conditional is that based on the output of a node, one of several paths may be taken.\n", + "The path that is taken is not known until that node is run (the LLM decides).\n", + "\n", + "1. Conditional Edge: after the agent is called, we should either:\n", + " a. If the agent said to take an action, then the function to invoke tools should be called\n", + " b. If the agent said that it was finished, then it should finish\n", + "2. Normal Edge: after the tools are invoked, it should always go back to the agent to decide what to do next\n", + "\n", + "Let's define the nodes, as well as a function to decide how what conditional edge to take.\n" + ] + }, { "cell_type": "code", - "execution_count": 7, - "id": "f422dc99-672e-414f-88b3-1a6dd21b6882", + "execution_count": 6, + "id": "b547109f-f9e8-4e77-a7e7-ed2bae7a72ab", "metadata": {}, "outputs": [], "source": [ - "from langchain_core.agents import AgentAction\n", + "from langgraph.prebuilt import ToolInvocation\n", "import json\n", "from langchain_core.messages import FunctionMessage\n", "\n", @@ -101,20 +262,36 @@ " messages = state['messages']\n", " response = model.invoke(messages)\n", " # We return a list, because this will get added to the existing list\n", - " return {\"messages\": [response]}\n", + " return {\"messages\": [response]}" + ] + }, + { + "cell_type": "markdown", + "id": "ac402f66-4442-4a1f-9f9b-4a5d97532ceb", + "metadata": {}, + "source": [ + "**MODIFICATION**\n", "\n", + "We modify the function that is calling the tool to first ask for user approval to continue. Note that this is a simple example and we could modify it to change the tool input, use some other channel besides input, etc." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "73fd6432-42e8-472a-89ca-bb5ddbbcc35a", + "metadata": {}, + "outputs": [], + "source": [ "# Define the function to execute tools\n", - "# Here we add some lines to add a human-in-the-loop\n", "def call_tool(state):\n", " messages = state['messages']\n", " # Based on the continue condition\n", " # we know the last message involves a function call\n", " last_message = messages[-1]\n", - " # We construct an AgentAction from the function_call\n", - " action = AgentAction(\n", + " # We construct an ToolInvocation from the function_call\n", + " action = ToolInvocation(\n", " tool=last_message.additional_kwargs[\"function_call\"][\"name\"],\n", " tool_input=json.loads(last_message.additional_kwargs[\"function_call\"][\"arguments\"]),\n", - " log=\"\",\n", " )\n", " response = input(prompt=f\"[y/n] continue with: {action}?\")\n", " if response == \"n\":\n", @@ -124,14 +301,23 @@ " # We use the response to create a FunctionMessage\n", " function_message = FunctionMessage(content=str(response), name=action.tool)\n", " # We return a list, because this will get added to the existing list\n", - " return {\"messages\": [function_message]}\n", - "\n" + " return {\"messages\": [function_message]}" + ] + }, + { + "cell_type": "markdown", + "id": "ffd6e892-946c-4899-8cc0-7c9291c1f73b", + "metadata": {}, + "source": [ + "## Define the graph\n", + "\n", + "We can now put it all together and define the graph!" ] }, { "cell_type": "code", "execution_count": 8, - "id": "1133ec83-7af9-4444-9f88-c793fbdce214", + "id": "813ae66c-3b58-4283-a02a-36da72a2ab90", "metadata": {}, "outputs": [], "source": [ @@ -178,51 +364,84 @@ "app = workflow.compile()" ] }, + { + "cell_type": "markdown", + "id": "547c3931-3dae-4281-ad4e-4b51305594d4", + "metadata": {}, + "source": [ + "## Use it!\n", + "\n", + "We can now use it!\n", + "This now exposes the [same interface](https://python.langchain.com/docs/expression_language/) as all other LangChain runnables." + ] + }, { "cell_type": "code", - "execution_count": 9, - "id": "289c648f-bfc6-464f-8df9-50be8b1b9e48", + "execution_count": 10, + "id": "f544977e-31f7-41f0-88c4-ec9c27b8cecb", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ + "Output from node 'agent':\n", + "---\n", "{'messages': [AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n \"query\": \"weather in San Francisco\"\\n}', 'name': 'tavily_search_results_json'}})]}\n", - "----\n" + "\n", + "---\n", + "\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ - "[y/n] continue with: tool='tavily_search_results_json' tool_input={'query': 'weather in San Francisco'} log=''? y\n" + "[y/n] continue with: tool='tavily_search_results_json' tool_input={'query': 'weather in San Francisco'}? y\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ - "{'messages': [FunctionMessage(content=\"[{'url': 'https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States', 'content': 'January 2024 Weather History in San Francisco California, United States Daily Precipitation in January 2024 in San Francisco Observed Weather in January 2024 in San Francisco San Francisco Temperature History January 2024 Hourly Temperature in January 2024 in San Francisco Hours of Daylight and Twilight in January 2024 in San Francisco80 deg, E Cloud Cover Mostly Cloudy 18,000 ft Mostly Clear 4,000 ft Partly Cloudy 15,000 ft Raw: KSFO 121956Z 08006KT 10SM FEW040 SCT150 BKN180 11/07 A3028 RMK AO2 SLP254 T01110067 This report shows the past weather for San Francisco, providing a weather history for January 2024.'}]\", name='tavily_search_results_json')]}\n", - "----\n", - "{'messages': [AIMessage(content='The weather in San Francisco is currently not available. However, you can check the historical weather data for San Francisco in January 2024 [here](https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States).')]}\n", - "----\n", - "{'messages': [HumanMessage(content='what is the weather in sf'), AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n \"query\": \"weather in San Francisco\"\\n}', 'name': 'tavily_search_results_json'}}), FunctionMessage(content=\"[{'url': 'https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States', 'content': 'January 2024 Weather History in San Francisco California, United States Daily Precipitation in January 2024 in San Francisco Observed Weather in January 2024 in San Francisco San Francisco Temperature History January 2024 Hourly Temperature in January 2024 in San Francisco Hours of Daylight and Twilight in January 2024 in San Francisco80 deg, E Cloud Cover Mostly Cloudy 18,000 ft Mostly Clear 4,000 ft Partly Cloudy 15,000 ft Raw: KSFO 121956Z 08006KT 10SM FEW040 SCT150 BKN180 11/07 A3028 RMK AO2 SLP254 T01110067 This report shows the past weather for San Francisco, providing a weather history for January 2024.'}]\", name='tavily_search_results_json'), AIMessage(content='The weather in San Francisco is currently not available. However, you can check the historical weather data for San Francisco in January 2024 [here](https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States).')]}\n", - "----\n" + "Output from node 'action':\n", + "---\n", + "{'messages': [FunctionMessage(content=\"[{'url': 'https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States', 'content': 'January 2024 Weather History in San Francisco California, United States Daily Precipitation in January 2024 in San Francisco Observed Weather in January 2024 in San Francisco San Francisco Temperature History January 2024 Hourly Temperature in January 2024 in San Francisco Hours of Daylight and Twilight in January 2024 in San FranciscoThis report shows the past weather for San Francisco, providing a weather history for January 2024. It features all historical weather data series we have available, including the San Francisco temperature history for January 2024. You can drill down from year to month and even day level reports by clicking on the graphs.'}]\", name='tavily_search_results_json')]}\n", + "\n", + "---\n", + "\n", + "Output from node 'agent':\n", + "---\n", + "{'messages': [AIMessage(content=\"I'm sorry, but I couldn't find the current weather in San Francisco. However, you can check the historical weather data for January 2024 in San Francisco [here](https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States).\")]}\n", + "\n", + "---\n", + "\n", + "Output from node '__end__':\n", + "---\n", + "{'messages': [HumanMessage(content='what is the weather in sf'), AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n \"query\": \"weather in San Francisco\"\\n}', 'name': 'tavily_search_results_json'}}), FunctionMessage(content=\"[{'url': 'https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States', 'content': 'January 2024 Weather History in San Francisco California, United States Daily Precipitation in January 2024 in San Francisco Observed Weather in January 2024 in San Francisco San Francisco Temperature History January 2024 Hourly Temperature in January 2024 in San Francisco Hours of Daylight and Twilight in January 2024 in San FranciscoThis report shows the past weather for San Francisco, providing a weather history for January 2024. It features all historical weather data series we have available, including the San Francisco temperature history for January 2024. You can drill down from year to month and even day level reports by clicking on the graphs.'}]\", name='tavily_search_results_json'), AIMessage(content=\"I'm sorry, but I couldn't find the current weather in San Francisco. However, you can check the historical weather data for January 2024 in San Francisco [here](https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States).\")]}\n", + "\n", + "---\n", + "\n" ] } ], "source": [ + "from langchain_core.messages import HumanMessage\n", + "\n", "inputs = {\"messages\": [HumanMessage(content=\"what is the weather in sf\")]}\n", - "for s in app.stream(inputs):\n", - " print(list(s.values())[0])\n", - " print(\"----\")" + "for output in app.stream(inputs):\n", + " # stream() yields dictionaries with output keyed by node name\n", + " for key, value in output.items():\n", + " print(f\"Output from node '{key}':\")\n", + " print(\"---\")\n", + " print(value)\n", + " print(\"\\n---\\n\")" ] }, { "cell_type": "code", "execution_count": null, - "id": "4cd4b39f-831a-4818-bf56-99cf301b0555", + "id": "08ae8246-11d5-40e1-8567-361e5bef8917", "metadata": {}, "outputs": [], "source": [] diff --git a/examples/chat_executor_with_function_calling/managing-agent-steps.ipynb b/examples/chat_executor_with_function_calling/managing-agent-steps.ipynb index b136431f5..0e547d597 100644 --- a/examples/chat_executor_with_function_calling/managing-agent-steps.ipynb +++ b/examples/chat_executor_with_function_calling/managing-agent-steps.ipynb @@ -1,87 +1,248 @@ { "cells": [ + { + "cell_type": "markdown", + "id": "51466c8d-8ce4-4b3d-be4e-18fdbeda5f53", + "metadata": {}, + "source": [ + "# Chat Executor\n", + "\n", + "In this example we will build a chat executor that better manages intermediate steps. The base chat executor will just put all messages into the model, but if the intermediate steps an agent is taking start to get long, you may want to modify that. In this example we will only include the ten most recent messages.\n", + "\n", + "This examples builds off the base chat executor. It is highly recommended you learn about that executor before going through this notebook. You can find documentation for that example [here](./base.ipynb).\n", + "\n", + "Any modifications of that example are called below with **MODIFICATION**, so if you are looking for the differences you can just search for that." + ] + }, + { + "cell_type": "markdown", + "id": "7cbd446a-808f-4394-be92-d45ab818953c", + "metadata": {}, + "source": [ + "## Setup\n", + "\n", + "First we need to install the packages required" + ] + }, { "cell_type": "code", "execution_count": 1, - "id": "aa9f9110-ea74-43af-aa72-6b45518abd6e", + "id": "af4ce0ba-7596-4e5f-8bf8-0b0bd6e62833", + "metadata": {}, + "outputs": [], + "source": [ + "!pip install --quiet -U langchain langchain_openai tavily-python" + ] + }, + { + "cell_type": "markdown", + "id": "0abe11f4-62ed-4dc4-8875-3db21e260d1d", + "metadata": {}, + "source": [ + "Next, we need to set API keys for OpenAI (the LLM we will use) and Tavily (the search tool we will use)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c903a1cf-2977-4e2d-ad7d-8b3946821d89", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import getpass\n", + "\n", + "os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key:\")\n", + "os.environ[\"TAVILY_API_KEY\"] = getpass.getpass(\"Tavily API Key:\")" + ] + }, + { + "cell_type": "markdown", + "id": "f0ed46a8-effe-4596-b0e1-a6a29ee16f5c", + "metadata": {}, + "source": [ + "Optionally, we can set API key for [LangSmith tracing](https://smith.langchain.com/), which will give us best-in-class observability." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "95e25aec-7c9f-4a63-b143-225d0e9a79c3", + "metadata": {}, + "outputs": [], + "source": [ + "os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n", + "os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass(\"LangSmith API Key:\")" + ] + }, + { + "cell_type": "markdown", + "id": "21ac643b-cb06-4724-a80c-2862ba4773f1", + "metadata": {}, + "source": [ + "## Set up the tools\n", + "\n", + "We will first define the tools we want to use.\n", + "For this simple example, we will use a built-in search tool via Tavily.\n", + "However, it is really easy to create your own tools - see documentation [here](https://python.langchain.com/docs/modules/agents/tools/custom_tools) on how to do that.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "d7ef57dd-5d6e-4ad3-9377-a92201c1310e", "metadata": {}, "outputs": [], "source": [ - "from langchain import hub\n", - "from langchain_openai import ChatOpenAI\n", "from langchain_community.tools.tavily_search import TavilySearchResults\n", - "from langgraph.prebuilt.messages_executor import create_messages_executor\n", - "from langchain_core.messages import HumanMessage" + "\n", + "tools = [TavilySearchResults(max_results=1)]" + ] + }, + { + "cell_type": "markdown", + "id": "01885785-b71a-44d1-b1d6-7b5b14d53b58", + "metadata": {}, + "source": [ + "We can now wrap these tools in a simple ToolExecutor.\n", + "This is a real simple class that takes in a ToolInvocation and calls that tool, returning the output.\n", + "A ToolInvocation is any class with `tool` and `tool_input` attribute.\n" ] }, { "cell_type": "code", "execution_count": 2, - "id": "0f005b2d-ddc0-4d60-8af9-9eb3dbeb45b7", + "id": "5cf3331e-ccb3-41c8-aeb9-a840a94d41e7", "metadata": {}, "outputs": [], "source": [ - "tools = [TavilySearchResults(max_results=1)]\n", - "model = ChatOpenAI()" + "from langgraph.prebuilt import ToolExecutor\n", + "\n", + "tool_executor = ToolExecutor(tools)" + ] + }, + { + "cell_type": "markdown", + "id": "5497ed70-fce3-47f1-9cad-46f912bad6a5", + "metadata": {}, + "source": [ + "## Set up the model\n", + "\n", + "Now we need to load the chat model we want to use.\n", + "Importantly, this should satisfy two criteria:\n", + "\n", + "1. It should work with messages. We will represent all agent state in the form of messages, so it needs to be able to work well with them.\n", + "2. It should work with OpenAI function calling. This means it should either be an OpenAI model or a model that exposes a similar interface.\n", + "\n", + "Note: these model requirements are not requirements for using LangGraph - they are just requirements for this one example.\n" ] }, { "cell_type": "code", "execution_count": 3, - "id": "02831581-f1ba-4701-ba33-f0f68468907d", + "id": "892b54b9-75f0-4804-9ed0-88b5e5532989", "metadata": {}, "outputs": [], "source": [ - "from langchain.tools.render import format_tool_to_openai_function" + "from langchain_openai import ChatOpenAI\n", + "\n", + "# We will set streaming=True so that we can stream tokens\n", + "# See the streaming section for more information on this.\n", + "model = ChatOpenAI(temperature=0, streaming=True)" + ] + }, + { + "cell_type": "markdown", + "id": "a77995c0-bae2-4cee-a036-8688a90f05b9", + "metadata": {}, + "source": [ + "\n", + "After we've done this, we should make sure the model knows that it has these tools available to call.\n", + "We can do this by converting the LangChain tools into the format for OpenAI function calling, and then bind them to the model class.\n" ] }, { "cell_type": "code", "execution_count": 4, - "id": "a5c0ca12-4922-461c-b740-62ff99f8ae56", + "id": "cd3cbae5-d92c-4559-a4aa-44721b80d107", "metadata": {}, "outputs": [], "source": [ - "model = model.bind_functions([format_tool_to_openai_function(t) for t in tools])" + "from langchain.tools.render import format_tool_to_openai_function\n", + "\n", + "functions = [format_tool_to_openai_function(t) for t in tools]\n", + "model = model.bind_functions(functions)" + ] + }, + { + "cell_type": "markdown", + "id": "8e8b9211-93d0-4ad5-aa7a-9c09099c53ff", + "metadata": {}, + "source": [ + "## Define the agent state\n", + "\n", + "The main type of graph in `langgraph` is the `StatefulGraph`.\n", + "This graph is parameterized by a state object that it passes around to each node.\n", + "Each node then returns operations to update that state.\n", + "These operations can either SET specific attributes on the state (e.g. overwrite the existing values) or ADD to the existing attribute.\n", + "Whether to set or add is denoted by annotating the state object you construct the graph with.\n", + "\n", + "For this example, the state we will track will just be a list of messages.\n", + "We want each node to just add messages to that list.\n", + "Therefore, we will use a `TypedDict` with one key (`messages`) and annotate it so that the `messages` attribute is always added to.\n" ] }, { "cell_type": "code", "execution_count": 5, - "id": "57547622-ddd8-4179-aa4a-e1b69ca4523c", - "metadata": {}, - "outputs": [], - "source": [ - "from langgraph.prebuilt.tool_executor import ToolExecutor\n", - "tool_executor = ToolExecutor(tools)" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "id": "96141ebd-32af-4c9d-a0b0-f46482b8bb88", + "id": "ea793afa-2eab-4901-910d-6eed90cd6564", "metadata": {}, "outputs": [], "source": [ "from typing import TypedDict, Annotated, Sequence\n", "import operator\n", "from langchain_core.messages import BaseMessage\n", - "# We create the AgentState that we will pass around\n", - "# This simply involves a list of messages\n", - "# We want steps to return messages to append to the list\n", - "# So we annotate the messages attribute with operator.add\n", + "\n", + "\n", "class AgentState(TypedDict):\n", " messages: Annotated[Sequence[BaseMessage], operator.add]" ] }, + { + "cell_type": "markdown", + "id": "e03c5094-9297-4d19-a04e-3eedc75cefb4", + "metadata": {}, + "source": [ + "## Define the nodes\n", + "\n", + "We now need to define a few different nodes in our graph.\n", + "In `langgraph`, a node can be either a function or a [runnable](https://python.langchain.com/docs/expression_language/).\n", + "There are two main nodes we need for this:\n", + "\n", + "1. The agent: responsible for deciding what (if any) actions to take.\n", + "2. A function to invoke tools: if the agent decides to take an action, this node will then execute that action.\n", + "\n", + "We will also need to define some edges.\n", + "Some of these edges may be conditional.\n", + "The reason they are conditional is that based on the output of a node, one of several paths may be taken.\n", + "The path that is taken is not known until that node is run (the LLM decides).\n", + "\n", + "1. Conditional Edge: after the agent is called, we should either:\n", + " a. If the agent said to take an action, then the function to invoke tools should be called\n", + " b. If the agent said that it was finished, then it should finish\n", + "2. Normal Edge: after the tools are invoked, it should always go back to the agent to decide what to do next\n", + "\n", + "Let's define the nodes, as well as a function to decide how what conditional edge to take.\n" + ] + }, { "cell_type": "code", - "execution_count": 20, - "id": "f422dc99-672e-414f-88b3-1a6dd21b6882", + "execution_count": null, + "id": "e718a9c5-6596-457f-ac25-a25d8cb8c259", "metadata": {}, "outputs": [], "source": [ - "from langchain_core.agents import AgentAction\n", + "from langgraph.prebuilt import ToolInvocation\n", "import json\n", "from langchain_core.messages import FunctionMessage\n", "\n", @@ -94,42 +255,74 @@ " return \"end\"\n", " # Otherwise if there is, we continue\n", " else:\n", - " return \"continue\"\n", + " return \"continue\"" + ] + }, + { + "cell_type": "markdown", + "id": "a763aa63-701c-40fa-a9d3-9d992ebe7e4d", + "metadata": {}, + "source": [ + "**MODIFICATION**\n", "\n", + "Here we don't pass all messages to the model but rather only pass the five most recent. Note that this is a pretty simplistic way to handle messages, and there may be other methods you may want to look into depending on your use case" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "714e4135-7cb5-4f17-b2ae-46f7e98bde61", + "metadata": {}, + "outputs": [], + "source": [ "# Define the function that calls the model\n", "def call_model(state):\n", - " messages = state['messages']\n", - " if len(messages) > 10:\n", - " messages = messages[-10:]\n", + " messages = state['messages'][-5:]\n", " response = model.invoke(messages)\n", " # We return a list, because this will get added to the existing list\n", - " return {\"messages\": [response]}\n", - "\n", + " return {\"messages\": [response]}" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "b3ca9564-63cc-4309-b158-5e8d3e907164", + "metadata": {}, + "outputs": [], + "source": [ "# Define the function to execute tools\n", "def call_tool(state):\n", " messages = state['messages']\n", " # Based on the continue condition\n", " # we know the last message involves a function call\n", " last_message = messages[-1]\n", - " # We construct an AgentAction from the function_call\n", - " action = AgentAction(\n", + " # We construct an ToolInvocation from the function_call\n", + " action = ToolInvocation(\n", " tool=last_message.additional_kwargs[\"function_call\"][\"name\"],\n", " tool_input=json.loads(last_message.additional_kwargs[\"function_call\"][\"arguments\"]),\n", - " log=\"\",\n", " )\n", " # We call the tool_executor and get back a response\n", " response = tool_executor.invoke(action)\n", " # We use the response to create a FunctionMessage\n", " function_message = FunctionMessage(content=str(response), name=action.tool)\n", " # We return a list, because this will get added to the existing list\n", - " return {\"messages\": [function_message]}\n", - "\n" + " return {\"messages\": [function_message]}" + ] + }, + { + "cell_type": "markdown", + "id": "ffd6e892-946c-4899-8cc0-7c9291c1f73b", + "metadata": {}, + "source": [ + "## Define the graph\n", + "\n", + "We can now put it all together and define the graph!" ] }, { "cell_type": "code", - "execution_count": 21, - "id": "1133ec83-7af9-4444-9f88-c793fbdce214", + "execution_count": 9, + "id": "813ae66c-3b58-4283-a02a-36da72a2ab90", "metadata": {}, "outputs": [], "source": [ @@ -176,38 +369,71 @@ "app = workflow.compile()" ] }, + { + "cell_type": "markdown", + "id": "547c3931-3dae-4281-ad4e-4b51305594d4", + "metadata": {}, + "source": [ + "## Use it!\n", + "\n", + "We can now use it!\n", + "This now exposes the [same interface](https://python.langchain.com/docs/expression_language/) as all other LangChain runnables." + ] + }, { "cell_type": "code", - "execution_count": 22, - "id": "289c648f-bfc6-464f-8df9-50be8b1b9e48", + "execution_count": 10, + "id": "f544977e-31f7-41f0-88c4-ec9c27b8cecb", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ + "Output from node 'agent':\n", + "---\n", "{'messages': [AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n \"query\": \"weather in San Francisco\"\\n}', 'name': 'tavily_search_results_json'}})]}\n", - "----\n", - "{'messages': [FunctionMessage(content=\"[{'url': 'https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States', 'content': 'January 2024 Weather History in San Francisco California, United States Daily Precipitation in January 2024 in San Francisco Observed Weather in January 2024 in San Francisco San Francisco Temperature History January 2024 Hourly Temperature in January 2024 in San Francisco Hours of Daylight and Twilight in January 2024 in San Francisco80 deg, E Cloud Cover Mostly Cloudy 18,000 ft Mostly Clear 4,000 ft Partly Cloudy 15,000 ft Raw: KSFO 121956Z 08006KT 10SM FEW040 SCT150 BKN180 11/07 A3028 RMK AO2 SLP254 T01110067 This report shows the past weather for San Francisco, providing a weather history for January 2024.'}]\", name='tavily_search_results_json')]}\n", - "----\n", - "{'messages': [AIMessage(content='The weather in San Francisco is currently not available. However, you can check the weather history for January 2024 in San Francisco on [weatherspark.com](https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States).')]}\n", - "----\n", - "{'messages': [HumanMessage(content='what is the weather in sf'), AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n \"query\": \"weather in San Francisco\"\\n}', 'name': 'tavily_search_results_json'}}), FunctionMessage(content=\"[{'url': 'https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States', 'content': 'January 2024 Weather History in San Francisco California, United States Daily Precipitation in January 2024 in San Francisco Observed Weather in January 2024 in San Francisco San Francisco Temperature History January 2024 Hourly Temperature in January 2024 in San Francisco Hours of Daylight and Twilight in January 2024 in San Francisco80 deg, E Cloud Cover Mostly Cloudy 18,000 ft Mostly Clear 4,000 ft Partly Cloudy 15,000 ft Raw: KSFO 121956Z 08006KT 10SM FEW040 SCT150 BKN180 11/07 A3028 RMK AO2 SLP254 T01110067 This report shows the past weather for San Francisco, providing a weather history for January 2024.'}]\", name='tavily_search_results_json'), AIMessage(content='The weather in San Francisco is currently not available. However, you can check the weather history for January 2024 in San Francisco on [weatherspark.com](https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States).')]}\n", - "----\n" + "\n", + "---\n", + "\n", + "Output from node 'action':\n", + "---\n", + "{'messages': [FunctionMessage(content=\"[{'url': 'https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States', 'content': 'January 2024 Weather History in San Francisco California, United States Daily Precipitation in January 2024 in San Francisco Observed Weather in January 2024 in San Francisco San Francisco Temperature History January 2024 Hourly Temperature in January 2024 in San Francisco Hours of Daylight and Twilight in January 2024 in San FranciscoThis report shows the past weather for San Francisco, providing a weather history for January 2024. It features all historical weather data series we have available, including the San Francisco temperature history for January 2024. You can drill down from year to month and even day level reports by clicking on the graphs.'}]\", name='tavily_search_results_json')]}\n", + "\n", + "---\n", + "\n", + "Output from node 'agent':\n", + "---\n", + "{'messages': [AIMessage(content=\"I'm sorry, but I couldn't find the current weather in San Francisco. However, you can check the historical weather data for January 2024 in San Francisco [here](https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States).\")]}\n", + "\n", + "---\n", + "\n", + "Output from node '__end__':\n", + "---\n", + "{'messages': [HumanMessage(content='what is the weather in sf'), AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n \"query\": \"weather in San Francisco\"\\n}', 'name': 'tavily_search_results_json'}}), FunctionMessage(content=\"[{'url': 'https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States', 'content': 'January 2024 Weather History in San Francisco California, United States Daily Precipitation in January 2024 in San Francisco Observed Weather in January 2024 in San Francisco San Francisco Temperature History January 2024 Hourly Temperature in January 2024 in San Francisco Hours of Daylight and Twilight in January 2024 in San FranciscoThis report shows the past weather for San Francisco, providing a weather history for January 2024. It features all historical weather data series we have available, including the San Francisco temperature history for January 2024. You can drill down from year to month and even day level reports by clicking on the graphs.'}]\", name='tavily_search_results_json'), AIMessage(content=\"I'm sorry, but I couldn't find the current weather in San Francisco. However, you can check the historical weather data for January 2024 in San Francisco [here](https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States).\")]}\n", + "\n", + "---\n", + "\n" ] } ], "source": [ + "from langchain_core.messages import HumanMessage\n", + "\n", "inputs = {\"messages\": [HumanMessage(content=\"what is the weather in sf\")]}\n", - "for s in app.stream(inputs):\n", - " print(list(s.values())[0])\n", - " print(\"----\")" + "for output in app.stream(inputs):\n", + " # stream() yields dictionaries with output keyed by node name\n", + " for key, value in output.items():\n", + " print(f\"Output from node '{key}':\")\n", + " print(\"---\")\n", + " print(value)\n", + " print(\"\\n---\\n\")" ] }, { "cell_type": "code", "execution_count": null, - "id": "4cd4b39f-831a-4818-bf56-99cf301b0555", + "id": "08ae8246-11d5-40e1-8567-361e5bef8917", "metadata": {}, "outputs": [], "source": [] diff --git a/examples/chat_executor_with_function_calling/respond-in-format.ipynb b/examples/chat_executor_with_function_calling/respond-in-format.ipynb index 85085b8e1..8ca16498b 100644 --- a/examples/chat_executor_with_function_calling/respond-in-format.ipynb +++ b/examples/chat_executor_with_function_calling/respond-in-format.ipynb @@ -1,112 +1,270 @@ { "cells": [ + { + "cell_type": "markdown", + "id": "51466c8d-8ce4-4b3d-be4e-18fdbeda5f53", + "metadata": {}, + "source": [ + "# Chat Executor\n", + "\n", + "In this example we will build a chat executor that responds in a specific format. We will do this by using OpenAI function calling. This is useful when you want to enforce that an agent's response is in a specific format. In this example, we will ask it respond as if a weatherman, so to return the temperature and then any other additional info.\n", + "\n", + "This examples builds off the base chat executor. It is highly recommended you learn about that executor before going through this notebook. You can find documentation for that example [here](./base.ipynb).\n", + "\n", + "Any modifications of that example are called below with **MODIFICATION**, so if you are looking for the differences you can just search for that." + ] + }, + { + "cell_type": "markdown", + "id": "7cbd446a-808f-4394-be92-d45ab818953c", + "metadata": {}, + "source": [ + "## Setup\n", + "\n", + "First we need to install the packages required" + ] + }, { "cell_type": "code", "execution_count": 1, - "id": "aa9f9110-ea74-43af-aa72-6b45518abd6e", + "id": "af4ce0ba-7596-4e5f-8bf8-0b0bd6e62833", + "metadata": {}, + "outputs": [], + "source": [ + "!pip install --quiet -U langchain langchain_openai tavily-python" + ] + }, + { + "cell_type": "markdown", + "id": "0abe11f4-62ed-4dc4-8875-3db21e260d1d", + "metadata": {}, + "source": [ + "Next, we need to set API keys for OpenAI (the LLM we will use) and Tavily (the search tool we will use)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c903a1cf-2977-4e2d-ad7d-8b3946821d89", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import getpass\n", + "\n", + "os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key:\")\n", + "os.environ[\"TAVILY_API_KEY\"] = getpass.getpass(\"Tavily API Key:\")" + ] + }, + { + "cell_type": "markdown", + "id": "f0ed46a8-effe-4596-b0e1-a6a29ee16f5c", + "metadata": {}, + "source": [ + "Optionally, we can set API key for [LangSmith tracing](https://smith.langchain.com/), which will give us best-in-class observability." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "95e25aec-7c9f-4a63-b143-225d0e9a79c3", + "metadata": {}, + "outputs": [], + "source": [ + "os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n", + "os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass(\"LangSmith API Key:\")" + ] + }, + { + "cell_type": "markdown", + "id": "21ac643b-cb06-4724-a80c-2862ba4773f1", + "metadata": {}, + "source": [ + "## Set up the tools\n", + "\n", + "We will first define the tools we want to use.\n", + "For this simple example, we will use a built-in search tool via Tavily.\n", + "However, it is really easy to create your own tools - see documentation [here](https://python.langchain.com/docs/modules/agents/tools/custom_tools) on how to do that.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "d7ef57dd-5d6e-4ad3-9377-a92201c1310e", "metadata": {}, "outputs": [], "source": [ - "from langchain import hub\n", - "from langchain_openai import ChatOpenAI\n", "from langchain_community.tools.tavily_search import TavilySearchResults\n", - "from langgraph.prebuilt.messages_executor import create_messages_executor\n", - "from langchain_core.messages import HumanMessage" + "\n", + "tools = [TavilySearchResults(max_results=1)]" + ] + }, + { + "cell_type": "markdown", + "id": "01885785-b71a-44d1-b1d6-7b5b14d53b58", + "metadata": {}, + "source": [ + "We can now wrap these tools in a simple ToolExecutor.\n", + "This is a real simple class that takes in a ToolInvocation and calls that tool, returning the output.\n", + "A ToolInvocation is any class with `tool` and `tool_input` attribute.\n" ] }, { "cell_type": "code", "execution_count": 2, - "id": "0f005b2d-ddc0-4d60-8af9-9eb3dbeb45b7", + "id": "5cf3331e-ccb3-41c8-aeb9-a840a94d41e7", "metadata": {}, "outputs": [], "source": [ - "tools = [TavilySearchResults(max_results=1)]\n", - "model = ChatOpenAI()" + "from langgraph.prebuilt import ToolExecutor\n", + "\n", + "tool_executor = ToolExecutor(tools)" + ] + }, + { + "cell_type": "markdown", + "id": "5497ed70-fce3-47f1-9cad-46f912bad6a5", + "metadata": {}, + "source": [ + "## Set up the model\n", + "\n", + "Now we need to load the chat model we want to use.\n", + "Importantly, this should satisfy two criteria:\n", + "\n", + "1. It should work with messages. We will represent all agent state in the form of messages, so it needs to be able to work well with them.\n", + "2. It should work with OpenAI function calling. This means it should either be an OpenAI model or a model that exposes a similar interface.\n", + "\n", + "Note: these model requirements are not requirements for using LangGraph - they are just requirements for this one example.\n" ] }, { "cell_type": "code", "execution_count": 3, - "id": "02831581-f1ba-4701-ba33-f0f68468907d", + "id": "892b54b9-75f0-4804-9ed0-88b5e5532989", "metadata": {}, "outputs": [], "source": [ - "from langchain.tools.render import format_tool_to_openai_function" + "from langchain_openai import ChatOpenAI\n", + "\n", + "# We will set streaming=True so that we can stream tokens\n", + "# See the streaming section for more information on this.\n", + "model = ChatOpenAI(temperature=0, streaming=True)" + ] + }, + { + "cell_type": "markdown", + "id": "a77995c0-bae2-4cee-a036-8688a90f05b9", + "metadata": {}, + "source": [ + "\n", + "After we've done this, we should make sure the model knows that it has these tools available to call.\n", + "We can do this by converting the LangChain tools into the format for OpenAI function calling, and then bind them to the model class.\n", + "\n", + "\n", + "**MODIFICATION**\n", + "\n", + "We also want to define a response schema for the language model and bind it to the model as a function as well" ] }, { "cell_type": "code", - "execution_count": 5, - "id": "a3bdf328-f34c-421e-9771-85f2740fbad6", + "execution_count": 4, + "id": "cd3cbae5-d92c-4559-a4aa-44721b80d107", "metadata": {}, "outputs": [], "source": [ - "# Here we bind an additional function beside just tools - the response format\n", - "functions = [format_tool_to_openai_function(t) for t in tools]\n", - "\n", + "from langchain.tools.render import format_tool_to_openai_function\n", "from langchain_core.pydantic_v1 import BaseModel, Field\n", + "from langchain_core.utils.function_calling import convert_pydantic_to_openai_function\n", "\n", "class Response(BaseModel):\n", " \"\"\"Final response to the user\"\"\"\n", " temperature: float = Field(description=\"the temperature\")\n", " other_notes: str = Field(description=\"any other notes about the weather\")\n", "\n", - "from langchain_core.utils.function_calling import convert_pydantic_to_openai_function" + "\n", + "functions = [format_tool_to_openai_function(t) for t in tools]\n", + "functions.append(convert_pydantic_to_openai_function(Response))\n", + "model = model.bind_functions(functions)" ] }, { - "cell_type": "code", - "execution_count": 6, - "id": "a5c0ca12-4922-461c-b740-62ff99f8ae56", + "cell_type": "markdown", + "id": "8e8b9211-93d0-4ad5-aa7a-9c09099c53ff", "metadata": {}, - "outputs": [], "source": [ - "model = model.bind_functions(functions + [convert_pydantic_to_openai_function(Response)])" + "## Define the agent state\n", + "\n", + "The main type of graph in `langgraph` is the `StatefulGraph`.\n", + "This graph is parameterized by a state object that it passes around to each node.\n", + "Each node then returns operations to update that state.\n", + "These operations can either SET specific attributes on the state (e.g. overwrite the existing values) or ADD to the existing attribute.\n", + "Whether to set or add is denoted by annotating the state object you construct the graph with.\n", + "\n", + "For this example, the state we will track will just be a list of messages.\n", + "We want each node to just add messages to that list.\n", + "Therefore, we will use a `TypedDict` with one key (`messages`) and annotate it so that the `messages` attribute is always added to.\n" ] }, { "cell_type": "code", - "execution_count": 7, - "id": "57547622-ddd8-4179-aa4a-e1b69ca4523c", - "metadata": {}, - "outputs": [], - "source": [ - "from langgraph.prebuilt.tool_executor import ToolExecutor\n", - "tool_executor = ToolExecutor(tools)" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "id": "96141ebd-32af-4c9d-a0b0-f46482b8bb88", + "execution_count": 5, + "id": "ea793afa-2eab-4901-910d-6eed90cd6564", "metadata": {}, "outputs": [], "source": [ "from typing import TypedDict, Annotated, Sequence\n", "import operator\n", "from langchain_core.messages import BaseMessage\n", - "# We create the AgentState that we will pass around\n", - "# This simply involves a list of messages\n", - "# We want steps to return messages to append to the list\n", - "# So we annotate the messages attribute with operator.add\n", + "\n", + "\n", "class AgentState(TypedDict):\n", " messages: Annotated[Sequence[BaseMessage], operator.add]" ] }, + { + "cell_type": "markdown", + "id": "e03c5094-9297-4d19-a04e-3eedc75cefb4", + "metadata": {}, + "source": [ + "## Define the nodes\n", + "\n", + "We now need to define a few different nodes in our graph.\n", + "In `langgraph`, a node can be either a function or a [runnable](https://python.langchain.com/docs/expression_language/).\n", + "There are two main nodes we need for this:\n", + "\n", + "1. The agent: responsible for deciding what (if any) actions to take.\n", + "2. A function to invoke tools: if the agent decides to take an action, this node will then execute that action.\n", + "\n", + "We will also need to define some edges.\n", + "Some of these edges may be conditional.\n", + "The reason they are conditional is that based on the output of a node, one of several paths may be taken.\n", + "The path that is taken is not known until that node is run (the LLM decides).\n", + "\n", + "1. Conditional Edge: after the agent is called, we should either:\n", + " a. If the agent said to take an action, then the function to invoke tools should be called\n", + " b. If the agent said that it was finished, then it should finish\n", + "2. Normal Edge: after the tools are invoked, it should always go back to the agent to decide what to do next\n", + "\n", + "Let's define the nodes, as well as a function to decide how what conditional edge to take.\n", + "\n", + "**MODIFICATION**\n", + "\n", + "We will change the `should_continue` function to check what function was called. If the function `Response` was called - that is the function that is NOT a tool, but rather the formatted response, so we should NOT continue in that case." + ] + }, { "cell_type": "code", - "execution_count": 14, - "id": "0759bddc-010a-4e3f-9f41-3a51c1ea5144", + "execution_count": 6, + "id": "3b541bb9-900c-40d0-964d-7b5dfee30667", "metadata": {}, "outputs": [], "source": [ - "from langchain_core.agents import AgentAction\n", + "from langgraph.prebuilt import ToolInvocation\n", "import json\n", "from langchain_core.messages import FunctionMessage\n", "\n", "# Define the function that determines whether to continue or not\n", - "# This needs to get updated\n", "def should_continue(state):\n", " messages = state['messages']\n", " last_message = messages[-1]\n", @@ -116,17 +274,10 @@ " # Otherwise if there is, we need to check what type of function call it is\n", " elif last_message.additional_kwargs[\"function_call\"][\"name\"] == \"Response\":\n", " return \"end\"\n", + " # Otherwise we continue\n", " else:\n", - " return \"continue\"" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "id": "2a048e72-525a-4dcb-a91d-efacaddd8848", - "metadata": {}, - "outputs": [], - "source": [ + " return \"continue\"\n", + "\n", "# Define the function that calls the model\n", "def call_model(state):\n", " messages = state['messages']\n", @@ -140,11 +291,10 @@ " # Based on the continue condition\n", " # we know the last message involves a function call\n", " last_message = messages[-1]\n", - " # We construct an AgentAction from the function_call\n", - " action = AgentAction(\n", + " # We construct an ToolInvocation from the function_call\n", + " action = ToolInvocation(\n", " tool=last_message.additional_kwargs[\"function_call\"][\"name\"],\n", " tool_input=json.loads(last_message.additional_kwargs[\"function_call\"][\"arguments\"]),\n", - " log=\"\",\n", " )\n", " # We call the tool_executor and get back a response\n", " response = tool_executor.invoke(action)\n", @@ -154,10 +304,20 @@ " return {\"messages\": [function_message]}" ] }, + { + "cell_type": "markdown", + "id": "ffd6e892-946c-4899-8cc0-7c9291c1f73b", + "metadata": {}, + "source": [ + "## Define the graph\n", + "\n", + "We can now put it all together and define the graph!" + ] + }, { "cell_type": "code", - "execution_count": 16, - "id": "1133ec83-7af9-4444-9f88-c793fbdce214", + "execution_count": 7, + "id": "813ae66c-3b58-4283-a02a-36da72a2ab90", "metadata": {}, "outputs": [], "source": [ @@ -204,38 +364,71 @@ "app = workflow.compile()" ] }, + { + "cell_type": "markdown", + "id": "547c3931-3dae-4281-ad4e-4b51305594d4", + "metadata": {}, + "source": [ + "## Use it!\n", + "\n", + "We can now use it!\n", + "This now exposes the [same interface](https://python.langchain.com/docs/expression_language/) as all other LangChain runnables." + ] + }, { "cell_type": "code", - "execution_count": 17, - "id": "289c648f-bfc6-464f-8df9-50be8b1b9e48", + "execution_count": 8, + "id": "f544977e-31f7-41f0-88c4-ec9c27b8cecb", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ + "Output from node 'agent':\n", + "---\n", "{'messages': [AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n \"query\": \"weather in San Francisco\"\\n}', 'name': 'tavily_search_results_json'}})]}\n", - "----\n", - "{'messages': [FunctionMessage(content=\"[{'url': 'https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States', 'content': 'January 2024 Weather History in San Francisco California, United States Daily Precipitation in January 2024 in San Francisco Observed Weather in January 2024 in San Francisco San Francisco Temperature History January 2024 Hourly Temperature in January 2024 in San Francisco Hours of Daylight and Twilight in January 2024 in San Francisco80 deg, E Cloud Cover Mostly Cloudy 18,000 ft Mostly Clear 4,000 ft Partly Cloudy 15,000 ft Raw: KSFO 121956Z 08006KT 10SM FEW040 SCT150 BKN180 11/07 A3028 RMK AO2 SLP254 T01110067 This report shows the past weather for San Francisco, providing a weather history for January 2024.'}]\", name='tavily_search_results_json')]}\n", - "----\n", - "{'messages': [AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n \"temperature\": 80,\\n \"other_notes\": \"Mostly cloudy\"\\n}', 'name': 'Response'}})]}\n", - "----\n", - "{'messages': [HumanMessage(content='what is the weather in sf'), AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n \"query\": \"weather in San Francisco\"\\n}', 'name': 'tavily_search_results_json'}}), FunctionMessage(content=\"[{'url': 'https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States', 'content': 'January 2024 Weather History in San Francisco California, United States Daily Precipitation in January 2024 in San Francisco Observed Weather in January 2024 in San Francisco San Francisco Temperature History January 2024 Hourly Temperature in January 2024 in San Francisco Hours of Daylight and Twilight in January 2024 in San Francisco80 deg, E Cloud Cover Mostly Cloudy 18,000 ft Mostly Clear 4,000 ft Partly Cloudy 15,000 ft Raw: KSFO 121956Z 08006KT 10SM FEW040 SCT150 BKN180 11/07 A3028 RMK AO2 SLP254 T01110067 This report shows the past weather for San Francisco, providing a weather history for January 2024.'}]\", name='tavily_search_results_json'), AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n \"temperature\": 80,\\n \"other_notes\": \"Mostly cloudy\"\\n}', 'name': 'Response'}})]}\n", - "----\n" + "\n", + "---\n", + "\n", + "Output from node 'action':\n", + "---\n", + "{'messages': [FunctionMessage(content=\"[{'url': 'https://www.whereandwhen.net/when/north-america/california/san-francisco-ca/january/', 'content': 'Best time to go to San Francisco? Weather in San Francisco in january 2024 How was the weather last january? Here is the day by day recorded weather in San Francisco in january 2023: Seasonal average climate and temperature of San Francisco in january The climate of San Francisco in january is tolerableWeather in San Francisco in january 2024. The weather in San Francisco in january comes from statistical datas on the past years. You can view the weather statistics the entire month, but also by using the tabs for the beginning, the middle and the end of the month. ... 15-01-2023 50°F to 52°F. 16-01-2023 45°F to 52°F. 17-01-2023 45°F to ...'}]\", name='tavily_search_results_json')]}\n", + "\n", + "---\n", + "\n", + "Output from node 'agent':\n", + "---\n", + "{'messages': [AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n \"temperature\": 50,\\n \"other_notes\": \"Tolerable weather\"\\n}', 'name': 'Response'}})]}\n", + "\n", + "---\n", + "\n", + "Output from node '__end__':\n", + "---\n", + "{'messages': [HumanMessage(content='what is the weather in sf'), AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n \"query\": \"weather in San Francisco\"\\n}', 'name': 'tavily_search_results_json'}}), FunctionMessage(content=\"[{'url': 'https://www.whereandwhen.net/when/north-america/california/san-francisco-ca/january/', 'content': 'Best time to go to San Francisco? Weather in San Francisco in january 2024 How was the weather last january? Here is the day by day recorded weather in San Francisco in january 2023: Seasonal average climate and temperature of San Francisco in january The climate of San Francisco in january is tolerableWeather in San Francisco in january 2024. The weather in San Francisco in january comes from statistical datas on the past years. You can view the weather statistics the entire month, but also by using the tabs for the beginning, the middle and the end of the month. ... 15-01-2023 50°F to 52°F. 16-01-2023 45°F to 52°F. 17-01-2023 45°F to ...'}]\", name='tavily_search_results_json'), AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n \"temperature\": 50,\\n \"other_notes\": \"Tolerable weather\"\\n}', 'name': 'Response'}})]}\n", + "\n", + "---\n", + "\n" ] } ], "source": [ + "from langchain_core.messages import HumanMessage\n", + "\n", "inputs = {\"messages\": [HumanMessage(content=\"what is the weather in sf\")]}\n", - "for s in app.stream(inputs):\n", - " print(list(s.values())[0])\n", - " print(\"----\")" + "for output in app.stream(inputs):\n", + " # stream() yields dictionaries with output keyed by node name\n", + " for key, value in output.items():\n", + " print(f\"Output from node '{key}':\")\n", + " print(\"---\")\n", + " print(value)\n", + " print(\"\\n---\\n\")" ] }, { "cell_type": "code", "execution_count": null, - "id": "4cd4b39f-831a-4818-bf56-99cf301b0555", + "id": "08ae8246-11d5-40e1-8567-361e5bef8917", "metadata": {}, "outputs": [], "source": [] diff --git a/langgraph/prebuilt/__init__.py b/langgraph/prebuilt/__init__.py index 89dfa34a3..e85922374 100644 --- a/langgraph/prebuilt/__init__.py +++ b/langgraph/prebuilt/__init__.py @@ -1,5 +1,5 @@ from langgraph.prebuilt.agent_executor import create_agent_executor from langgraph.prebuilt import chat_executor -from langgraph.prebuilt.tool_executor import ToolExecutor +from langgraph.prebuilt.tool_executor import ToolExecutor, ToolInvocation -__all__ = ["create_agent_executor", "chat_executor", "ToolExecutor"] +__all__ = ["create_agent_executor", "chat_executor", "ToolExecutor", ToolInvocation] diff --git a/langgraph/prebuilt/tool_executor.py b/langgraph/prebuilt/tool_executor.py index 121ef0855..82fe5e88a 100644 --- a/langgraph/prebuilt/tool_executor.py +++ b/langgraph/prebuilt/tool_executor.py @@ -1,14 +1,30 @@ from typing import Any, Sequence -from langchain_core.agents import AgentAction +from typing import Union from langchain_core.runnables import RunnableBinding, RunnableLambda from langchain_core.tools import BaseTool +from langchain_core.load.serializable import Serializable INVALID_TOOL_MSG_TEMPLATE = ( "{requested_tool_name} is not a valid tool, " "try one of [{available_tool_names_str}]." ) + +class ToolInvocationInterface: + """Interface for invoking a tool""" + tool: str + tool_input: Union[str, dict] + + +class ToolInvocation(Serializable): + """Information about how to invoke a tool.""" + + tool: str + """The name of the Tool to execute.""" + tool_input: Union[str, dict] + """The input to pass in to the Tool.""" + class ToolExecutor(RunnableBinding): tools: Sequence[BaseTool] @@ -19,7 +35,7 @@ class ToolExecutor(RunnableBinding): bound = RunnableLambda(self._execute, afunc=self._aexecute) super().__init__(bound=bound, tools=tools, tool_map ={t.name: t for t in tools}, invalid_tool_msg_template=invalid_tool_msg_template, **kwargs) - def _execute(self, tool_invocation: AgentAction) -> Any: + def _execute(self, tool_invocation: ToolInvocationInterface) -> Any: if tool_invocation.tool not in self.tool_map: return self.invalid_tool_msg_template.format( requested_tool_name=tool_invocation.tool, @@ -30,7 +46,7 @@ class ToolExecutor(RunnableBinding): output = tool.invoke(tool_invocation.tool_input) return output - async def _aexecute(self, tool_invocation: AgentAction) -> Any: + async def _aexecute(self, tool_invocation: ToolInvocationInterface) -> Any: if tool_invocation.tool not in self.tool_map: return self.invalid_tool_msg_template.format( requested_tool_name=tool_invocation.tool,