From da5e6d7319c8ac152d9ac79fb78e632daa760cbc Mon Sep 17 00:00:00 2001 From: Harrison Chase Date: Mon, 15 Jan 2024 13:16:05 -0800 Subject: [PATCH] stash --- README.md | 483 ++++--- examples/agent_executor.ipynb | 104 -- examples/agent_executor/base.ipynb | 274 ++++ .../force-calling-a-tool-first.ipynb | 268 ++++ examples/agent_executor/high-level.ipynb | 302 ++++ .../agent_executor/human-in-the-loop.ipynb | 239 ++++ .../agent_executor/managing-agent-steps.ipynb | 226 +++ .../base.ipynb | 0 .../dynamically-returning-directly.ipynb} | 0 .../force-calling-a-tool-first.ipynb | 0 .../high-level.ipynb} | 50 +- .../human-in-the-loop.ipynb | 0 .../managing-agent-steps.ipynb | 0 .../respond-in-format.ipynb | 0 examples/combine_docs.ipynb | 371 ----- examples/draft-revise-loop.py | 116 -- examples/langgraph.ipynb | 1241 ----------------- examples/rag.py | 58 - examples/readme.py | 132 -- examples/recursive-web-loader.py | 134 -- langgraph/prebuilt/__init__.py | 5 + langgraph/prebuilt/agent_executor.py | 48 +- ...{messages_executor.py => chat_executor.py} | 37 +- langgraph/prebuilt/tool_executor.py | 8 +- 24 files changed, 1679 insertions(+), 2417 deletions(-) delete mode 100644 examples/agent_executor.ipynb create mode 100644 examples/agent_executor/base.ipynb create mode 100644 examples/agent_executor/force-calling-a-tool-first.ipynb create mode 100644 examples/agent_executor/high-level.ipynb create mode 100644 examples/agent_executor/human-in-the-loop.ipynb create mode 100644 examples/agent_executor/managing-agent-steps.ipynb rename examples/{messages_executor_how_to => chat_executor_with_function_calling}/base.ipynb (100%) rename examples/{messages_executor_how_to/dynamically_returning_directly.ipynb => chat_executor_with_function_calling/dynamically-returning-directly.ipynb} (100%) rename examples/{messages_executor_how_to => chat_executor_with_function_calling}/force-calling-a-tool-first.ipynb (100%) rename examples/{messages_executor.ipynb => chat_executor_with_function_calling/high-level.ipynb} (67%) rename examples/{messages_executor_how_to => chat_executor_with_function_calling}/human-in-the-loop.ipynb (100%) rename examples/{messages_executor_how_to => chat_executor_with_function_calling}/managing-agent-steps.ipynb (100%) rename examples/{messages_executor_how_to => chat_executor_with_function_calling}/respond-in-format.ipynb (100%) delete mode 100644 examples/combine_docs.ipynb delete mode 100644 examples/draft-revise-loop.py delete mode 100644 examples/langgraph.ipynb delete mode 100644 examples/rag.py delete mode 100644 examples/readme.py delete mode 100644 examples/recursive-web-loader.py rename langgraph/prebuilt/{messages_executor.py => chat_executor.py} (75%) diff --git a/README.md b/README.md index 37be4a254..d3863b8d4 100644 --- a/README.md +++ b/README.md @@ -65,12 +65,52 @@ tools = [TavilySearchResults(max_results=1)] prompt = hub.pull("hwchase17/openai-functions-agent") # Choose the LLM that will drive the agent -llm = ChatOpenAI(model="gpt-3.5-turbo-1106") +# 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) ``` +### Define the agent state + +The main type of graph in `langgraph` is the `StatefulGraph`. +This graph is parameterized by a state object that it passes around to each node. +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! + +```python +from typing import TypedDict, Annotated, Sequence, Union +from langchain_core.agents import AgentAction, AgentFinish +from langchain_core.messages import BaseMessage +import operator + + +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] + +``` + ### Define the nodes We now need to define a few different nodes in our graph. @@ -93,38 +133,31 @@ 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.runnables import RunnablePassthrough from langchain_core.agents import AgentFinish +from langgraph.prebuilt.tool_executor import ToolExecutor +# 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 -# Note that here, we are using `.assign` to add the output of the agent to the dictionary -# This dictionary will be returned from the node -# The reason we don't want to return just the result of `agent_runnable` from this node is -# that we want to continue passing around all the other inputs -agent = RunnablePassthrough.assign( - agent_outcome = agent_runnable -) +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.pop('agent_outcome') - # Get the tool to use - tool_to_use = {t.name: t for t in tools}[agent_action.tool] - # Call that tool on the input - observation = tool_to_use.invoke(agent_action.tool_input) - # We now add in the action and the observation to the `intermediate_steps` list - # This is the list of all previous actions taken and their output - data['intermediate_steps'].append((agent_action, observation)) - return data + 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): - return "exit" + 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 @@ -134,51 +167,51 @@ def should_continue(data): ### Define the graph -We can now put it alltogether and define the graph! +We can now put it all together and define the graph! ```python -from langgraph.graph import END, Graph +from langgraph.graph import END, StateGraph -workflow = Graph() +# Define a new graph + workflow = StateGraph(AgentState) -# Add the agent node, we give it name `agent` which we will use later -workflow.add_node("agent", agent) -# Add the tools node, we give it name `tools` which we will use later -workflow.add_node("tools", execute_tools) + # Define the two nodes we will cycle between + workflow.add_node("agent", run_agent) + workflow.add_node("action", execute_tools) -# 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": "tools", - # Otherwise we finish. - "exit": 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('tools', '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 + chain = workflow.compile() ``` ### Use it! @@ -187,7 +220,7 @@ We can now use it! This now exposes the [same interface](https://python.langchain.com/docs/expression_language/) as all other LangChain runnables ```python -chain.invoke({"input": "what is the weather in sf", "intermediate_steps": []}) +chain.invoke({"input": "what is the weather in sf"}) ``` ## Streaming @@ -200,7 +233,7 @@ One of the benefits of using LangGraph is that it is easy to stream output as it ```python for output in chain.stream( - {"input": "what is the weather in sf", "intermediate_steps": []} + {"input": "what is the weather in sf"} ): # stream() yields dictionaries with output keyed by node name for key, value in output.items(): @@ -213,102 +246,34 @@ 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'}})]), - 'input': 'what is the weather in sf', - 'intermediate_steps': []} +{'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'}})])} --- -Output from node 'tools': +Output from node 'action': --- -{'input': 'what is the weather in sf', - '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'}})]), - [{'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 8% 46% 29% 12% 8% Evolution of ' - 'daily average temperature and ' - 'precipitation in San Francisco in ' - 'januaryWeather 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. ... 08-01-2023 ' - '52°F to 58°F. 09-01-2023 54°F to 61°F. ' - '10-01-2023 52°F to ...', - 'url': 'https://www.whereandwhen.net/when/north-america/california/san-francisco-ca/january/'}])]} +{'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.'}]")]} --- Output from node 'agent': --- -{'agent_outcome': AgentFinish(return_values={'output': 'The weather in San Francisco in January ranges from 52°F to 61°F. For more detailed and current weather information, you may want to check a reliable weather website or app.'}, log='The weather in San Francisco in January ranges from 52°F to 61°F. For more detailed and current weather information, you may want to check a reliable weather website or app.'), - 'input': 'what is the weather in sf', - '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'}})]), - [{'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 8% 46% 29% 12% 8% Evolution of ' - 'daily average temperature and ' - 'precipitation in San Francisco in ' - 'januaryWeather 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. ... 08-01-2023 ' - '52°F to 58°F. 09-01-2023 54°F to 61°F. ' - '10-01-2023 52°F to ...', - 'url': 'https://www.whereandwhen.net/when/north-america/california/san-francisco-ca/january/'}])]} +{'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.")} --- Output from node '__end__': --- -{'agent_outcome': AgentFinish(return_values={'output': 'The weather in San Francisco in January ranges from 52°F to 61°F. For more detailed and current weather information, you may want to check a reliable weather website or app.'}, log='The weather in San Francisco in January ranges from 52°F to 61°F. For more detailed and current weather information, you may want to check a reliable weather website or app.'), - 'input': 'what is the weather in sf', - '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'}})]), - [{'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 8% 46% 29% 12% 8% Evolution of ' - 'daily average temperature and ' - 'precipitation in San Francisco in ' - 'januaryWeather 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. ... 08-01-2023 ' - '52°F to 58°F. 09-01-2023 54°F to 61°F. ' - '10-01-2023 52°F to ...', - 'url': 'https://www.whereandwhen.net/when/north-america/california/san-francisco-ca/january/'}])]} +{'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.'}]")]} --- ``` ### Streaming LLM Tokens -You can also access the LLM tokens as they are produced by each node. In this case only the "agent" node produces LLM tokens. +You can also access the LLM tokens as they are produced by each node. +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( @@ -395,20 +360,106 @@ content=')' content='' ``` + + + + +## When to Use + +When should you use this versus [LangChain Expression Language](https://python.langchain.com/docs/expression_language/)? + +If you need cycles. + +Langchain Expression Language allows you to easily define chains (DAGs) but does not have a good mechanism for adding in cycles. +`langgraph` adds that syntax. + +## Examples + + +### ChatExecutor: with function calling + +### AgentExecutor + + ## Documentation There are only a few new APIs to use. -The main new class is `Graph`. +### StateGraph + +The main entrypoint is `StateGraph`. ```python -from langgraph.graph import Graph +from langgraph.graph import StateGraph ``` This class is responsible for constructing the graph. It exposes an interface inspired by [NetworkX](https://networkx.org/documentation/latest/). +This graph is parameterized by a state object that it passes around to each node. -### `.add_node` + +#### `__init__` + +```python + def __init__(self, schema: Type[Any]) -> None: +``` + +When constructing the graph, you need to pass in a schema for a state. +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 recommended way to specify the schema is with a typed dictionary: `from typing import TypedDict` + +You can then annotate the different attributes using `from typing imoport Annotated`. +Currently, the only supported annotation is `import operator; operator.add`. +This annotation will make it so that any node that returns this attribute ADDS that new result to the existing value. + +Let's take a look at an example: + +```python +from typing import TypedDict, Annotated, Union +from langchain_core.agents import AgentAction, AgentFinish +import operator + + +class AgentState(TypedDict): + # The input string + input: str + # 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] + +``` + +We can then use this like: + +```python +# Initialize the StateGraph with this state +graph = StateGraph(AgentState) +# Create nodes and edges +... +# Compile the graph +app = graph.compile() + +# The inputs should be a dictionary, because the state is a TypedDict +inputs = { + # Let's assume this the input + "input": "hi" + # Let's assume agent_outcome is set by the graph as some point + # It doesn't need to be provided, and it will be None by default + # Let's assume `intermediate_steps` is built up over time by the graph + # It doesn't need to provided, and it will be empty list by default + # The reason `intermediate_steps` is an empty list and not `None` is because + # it's annotated with `operator.add` +} +``` + +#### `.add_node` ```python def add_node(self, key: str, action: RunnableLike) -> None: @@ -420,7 +471,7 @@ It takes two arguments: - `key`: A string representing the name of the node. This must be unique. - `action`: The action to take when this node is called. This should either be a function or a runnable. -### `.add_edge` +#### `.add_edge` ```python def add_edge(self, start_key: str, end_key: str) -> None: @@ -433,7 +484,7 @@ It takes two arguments. - `start_key`: A string representing the name of the start node. This key must have already been registered in the graph. - `end_key`: A string representing the name of the end node. This key must have already been registered in the graph. -### `.add_conditional_edges` +#### `.add_conditional_edges` ```python def add_conditional_edges( @@ -452,7 +503,7 @@ This takes three arguments: - `condition`: A function to call to decide what to do next. The input will be the output of the start node. It should return a string that is present in `conditional_edge_mapping` and represents the edge to take. - `conditional_edge_mapping`: A mapping of string to string. The keys should be strings that may be returned by `condition`. The values should be the downstream node to call if that condition is returned. -### `.set_entry_point` +#### `.set_entry_point` ```python def set_entry_point(self, key: str) -> None: @@ -464,7 +515,7 @@ It only takes one argument: - `key`: The name of the node that should be called first. -### `.set_finish_point` +#### `.set_finish_point` ```python def set_finish_point(self, key: str) -> None: @@ -478,6 +529,17 @@ It only has one argument: Note: This does not need to be called if at any point you previously created an edge (conditional or normal) to `END` +### Graph + +```python +from langgraph.graph import Graph + +graph = Graph() +``` + +This has the same interface as `StateGraph` with the exception that it doesn't update a state object over time, and rather relies on passing around the full state from each step. +This means that whatever is returned from one node is the input to the next as is. + ### `END` ```python @@ -491,89 +553,86 @@ It can be used in two places: - As the `end_key` in `add_edge` - As a value in `conditional_edge_mapping` as passed to `add_conditional_edges` -## When to Use -When should you use this versus [LangChain Expression Language](https://python.langchain.com/docs/expression_language/)? +## Prebuilt Examples -If you need cycles. +There are also a few methods we've added to make it easy to use common, prebuilt graphs and components. -Langchain Expression Language allows you to easily define chains (DAGs) but does not have a good mechanism for adding in cycles. -`langgraph` adds that syntax. - -## Examples - -### AgentExecutor - -See the above Quick Start for an example of re-creating the LangChain [`AgentExecutor`](https://python.langchain.com/docs/modules/agents/concepts#agentexecutor) class. - -### Forced Function Calling - -One simple modification of the above Graph is to modify it such that a certain tool is always called first. -This can be useful if you want to enforce a certain tool is called, but still want to enable agentic behavior after the fact. - -Assuming you have done the above Quick Start, you can build off it like: - -#### Define the first tool call - -Here, we manually define the first tool call that we will make. -Notice that it does that same thing as `agent` would have done (adds the `agent_outcome` key). -This is so that we can easily plug it in. +### ToolExecutor ```python -from langchain_core.agents import AgentActionMessageLog - -def first_agent(inputs): - action = AgentActionMessageLog( - # We force call this tool - tool="tavily_search_results_json", - # We just pass in the `input` key to this tool - tool_input=inputs["input"], - log="", - message_log=[] - ) - inputs["agent_outcome"] = action - return inputs +from langgraph.prebuilt import ToolExecutor ``` -#### Create the graph - -We can now create a new graph with this new node +This is a simple helper class to help with calling tools. +It is parameterized by a list of tools: ```python -workflow = Graph() - -# Add the same nodes as before, plus this "first agent" -workflow.add_node("first_agent", first_agent) -workflow.add_node("agent", agent) -workflow.add_node("tools", execute_tools) - -# We now set the entry point to be this first agent -workflow.set_entry_point("first_agent") - -# We define the same edges as before -workflow.add_conditional_edges( - "agent", - should_continue, - { - "continue": "tools", - "exit": END - } -) -workflow.add_edge('tools', 'agent') - -# We also define a new edge, from the "first agent" to the tools node -# This is so that we can call the tool -workflow.add_edge('first_agent', 'tools') - -# We now compile the graph as before -chain = workflow.compile() +tools = [...] +tool_executor = ToolExecutor(tools) ``` -#### Use it! +It then exposes a [runnable interface](https://python.langchain.com/docs/expression_language/interface). +It can be used to call tools: you can pass in an [AgentAction](https://python.langchain.com/docs/modules/agents/concepts#agentaction) and it will look up the relevant tool and call it with the appropriate input. -We can now use it as before! -Depending on whether or not the first tool call is actually useful, this may save you an LLM call or two. +### chat_executor.create_function_calling_executor ```python -chain.invoke({"input": "what is the weather in sf", "intermediate_steps": []}) +from langgraph.prebuilt import chat_executor +``` + +This is a helper function for creating a graph that works with a chat model that utilizes function calling. +Can be created by passing in a model and a list of tools. +The model must be one that supports OpenAI function calling. + +```python +from langchain_openai import ChatOpenAI +from langchain_community.tools.tavily_search import TavilySearchResults +from langgraph.prebuilt import chat_executor +from langchain_core.messages import HumanMessage + +tools = [TavilySearchResults(max_results=1)] +model = ChatOpenAI() + +app = chat_executor.create_function_calling_executor(model, tools) + +inputs = {"messages": [HumanMessage(content="what is the weather in sf")]} +for s in app.stream(inputs): + print(list(s.values())[0]) + print("----") +``` + +### create_agent_executor + +```python +from langgraph.prebuilt import create_agent_executor +``` + +This is a helper function for creating a graph that works with [LangChain Agents](https://python.langchain.com/docs/modules/agents/). +Can be created by passing in an agent and a list of tools. + +```python +from langgraph.prebuilt import create_agent_executor +from langchain_openai import ChatOpenAI +from langchain import hub +from langchain.agents import create_openai_functions_agent +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 +llm = ChatOpenAI(model="gpt-3.5-turbo-1106") + +# Construct the OpenAI Functions agent +agent_runnable = create_openai_functions_agent(llm, tools, prompt) + +app = create_agent_executor(agent_runnable, tools) + +inputs = {"input": "what is the weather in sf", "chat_history": []} +for s in app.stream(inputs): + print(list(s.values())[0]) + print("----") ``` diff --git a/examples/agent_executor.ipynb b/examples/agent_executor.ipynb deleted file mode 100644 index 610a74998..000000000 --- a/examples/agent_executor.ipynb +++ /dev/null @@ -1,104 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "id": "e2fdcac4-d134-402b-b423-b0cf4b939f5d", - "metadata": {}, - "outputs": [], - "source": [ - "from langgraph.prebuilt.agent_executor import create_agent_executor\n", - "from langchain_openai import ChatOpenAI\n", - "from langchain import hub\n", - "from langchain.agents import create_openai_functions_agent\n", - "from langchain_community.tools.tavily_search import TavilySearchResults" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "afb59979-c7a3-435f-b147-f8d501f6ff13", - "metadata": {}, - "outputs": [], - "source": [ - "tools = [TavilySearchResults(max_results=1)]\n", - "\n", - "# Get the prompt to use - you can modify this!\n", - "prompt = hub.pull(\"hwchase17/openai-functions-agent\")\n", - "\n", - "# Choose the LLM that will drive the agent\n", - "llm = ChatOpenAI(model=\"gpt-3.5-turbo-1106\")\n", - "\n", - "# Construct the OpenAI Functions agent\n", - "agent_runnable = create_openai_functions_agent(llm, tools, prompt)" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "9be722f0-c9ab-4bd2-af27-66adf51134d2", - "metadata": {}, - "outputs": [], - "source": [ - "app = create_agent_executor(agent_runnable, tools)" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "id": "019b591b-fd71-4ee8-ae94-06d0e2dc6a4d", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'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'}})])}\n", - "----\n", - "{'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.'}]\")]}\n", - "----\n", - "{'agent_outcome': AgentFinish(return_values={'output': \"It seems that I couldn't retrieve the current weather information for San Francisco. I recommend checking a reliable weather website or using a weather app to get the most up-to-date weather forecast for San Francisco.\"}, log=\"It seems that I couldn't retrieve the current weather information for San Francisco. I recommend checking a reliable weather website or using a weather app to get the most up-to-date weather forecast for San Francisco.\")}\n", - "----\n", - "{'input': 'what is the weather in sf', 'agent_outcome': AgentFinish(return_values={'output': \"It seems that I couldn't retrieve the current weather information for San Francisco. I recommend checking a reliable weather website or using a weather app to get the most up-to-date weather forecast for San Francisco.\"}, log=\"It seems that I couldn't retrieve the current weather information for San Francisco. I recommend checking a reliable weather website or using a weather app to get the most up-to-date weather forecast for San Francisco.\"), '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.'}]\")]}\n", - "----\n" - ] - } - ], - "source": [ - "inputs = {\"input\": \"what is the weather in sf\"}\n", - "for s in app.stream(inputs):\n", - " print(list(s.values())[0])\n", - " print(\"----\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "162f647a-36b4-45c3-a171-c8452b05af01", - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.11.1" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/examples/agent_executor/base.ipynb b/examples/agent_executor/base.ipynb new file mode 100644 index 000000000..83b17b20e --- /dev/null +++ b/examples/agent_executor/base.ipynb @@ -0,0 +1,274 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "f725852e-71ef-4615-8cac-011a516fbe72", + "metadata": {}, + "source": [ + "# Agent Executor From Scratch\n", + "\n", + "In this notebook we will go over how to build a basic agent executor from scratch." + ] + }, + { + "cell_type": "markdown", + "id": "5dace4a9-7c9e-4da2-bf7b-e58d0d05581e", + "metadata": {}, + "source": [ + "## Create the LangChain agent\n", + "\n", + "First, we will create the LangChain agent. For more information on LangChain agents, see [this documentation](https://python.langchain.com/docs/modules/agents/)" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "4499eb16-bca8-4a60-9a3a-2f34ae3f7078", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain import hub\n", + "from langchain.agents import create_openai_functions_agent\n", + "from langchain_openai.chat_models import ChatOpenAI\n", + "from langchain_community.tools.tavily_search import TavilySearchResults\n", + "\n", + "tools = [TavilySearchResults(max_results=1)]\n", + "\n", + "# Get the prompt to use - you can modify this!\n", + "prompt = hub.pull(\"hwchase17/openai-functions-agent\")\n", + "\n", + "# Choose the LLM that will drive the agent\n", + "llm = ChatOpenAI(model=\"gpt-3.5-turbo-1106\", streaming=True)\n", + "\n", + "# Construct the OpenAI Functions agent\n", + "agent_runnable = create_openai_functions_agent(llm, tools, prompt)" + ] + }, + { + "cell_type": "markdown", + "id": "972e58b3-fe3c-449d-b3c4-8fa2217afd07", + "metadata": {}, + "source": [ + "## Define the graph state\n", + "\n", + "We now define the graph state. The state for the traditional LangChain agent has a few attributes:\n", + "\n", + "1. `input`: This is the input string representing the main ask from the user, passed in as input.\n", + "2. `chat_history`: This is any previous conversation messages, also passed in as input.\n", + "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.\n", + "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.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "c941fb10-dbe5-4d6a-ab7d-133d01c33cc4", + "metadata": {}, + "outputs": [], + "source": [ + "from typing import TypedDict, Annotated, List, Union\n", + "from langchain_core.agents import AgentAction, AgentFinish\n", + "from langchain_core.messages import BaseMessage\n", + "import operator\n", + "\n", + "\n", + "class AgentState(TypedDict):\n", + " # The input string\n", + " input: str\n", + " # The list of previous messages in the conversation\n", + " chat_history: list[BaseMessage]\n", + " # The outcome of a given call to the agent\n", + " # Needs `None` as a valid type, since this is what this will start as\n", + " agent_outcome: Union[AgentAction, AgentFinish, None]\n", + " # List of actions and corresponding observations\n", + " # Here we annotate this with `operator.add` to indicate that operations to\n", + " # this state should be ADDED to the existing values (not overwrite it)\n", + " intermediate_steps: Annotated[list[tuple[AgentAction, str]], operator.add]\n" + ] + }, + { + "cell_type": "markdown", + "id": "cd27b281-cc9a-49c9-be78-8b98a7d905c4", + "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." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "d61a970d-edf4-4eef-9678-28bab7c72331", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain_core.agents import AgentFinish\n", + "from langgraph.prebuilt.tool_executor import ToolExecutor\n", + "\n", + "# This a helper class we have that is useful for running tools\n", + "# It takes in an agent action and calls that tool and returns the result\n", + "tool_executor = ToolExecutor(tools)\n", + "\n", + "# Define the agent\n", + "def run_agent(data):\n", + " agent_outcome = agent_runnable.invoke(data)\n", + " return {\"agent_outcome\": agent_outcome}\n", + "\n", + "# Define the function to execute tools\n", + "def execute_tools(data):\n", + " # Get the most recent agent_outcome - this is the key added in the `agent` above\n", + " agent_action = data['agent_outcome']\n", + " output = tool_executor.invoke(agent_action)\n", + " return {\"intermediate_steps\": [(agent_action, str(output))]}\n", + "\n", + "# Define logic that will be used to determine which conditional edge to go down\n", + "def should_continue(data):\n", + " # If the agent outcome is an AgentFinish, then we return `exit` string\n", + " # This will be used when setting up the graph to define the flow\n", + " if isinstance(data['agent_outcome'], AgentFinish):\n", + " return \"end\"\n", + " # Otherwise, an AgentAction is returned\n", + " # Here we return `continue` string\n", + " # This will be used when setting up the graph to define the flow\n", + " else:\n", + " return \"continue\"" + ] + }, + { + "cell_type": "markdown", + "id": "c0b211f4-0c5c-4792-b18d-cd70907c71e7", + "metadata": {}, + "source": [ + "## Define the graph\n", + "\n", + "We can now put it all together and define the graph!" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "c4054dde-4618-49b7-998a-daa0c1d6d6c0", + "metadata": {}, + "outputs": [], + "source": [ + "from langgraph.graph import END, StateGraph\n", + "\n", + "# Define a new graph\n", + "workflow = StateGraph(AgentState)\n", + "\n", + "# Define the two nodes we will cycle between\n", + "workflow.add_node(\"agent\", run_agent)\n", + "workflow.add_node(\"action\", execute_tools)\n", + "\n", + "# Set the entrypoint as `agent`\n", + "# This means that this node is the first one called\n", + "workflow.set_entry_point(\"agent\")\n", + "\n", + "# We now add a conditional edge\n", + "workflow.add_conditional_edges(\n", + " # First, we define the start node. We use `agent`.\n", + " # This means these are the edges taken after the `agent` node is called.\n", + " \"agent\",\n", + " # Next, we pass in the function that will determine which node is called next.\n", + " should_continue,\n", + " # Finally we pass in a mapping.\n", + " # The keys are strings, and the values are other nodes.\n", + " # END is a special node marking that the graph should finish.\n", + " # What will happen is we will call `should_continue`, and then the output of that\n", + " # will be matched against the keys in this mapping.\n", + " # Based on which one it matches, that node will then be called.\n", + " {\n", + " # If `tools`, then we call the tool node.\n", + " \"continue\": \"action\",\n", + " # Otherwise we finish.\n", + " \"end\": END\n", + " }\n", + ")\n", + "\n", + "# We now add a normal edge from `tools` to `agent`.\n", + "# This means that after `tools` is called, `agent` node is called next.\n", + "workflow.add_edge('action', 'agent')\n", + "\n", + "# Finally, we compile it!\n", + "# This compiles it into a LangChain Runnable,\n", + "# meaning you can use it as you would any other runnable\n", + "app = workflow.compile()" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "214ae46e-c297-465d-86db-2b0312ed3530", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'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'}})])}\n", + "----\n", + "{'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.'}]\")]}\n", + "----\n", + "{'agent_outcome': AgentFinish(return_values={'output': \"I'm unable to provide the current weather in San Francisco at the moment. If you'd like, I can look up a reliable source to find the current weather for you.\"}, log=\"I'm unable to provide the current weather in San Francisco at the moment. If you'd like, I can look up a reliable source to find the current weather for you.\")}\n", + "----\n", + "{'input': 'what is the weather in sf', 'chat_history': [], 'agent_outcome': AgentFinish(return_values={'output': \"I'm unable to provide the current weather in San Francisco at the moment. If you'd like, I can look up a reliable source to find the current weather for you.\"}, log=\"I'm unable to provide the current weather in San Francisco at the moment. If you'd like, I can look up a reliable source to find the current weather for you.\"), '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.'}]\")]}\n", + "----\n" + ] + } + ], + "source": [ + "inputs = {\"input\": \"what is the weather in sf\", \"chat_history\": []}\n", + "for s in app.stream(inputs):\n", + " print(list(s.values())[0])\n", + " print(\"----\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2eb662bc-de7d-4a57-a3e8-2f00dcf4ff8b", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.1" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/examples/agent_executor/force-calling-a-tool-first.ipynb b/examples/agent_executor/force-calling-a-tool-first.ipynb new file mode 100644 index 000000000..70712a949 --- /dev/null +++ b/examples/agent_executor/force-calling-a-tool-first.ipynb @@ -0,0 +1,268 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "4499eb16-bca8-4a60-9a3a-2f34ae3f7078", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain import hub\n", + "from langchain.agents import create_openai_functions_agent\n", + "from langchain_openai.chat_models import ChatOpenAI\n", + "from langchain_community.tools.tavily_search import TavilySearchResults\n", + "\n", + "tools = [TavilySearchResults(max_results=1)]\n", + "\n", + "# Get the prompt to use - you can modify this!\n", + "prompt = hub.pull(\"hwchase17/openai-functions-agent\")\n", + "\n", + "# Choose the LLM that will drive the agent\n", + "llm = ChatOpenAI(model=\"gpt-3.5-turbo-1106\", streaming=True)\n", + "\n", + "# Construct the OpenAI Functions agent\n", + "agent_runnable = create_openai_functions_agent(llm, tools, prompt)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "c941fb10-dbe5-4d6a-ab7d-133d01c33cc4", + "metadata": {}, + "outputs": [], + "source": [ + "from typing import TypedDict, Annotated, List, Union\n", + "from langchain_core.agents import AgentAction, AgentFinish\n", + "from langchain_core.messages import BaseMessage\n", + "import operator\n", + "\n", + "\n", + "class AgentState(TypedDict):\n", + " # The input string\n", + " input: str\n", + " # The list of previous messages in the conversation\n", + " chat_history: list[BaseMessage]\n", + " # The outcome of a given call to the agent\n", + " # Needs `None` as a valid type, since this is what this will start as\n", + " agent_outcome: Union[AgentAction, AgentFinish, None]\n", + " # List of actions and corresponding observations\n", + " # Here we annotate this with `operator.add` to indicate that operations to\n", + " # this state should be ADDED to the existing values (not overwrite it)\n", + " intermediate_steps: Annotated[list[tuple[AgentAction, str]], operator.add]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "d61a970d-edf4-4eef-9678-28bab7c72331", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain_core.agents import AgentFinish\n", + "from langgraph.prebuilt.tool_executor import ToolExecutor\n", + "\n", + "# This a helper class we have that is useful for running tools\n", + "# It takes in an agent action and calls that tool and returns the result\n", + "tool_executor = ToolExecutor(tools)\n", + "\n", + "# Define the agent\n", + "def run_agent(data):\n", + " agent_outcome = agent_runnable.invoke(data)\n", + " return {\"agent_outcome\": agent_outcome}\n", + "\n", + "# Define the function to execute tools\n", + "def execute_tools(data):\n", + " # Get the most recent agent_outcome - this is the key added in the `agent` above\n", + " agent_action = data['agent_outcome']\n", + " output = tool_executor.invoke(agent_action)\n", + " return {\"intermediate_steps\": [(agent_action, str(output))]}\n", + "\n", + "# Define logic that will be used to determine which conditional edge to go down\n", + "def should_continue(data):\n", + " # If the agent outcome is an AgentFinish, then we return `exit` string\n", + " # This will be used when setting up the graph to define the flow\n", + " if isinstance(data['agent_outcome'], AgentFinish):\n", + " return \"end\"\n", + " # Otherwise, an AgentAction is returned\n", + " # Here we return `continue` string\n", + " # This will be used when setting up the graph to define the flow\n", + " else:\n", + " return \"continue\"" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "4883e47a-0a15-429c-bf31-1e8afe982a77", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'tavily_search_results_json'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tools[0].name" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "fb16db55-ff1a-4e16-94c1-dcb8b2a8f0ba", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain_core.agents import AgentActionMessageLog\n", + "\n", + "def first_agent(inputs):\n", + " action = AgentActionMessageLog(\n", + " # We force call this tool\n", + " tool=\"tavily_search_results_json\",\n", + " # We just pass in the `input` key to this tool\n", + " tool_input=inputs[\"input\"],\n", + " log=\"\",\n", + " message_log=[]\n", + " )\n", + " return {\"agent_outcome\": action}" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "c4054dde-4618-49b7-998a-daa0c1d6d6c0", + "metadata": {}, + "outputs": [], + "source": [ + "from langgraph.graph import END, StateGraph\n", + "\n", + "# Define a new graph\n", + "workflow = StateGraph(AgentState)\n", + "\n", + "# Define the two nodes we will cycle between\n", + "workflow.add_node(\"agent\", run_agent)\n", + "workflow.add_node(\"action\", execute_tools)\n", + "workflow.add_node(\"first_agent\", first_agent)\n", + "\n", + "# Set the entrypoint as `agent`\n", + "# This means that this node is the first one called\n", + "workflow.set_entry_point(\"first_agent\")\n", + "\n", + "# We now add a conditional edge\n", + "workflow.add_conditional_edges(\n", + " # First, we define the start node. We use `agent`.\n", + " # This means these are the edges taken after the `agent` node is called.\n", + " \"agent\",\n", + " # Next, we pass in the function that will determine which node is called next.\n", + " should_continue,\n", + " # Finally we pass in a mapping.\n", + " # The keys are strings, and the values are other nodes.\n", + " # END is a special node marking that the graph should finish.\n", + " # What will happen is we will call `should_continue`, and then the output of that\n", + " # will be matched against the keys in this mapping.\n", + " # Based on which one it matches, that node will then be called.\n", + " {\n", + " # If `tools`, then we call the tool node.\n", + " \"continue\": \"action\",\n", + " # Otherwise we finish.\n", + " \"end\": END\n", + " }\n", + ")\n", + "\n", + "# We now add a normal edge from `tools` to `agent`.\n", + "# This means that after `tools` is called, `agent` node is called next.\n", + "workflow.add_edge('action', 'agent')\n", + "\n", + "workflow.add_edge('first_agent', 'action')\n", + "\n", + "# Finally, we compile it!\n", + "# This compiles it into a LangChain Runnable,\n", + "# meaning you can use it as you would any other runnable\n", + "chain = workflow.compile()" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "214ae46e-c297-465d-86db-2b0312ed3530", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Output from node 'first_agent':\n", + "---\n", + "{'agent_outcome': AgentActionMessageLog(tool='tavily_search_results_json', tool_input='what is the weather in sf', log='', message_log=[])}\n", + "\n", + "---\n", + "\n", + "Output from node 'action':\n", + "---\n", + "{'intermediate_steps': [(AgentActionMessageLog(tool='tavily_search_results_json', tool_input='what is the weather in sf', log='', message_log=[]), \"[{'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. ...'}]\")]}\n", + "\n", + "---\n", + "\n", + "Output from node 'agent':\n", + "---\n", + "{'agent_outcome': AgentFinish(return_values={'output': \"I'm sorry, but I couldn't find the current weather in San Francisco. If you'd like, I can try another source to get the current weather for you.\"}, log=\"I'm sorry, but I couldn't find the current weather in San Francisco. If you'd like, I can try another source to get the current weather for you.\")}\n", + "\n", + "---\n", + "\n", + "Output from node '__end__':\n", + "---\n", + "{'input': 'what is the weather in sf', 'chat_history': [], 'agent_outcome': AgentFinish(return_values={'output': \"I'm sorry, but I couldn't find the current weather in San Francisco. If you'd like, I can try another source to get the current weather for you.\"}, log=\"I'm sorry, but I couldn't find the current weather in San Francisco. If you'd like, I can try another source to get the current weather for you.\"), 'intermediate_steps': [(AgentActionMessageLog(tool='tavily_search_results_json', tool_input='what is the weather in sf', log='', message_log=[]), \"[{'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. ...'}]\")]}\n", + "\n", + "---\n", + "\n" + ] + } + ], + "source": [ + "for output in chain.stream(\n", + " {\"input\": \"what is the weather in sf\", \"chat_history\": []}\n", + "):\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": "2eb662bc-de7d-4a57-a3e8-2f00dcf4ff8b", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.1" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/examples/agent_executor/high-level.ipynb b/examples/agent_executor/high-level.ipynb new file mode 100644 index 000000000..f2d853d5a --- /dev/null +++ b/examples/agent_executor/high-level.ipynb @@ -0,0 +1,302 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "f961801a-6025-4b73-be3b-c3a8a75d4167", + "metadata": {}, + "source": [ + "# Agent Executor\n", + "\n", + "This notebook walks through an example creating an agent executor to work with an existing LangChain agent.\n", + "This is useful for getting started quickly.\n", + "However, it is highly likely you will want to customize the logic - for information on that, check out the other examples in this folder." + ] + }, + { + "cell_type": "markdown", + "id": "6ae180d9-abd3-4a44-8fb1-a2c89434fbeb", + "metadata": {}, + "source": [ + "## Set up LangChain Agent\n", + "\n", + "First, will set up our LangChain Agent. \n", + "See documentation [here](https://python.langchain.com/docs/modules/agents/) for more information on what these agents are and how to think about them" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "e2fdcac4-d134-402b-b423-b0cf4b939f5d", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain_openai import ChatOpenAI\n", + "from langchain import hub\n", + "from langchain.agents import create_openai_functions_agent\n", + "from langchain_community.tools.tavily_search import TavilySearchResults" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "afb59979-c7a3-435f-b147-f8d501f6ff13", + "metadata": {}, + "outputs": [], + "source": [ + "tools = [TavilySearchResults(max_results=1)]\n", + "\n", + "# Get the prompt to use - you can modify this!\n", + "prompt = hub.pull(\"hwchase17/openai-functions-agent\")\n", + "\n", + "# Choose the LLM that will drive the agent\n", + "llm = ChatOpenAI(model=\"gpt-3.5-turbo-1106\")\n", + "\n", + "# Construct the OpenAI Functions agent\n", + "agent_runnable = create_openai_functions_agent(llm, tools, prompt)" + ] + }, + { + "cell_type": "markdown", + "id": "0bcb5ff8-b2d1-4fb2-bed4-3726f96db772", + "metadata": {}, + "source": [ + "## Create agent executor\n", + "\n", + "Now we will use the high level method to create the agent executor" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "7a138eb4-a469-4b30-a059-99d6ea944648", + "metadata": {}, + "outputs": [], + "source": [ + "from langgraph.prebuilt import create_agent_executor" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "9be722f0-c9ab-4bd2-af27-66adf51134d2", + "metadata": {}, + "outputs": [], + "source": [ + "app = create_agent_executor(agent_runnable, tools)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "019b591b-fd71-4ee8-ae94-06d0e2dc6a4d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'agent_outcome': AgentActionMessageLog(tool='tavily_search_results_json', tool_input={'query': 'current weather in San Francisco'}, log=\"\\nInvoking: `tavily_search_results_json` with `{'query': 'current weather in San Francisco'}`\\n\\n\\n\", message_log=[AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\"query\":\"current weather in San Francisco\"}', 'name': 'tavily_search_results_json'}})])}\n", + "----\n", + "{'intermediate_steps': [(AgentActionMessageLog(tool='tavily_search_results_json', tool_input={'query': 'current weather in San Francisco'}, log=\"\\nInvoking: `tavily_search_results_json` with `{'query': 'current weather in San Francisco'}`\\n\\n\\n\", message_log=[AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\"query\":\"current weather in San Francisco\"}', 'name': 'tavily_search_results_json'}})]), \"[{'url': 'https://en.climate-data.org/north-america/united-states-of-america/california/san-francisco-385/t/january-1/', 'content': 'San Francisco Weather in January you can find all information about the weather in San Francisco in January: San Francisco weather in January San Francisco weather by month // weather averages 9.6 (49.2) 6.2 (43.2) 14 (57.3) 113 San Francisco weather in January // weather averages Airport close to San FranciscoWeather ☀ ⛅ San Francisco ☀ ⛅ January ☀ ⛅ Information on temperature, sunshine hours, water temperature & rainfall in January for San Francisco. ... Are you planning a holiday with hopefully nice weather in San Francisco in January 2024? Here you can find all information about the weather in San Francisco in January: ... 15. January ...'}]\")]}\n", + "----\n", + "{'agent_outcome': AgentFinish(return_values={'output': \"I couldn't find the current weather in San Francisco. However, you can visit a reliable weather website or check a weather app for the most up-to-date information.\"}, log=\"I couldn't find the current weather in San Francisco. However, you can visit a reliable weather website or check a weather app for the most up-to-date information.\")}\n", + "----\n", + "{'input': 'what is the weather in sf', 'chat_history': [], 'agent_outcome': AgentFinish(return_values={'output': \"I couldn't find the current weather in San Francisco. However, you can visit a reliable weather website or check a weather app for the most up-to-date information.\"}, log=\"I couldn't find the current weather in San Francisco. However, you can visit a reliable weather website or check a weather app for the most up-to-date information.\"), 'intermediate_steps': [(AgentActionMessageLog(tool='tavily_search_results_json', tool_input={'query': 'current weather in San Francisco'}, log=\"\\nInvoking: `tavily_search_results_json` with `{'query': 'current weather in San Francisco'}`\\n\\n\\n\", message_log=[AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\"query\":\"current weather in San Francisco\"}', 'name': 'tavily_search_results_json'}})]), \"[{'url': 'https://en.climate-data.org/north-america/united-states-of-america/california/san-francisco-385/t/january-1/', 'content': 'San Francisco Weather in January you can find all information about the weather in San Francisco in January: San Francisco weather in January San Francisco weather by month // weather averages 9.6 (49.2) 6.2 (43.2) 14 (57.3) 113 San Francisco weather in January // weather averages Airport close to San FranciscoWeather ☀ ⛅ San Francisco ☀ ⛅ January ☀ ⛅ Information on temperature, sunshine hours, water temperature & rainfall in January for San Francisco. ... Are you planning a holiday with hopefully nice weather in San Francisco in January 2024? Here you can find all information about the weather in San Francisco in January: ... 15. January ...'}]\")]}\n", + "----\n" + ] + } + ], + "source": [ + "inputs = {\"input\": \"what is the weather in sf\", \"chat_history\": []}\n", + "for s in app.stream(inputs):\n", + " print(list(s.values())[0])\n", + " print(\"----\")" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "c6a664cd-083e-4d85-aeaf-501463881f05", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "AgentFinish(return_values={'output': \"I couldn't find the current weather in San Francisco. However, you can check the weather on a reliable weather website or using a weather app for the most up-to-date information.\"}, log=\"I couldn't find the current weather in San Francisco. However, you can check the weather on a reliable weather website or using a weather app for the most up-to-date information.\")" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s['__end__']['agent_outcome']" + ] + }, + { + "cell_type": "markdown", + "id": "a7bd3e55-ee7e-4276-81bd-39e6131fcf77", + "metadata": {}, + "source": [ + "## Custom Input Schema\n", + "\n", + "By default, the `create_agent_executor` assumes that the input will be a dictionary with two keys: `input` and `chat_history`. \n", + "If this is not the case, you can easily customize the input schema.\n", + "You should do this, by defining a schema as a TypedDict.\n", + "\n", + "For this example, we will create a new agent that expects `question` and `language` as inputs." + ] + }, + { + "cell_type": "markdown", + "id": "a98c5ec5-f836-4b3c-b37b-00102b496366", + "metadata": {}, + "source": [ + "### Create New Agent" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "676841ec-b5a6-495e-a88a-7eb0ab3cbae6", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n", + "\n", + "prompt = ChatPromptTemplate.from_messages([\n", + " (\"human\", \"Respond to the user question: {question}. Answer in this language: {language}\"),\n", + " MessagesPlaceholder(variable_name=\"agent_scratchpad\")\n", + "])\n", + "agent_runnable = create_openai_functions_agent(llm, tools, prompt)" + ] + }, + { + "cell_type": "markdown", + "id": "5889d980-d209-447b-8489-1d4873acfdc2", + "metadata": {}, + "source": [ + "### Define Input Schema" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "3d1df06d-1564-46a1-a72f-58dfc65927bc", + "metadata": {}, + "outputs": [], + "source": [ + "from typing import TypedDict" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "2fdbb687-9c72-42c7-afcb-3f8940f3e5f4", + "metadata": {}, + "outputs": [], + "source": [ + "class InputSchema(TypedDict):\n", + " question: str\n", + " language: str" + ] + }, + { + "cell_type": "markdown", + "id": "329bb518-02a8-477c-8898-d04cb64fc460", + "metadata": {}, + "source": [ + "### Create new agent executor" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "1ad88990-896d-48d5-bd34-01c9f6a37734", + "metadata": {}, + "outputs": [], + "source": [ + "app = create_agent_executor(agent_runnable, tools, input_schema=InputSchema)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "13ffa18c-9a9f-4e0e-8298-32aeff94ce5d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'agent_outcome': AgentActionMessageLog(tool='tavily_search_results_json', tool_input={'query': 'che tempo fa a sf'}, log=\"\\nInvoking: `tavily_search_results_json` with `{'query': 'che tempo fa a sf'}`\\n\\n\\n\", message_log=[AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\"query\":\"che tempo fa a sf\"}', 'name': 'tavily_search_results_json'}})])}\n", + "----\n", + "{'intermediate_steps': [(AgentActionMessageLog(tool='tavily_search_results_json', tool_input={'query': 'che tempo fa a sf'}, log=\"\\nInvoking: `tavily_search_results_json` with `{'query': 'che tempo fa a sf'}`\\n\\n\\n\", message_log=[AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\"query\":\"che tempo fa a sf\"}', 'name': 'tavily_search_results_json'}})]), '[{\\'url\\': \\'https://www.mxbars.net/2024/01/14/san-francisco-supercross-2024-results-and-points-video/\\', \\'content\\': \"Scritto domenica 14 Gennaio 2024 alle 04:38. SAN FRANCISCO Oracle Park, CA January 13, 2024 sera gli orari sono anticipati di due ore causa mal tempo per non compromettere lo spettacolo! secondo weekend con la seconda tappa e pista molto tecnica per San Francisco dove sta piovendo e lo stadio aperto non . Commenta la gara\\\\xa0CLICCANDO\\\\xa0il link! http://forum.mxbars.net/viewtopic.php?f=18&t=50182SAN FRANCISCO. Oracle Park, CA. January 13, 2024. Ecco che la NUOVA stagione del Monster Energy Supercross 2024 continua, dopo Anaheim 1 si passa al secondo weekend con la seconda tappa e pista molto tecnica per San Francisco dove sta piovendo e lo stadio aperto non vede possibilità di chiudersi, per la lotta nella 450 dove sono tutti agguerriti e quest\\'anno il livello è ancora più alto ...\"}]')]}\n", + "----\n", + "{'agent_outcome': AgentFinish(return_values={'output': 'Al momento sta piovendo a San Francisco.'}, log='Al momento sta piovendo a San Francisco.')}\n", + "----\n", + "{'question': 'what is the weather in sf', 'language': 'italian', 'agent_outcome': AgentFinish(return_values={'output': 'Al momento sta piovendo a San Francisco.'}, log='Al momento sta piovendo a San Francisco.'), 'intermediate_steps': [(AgentActionMessageLog(tool='tavily_search_results_json', tool_input={'query': 'che tempo fa a sf'}, log=\"\\nInvoking: `tavily_search_results_json` with `{'query': 'che tempo fa a sf'}`\\n\\n\\n\", message_log=[AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\"query\":\"che tempo fa a sf\"}', 'name': 'tavily_search_results_json'}})]), '[{\\'url\\': \\'https://www.mxbars.net/2024/01/14/san-francisco-supercross-2024-results-and-points-video/\\', \\'content\\': \"Scritto domenica 14 Gennaio 2024 alle 04:38. SAN FRANCISCO Oracle Park, CA January 13, 2024 sera gli orari sono anticipati di due ore causa mal tempo per non compromettere lo spettacolo! secondo weekend con la seconda tappa e pista molto tecnica per San Francisco dove sta piovendo e lo stadio aperto non . Commenta la gara\\\\xa0CLICCANDO\\\\xa0il link! http://forum.mxbars.net/viewtopic.php?f=18&t=50182SAN FRANCISCO. Oracle Park, CA. January 13, 2024. Ecco che la NUOVA stagione del Monster Energy Supercross 2024 continua, dopo Anaheim 1 si passa al secondo weekend con la seconda tappa e pista molto tecnica per San Francisco dove sta piovendo e lo stadio aperto non vede possibilità di chiudersi, per la lotta nella 450 dove sono tutti agguerriti e quest\\'anno il livello è ancora più alto ...\"}]')]}\n", + "----\n" + ] + } + ], + "source": [ + "inputs = {\"question\": \"what is the weather in sf\", \"language\": \"italian\"}\n", + "for s in app.stream(inputs):\n", + " print(list(s.values())[0])\n", + " print(\"----\")" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "fd60f5d6-bd4b-4995-80dd-63f268c17cff", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "AgentFinish(return_values={'output': 'Il clima a San Francisco durante il mese di gennaio è generalmente fresco con temperature medie di circa 9.6°C (49.2°F) e massime di 14°C (57.3°F). Si consiglia di prepararsi a temperature fresche se si pianifica una visita a San Francisco in gennaio.'}, log='Il clima a San Francisco durante il mese di gennaio è generalmente fresco con temperature medie di circa 9.6°C (49.2°F) e massime di 14°C (57.3°F). Si consiglia di prepararsi a temperature fresche se si pianifica una visita a San Francisco in gennaio.')" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s['__end__']['agent_outcome']" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "20cac1a0-0c51-4cbd-ae27-929d71db2b56", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.1" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/examples/agent_executor/human-in-the-loop.ipynb b/examples/agent_executor/human-in-the-loop.ipynb new file mode 100644 index 000000000..3aa623330 --- /dev/null +++ b/examples/agent_executor/human-in-the-loop.ipynb @@ -0,0 +1,239 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "4499eb16-bca8-4a60-9a3a-2f34ae3f7078", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain import hub\n", + "from langchain.agents import create_openai_functions_agent\n", + "from langchain_openai.chat_models import ChatOpenAI\n", + "from langchain_community.tools.tavily_search import TavilySearchResults\n", + "\n", + "tools = [TavilySearchResults(max_results=1)]\n", + "\n", + "# Get the prompt to use - you can modify this!\n", + "prompt = hub.pull(\"hwchase17/openai-functions-agent\")\n", + "\n", + "# Choose the LLM that will drive the agent\n", + "llm = ChatOpenAI(model=\"gpt-3.5-turbo-1106\", streaming=True)\n", + "\n", + "# Construct the OpenAI Functions agent\n", + "agent_runnable = create_openai_functions_agent(llm, tools, prompt)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "c941fb10-dbe5-4d6a-ab7d-133d01c33cc4", + "metadata": {}, + "outputs": [], + "source": [ + "from typing import TypedDict, Annotated, List, Union\n", + "from langchain_core.agents import AgentAction, AgentFinish\n", + "from langchain_core.messages import BaseMessage\n", + "import operator\n", + "\n", + "\n", + "class AgentState(TypedDict):\n", + " # The input string\n", + " input: str\n", + " # The list of previous messages in the conversation\n", + " chat_history: list[BaseMessage]\n", + " # The outcome of a given call to the agent\n", + " # Needs `None` as a valid type, since this is what this will start as\n", + " agent_outcome: Union[AgentAction, AgentFinish, None]\n", + " # List of actions and corresponding observations\n", + " # Here we annotate this with `operator.add` to indicate that operations to\n", + " # this state should be ADDED to the existing values (not overwrite it)\n", + " intermediate_steps: Annotated[list[tuple[AgentAction, str]], operator.add]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "d61a970d-edf4-4eef-9678-28bab7c72331", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain_core.agents import AgentFinish\n", + "from langgraph.prebuilt.tool_executor import ToolExecutor\n", + "\n", + "# This a helper class we have that is useful for running tools\n", + "# It takes in an agent action and calls that tool and returns the result\n", + "tool_executor = ToolExecutor(tools)\n", + "\n", + "# Define the agent\n", + "def run_agent(data):\n", + " agent_outcome = agent_runnable.invoke(data)\n", + " return {\"agent_outcome\": agent_outcome}\n", + "\n", + "# Define the function to execute tools\n", + "def execute_tools(data):\n", + " # Get the most recent agent_outcome - this is the key added in the `agent` above\n", + " agent_action = data['agent_outcome']\n", + " response = input(prompt=f\"[y/n] continue with: {agent_action}?\")\n", + " if response == \"n\":\n", + " raise ValueError\n", + " output = tool_executor.invoke(agent_action)\n", + " return {\"intermediate_steps\": [(agent_action, str(output))]}\n", + "\n", + "# Define logic that will be used to determine which conditional edge to go down\n", + "def should_continue(data):\n", + " # If the agent outcome is an AgentFinish, then we return `exit` string\n", + " # This will be used when setting up the graph to define the flow\n", + " if isinstance(data['agent_outcome'], AgentFinish):\n", + " return \"end\"\n", + " # Otherwise, an AgentAction is returned\n", + " # Here we return `continue` string\n", + " # This will be used when setting up the graph to define the flow\n", + " else:\n", + " return \"continue\"" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "c4054dde-4618-49b7-998a-daa0c1d6d6c0", + "metadata": {}, + "outputs": [], + "source": [ + "from langgraph.graph import END, StateGraph\n", + "\n", + "# Define a new graph\n", + "workflow = StateGraph(AgentState)\n", + "\n", + "# Define the two nodes we will cycle between\n", + "workflow.add_node(\"agent\", run_agent)\n", + "workflow.add_node(\"action\", execute_tools)\n", + "\n", + "# Set the entrypoint as `agent`\n", + "# This means that this node is the first one called\n", + "workflow.set_entry_point(\"agent\")\n", + "\n", + "# We now add a conditional edge\n", + "workflow.add_conditional_edges(\n", + " # First, we define the start node. We use `agent`.\n", + " # This means these are the edges taken after the `agent` node is called.\n", + " \"agent\",\n", + " # Next, we pass in the function that will determine which node is called next.\n", + " should_continue,\n", + " # Finally we pass in a mapping.\n", + " # The keys are strings, and the values are other nodes.\n", + " # END is a special node marking that the graph should finish.\n", + " # What will happen is we will call `should_continue`, and then the output of that\n", + " # will be matched against the keys in this mapping.\n", + " # Based on which one it matches, that node will then be called.\n", + " {\n", + " # If `tools`, then we call the tool node.\n", + " \"continue\": \"action\",\n", + " # Otherwise we finish.\n", + " \"end\": END\n", + " }\n", + ")\n", + "\n", + "# We now add a normal edge from `tools` to `agent`.\n", + "# This means that after `tools` is called, `agent` node is called next.\n", + "workflow.add_edge('action', 'agent')\n", + "\n", + "# Finally, we compile it!\n", + "# This compiles it into a LangChain Runnable,\n", + "# meaning you can use it as you would any other runnable\n", + "chain = workflow.compile()" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "214ae46e-c297-465d-86db-2b0312ed3530", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Output from node 'agent':\n", + "---\n", + "{'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'}})])}\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=\"\\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'}})]? y\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Output from node 'action':\n", + "---\n", + "{'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.'}]\")]}\n", + "\n", + "---\n", + "\n", + "Output from node 'agent':\n", + "---\n", + "{'agent_outcome': AgentFinish(return_values={'output': \"It seems that I couldn't retrieve the current weather for 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 for San Francisco. However, you can easily check the current weather in San Francisco by using a weather website or app.\")}\n", + "\n", + "---\n", + "\n", + "Output from node '__end__':\n", + "---\n", + "{'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 for 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 for 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.'}]\")]}\n", + "\n", + "---\n", + "\n" + ] + } + ], + "source": [ + "for output in chain.stream(\n", + " {\"input\": \"what is the weather in sf\", \"chat_history\": []}\n", + "):\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": "2eb662bc-de7d-4a57-a3e8-2f00dcf4ff8b", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.1" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/examples/agent_executor/managing-agent-steps.ipynb b/examples/agent_executor/managing-agent-steps.ipynb new file mode 100644 index 000000000..7d7ffa498 --- /dev/null +++ b/examples/agent_executor/managing-agent-steps.ipynb @@ -0,0 +1,226 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "4499eb16-bca8-4a60-9a3a-2f34ae3f7078", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain import hub\n", + "from langchain.agents import create_openai_functions_agent\n", + "from langchain_openai.chat_models import ChatOpenAI\n", + "from langchain_community.tools.tavily_search import TavilySearchResults\n", + "\n", + "tools = [TavilySearchResults(max_results=1)]\n", + "\n", + "# Get the prompt to use - you can modify this!\n", + "prompt = hub.pull(\"hwchase17/openai-functions-agent\")\n", + "\n", + "# Choose the LLM that will drive the agent\n", + "llm = ChatOpenAI(model=\"gpt-3.5-turbo-1106\", streaming=True)\n", + "\n", + "# Construct the OpenAI Functions agent\n", + "agent_runnable = create_openai_functions_agent(llm, tools, prompt)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "c941fb10-dbe5-4d6a-ab7d-133d01c33cc4", + "metadata": {}, + "outputs": [], + "source": [ + "from typing import TypedDict, Annotated, List, Union\n", + "from langchain_core.agents import AgentAction, AgentFinish\n", + "from langchain_core.messages import BaseMessage\n", + "import operator\n", + "\n", + "\n", + "class AgentState(TypedDict):\n", + " # The input string\n", + " input: str\n", + " # The list of previous messages in the conversation\n", + " chat_history: list[BaseMessage]\n", + " # The outcome of a given call to the agent\n", + " # Needs `None` as a valid type, since this is what this will start as\n", + " agent_outcome: Union[AgentAction, AgentFinish, None]\n", + " # List of actions and corresponding observations\n", + " # Here we annotate this with `operator.add` to indicate that operations to\n", + " # this state should be ADDED to the existing values (not overwrite it)\n", + " intermediate_steps: Annotated[list[tuple[AgentAction, str]], operator.add]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "d61a970d-edf4-4eef-9678-28bab7c72331", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain_core.agents import AgentFinish\n", + "from langgraph.prebuilt.tool_executor import ToolExecutor\n", + "\n", + "# This a helper class we have that is useful for running tools\n", + "# It takes in an agent action and calls that tool and returns the result\n", + "tool_executor = ToolExecutor(tools)\n", + "\n", + "# Define the agent\n", + "def run_agent(data):\n", + " inputs = data.copy()\n", + " if len(inputs['intermediate_steps']) > 5:\n", + " inputs['intermediate_steps'] = inputs['intermediate_steps'][-5:]\n", + " agent_outcome = agent_runnable.invoke(inputs)\n", + " return {\"agent_outcome\": agent_outcome}\n", + "\n", + "# Define the function to execute tools\n", + "def execute_tools(data):\n", + " # Get the most recent agent_outcome - this is the key added in the `agent` above\n", + " agent_action = data['agent_outcome']\n", + " output = tool_executor.invoke(agent_action)\n", + " return {\"intermediate_steps\": [(agent_action, str(output))]}\n", + "\n", + "# Define logic that will be used to determine which conditional edge to go down\n", + "def should_continue(data):\n", + " # If the agent outcome is an AgentFinish, then we return `exit` string\n", + " # This will be used when setting up the graph to define the flow\n", + " if isinstance(data['agent_outcome'], AgentFinish):\n", + " return \"end\"\n", + " # Otherwise, an AgentAction is returned\n", + " # Here we return `continue` string\n", + " # This will be used when setting up the graph to define the flow\n", + " else:\n", + " return \"continue\"" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "c4054dde-4618-49b7-998a-daa0c1d6d6c0", + "metadata": {}, + "outputs": [], + "source": [ + "from langgraph.graph import END, StateGraph\n", + "\n", + "# Define a new graph\n", + "workflow = StateGraph(AgentState)\n", + "\n", + "# Define the two nodes we will cycle between\n", + "workflow.add_node(\"agent\", run_agent)\n", + "workflow.add_node(\"action\", execute_tools)\n", + "\n", + "# Set the entrypoint as `agent`\n", + "# This means that this node is the first one called\n", + "workflow.set_entry_point(\"agent\")\n", + "\n", + "# We now add a conditional edge\n", + "workflow.add_conditional_edges(\n", + " # First, we define the start node. We use `agent`.\n", + " # This means these are the edges taken after the `agent` node is called.\n", + " \"agent\",\n", + " # Next, we pass in the function that will determine which node is called next.\n", + " should_continue,\n", + " # Finally we pass in a mapping.\n", + " # The keys are strings, and the values are other nodes.\n", + " # END is a special node marking that the graph should finish.\n", + " # What will happen is we will call `should_continue`, and then the output of that\n", + " # will be matched against the keys in this mapping.\n", + " # Based on which one it matches, that node will then be called.\n", + " {\n", + " # If `tools`, then we call the tool node.\n", + " \"continue\": \"action\",\n", + " # Otherwise we finish.\n", + " \"end\": END\n", + " }\n", + ")\n", + "\n", + "# We now add a normal edge from `tools` to `agent`.\n", + "# This means that after `tools` is called, `agent` node is called next.\n", + "workflow.add_edge('action', 'agent')\n", + "\n", + "# Finally, we compile it!\n", + "# This compiles it into a LangChain Runnable,\n", + "# meaning you can use it as you would any other runnable\n", + "chain = workflow.compile()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "214ae46e-c297-465d-86db-2b0312ed3530", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Output from node 'agent':\n", + "---\n", + "{'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'}})])}\n", + "\n", + "---\n", + "\n", + "Output from node 'action':\n", + "---\n", + "{'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.'}]\")]}\n", + "\n", + "---\n", + "\n", + "Output from node 'agent':\n", + "---\n", + "{'agent_outcome': AgentFinish(return_values={'output': \"I'm sorry, I couldn't find the current weather for San Francisco. If you'd like, I can search for the current weather using a different method.\"}, log=\"I'm sorry, I couldn't find the current weather for San Francisco. If you'd like, I can search for the current weather using a different method.\")}\n", + "\n", + "---\n", + "\n", + "Output from node '__end__':\n", + "---\n", + "{'input': 'what is the weather in sf', 'chat_history': [], 'agent_outcome': AgentFinish(return_values={'output': \"I'm sorry, I couldn't find the current weather for San Francisco. If you'd like, I can search for the current weather using a different method.\"}, log=\"I'm sorry, I couldn't find the current weather for San Francisco. If you'd like, I can search for the current weather using a different method.\"), '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.'}]\")]}\n", + "\n", + "---\n", + "\n" + ] + } + ], + "source": [ + "for output in chain.stream(\n", + " {\"input\": \"what is the weather in sf\", \"chat_history\": []}\n", + "):\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": "2eb662bc-de7d-4a57-a3e8-2f00dcf4ff8b", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.1" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/examples/messages_executor_how_to/base.ipynb b/examples/chat_executor_with_function_calling/base.ipynb similarity index 100% rename from examples/messages_executor_how_to/base.ipynb rename to examples/chat_executor_with_function_calling/base.ipynb diff --git a/examples/messages_executor_how_to/dynamically_returning_directly.ipynb b/examples/chat_executor_with_function_calling/dynamically-returning-directly.ipynb similarity index 100% rename from examples/messages_executor_how_to/dynamically_returning_directly.ipynb rename to examples/chat_executor_with_function_calling/dynamically-returning-directly.ipynb diff --git a/examples/messages_executor_how_to/force-calling-a-tool-first.ipynb b/examples/chat_executor_with_function_calling/force-calling-a-tool-first.ipynb similarity index 100% rename from examples/messages_executor_how_to/force-calling-a-tool-first.ipynb rename to examples/chat_executor_with_function_calling/force-calling-a-tool-first.ipynb diff --git a/examples/messages_executor.ipynb b/examples/chat_executor_with_function_calling/high-level.ipynb similarity index 67% rename from examples/messages_executor.ipynb rename to examples/chat_executor_with_function_calling/high-level.ipynb index e0e145a98..9eed3d07c 100644 --- a/examples/messages_executor.ipynb +++ b/examples/chat_executor_with_function_calling/high-level.ipynb @@ -1,5 +1,28 @@ { "cells": [ + { + "cell_type": "markdown", + "id": "8bcd1a3d-7c50-4f58-be4e-1ed654aa33be", + "metadata": {}, + "source": [ + "# Chat Executor: with function calling\n", + "\n", + "This notebook walks through an example creating a chat executor that uses function calling.\n", + "This is useful for getting started quickly.\n", + "However, it is highly likely you will want to customize the logic - for information on that, check out the other examples in this folder." + ] + }, + { + "cell_type": "markdown", + "id": "e130cf70-a30e-47d7-8fd5-464f1a92e374", + "metadata": {}, + "source": [ + "## Set up the chat model and tools\n", + "\n", + "Here we will define the chat model and tools that we want to use.\n", + "Importantly, this model MUST support OpenAI function calling." + ] + }, { "cell_type": "code", "execution_count": 1, @@ -7,10 +30,9 @@ "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 langgraph.prebuilt import chat_executor\n", "from langchain_core.messages import HumanMessage" ] }, @@ -25,6 +47,16 @@ "model = ChatOpenAI()" ] }, + { + "cell_type": "markdown", + "id": "43064805-2ac9-4b5a-850c-a68dd7282350", + "metadata": {}, + "source": [ + "## Create executor\n", + "\n", + "We can now use the high level interface to create the executor" + ] + }, { "cell_type": "code", "execution_count": 3, @@ -32,7 +64,15 @@ "metadata": {}, "outputs": [], "source": [ - "app = create_messages_executor(model, tools)" + "app = chat_executor.create_function_calling_executor(model, tools)" + ] + }, + { + "cell_type": "markdown", + "id": "d63dbfc7-a5c1-4a03-991c-f0789ba52c52", + "metadata": {}, + "source": [ + "We can now invoke this executor. The input to this must be a dictionary with a single `messsages` key that contains a list of messages." ] }, { @@ -49,9 +89,9 @@ "----\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", - "{'messages': [AIMessage(content=\"I apologize, but I couldn't retrieve the current weather information for San Francisco. However, you can check the weather history for January 2024 in San Francisco on this website: [San Francisco Weather History](https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States).\")]}\n", + "{'messages': [AIMessage(content=\"I couldn't find the current weather in San Francisco. However, you can check historical weather data for 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 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 apologize, but I couldn't retrieve the current weather information for San Francisco. However, you can check the weather history for January 2024 in San Francisco on this website: [San Francisco Weather History](https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States).\")]}\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 couldn't find the current weather in San Francisco. However, you can check historical weather data for January 2024 [here](https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States).\")]}\n", "----\n" ] } diff --git a/examples/messages_executor_how_to/human-in-the-loop.ipynb b/examples/chat_executor_with_function_calling/human-in-the-loop.ipynb similarity index 100% rename from examples/messages_executor_how_to/human-in-the-loop.ipynb rename to examples/chat_executor_with_function_calling/human-in-the-loop.ipynb diff --git a/examples/messages_executor_how_to/managing-agent-steps.ipynb b/examples/chat_executor_with_function_calling/managing-agent-steps.ipynb similarity index 100% rename from examples/messages_executor_how_to/managing-agent-steps.ipynb rename to examples/chat_executor_with_function_calling/managing-agent-steps.ipynb diff --git a/examples/messages_executor_how_to/respond-in-format.ipynb b/examples/chat_executor_with_function_calling/respond-in-format.ipynb similarity index 100% rename from examples/messages_executor_how_to/respond-in-format.ipynb rename to examples/chat_executor_with_function_calling/respond-in-format.ipynb diff --git a/examples/combine_docs.ipynb b/examples/combine_docs.ipynb deleted file mode 100644 index d981c2444..000000000 --- a/examples/combine_docs.ipynb +++ /dev/null @@ -1,371 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "780c1001-557c-4b03-8ebd-a2a381d5f85d", - "metadata": {}, - "source": [ - "# Combine Docs\n", - "\n", - "LangGraph is a great choice for implementating workflows that involve operating over longer documents because of its recursive nature" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "id": "624c452c-ddd5-4390-9065-7ec55dc64b96", - "metadata": {}, - "outputs": [], - "source": [ - "from langchain.chat_models.openai import ChatOpenAI\n", - "from langchain.prompts import ChatPromptTemplate, PromptTemplate\n", - "from langchain.schema.output_parser import StrOutputParser\n", - "from langchain.schema.runnable import Runnable\n", - "from langchain.schema.output_parser import StrOutputParser\n", - "from langchain.schema.document import Document\n", - "from langchain.schema import format_document\n", - "\n", - "from langgraph.pregel import Channel, Pregel\n", - "from langgraph.channels import Topic" - ] - }, - { - "cell_type": "markdown", - "id": "271728d7-b3c8-4ec6-a728-19835e282ec3", - "metadata": {}, - "source": [ - "## Stuff Documents\n", - "\n", - "Stuff documents is simple - just a chain" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "0462aff0-1b88-49cc-bfe2-3c169d5e1d63", - "metadata": {}, - "outputs": [], - "source": [ - "from langchain.schema.runnable import RunnableLambda" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "59d6430b-c113-4498-9ffc-f4623f7a0b5c", - "metadata": {}, - "outputs": [], - "source": [ - "DEFAULT_DOCUMENT_PROMPT = PromptTemplate.from_template(template=\"{page_content}\")\n", - "\n", - "_combine_documents = RunnableLambda(\n", - " lambda x: format_document(x, DEFAULT_DOCUMENT_PROMPT)\n", - ").map() | (lambda x: \"\\n\\n\".join(x))" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "29b2668d-e4a6-4876-9b04-bdc841774c62", - "metadata": {}, - "outputs": [], - "source": [ - "docs = [\n", - " Document(page_content=\"Harrison used to work at Kensho\"),\n", - " Document(page_content=\"Ankush worked at Facebook\"),\n", - "]" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "id": "17da58b7-8685-4d0a-9a47-c398c085d477", - "metadata": {}, - "outputs": [], - "source": [ - "stuff_chain = (\n", - " {\n", - " \"question\": lambda x: x[\"question\"],\n", - " \"context\": (lambda x: x[\"docs\"]) | _combine_documents,\n", - " }\n", - " | ChatPromptTemplate.from_messages(\n", - " [\n", - " (\n", - " \"system\",\n", - " \"Answer user questions based on the following documents:\\n\\n{context}\",\n", - " ),\n", - " (\"human\", \"{question}\"),\n", - " ]\n", - " )\n", - " | ChatOpenAI()\n", - " | StrOutputParser()\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "id": "87295b71-0afc-4901-b57c-a7b945aa4bd9", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Harrison used to work at Kensho.'" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "stuff_chain.invoke({\"question\": \"where did harrison work\", \"docs\": docs})" - ] - }, - { - "cell_type": "markdown", - "id": "fff324c1-7fbf-41e5-861f-a10ba0112dbd", - "metadata": {}, - "source": [ - "## Reduce Documents\n", - "\n", - "Reduce documents tries to merge documents recursively." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "id": "b15f5abb-1cfe-4965-a021-c891506c5dd2", - "metadata": {}, - "outputs": [], - "source": [ - "many_docs = docs * 5" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "id": "ccad04a3-fd3f-4e73-b895-29e53535f000", - "metadata": {}, - "outputs": [], - "source": [ - "def _split_list_of_docs(docs, max_length=70):\n", - " new_result_doc_list = []\n", - " _sub_result_docs = []\n", - " for doc in docs:\n", - " _sub_result_docs.append(doc)\n", - " _num_tokens = sum([len(d.page_content) for d in _sub_result_docs])\n", - " if _num_tokens > max_length:\n", - " if len(_sub_result_docs) == 1:\n", - " raise ValueError(\n", - " \"A single document was longer than the context length,\"\n", - " \" we cannot handle this.\"\n", - " )\n", - " new_result_doc_list.append(_sub_result_docs[:-1])\n", - " _sub_result_docs = _sub_result_docs[-1:]\n", - " new_result_doc_list.append(_sub_result_docs)\n", - " return new_result_doc_list" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "id": "11cfd337-9f3b-4b26-ba30-251e17b18994", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "[[Document(page_content='Harrison used to work at Kensho'),\n", - " Document(page_content='Ankush worked at Facebook')],\n", - " [Document(page_content='Harrison used to work at Kensho'),\n", - " Document(page_content='Ankush worked at Facebook')],\n", - " [Document(page_content='Harrison used to work at Kensho'),\n", - " Document(page_content='Ankush worked at Facebook')],\n", - " [Document(page_content='Harrison used to work at Kensho'),\n", - " Document(page_content='Ankush worked at Facebook')],\n", - " [Document(page_content='Harrison used to work at Kensho'),\n", - " Document(page_content='Ankush worked at Facebook')]]" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Just to show what its like split\n", - "split_docs = _split_list_of_docs(many_docs)\n", - "split_docs" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "id": "8d524ba6-0939-4a5d-8db0-4fa1ef06eaeb", - "metadata": {}, - "outputs": [], - "source": [ - "channels = {\n", - " # input\n", - " \"docs\": Topic(Document),\n", - " # intermediate\n", - " \"docs_to_finalize\": Topic(Document),\n", - "}" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "id": "67370694-86f4-4b64-9d4f-38b2e306abeb", - "metadata": {}, - "outputs": [], - "source": [ - "def decide(docs: list[Document]) -> Runnable:\n", - " if len(_split_list_of_docs(docs)) > 1:\n", - " # send back to the beginning if we still need to collapse more\n", - " return Channel.write_to(\"docs\")\n", - " else:\n", - " # send to the finalizer if we're ready to produce final answer\n", - " return Channel.write_to(\"docs_to_finalize\")\n", - "\n", - "\n", - "def split_docs_with_question(input: dict[str, str | list[Document]]) -> list[dict[str, str | list[Document]]]:\n", - " return [\n", - " {\"docs\": docs, \"question\": input[\"question\"]}\n", - " for docs in _split_list_of_docs(input[\"docs\"])\n", - " ]\n", - "\n", - "\n", - "collapse = (\n", - " Channel.subscribe_to([\"docs\", \"question\"])\n", - " | split_docs_with_question\n", - " | stuff_chain.map() # Collapse each list of docs to a single string\n", - " | (lambda x: [Document(page_content=s) for s in x]) # A new (smaller) list of docs\n", - " | decide\n", - ")\n", - "\n", - "# Convert final set of docs to an answer\n", - "finalize = (\n", - " Channel.subscribe_to(\"docs_to_finalize\", key=\"docs\").join([\"question\"])\n", - " | stuff_chain\n", - " | Channel.write_to(\"answer\")\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "id": "3019e7d2-ab7f-4868-b43c-ad898d824a26", - "metadata": {}, - "outputs": [], - "source": [ - "reduce_chain = Pregel(\n", - " chains={\n", - " \"collapse\": collapse,\n", - " \"finalize\": finalize,\n", - " },\n", - " channels=channels,\n", - " input=[\"question\", \"docs\"],\n", - " output=\"answer\",\n", - " debug=True,\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "id": "69fcb829-3dae-432a-8db3-11bbb179a7d2", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\u001b[36;1m\u001b[1;3m[pregel/step]\u001b[0m \u001b[1mStarting step 0 with 1 task. Next tasks:\n", - "\u001b[0m- collapse({'docs': [Document(page_content='Harrison used to work at Kensho'),\n", - " Document(page_content='Ankush worked at Facebook'),\n", - " Document(page_content='Harrison used to work at Kensho'),\n", - " Document(page_content='Ankush worked at Facebook'),\n", - " Document(page_content='Harrison used to work at Kensho'),\n", - " Document(page_content='Ankush worked at Facebook'),\n", - " Document(page_content='Harrison used to work at Kensho'),\n", - " Document(page_content='Ankush worked at Facebook'),\n", - " Document(page_content='Harrison used to work at Kensho'),\n", - " Document(page_content='Ankush worked at Facebook')],\n", - " 'question': 'where did harrison work'})\n", - "\u001b[36;1m\u001b[1;3m[pregel/checkpoint]\u001b[0m \u001b[1mFinishing step 0. Channel values:\n", - "\u001b[0m{'docs': [...], 'docs_to_finalize': [], 'question': 'where did harrison work'}\n", - "\u001b[36;1m\u001b[1;3m[pregel/step]\u001b[0m \u001b[1mStarting step 1 with 1 task. Next tasks:\n", - "\u001b[0m- collapse({'docs': [Document(page_content='Harrison used to work at Kensho.'),\n", - " Document(page_content='Harrison used to work at Kensho.'),\n", - " Document(page_content='Harrison used to work at Kensho.'),\n", - " Document(page_content='Harrison used to work at Kensho.'),\n", - " Document(page_content='Harrison used to work at Kensho.')],\n", - " 'question': 'where did harrison work'})\n", - "\u001b[36;1m\u001b[1;3m[pregel/checkpoint]\u001b[0m \u001b[1mFinishing step 1. Channel values:\n", - "\u001b[0m{'docs': [...], 'docs_to_finalize': [], 'question': 'where did harrison work'}\n", - "\u001b[36;1m\u001b[1;3m[pregel/step]\u001b[0m \u001b[1mStarting step 2 with 1 task. Next tasks:\n", - "\u001b[0m- collapse({'docs': [Document(page_content='Harrison used to work at Kensho.'),\n", - " Document(page_content='Harrison used to work at Kensho.'),\n", - " Document(page_content='Harrison used to work at Kensho.')],\n", - " 'question': 'where did harrison work'})\n", - "\u001b[36;1m\u001b[1;3m[pregel/checkpoint]\u001b[0m \u001b[1mFinishing step 2. Channel values:\n", - "\u001b[0m{'docs': [], 'docs_to_finalize': [...], 'question': 'where did harrison work'}\n", - "\u001b[36;1m\u001b[1;3m[pregel/step]\u001b[0m \u001b[1mStarting step 3 with 1 task. Next tasks:\n", - "\u001b[0m- finalize({'docs': [Document(page_content='Harrison used to work at Kensho.'),\n", - " Document(page_content='Harrison used to work at Kensho.')]})\n", - "\u001b[36;1m\u001b[1;3m[pregel/checkpoint]\u001b[0m \u001b[1mFinishing step 3. Channel values:\n", - "\u001b[0m{'answer': 'Harrison used to work at Kensho.',\n", - " 'docs': [],\n", - " 'docs_to_finalize': [],\n", - " 'question': 'where did harrison work'}\n" - ] - }, - { - "data": { - "text/plain": [ - "'Harrison used to work at Kensho.'" - ] - }, - "execution_count": 25, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "reduce_chain.invoke({\"question\": \"where did harrison work\", \"docs\": many_docs})" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "265b29cd-d4f4-4e48-8d4e-b759e909ac2e", - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.11.5" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/examples/draft-revise-loop.py b/examples/draft-revise-loop.py deleted file mode 100644 index fae95a14e..000000000 --- a/examples/draft-revise-loop.py +++ /dev/null @@ -1,116 +0,0 @@ -from __future__ import annotations - -from langchain.chat_models.openai import ChatOpenAI -from langchain.output_parsers.openai_functions import JsonOutputFunctionsParser -from langchain_core.output_parsers import StrOutputParser -from langchain_core.prompts import SystemMessagePromptTemplate - -from langgraph.pregel import Channel, Pregel - -# prompts - -drafter_prompt = ( - SystemMessagePromptTemplate.from_template( - "You are an expert on turtles, who likes to write in pirate-speak. You have been tasked by your editor with drafting a 100-word article answering the following question." - ) - + "Question:\n\n{question}" -) - -reviser_prompt = ( - SystemMessagePromptTemplate.from_template( - "You are an expert on turtles. You have been tasked by your editor with revising the following draft, which was written by a non-expert. You may follow the editor's notes or not, as you see fit." - ) - + "Draft:\n\n{draft}" - + "Editor's notes:\n\n{notes}" -) - -editor_prompt = ( - SystemMessagePromptTemplate.from_template( - "You are an editor. You have been tasked with editing the following draft, which was written by a non-expert. Please accept the draft if it is good enough to publish, or send it for revision, along with your notes to guide the revision." - ) - + "Draft:\n\n{draft}" -) - -editor_functions = [ - { - "name": "revise", - "description": "Sends the draft for revision", - "parameters": { - "type": "object", - "properties": { - "notes": { - "type": "string", - "description": "The editor's notes to guide the revision.", - }, - }, - }, - }, - { - "name": "accept", - "description": "Accepts the draft", - "parameters": { - "type": "object", - "properties": {"ready": {"const": True}}, - }, - }, -] - -# llms - -gpt3 = ChatOpenAI(model="gpt-3.5-turbo") -gpt4 = ChatOpenAI(model="gpt-4") - -# chains - -drafter_chain = drafter_prompt | gpt3 | StrOutputParser() - -editor_chain = ( - editor_prompt - | gpt4.bind(functions=editor_functions) - | JsonOutputFunctionsParser(args_only=False) -) - -reviser_chain = reviser_prompt | gpt3 | StrOutputParser() - -# application - -drafter = ( - # subscribe to question channel as a dict with a single key, "question" - Channel.subscribe_to(["question"]) | drafter_chain | Channel.write_to("draft") -) - -editor = ( - # subscribe to draft channel as a dict with a single key, "draft" - Channel.subscribe_to(["draft"]) - | editor_chain - | Channel.write_to( - # send to "notes" channel if the editor does not accept the draft - notes=lambda x: x["arguments"]["notes"] if x["name"] == "revise" else None - ) -) - -reviser = ( - # subscribe to new values of "notes" channel, - # and join them with the input value (question) and "draft" - Channel.subscribe_to(["notes"]).join(["question", "draft"]) - | reviser_chain - | Channel.write_to("draft") -) - -draft_revise_loop = Pregel( - chains={ - "drafter": drafter, - "editor": editor, - "reviser": reviser, - }, - # input will be a dict with a single key, "question" - input=["question"], - # output will be the value of "draft" - output="draft", - # debug logging - debug=True, -) - -# run - -print(draft_revise_loop.invoke({"question": "What food do turtles eat?"})) diff --git a/examples/langgraph.ipynb b/examples/langgraph.ipynb deleted file mode 100644 index cc558d276..000000000 --- a/examples/langgraph.ipynb +++ /dev/null @@ -1,1241 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "396e20d9-8684-40ea-a46a-e3dfa36ed5a6", - "metadata": {}, - "source": [ - "## Existing Agent Executor" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "id": "d642e6af-217a-4414-a78c-509b44155eca", - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/Users/harrisonchase/workplace/langchain/libs/core/langchain_core/_api/deprecation.py:117: LangChainDeprecationWarning: The class `langchain_community.chat_models.openai.ChatOpenAI` was deprecated in langchain-community 0.0.10 and will be removed in 0.2.0. An updated version of the class exists in the langchain-openai package and should be used instead. To use it run `pip install -U langchain-openai` and import as `from langchain_openai import ChatOpenAI`.\n", - " warn_deprecated(\n" - ] - } - ], - "source": [ - "from langchain import hub\n", - "from langchain.agents import create_openai_functions_agent\n", - "from langchain.chat_models import ChatOpenAI\n", - "from langchain_community.chat_models import ChatOpenAI\n", - "from langchain_community.tools.tavily_search import TavilySearchResults\n", - "from langchain_core.runnables import RunnablePassthrough\n", - "from langgraph.prebuilt.tool_executor import ToolExecutor\n", - "from langgraph.prebuilt.agent_executor import create_agent_executor\n", - "\n", - "from langgraph.graph import END, Graph\n", - "\n", - "tools = [TavilySearchResults(max_results=1)]\n", - "\n", - "# Get the prompt to use - you can modify this!\n", - "prompt = hub.pull(\"hwchase17/openai-functions-agent\")\n", - "\n", - "# Choose the LLM that will drive the agent\n", - "llm = ChatOpenAI(model=\"gpt-3.5-turbo-1106\")\n", - "\n", - "# Construct the OpenAI Functions agent\n", - "agent_runnable = create_openai_functions_agent(llm, tools, prompt)\n", - "tool_executor = ToolExecutor(tools)\n", - "chain = create_agent_executor(agent_runnable, tool_executor)" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "c46bd262-9605-4449-9391-f6b6e0fe440e", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'input': 'what is the weather in sf',\n", - " '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': {'name': 'tavily_search_results_json', 'arguments': '{\"query\":\"weather in San Francisco\"}'}})]),\n", - " [{'url': 'https://www.whereandwhen.net/when/north-america/california/san-francisco-ca/january/',\n", - " '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 8% 46% 29% 12% 8% Evolution of daily average temperature and precipitation in San Francisco in januaryWeather 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. ... 12-01-2023 50°F to 59°F. 13-01-2023 54°F to 58°F. 14-01-2023 54°F to ...'}])],\n", - " 'agent_outcome': AgentFinish(return_values={'output': 'I found information about the weather in San Francisco in January 2024. It looks like the weather statistics for January 2023 are available, showing the average temperatures and precipitation for each day. If you need current weather information, I can help you find a reliable source for that.'}, log='I found information about the weather in San Francisco in January 2024. It looks like the weather statistics for January 2023 are available, showing the average temperatures and precipitation for each day. If you need current weather information, I can help you find a reliable source for that.')}" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "chain.invoke({\"input\": \"what is the weather in sf\"})" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "dff32d55-f8b3-45fd-8de7-daa973aaa20b", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "2d518968-2c61-4f4b-a2ae-8f8a545a0e7b", - "metadata": {}, - "source": [ - "## Agent Messages Executor" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "4c468b87-3ff4-4d61-87bb-4fe0b61bca13", - "metadata": {}, - "outputs": [], - "source": [ - "from langchain_core.agents import AgentAction\n", - "from langchain_core.messages import FunctionMessage, HumanMessage, SystemMessage\n", - "from langchain.tools.render import format_tool_to_openai_function\n", - "import json\n", - "from langchain.chat_models import ChatOpenAI\n", - "\n", - "from langchain_community.tools.tavily_search import TavilySearchResults\n", - "\n", - "from langgraph.prebuilt.tool_executor import ToolExecutor\n", - "from langgraph.prebuilt.agent_messages_executor import create_agent_messages_executor\n", - "tools = [TavilySearchResults(max_results=1)]\n", - "model = ChatOpenAI().bind_functions([format_tool_to_openai_function(t) for t in tools])" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "659351eb-194c-4cc0-8f8b-ebbcc753ee38", - "metadata": {}, - "outputs": [], - "source": [ - "def call_model(messages):\n", - " response = model.invoke(messages)\n", - " return messages + [response]\n", - "\n", - "\n", - "def exit(messages):\n", - " last_message = messages[-1]\n", - " if \"function_call\" not in last_message.additional_kwargs:\n", - " return \"end\"\n", - " else:\n", - " return \"function\"\n", - "\n", - "tool_executor = ToolExecutor(tools)\n", - "def call_tool(messages):\n", - " last_message = messages[-1]\n", - " action = AgentAction(\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 = tool_executor.execute(action)\n", - " function_message = FunctionMessage(content=str(response[0][1]), name=action.tool)\n", - " return messages + [function_message]" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "id": "b78d8ba1-a428-4022-b5fc-cfb0744d2ac1", - "metadata": {}, - "outputs": [], - "source": [ - "chain = create_agent_messages_executor(call_model, call_tool, exit)" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "id": "e6f7d494-d922-4f9b-8499-36b179917522", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[HumanMessage(content='what is the weather in sf'), AIMessage(content='', additional_kwargs={'function_call': {'name': 'tavily_search_results_json', 'arguments': '{\\n \"query\": \"weather in San Francisco\"\\n}'}})]\n", - "----\n", - "[HumanMessage(content='what is the weather in sf'), AIMessage(content='', additional_kwargs={'function_call': {'name': 'tavily_search_results_json', 'arguments': '{\\n \"query\": \"weather in San Francisco\"\\n}'}}), 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 8% 46% 29% 12% 8% Evolution of daily average temperature and precipitation in San Francisco in januaryWeather 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. ... 12-01-2023 50°F to 59°F. 13-01-2023 54°F to 58°F. 14-01-2023 54°F to ...'}]\", name='tavily_search_results_json')]\n", - "----\n", - "[HumanMessage(content='what is the weather in sf'), AIMessage(content='', additional_kwargs={'function_call': {'name': 'tavily_search_results_json', 'arguments': '{\\n \"query\": \"weather in San Francisco\"\\n}'}}), 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 8% 46% 29% 12% 8% Evolution of daily average temperature and precipitation in San Francisco in januaryWeather 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. ... 12-01-2023 50°F to 59°F. 13-01-2023 54°F to 58°F. 14-01-2023 54°F to ...'}]\", name='tavily_search_results_json'), AIMessage(content=\"I apologize, but I couldn't find the current weather information for San Francisco. However, you can check the weather in San Francisco for January 2024 on this website: [San Francisco Weather in January](https://www.whereandwhen.net/when/north-america/california/san-francisco-ca/january/).\")]\n", - "----\n", - "[HumanMessage(content='what is the weather in sf'), AIMessage(content='', additional_kwargs={'function_call': {'name': 'tavily_search_results_json', 'arguments': '{\\n \"query\": \"weather in San Francisco\"\\n}'}}), 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 8% 46% 29% 12% 8% Evolution of daily average temperature and precipitation in San Francisco in januaryWeather 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. ... 12-01-2023 50°F to 59°F. 13-01-2023 54°F to 58°F. 14-01-2023 54°F to ...'}]\", name='tavily_search_results_json'), AIMessage(content=\"I apologize, but I couldn't find the current weather information for San Francisco. However, you can check the weather in San Francisco for January 2024 on this website: [San Francisco Weather in January](https://www.whereandwhen.net/when/north-america/california/san-francisco-ca/january/).\")]\n", - "----\n" - ] - } - ], - "source": [ - "messages = [HumanMessage(content=\"what is the weather in sf\")]\n", - "for s in chain.stream(messages):\n", - " print(list(s.values())[0])\n", - " print(\"----\")" - ] - }, - { - "cell_type": "markdown", - "id": "479f258a-6b61-42a5-85bb-c1c141f6d1fb", - "metadata": {}, - "source": [ - "### Human in the Loop\n", - "\n", - "#### Require confirmation" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "id": "b7e94bc5-38c0-403c-8902-91eafd7e0c92", - "metadata": {}, - "outputs": [], - "source": [ - "def call_tool(messages):\n", - " last_message = messages[-1]\n", - " action = AgentAction(\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] Okay to call this tool? {action}\")\n", - " if response == \"n\":\n", - " raise ValueError\n", - " response = tool_executor.execute(action)\n", - " function_message = FunctionMessage(content=str(response[0][1]), name=action.tool)\n", - " return messages + [function_message]" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "id": "e112af6b-6339-4a54-bf90-dcc0a3b3f7ed", - "metadata": {}, - "outputs": [], - "source": [ - "chain = create_agent_messages_executor(call_model, call_tool, exit)" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "id": "b3ad0780-8f1f-4ddd-9472-6f6400f0dcee", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[HumanMessage(content='what is the weather in sf'), AIMessage(content='', additional_kwargs={'function_call': {'name': 'tavily_search_results_json', 'arguments': '{\\n \"query\": \"weather in San Francisco\"\\n}'}})]\n", - "----\n" - ] - }, - { - "name": "stdin", - "output_type": "stream", - "text": [ - "[y/n] Okay to call this tool? tool='tavily_search_results_json' tool_input={'query': 'weather in San Francisco'} log='' \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[HumanMessage(content='what is the weather in sf'), AIMessage(content='', additional_kwargs={'function_call': {'name': 'tavily_search_results_json', 'arguments': '{\\n \"query\": \"weather in San Francisco\"\\n}'}}), 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 8% 46% 29% 12% 8% Evolution of daily average temperature and precipitation in San Francisco in januaryWeather 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. ... 12-01-2023 50°F to 59°F. 13-01-2023 54°F to 58°F. 14-01-2023 54°F to ...'}]\", name='tavily_search_results_json')]\n", - "----\n", - "[HumanMessage(content='what is the weather in sf'), AIMessage(content='', additional_kwargs={'function_call': {'name': 'tavily_search_results_json', 'arguments': '{\\n \"query\": \"weather in San Francisco\"\\n}'}}), 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 8% 46% 29% 12% 8% Evolution of daily average temperature and precipitation in San Francisco in januaryWeather 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. ... 12-01-2023 50°F to 59°F. 13-01-2023 54°F to 58°F. 14-01-2023 54°F to ...'}]\", name='tavily_search_results_json'), AIMessage(content=\"I'm sorry, but I couldn't retrieve the current weather information for San Francisco. However, you can visit this [link](https://www.whereandwhen.net/when/north-america/california/san-francisco-ca/january/) to check the weather statistics for San Francisco in January.\")]\n", - "----\n", - "[HumanMessage(content='what is the weather in sf'), AIMessage(content='', additional_kwargs={'function_call': {'name': 'tavily_search_results_json', 'arguments': '{\\n \"query\": \"weather in San Francisco\"\\n}'}}), 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 8% 46% 29% 12% 8% Evolution of daily average temperature and precipitation in San Francisco in januaryWeather 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. ... 12-01-2023 50°F to 59°F. 13-01-2023 54°F to 58°F. 14-01-2023 54°F to ...'}]\", name='tavily_search_results_json'), AIMessage(content=\"I'm sorry, but I couldn't retrieve the current weather information for San Francisco. However, you can visit this [link](https://www.whereandwhen.net/when/north-america/california/san-francisco-ca/january/) to check the weather statistics for San Francisco in January.\")]\n", - "----\n" - ] - } - ], - "source": [ - "messages = [HumanMessage(content=\"what is the weather in sf\")]\n", - "for s in chain.stream(messages):\n", - " print(list(s.values())[0])\n", - " print(\"----\")" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "id": "1653c386-dd60-4283-ac03-21f00d57bddb", - "metadata": { - "scrolled": true - }, - "outputs": [], - "source": [ - "def call_tool(messages):\n", - " last_message = messages[-1]\n", - " action = AgentAction(\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] Okay to call this tool? {action}\")\n", - " if response == \"**EXIT**\":\n", - " raise ValueError\n", - " elif response:\n", - " print(\"foo\")\n", - " action.tool_input = response\n", - " response = tool_executor.execute(action)\n", - " function_message = FunctionMessage(content=str(response[0][1]), name=action.tool)\n", - " return messages + [function_message]" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "id": "b3d06fbc-a8d8-4a2a-b436-98975a69ef38", - "metadata": {}, - "outputs": [], - "source": [ - "chain = create_agent_messages_executor(call_model, call_tool, exit)" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "id": "1aeb7d89-2cb1-4e41-98b4-cb645d1245c6", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[HumanMessage(content='what is the weather in sf'), AIMessage(content='', additional_kwargs={'function_call': {'name': 'tavily_search_results_json', 'arguments': '{\\n \"query\": \"weather in San Francisco\"\\n}'}})]\n", - "----\n" - ] - }, - { - "name": "stdin", - "output_type": "stream", - "text": [ - "[y/n] Okay to call this tool? tool='tavily_search_results_json' tool_input={'query': 'weather in San Francisco'} log='' {'query': 'current weather in SF'}\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "foo\n", - "[HumanMessage(content='what is the weather in sf'), AIMessage(content='', additional_kwargs={'function_call': {'name': 'tavily_search_results_json', 'arguments': '{\\n \"query\": \"weather in San Francisco\"\\n}'}}), FunctionMessage(content=\"[{'url': 'https://en.climate-data.org/north-america/united-states-of-america/california/san-francisco-385/t/january-1/', 'content': 'San Francisco Weather in January you can find all information about the weather in San Francisco in January: San Francisco weather in January San Francisco weather by month // weather averages 9.6 (49.2) 6.2 (43.2) 14 (57.3) 113 San Francisco weather in January // weather averages Airport close to San FranciscoData: 1991 - 2021 Min. Temperature °C (°F), Max. Temperature °C (°F), Precipitation / Rainfall mm (in), Humidity, Rainy days. Data: 1999 - 2019: avg. Sun hours San Francisco weather and climate for further months San Francisco in February San Francisco in March San Francisco in April San Francisco in May San Francisco in June San Francisco in July'}]\", name='tavily_search_results_json')]\n", - "----\n", - "[HumanMessage(content='what is the weather in sf'), AIMessage(content='', additional_kwargs={'function_call': {'name': 'tavily_search_results_json', 'arguments': '{\\n \"query\": \"weather in San Francisco\"\\n}'}}), FunctionMessage(content=\"[{'url': 'https://en.climate-data.org/north-america/united-states-of-america/california/san-francisco-385/t/january-1/', 'content': 'San Francisco Weather in January you can find all information about the weather in San Francisco in January: San Francisco weather in January San Francisco weather by month // weather averages 9.6 (49.2) 6.2 (43.2) 14 (57.3) 113 San Francisco weather in January // weather averages Airport close to San FranciscoData: 1991 - 2021 Min. Temperature °C (°F), Max. Temperature °C (°F), Precipitation / Rainfall mm (in), Humidity, Rainy days. Data: 1999 - 2019: avg. Sun hours San Francisco weather and climate for further months San Francisco in February San Francisco in March San Francisco in April San Francisco in May San Francisco in June San Francisco in July'}]\", 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 website](https://en.climate-data.org/north-america/united-states-of-america/california/san-francisco-385/t/january-1/) to find information about the weather in San Francisco for the month of January.\")]\n", - "----\n", - "[HumanMessage(content='what is the weather in sf'), AIMessage(content='', additional_kwargs={'function_call': {'name': 'tavily_search_results_json', 'arguments': '{\\n \"query\": \"weather in San Francisco\"\\n}'}}), FunctionMessage(content=\"[{'url': 'https://en.climate-data.org/north-america/united-states-of-america/california/san-francisco-385/t/january-1/', 'content': 'San Francisco Weather in January you can find all information about the weather in San Francisco in January: San Francisco weather in January San Francisco weather by month // weather averages 9.6 (49.2) 6.2 (43.2) 14 (57.3) 113 San Francisco weather in January // weather averages Airport close to San FranciscoData: 1991 - 2021 Min. Temperature °C (°F), Max. Temperature °C (°F), Precipitation / Rainfall mm (in), Humidity, Rainy days. Data: 1999 - 2019: avg. Sun hours San Francisco weather and climate for further months San Francisco in February San Francisco in March San Francisco in April San Francisco in May San Francisco in June San Francisco in July'}]\", 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 website](https://en.climate-data.org/north-america/united-states-of-america/california/san-francisco-385/t/january-1/) to find information about the weather in San Francisco for the month of January.\")]\n", - "----\n" - ] - } - ], - "source": [ - "messages = [HumanMessage(content=\"what is the weather in sf\")]\n", - "for s in chain.stream(messages):\n", - " print(list(s.values())[0])\n", - " print(\"----\")" - ] - }, - { - "cell_type": "markdown", - "id": "4b003c09-ceeb-44bf-94c5-502bde98e3c1", - "metadata": {}, - "source": [ - "## Respond in a specific format" - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "id": "fac71b90-d7d0-4cab-af3b-f3ed8264472f", - "metadata": {}, - "outputs": [], - "source": [ - "from langchain_core.pydantic_v1 import BaseModel, Field\n", - "from typing import List" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "id": "5d80baf8-977d-44b4-a092-9d449f16a577", - "metadata": {}, - "outputs": [], - "source": [ - "class Answer(BaseModel):\n", - " \"\"\"Final Response\"\"\"\n", - " temp: int = Field(description=\"current temperature, in Farenheit\")\n", - " source: List[str] = Field(description=\"URLs to go to to learn more info\")" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "id": "ead5fb7e-ad02-4554-9595-2cbac99207a8", - "metadata": {}, - "outputs": [], - "source": [ - "from langchain_core.utils.function_calling import convert_pydantic_to_openai_function\n", - "model = ChatOpenAI().bind_functions([format_tool_to_openai_function(t) for t in tools] + [convert_pydantic_to_openai_function(Answer)])" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "id": "8e708efa-501e-42e6-94bd-71f72b61ec48", - "metadata": {}, - "outputs": [], - "source": [ - "def call_model(messages):\n", - " response = model.invoke(messages)\n", - " return messages + [response]\n", - "\n", - "\n", - "def exit(messages):\n", - " last_message = messages[-1]\n", - " if \"function_call\" not in last_message.additional_kwargs:\n", - " return \"end\"\n", - " elif \"function_call\" in last_message.additional_kwargs and last_message.additional_kwargs[\"function_call\"][\"name\"] == \"Answer\":\n", - " return \"end\"\n", - " else:\n", - " return \"function\"" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "id": "40264371-76ad-4216-8554-8afb7632d741", - "metadata": {}, - "outputs": [], - "source": [ - "chain = create_agent_messages_executor(call_model, call_tool, exit)" - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "id": "7f790767-3272-4b28-8264-ac621606be18", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[HumanMessage(content='what is the weather in sf'), AIMessage(content='', additional_kwargs={'function_call': {'name': 'tavily_search_results_json', 'arguments': '{\\n \"query\": \"current weather in San Francisco\"\\n}'}})]\n", - "----\n" - ] - }, - { - "name": "stdin", - "output_type": "stream", - "text": [ - "[y/n] Okay to call this tool? tool='tavily_search_results_json' tool_input={'query': 'current weather in San Francisco'} log='' \n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[HumanMessage(content='what is the weather in sf'), AIMessage(content='', additional_kwargs={'function_call': {'name': 'tavily_search_results_json', 'arguments': '{\\n \"query\": \"current weather in San Francisco\"\\n}'}}), FunctionMessage(content=\"[{'url': 'https://en.climate-data.org/north-america/united-states-of-america/california/san-francisco-385/t/january-1/', 'content': 'San Francisco Weather in January you can find all information about the weather in San Francisco in January: San Francisco weather in January San Francisco weather by month // weather averages 9.6 (49.2) 6.2 (43.2) 14 (57.3) 113 San Francisco weather in January // weather averages Airport close to San FranciscoData: 1991 - 2021 Min. Temperature °C (°F), Max. Temperature °C (°F), Precipitation / Rainfall mm (in), Humidity, Rainy days. Data: 1999 - 2019: avg. Sun hours San Francisco weather and climate for further months San Francisco in February San Francisco in March San Francisco in April San Francisco in May San Francisco in June San Francisco in July'}]\", name='tavily_search_results_json')]\n", - "----\n", - "[HumanMessage(content='what is the weather in sf'), AIMessage(content='', additional_kwargs={'function_call': {'name': 'tavily_search_results_json', 'arguments': '{\\n \"query\": \"current weather in San Francisco\"\\n}'}}), FunctionMessage(content=\"[{'url': 'https://en.climate-data.org/north-america/united-states-of-america/california/san-francisco-385/t/january-1/', 'content': 'San Francisco Weather in January you can find all information about the weather in San Francisco in January: San Francisco weather in January San Francisco weather by month // weather averages 9.6 (49.2) 6.2 (43.2) 14 (57.3) 113 San Francisco weather in January // weather averages Airport close to San FranciscoData: 1991 - 2021 Min. Temperature °C (°F), Max. Temperature °C (°F), Precipitation / Rainfall mm (in), Humidity, Rainy days. Data: 1999 - 2019: avg. Sun hours San Francisco weather and climate for further months San Francisco in February San Francisco in March San Francisco in April San Francisco in May San Francisco in June San Francisco in July'}]\", name='tavily_search_results_json'), AIMessage(content='', additional_kwargs={'function_call': {'name': 'Answer', 'arguments': '{\\n \"temp\": 65,\\n \"source\": [\"https://en.climate-data.org/north-america/united-states-of-america/california/san-francisco-385/t/january-1/\"]\\n}'}})]\n", - "----\n", - "[HumanMessage(content='what is the weather in sf'), AIMessage(content='', additional_kwargs={'function_call': {'name': 'tavily_search_results_json', 'arguments': '{\\n \"query\": \"current weather in San Francisco\"\\n}'}}), FunctionMessage(content=\"[{'url': 'https://en.climate-data.org/north-america/united-states-of-america/california/san-francisco-385/t/january-1/', 'content': 'San Francisco Weather in January you can find all information about the weather in San Francisco in January: San Francisco weather in January San Francisco weather by month // weather averages 9.6 (49.2) 6.2 (43.2) 14 (57.3) 113 San Francisco weather in January // weather averages Airport close to San FranciscoData: 1991 - 2021 Min. Temperature °C (°F), Max. Temperature °C (°F), Precipitation / Rainfall mm (in), Humidity, Rainy days. Data: 1999 - 2019: avg. Sun hours San Francisco weather and climate for further months San Francisco in February San Francisco in March San Francisco in April San Francisco in May San Francisco in June San Francisco in July'}]\", name='tavily_search_results_json'), AIMessage(content='', additional_kwargs={'function_call': {'name': 'Answer', 'arguments': '{\\n \"temp\": 65,\\n \"source\": [\"https://en.climate-data.org/north-america/united-states-of-america/california/san-francisco-385/t/january-1/\"]\\n}'}})]\n", - "----\n" - ] - } - ], - "source": [ - "messages = [HumanMessage(content=\"what is the weather in sf\")]\n", - "for s in chain.stream(messages):\n", - " print(list(s.values())[0])\n", - " print(\"----\")" - ] - }, - { - "cell_type": "markdown", - "id": "ddc74d57-b82a-406d-9f19-ef7808ee9ceb", - "metadata": {}, - "source": [ - "## Tool State" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "id": "bacb638e-5bd5-425c-a019-b8345a24ef17", - "metadata": {}, - "outputs": [], - "source": [ - "from langchain_core.tools import tool\n", - "from langchain_core.pydantic_v1 import BaseModel, Field" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "e3e2a10a-b1c9-4320-8f01-a42b9c6b132d", - "metadata": {}, - "outputs": [], - "source": [ - "class IntSchema(BaseModel):\n", - " num: int\n", - " ls: dict\n", - "\n", - " @classmethod\n", - " def schema(cls):\n", - " schema = super().schema()\n", - " properties = schema.get('properties', {})\n", - " properties.pop('ls', None) # Remove the hidden attribute\n", - " return schema" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "3bd82332-4f47-42b8-b70b-b8b2dedac19c", - "metadata": {}, - "outputs": [], - "source": [ - "@tool(args_schema=IntSchema)\n", - "def add_int(num, ls):\n", - " \"\"\"Call this to add number to the list.\"\"\"\n", - " ls[\"foo\"].append(num)\n", - " print(ls)\n", - " return \"Done!\"" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "2a8d4bed-04c8-4bf2-ac84-5356cbd07d3a", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'num': {'title': 'Num', 'type': 'integer'}}" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "IntSchema.schema()[\"properties\"]" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "id": "9471f29b-0c10-4d07-8d15-f3463c453fcb", - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/Users/harrisonchase/workplace/langchain/libs/core/langchain_core/_api/deprecation.py:117: LangChainDeprecationWarning: The class `langchain_community.chat_models.openai.ChatOpenAI` was deprecated in langchain-community 0.0.10 and will be removed in 0.2.0. An updated version of the class exists in the langchain-openai package and should be used instead. To use it run `pip install -U langchain-openai` and import as `from langchain_openai import ChatOpenAI`.\n", - " warn_deprecated(\n" - ] - } - ], - "source": [ - "from langchain_core.agents import AgentAction\n", - "from langchain_core.messages import FunctionMessage, HumanMessage, SystemMessage\n", - "from langchain.tools.render import format_tool_to_openai_function\n", - "import json\n", - "from langchain.chat_models import ChatOpenAI\n", - "\n", - "from langchain_community.tools.tavily_search import TavilySearchResults\n", - "\n", - "from langgraph.prebuilt.tool_executor import ToolExecutor\n", - "from langgraph.prebuilt.executor import create_executor\n", - "tools = [add_int]\n", - "model = ChatOpenAI().bind_functions([format_tool_to_openai_function(t) for t in tools])" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "id": "bf64ec0f-4563-446a-952b-c9ad3016d063", - "metadata": {}, - "outputs": [], - "source": [ - "ls = {\"foo\": []}\n", - "def call_model(messages):\n", - " response = model.invoke(messages)\n", - " return messages + [response]\n", - "\n", - "\n", - "def exit(messages):\n", - " last_message = messages[-1]\n", - " if \"function_call\" not in last_message.additional_kwargs:\n", - " return \"end\"\n", - " else:\n", - " return \"continue\"\n", - "\n", - "tool_executor = ToolExecutor(tools)\n", - "def call_tool(messages):\n", - " last_message = messages[-1]\n", - " agent_action = AgentAction(\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", - " agent_action.tool_input[\"ls\"] = ls\n", - " tool_to_use = {t.name: t for t in tools}[agent_action.tool]\n", - " # Call that tool on the input\n", - " observation = tool_to_use.invoke(agent_action.tool_input)\n", - " function_message = FunctionMessage(content=str(observation), name=agent_action.tool)\n", - " return messages + [function_message]" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "id": "83e7429b-1981-44ec-b567-50aa3a7b1731", - "metadata": {}, - "outputs": [], - "source": [ - "chain = create_executor(call_model, call_tool, exit)" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "id": "bda94e2c-9cf7-49b4-b687-466f1cbf81b2", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'foo': []}" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "ls" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "id": "9b63e7d1-a421-4a6b-ad47-93d8b497fd3e", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[HumanMessage(content='add the number one to the list'), AIMessage(content='', additional_kwargs={'function_call': {'name': 'add_int', 'arguments': '{\\n \"num\": 1\\n}'}})]\n", - "----\n", - "{'foo': [1]}\n", - "[HumanMessage(content='add the number one to the list'), AIMessage(content='', additional_kwargs={'function_call': {'name': 'add_int', 'arguments': '{\\n \"num\": 1\\n}'}}), FunctionMessage(content='Done!', name='add_int')]\n", - "----\n", - "[HumanMessage(content='add the number one to the list'), AIMessage(content='', additional_kwargs={'function_call': {'name': 'add_int', 'arguments': '{\\n \"num\": 1\\n}'}}), FunctionMessage(content='Done!', name='add_int'), AIMessage(content='The number one has been added to the list.')]\n", - "----\n", - "[HumanMessage(content='add the number one to the list'), AIMessage(content='', additional_kwargs={'function_call': {'name': 'add_int', 'arguments': '{\\n \"num\": 1\\n}'}}), FunctionMessage(content='Done!', name='add_int'), AIMessage(content='The number one has been added to the list.')]\n", - "----\n" - ] - } - ], - "source": [ - "messages = [HumanMessage(content=\"add the number one to the list\")]\n", - "for s in chain.stream(messages):\n", - " print(list(s.values())[0])\n", - " print(\"----\")" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "id": "b2bbf236-c6b1-472c-a1a6-64d057096584", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'foo': [1]}" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "ls" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "id": "7679dc19-8ee4-4139-b2bd-2773ed378193", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[HumanMessage(content='add the number 3 to the list'), AIMessage(content='', additional_kwargs={'function_call': {'name': 'add_int', 'arguments': '{\\n \"num\": 3\\n}'}})]\n", - "----\n", - "{'foo': [1, 3]}\n", - "[HumanMessage(content='add the number 3 to the list'), AIMessage(content='', additional_kwargs={'function_call': {'name': 'add_int', 'arguments': '{\\n \"num\": 3\\n}'}}), FunctionMessage(content='Done!', name='add_int')]\n", - "----\n", - "[HumanMessage(content='add the number 3 to the list'), AIMessage(content='', additional_kwargs={'function_call': {'name': 'add_int', 'arguments': '{\\n \"num\": 3\\n}'}}), FunctionMessage(content='Done!', name='add_int'), AIMessage(content='The number 3 has been added to the list.')]\n", - "----\n", - "[HumanMessage(content='add the number 3 to the list'), AIMessage(content='', additional_kwargs={'function_call': {'name': 'add_int', 'arguments': '{\\n \"num\": 3\\n}'}}), FunctionMessage(content='Done!', name='add_int'), AIMessage(content='The number 3 has been added to the list.')]\n", - "----\n" - ] - } - ], - "source": [ - "messages = [HumanMessage(content=\"add the number 3 to the list\")]\n", - "for s in chain.stream(messages):\n", - " print(list(s.values())[0])\n", - " print(\"----\")" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "id": "acb104ef-270d-4a03-b14c-c01b8e391935", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'foo': [1, 3]}" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "ls" - ] - }, - { - "cell_type": "markdown", - "id": "592c3886-71d1-4539-80dd-111e55cc3a85", - "metadata": {}, - "source": [ - "## Reflexion Agent" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "f6f96e81-4a20-4599-a625-8d18df6fa76d", - "metadata": {}, - "outputs": [], - "source": [ - "from langchain.agents import AgentExecutor, BaseMultiActionAgent, Tool\n", - "from langchain.schema import AgentAction, AgentFinish\n", - "from langchain_core.language_models.chat_models import BaseChatModel\n", - "from langchain.chains import LLMChain\n", - "\n", - "from langchain.globals import set_llm_cache\n", - "\n", - "from dotenv import load_dotenv\n", - "\n", - "from pydantic import BaseModel\n", - "\n", - "from langchain.chat_models import ChatOpenAI\n", - "from langchain.cache import SQLiteCache\n", - "\n", - "from langchain_core.output_parsers import BaseOutputParser\n", - "\n", - "from langchain.prompts.chat import ChatPromptTemplate\n", - "from langchain.callbacks import get_openai_callback\n", - "from langchain.tools.tavily_search import TavilySearchResults\n", - "from langchain.utilities.tavily_search import TavilySearchAPIWrapper\n", - "from langchain.pydantic_v1 import BaseModel\n", - "import os\n", - "\n", - "from langchain.agents import AgentType, initialize_agent, load_tools\n", - "\n", - "set_llm_cache(SQLiteCache(database_path=\".langchain.db\"))\n", - "\n", - "\n", - "llm = ChatOpenAI(\n", - " temperature=0.0,\n", - " max_tokens=2000,\n", - " max_retries=100,\n", - " model=\"gpt-4-1106-preview\",\n", - ")\n", - "\n", - "search = TavilySearchAPIWrapper()\n", - "tavily_tool = TavilySearchResults(api_wrapper=search, max_results=5)\n", - "\n", - "NEXT_STEP_TEMPLATE = \"\"\"You are expert researcher trying answer a question ~250 words. You are asked to answer the following question: {question}\n", - "\n", - "The way you are going to answer the question is as follows:\n", - "\n", - "1. Revise your previous answer using the new information.\n", - " - You should use the previous critique to add important information to your answer.\n", - " _ You MUST include numerical citations in your revised answer to ensure it can be verified.\n", - " - Add a \"References\" section to the bottom of your answer (which does not count towards the word limit). In form of:\n", - " - [1] https://example.com\n", - " - [2] https://example.com\n", - " - You should use the previous critique to remove superfluous information from your answer and make SURE it is not more than 250 words.\n", - "2. Reflect and critique your answer. Specifically, you should:\n", - " - Think about what is missing from your answer.\n", - " - Think about what is superfluous in your answer.\n", - " - Think about what search query you should use next to improve your answer.\n", - " Give your answer in exactly 2 parts. The first should address what is missing from your answer. The second should address what could be removed from your answer. Your should be VERY harsh as we really want to improve the answer.\n", - "3. Give the search query you came up with to improve your answer.\n", - "\n", - "Previous steps: \n", - "\n", - "{previous_steps}\n", - "\n", - "===\n", - "\n", - "Format your answer as follows:\n", - "\n", - "Revised answer: [give your revised answer based on the previous critique and new information from the search engine then the \"References\" section]\n", - "Critique: [give your harsh critique of your revised answer in 2 parts: what is missing and what is superfluous]\n", - "Search query: [give the new search query you came up with to enter into the search engine to improve your answer. If you have more than one, make sure they are comma separated and in quotes]\n", - "\n", - "SAY NOTHING else please.\"\"\"\n", - "\n", - "INITIAL_ANSWER_TEMPLATE = \"\"\"You are expert researcher trying answer a question ~250 words. You are asked to answer the following question: {question}\n", - "\n", - "The way you are going to answer the question is as follows:\n", - "\n", - "1. Give a detailed in ~250 words.\n", - "2. Reflect and critique your answer. Specifically, you should:\n", - " - Think about what is missing from your answer.\n", - " - Think about what is superfluous in your answer.\n", - " - Think about what search query you should use next to improve your answer.\n", - " Give your answer in exactly 2 parts. The first should address what is missing from your answer. The second should address what could be removed from your answer. Your should be VERY harsh as we really want to improve the answer.\n", - "3. Give the search query you came up with to improve your answer.\n", - "\n", - "===\n", - "\n", - "Format your answer as follows:\n", - "\n", - "Answer: [give your initial answer]\n", - "Critique: [give your harsh critique of your answer in 2 parts: what is missing and what is superfluous]\n", - "Search query: [give the search query you came up with to improve your answer. If you have more than one, make sure they are comma separated and in quotes]\n", - "\n", - "SAY NOTHING else please.\"\"\"\n", - "\n", - "\n", - "class ReflexionStep(BaseModel):\n", - " \"\"\"A single step in the reflexion process.\"\"\"\n", - "\n", - " answer: str\n", - " critique: str\n", - " search_query: str\n", - "\n", - " def __str__(self):\n", - " return f\"Answer: {self.answer}\\nCritique: {self.critique}\\nSearch query: {self.search_query}\"\n", - "\n", - "def _parse_reflexion_step(output: str) -> tuple[str, str, str]:\n", - " # find answer using .split()\n", - " if (\"Answer:\" not in output and \"Revised answer:\" not in output) or not \"Critique:\" in output or not \"Search query:\" in output:\n", - " raise ValueError(f\"The output is not formatted correctly. Output: {output}\")\n", - " if \"Answer:\" in output:\n", - " answer = output.split(\"Answer:\")[1].split(\"Critique:\")[0].strip()\n", - " else:\n", - " answer = output.split(\"Revised answer:\")[1].split(\"Critique:\")[0].strip()\n", - " critique = output.split(\"Critique:\")[1].split(\"Search query:\")[0].strip()\n", - " search_query = output.split(\"Search query:\")[1].strip()\n", - " return answer, critique, search_query\n", - "\n", - "class ReflexionStepParser(BaseOutputParser[ReflexionStep]):\n", - " \"\"\"Parser for the reflexion step.\"\"\"\n", - "\n", - " def parse(self, output: str) -> ReflexionStep:\n", - " \"\"\"Parse the output.\"\"\"\n", - " # try to find answer or initial answer\n", - " answer, critique, search_query = _parse_reflexion_step(output)\n", - " return ReflexionStep(\n", - " answer=answer, critique=critique, search_query=search_query\n", - " )" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "7708fa95-547b-4bea-b126-3656de7d5873", - "metadata": {}, - "outputs": [], - "source": [ - "initial_chain = RunnablePassthrough.assign(\n", - " agent_outcome = ChatPromptTemplate.from_template(INITIAL_ANSWER_TEMPLATE) | llm | ReflexionStepParser() | (lambda x: AgentAction(\n", - " tool=\"tavily_search_results_json\",\n", - " tool_input=x.search_query,\n", - " log=str(x),\n", - " ))\n", - ")\n", - "\n", - "def prep_next(inputs):\n", - " intermediate_steps = inputs[\"intermediate_steps\"]\n", - " previous_steps = list[str]()\n", - "\n", - " for i, (action, observation) in enumerate(intermediate_steps, start=1):\n", - " last_step_str = f\"\"\"Step {i}:\n", - "\n", - "{action.log}\n", - "\n", - "Search output for \"{action.tool_input}\":\n", - "\n", - "{observation}\"\"\"\n", - " previous_steps.append(last_step_str)\n", - "\n", - " previous_steps_str = \"\\n\\n\".join(previous_steps)\n", - " inputs[\"previous_steps\"] = previous_steps_str\n", - " return inputs\n", - " \n", - "next_chain = RunnablePassthrough.assign(\n", - " agent_outcome = prep_next | ChatPromptTemplate.from_template(NEXT_STEP_TEMPLATE) | llm | ReflexionStepParser() | (lambda x: AgentAction(\n", - " tool=\"tavily_search_results_json\",\n", - " tool_input=x.search_query,\n", - " log=str(x),\n", - " ))\n", - ")\n", - "\n", - "def finish(inputs):\n", - " intermediate_steps = inputs[\"intermediate_steps\"]\n", - " last_action, _ = intermediate_steps[-1]\n", - " last_step_str = last_action.log\n", - " # extract answer\n", - " answer, _, _ = _parse_reflexion_step(last_step_str)\n", - "\n", - " first_action, _ = intermediate_steps[0]\n", - " first_step_str = first_action.log\n", - " # extract answer\n", - " initial_answer, _, _ = _parse_reflexion_step(first_step_str)\n", - "\n", - " return AgentFinish(\n", - " log=\"Reached max steps.\",\n", - " return_values={\"output\": answer, \"initial_answer\": initial_answer},\n", - " )\n", - "\n", - "\n", - "def execute_tools(data):\n", - " agent_action = data.pop('agent_outcome')\n", - " observation = {t.name: t for t in tools}[agent_action.tool].invoke(agent_action.tool_input)\n", - " data['intermediate_steps'].append((agent_action, observation))\n", - " return data\n" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "id": "d6cdd1cd-e480-4dd7-99b4-9018eb243b4d", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "AgentFinish(return_values={'output': \"The current weather in San Francisco can be accessed through various weather reporting services, which provide real-time temperature, humidity, wind, and chances of precipitation [1]. Historically, San Francisco experiences a mild, Mediterranean climate with average temperatures ranging from the low 50s to the mid-60s Fahrenheit. The city's unique topography creates microclimates, leading to significant weather variations across different neighborhoods. San Francisco's summers are notably cooler compared to other Californian cities, largely due to the cold California Current and persistent fog, especially in June and July. Winters are mild and the wettest months span from November to March, with an annual rainfall average of approximately 23 inches. Wind is a prominent feature, with spring being particularly windy. For historical weather extremes and average wind speeds, additional specific data can be sought from climatological records.\\n\\nReferences:\\n[1] https://www.weather25.com/north-america/usa/california/san-francisco\", 'initial_answer': \"The weather in San Francisco (SF) is characterized by a mild, Mediterranean-like climate with wet winters and dry summers. The city's unique topography and coastal location result in microclimates, where weather conditions can vary significantly from one neighborhood to another. Average temperatures typically range from the low 50s to the mid-60s Fahrenheit throughout the year. Summers in San Francisco are often cooler than in other parts of California due to the cold California Current offshore and the presence of fog, particularly in June and July. The fog usually burns off by the afternoon, leading to clearer skies and slightly warmer temperatures. Winters are mild and moist, with the majority of the city's rainfall occurring between November and March. Rainfall averages around 23 inches annually. Wind is also a notable feature of San Francisco's weather, with spring being the windiest season. Despite the general patterns, it's always advisable to dress in layers due to the potential for rapid weather changes.\"}, log='Reached max steps.')" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "workflow = Graph()\n", - "\n", - "# add actors\n", - "workflow.add_node(\"initial\", initial_chain)\n", - "workflow.add_node(\"next\", next_chain)\n", - "workflow.add_node(\"finish\", finish)\n", - "workflow.add_node(\"tools\", execute_tools)\n", - "\n", - "# Enter with initial actor, then loop through tools -> next steps until finished\n", - "workflow.set_entry_point('initial')\n", - "\n", - "workflow.add_edge('initial', 'tools')\n", - "workflow.add_conditional_edges(\n", - " 'tools',\n", - " lambda x: \"exit\" if len(x['intermediate_steps']) >= 2 else \"continue\",\n", - " {\n", - " \"continue\": 'next',\n", - " \"exit\": 'finish'\n", - " }\n", - ")\n", - "workflow.add_edge('next', 'tools')\n", - "workflow.set_finish_point('finish')\n", - "\n", - "chain = workflow.compile()\n", - "\n", - "chain.invoke({\"question\": \"what is the weather in sf\", \"intermediate_steps\": []})" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "9babf196-b1fd-492d-9197-96a674f5e81d", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "0c349b42-ae43-4564-90d2-80eff5b9c236", - "metadata": {}, - "outputs": [], - "source": [ - "## Plan and Execute\n", - "\n", - "from langchain_core.pydantic_v1 import BaseModel, Field\n", - "from typing import List, Tuple\n", - "\n", - "\n", - "class PlanExecute(BaseModel):\n", - "\n", - " plan: List[str] = []\n", - " past_steps: List[Tuple] = []\n", - " response: str = \"\"\n", - "\n", - "from langchain_core.prompts import ChatPromptTemplate\n", - "from langchain_openai import ChatOpenAI\n", - "from langchain.chains.openai_functions import create_structured_output_runnable\n", - "from langchain_core.tools import tool\n", - "\n", - "class Plan(BaseModel):\n", - " \"\"\"Plan to follow in future\"\"\"\n", - " steps: List[str] = Field(description=\"different steps to follow, should be in sorted order\")\n", - "\n", - "planner_prompt = ChatPromptTemplate.from_template(\"\"\"For the given objective, come up with a simple step by step plan. \\\n", - "This plan should involve individual tasks, that if executed correctly will yield the correct answer. Do not add any superfluous steps. \\\n", - "The result of the final step should be the final answer. Make sure that each step has all the information needed - do not skip steps.\n", - "\n", - "{objective}\"\"\")\n", - "planner = create_structured_output_runnable(Plan, ChatOpenAI(model=\"gpt-4-1106-preview\", temperature=0), planner_prompt)\n", - "\n", - "planner.invoke({'objective': 'what is leo dicaprios gf age raised to .23'})\n", - "\n", - "@tool\n", - "def search(query:str):\n", - " \"\"\"Get a response from google\"\"\"\n", - " return 25\n", - "\n", - "@tool\n", - "def math(equation: str):\n", - " \"\"\"Solve a math equation\"\"\"\n", - " return .34\n", - "\n", - "tools = [search, math]\n", - "\n", - "from langchain import hub\n", - "from langchain.agents import create_openai_functions_agent\n", - "# Get the prompt to use - you can modify this!\n", - "prompt = hub.pull(\"hwchase17/openai-functions-agent\")\n", - "# Choose the LLM that will drive the agent\n", - "llm = ChatOpenAI(model=\"gpt-3.5-turbo-1106\")\n", - "# Construct the OpenAI Functions agent\n", - "agent_runnable = create_openai_functions_agent(llm, tools, prompt)\n", - "\n", - "from langchain_core.runnables import RunnablePassthrough\n", - "from langchain_core.agents import AgentFinish\n", - "\n", - "\n", - "# Define the agent\n", - "# Note that here, we are using `.assign` to add the output of the agent to the dictionary\n", - "# This dictionary will be returned from the node\n", - "# The reason we don't want to return just the result of `agent_runnable` from this node is\n", - "# that we want to continue passing around all the other inputs\n", - "agent = RunnablePassthrough.assign(\n", - " agent_outcome = agent_runnable\n", - ")\n", - "\n", - "# Define the function to execute tools\n", - "def action(data):\n", - " # Get the most recent agent_outcome - this is the key added in the `agent` above\n", - " agent_action = data.pop('agent_outcome')\n", - " # Get the tool to use\n", - " tool_to_use = {t.name: t for t in tools}[agent_action.tool]\n", - " # Call that tool on the input\n", - " observation = tool_to_use.invoke(agent_action.tool_input)\n", - " # We now add in the action and the observation to the `intermediate_steps` list\n", - " # This is the list of all previous actions taken and their output\n", - " data['intermediate_steps'].append((agent_action, observation))\n", - " return data\n", - "\n", - "# Define logic that will be used to determine which conditional edge to go down\n", - "def should_continue(data):\n", - " # If the agent outcome is an AgentFinish, then we return `exit` string\n", - " # This will be used when setting up the graph to define the flow\n", - " if isinstance(data['agent_outcome'], AgentFinish):\n", - " return \"end\"\n", - " # Otherwise, an AgentAction is returned\n", - " # Here we return `continue` string\n", - " # This will be used when setting up the graph to define the flow\n", - " else:\n", - " return \"continue\"\n", - "\n", - "def plan(inputs):\n", - " inputs['state'] = PlanExecute(plan=planner.invoke(inputs).steps)\n", - " inputs['input'] = inputs['state'].plan[0]\n", - " inputs['intermediate_steps'] = []\n", - " return inputs\n", - "\n", - "from langchain.chains.openai_functions import create_openai_fn_runnable\n", - "class Response(BaseModel):\n", - " \"\"\"Response to user.\"\"\"\n", - " response: str\n", - "\n", - "replanner_prompt = ChatPromptTemplate.from_template(\"\"\"For the given objective, come up with a simple step by step plan. \\\n", - "This plan should involve individual tasks, that if executed correctly will yield the correct answer. Do not add any superfluous steps. \\\n", - "The result of the final step should be the final answer. Make sure that each step has all the information needed - do not skip steps.\n", - "\n", - "Your objective was this:\n", - "{objective}\n", - "\n", - "Your original plan was this:\n", - "{plan}\n", - "\n", - "You have currently done the follow steps:\n", - "{steps}\n", - "\n", - "Update your plan accordingly. If no more steps are needed and you can return to the user, then respond with that. Otherwise, fill out the plan.\"\"\")\n", - "\n", - "\n", - "replanner = create_openai_fn_runnable([Plan, Response], ChatOpenAI(model=\"gpt-4-1106-preview\", temperature=0), replanner_prompt)\n", - "\n", - "replanner.invoke({\"objective\": \"look up the temperature\", \"plan\": \"look up the temperature\", \"steps\": [(\"look up the temperature\", \"i am in san diego\")]})\n", - "\n", - "def replan(inputs):\n", - " inputs['state'].past_steps.append((inputs['state'].plan[0], inputs['agent_outcome'].return_values['output']))\n", - " sub_inputs = {\n", - " \"objective\": inputs[\"objective\"],\n", - " \"plan\": inputs[\"state\"].plan,\n", - " \"steps\": inputs[\"state\"].past_steps\n", - " }\n", - " output = replanner.invoke(sub_inputs)\n", - " if isinstance(output, Response):\n", - " inputs['state'].response = output.response\n", - " else:\n", - " inputs['state'].plan = output.steps\n", - " inputs['input'] = inputs['state'].plan[0]\n", - " return inputs\n", - "\n", - "\n", - "def should_end(inputs):\n", - " if inputs['state'].response:\n", - " return True\n", - " else:\n", - " return False\n", - "\n", - "from langgraph.graph import END, Graph\n", - "\n", - "workflow = Graph()\n", - "\n", - "# Add the plan node\n", - "workflow.add_node(\"plan\", plan)\n", - "\n", - "# Add the agent node, we give it name `agent` which we will use later\n", - "workflow.add_node(\"agent\", agent)\n", - "# Add the action node, we give it name `action` which we will use later\n", - "workflow.add_node(\"action\", action)\n", - "\n", - "# Add a replan node\n", - "workflow.add_node(\"replan\", replan)\n", - "\n", - "# Set the entrypoint as `agent`\n", - "# This means that this node is the first one called\n", - "workflow.set_entry_point(\"plan\")\n", - "\n", - "# We now add a conditional edge\n", - "workflow.add_conditional_edges(\n", - " # First, we define the start node. We use `agent`.\n", - " # This means these are the edges taken after the `agent` node is called.\n", - " \"agent\",\n", - " # Next, we pass in the function that will determine which node is called next.\n", - " should_continue,\n", - " # Finally we pass in a mapping.\n", - " # The keys are strings, and the values are other nodes.\n", - " # END is a special node marking that the graph should finish.\n", - " # What will happen is we will call `should_continue`, and then the output of that\n", - " # will be matched against the keys in this mapping.\n", - " # Based on which one it matches, that node will then be called.\n", - " {\n", - " # If `tools`, then we call the tool node.\n", - " \"continue\": \"action\",\n", - " # Otherwise we go back to replan\n", - " \"end\": \"replan\"\n", - " }\n", - ")\n", - "\n", - "# From plan we go to agent\n", - "workflow.add_edge('plan', 'agent')\n", - "\n", - "# We now add a normal edge from `tools` to `agent`.\n", - "# This means that after `tools` is called, `agent` node is called next.\n", - "workflow.add_edge('action', 'agent')\n", - "\n", - "workflow.add_conditional_edges(\n", - " \"replan\",\n", - " # Next, we pass in the function that will determine which node is called next.\n", - " should_end,\n", - " {\n", - " # If `tools`, then we call the tool node.\n", - " True: END,\n", - " False: \"agent\",\n", - " }\n", - ")\n", - "\n", - "# Finally, we compile it!\n", - "# This compiles it into a LangChain Runnable,\n", - "# meaning you can use it as you would any other runnable\n", - "chain = workflow.compile()\n", - "\n", - "for s in chain.stream({\"objective\": \"what is leo dicaprios gf age raised to .34\"}):\n", - " print(s)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "bb6fb6a8-f6f5-444a-9033-ef9e0bd6fd23", - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.11.1" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/examples/rag.py b/examples/rag.py deleted file mode 100644 index 414d4ab10..000000000 --- a/examples/rag.py +++ /dev/null @@ -1,58 +0,0 @@ -from langchain.chat_models import ChatOpenAI -from langchain.embeddings import OpenAIEmbeddings -from langchain.vectorstores import FAISS -from langchain_core.messages import AIMessage, AnyMessage, FunctionMessage -from langchain_core.prompts import PromptTemplate - -from langgraph.channels import Topic -from langgraph.pregel import Channel, Pregel - -texts = ["harrison went to kensho"] -embeddings = OpenAIEmbeddings() -db = FAISS.from_texts(texts, embeddings) - -retriever = db.as_retriever() - - -prompt = PromptTemplate.from_template( - """Answer the question "{question}" based on the following context: {context}""" -) - -model = ChatOpenAI() - -chain = ( - Channel.subscribe_to(["question"]) - | { - "context": (lambda x: x["question"]) - | Channel.write_to( - messages=lambda _input: AIMessage( - content="", - additional_kwargs={ - "function_call": "retrieval", - "arguments": {"question": _input}, - }, - ) - ) - | retriever - | Channel.write_to( - messages=lambda documents: FunctionMessage.construct( - content=documents, # function message requires content to be str - name="retrieval", - ) - ), - "question": lambda x: x["question"], - } - | prompt - | model - | Channel.write_to(messages=lambda message: [message]) -) - -app = Pregel( - chains={"chain": chain}, - channels={"messages": Topic(AnyMessage)}, - input=["question"], - output=["messages"], -) - -for s in app.stream({"question": "where did harrison go"}): - print(s) diff --git a/examples/readme.py b/examples/readme.py deleted file mode 100644 index b7077a321..000000000 --- a/examples/readme.py +++ /dev/null @@ -1,132 +0,0 @@ -import asyncio -from pprint import pprint - -from langchain import hub -from langchain.agents import create_openai_functions_agent -from langchain_community.tools.tavily_search import TavilySearchResults -from langchain_core.agents import AgentFinish -from langchain_core.runnables import RunnablePassthrough -from langchain_openai.chat_models import ChatOpenAI - -from langgraph.graph import END, Graph - -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 -llm = ChatOpenAI(model="gpt-3.5-turbo-1106") - -# Construct the OpenAI Functions agent -agent_runnable = create_openai_functions_agent(llm, tools, prompt) - - -# Define the agent -# Note that here, we are using `.assign` to add the output of the agent to the dictionary -# This dictionary will be returned from the node -# The reason we don't want to return just the result of `agent_runnable` from this node is -# that we want to continue passing around all the other inputs -agent = RunnablePassthrough.assign(agent_outcome=agent_runnable) - - -# 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.pop("agent_outcome") - # Get the tool to use - tool_to_use = {t.name: t for t in tools}[agent_action.tool] - # Call that tool on the input - observation = tool_to_use.invoke(agent_action.tool_input) - # We now add in the action and the observation to the `intermediate_steps` list - # This is the list of all previous actions taken and their output - data["intermediate_steps"].append((agent_action, observation)) - return data - - -# 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): - return "exit" - # Otherwise, an AgentAction is returned - # Here we return `continue` string - # This will be used when setting up the graph to define the flow - else: - return "continue" - - -# Define the graph - - -workflow = Graph() - -# Add the agent node, we give it name `agent` which we will use later -workflow.add_node("agent", agent) -# Add the tools node, we give it name `tools` which we will use later -workflow.add_node("tools", execute_tools) - -# 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": "tools", - # Otherwise we finish. - "exit": 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("tools", "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() - - -def main(): - for output in chain.stream( - {"input": "what is the weather in sf", "intermediate_steps": []} - ): - for key, value in output.items(): - print(f"Output from node '{key}':") - print("---") - pprint(value) - print("\n---\n") - - -async def amain(): - async for output in chain.astream_log( - {"input": "what is the weather in sf", "intermediate_steps": []}, - include_types=["llm"], - ): - for op in output.ops: - if op["path"] == "/streamed_output/-": - # this is the output from .stream() - ... - elif op["path"].startswith("/logs/") and op["path"].endswith( - "/streamed_output/-" - ): - # these are tokens from the LLM - print(op["value"]) - - -asyncio.run(amain()) diff --git a/examples/recursive-web-loader.py b/examples/recursive-web-loader.py deleted file mode 100644 index 1d6f186ba..000000000 --- a/examples/recursive-web-loader.py +++ /dev/null @@ -1,134 +0,0 @@ -from contextlib import asynccontextmanager, contextmanager -from typing import AsyncGenerator, Callable, FrozenSet, Generator, Optional, TypedDict - -import httpx -from langchain_core.documents import Document -from langchain_core.runnables import RunnableLambda, RunnablePassthrough -from langchain_core.utils.html import extract_sub_links - -from langgraph.channels.context import Context -from langgraph.channels.topic import Topic -from langgraph.pregel import Channel, Pregel - -# Load url with sync httpx client - - -@contextmanager -def httpx_client() -> Generator[httpx.Client, None, None]: - with httpx.HTTPTransport(retries=3) as transport, httpx.Client( - transport=transport - ) as client: - yield client - - -class LoadUrlInput(TypedDict): - url: str - visited: FrozenSet[str] - client: httpx.Client - - -def load_url(input: LoadUrlInput) -> str: - response = input["client"].get(input["url"]) - return response.text - - -# Same as above but with async httpx client - - -@asynccontextmanager -async def httpx_aclient() -> AsyncGenerator[httpx.AsyncClient, None]: - async with httpx.AsyncHTTPTransport(retries=3) as transport, httpx.AsyncClient( - transport=transport - ) as client: - yield client - - -class LoadUrlInputAsync(TypedDict): - url: str - visited: FrozenSet[str] - client: httpx.AsyncClient - - -async def load_url_async(input: LoadUrlInputAsync) -> str: - response = await input["client"].get(input["url"]) - return response.text - - -# default metadata extractor copied from langchain.document_loaders - - -def _metadata_extractor(raw_html: str, url: str) -> dict: - """Extract metadata from raw html using BeautifulSoup.""" - metadata = {"source": url} - - try: - from bs4 import BeautifulSoup - except ImportError: - return metadata - soup = BeautifulSoup(raw_html, "html.parser") - if title := soup.find("title"): - metadata["title"] = title.get_text() - if description := soup.find("meta", attrs={"name": "description"}): - metadata["description"] = description.get("content", None) - if html := soup.find("html"): - metadata["language"] = html.get("lang", None) - return metadata - - -def recursive_web_loader( - *, - max_depth: int = 2, - extractor: Optional[Callable[[str], str]] = None, - metadata_extractor: Optional[Callable[[str, str], dict]] = None, -) -> Pregel: - # assign default extractors - extractor = extractor or (lambda x: x) - metadata_extractor = metadata_extractor or _metadata_extractor - # define the channels - channels = { - "next_urls": Topic(str, unique=True), - "documents": Topic(Document, accumulate=True), - "client": Context(httpx_client, httpx_aclient), - } - # the main chain that gets executed recursively - # while there are urls in next_urls - visitor = ( - # run the chain below for each url in next_urls - # adding the current values of base_url and httpx client - Channel.subscribe_to_each("next_urls", key="url").join(["client", "base_url"]) - # load the url (with sync and async implementations) - | RunnablePassthrough.assign(body=RunnableLambda(load_url, load_url_async)) - | Channel.write_to( - # send a new document to the documents stream - documents=lambda x: Document( - page_content=extractor(x["body"]), - metadata=metadata_extractor(x["body"], x["url"]), - ), - # send the next urls to the next_urls topic - next_urls=lambda x: extract_sub_links( - x["body"], x["url"], base_url=x["base_url"] - ), - ) - ) - return Pregel( - channels=channels, - chains={ - # use the base_url as the first url to visit - "input": Channel.subscribe_to("base_url") | Channel.write_to("next_urls"), - # add the main chain - "visitor": visitor, - }, - # this will accept a string as input - input="base_url", - # and return a dict with documents and visited set - output=["documents", "visited"], - # debug logging - debug=True, - ).with_config({"recursion_limit": max_depth + 1}) - - -loader = recursive_web_loader(max_depth=3) - -documents = loader.invoke("https://docs.python.org/3.9/") - -print(len(documents["documents"])) diff --git a/langgraph/prebuilt/__init__.py b/langgraph/prebuilt/__init__.py index e69de29bb..89dfa34a3 100644 --- a/langgraph/prebuilt/__init__.py +++ b/langgraph/prebuilt/__init__.py @@ -0,0 +1,5 @@ +from langgraph.prebuilt.agent_executor import create_agent_executor +from langgraph.prebuilt import chat_executor +from langgraph.prebuilt.tool_executor import ToolExecutor + +__all__ = ["create_agent_executor", "chat_executor", "ToolExecutor"] diff --git a/langgraph/prebuilt/agent_executor.py b/langgraph/prebuilt/agent_executor.py index 77b365e35..45fcb9ade 100644 --- a/langgraph/prebuilt/agent_executor.py +++ b/langgraph/prebuilt/agent_executor.py @@ -1,10 +1,39 @@ -from typing import Annotated, TypedDict import operator +from typing import Annotated, TypedDict, Union, Sequence + from langchain_core.agents import AgentAction, AgentFinish -from langgraph.graph import StateGraph, END +from langchain_core.messages import BaseMessage + +from langgraph.graph import END, StateGraph from langgraph.prebuilt.tool_executor import ToolExecutor +def _get_agent_state(input_schema= None): + if input_schema is None: + 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] + + else: + class AgentState(input_schema): + # 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] + + return AgentState def create_agent_executor(agent_runnable, tools, input_schema=None): @@ -14,18 +43,9 @@ def create_agent_executor(agent_runnable, tools, input_schema=None): else: tool_executor = ToolExecutor(tools) + state = _get_agent_state(input_schema) - if input_schema is None: - class AgentState(TypedDict): - input: str - agent_outcome: AgentAction | AgentFinish | None - intermediate_steps: Annotated[list[tuple[AgentAction, str]], operator.add] - - else: - class AgentState(input_schema): - agent_outcome: AgentAction | AgentFinish | None - intermediate_steps: Annotated[list[tuple[AgentAction, str]], operator.add] - + # 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 @@ -49,7 +69,7 @@ def create_agent_executor(agent_runnable, tools, input_schema=None): return {"intermediate_steps": [(agent_action, str(output))]} # Define a new graph - workflow = StateGraph(AgentState) + workflow = StateGraph(state) # Define the two nodes we will cycle between workflow.add_node("agent", run_agent) diff --git a/langgraph/prebuilt/messages_executor.py b/langgraph/prebuilt/chat_executor.py similarity index 75% rename from langgraph/prebuilt/messages_executor.py rename to langgraph/prebuilt/chat_executor.py index 760f20ab1..eebb6998c 100644 --- a/langgraph/prebuilt/messages_executor.py +++ b/langgraph/prebuilt/chat_executor.py @@ -1,35 +1,23 @@ -from langchain_core.runnables import RunnablePassthrough -from langchain_core.messages import FunctionMessage -from langchain_core.agents import AgentFinish, AgentAction import json +import operator +from typing import Annotated, Sequence, TypedDict from langchain.tools.render import format_tool_to_openai_function +from langchain_core.agents import AgentAction +from langchain_core.messages import BaseMessage, FunctionMessage + +from langgraph.graph import END, StateGraph from langgraph.prebuilt.tool_executor import ToolExecutor -from langchain_core.utils.function_calling import convert_pydantic_to_openai_function -from typing import Annotated, TypedDict, Sequence -from langchain_core.messages import BaseMessage -import operator -from langchain_core.agents import AgentAction, AgentFinish -from langgraph.graph import StateGraph, END -def _get_tool_executor_and_functions(tools, response_format): +def create_function_calling_executor(model, tools): if isinstance(tools, ToolExecutor): tool_executor = tools tool_classes = tools.tools else: tool_executor = ToolExecutor(tools) tool_classes = tools - - functions = [format_tool_to_openai_function(t) for t in tool_classes] - if response_format is not None: - functions.append(convert_pydantic_to_openai_function(response_format)) - return tool_executor, functions - - -def create_messages_executor(model, tools, response_format = None): - tool_executor, functions = _get_tool_executor_and_functions(tools, response_format) - model = model.bind_functions([format_tool_to_openai_function(t) for t in tools]) + model = model.bind_functions([format_tool_to_openai_function(t) for t in tool_classes]) # Define the function that determines whether to continue or not def should_continue(state): @@ -38,14 +26,9 @@ def create_messages_executor(model, tools, response_format = None): # If there is no function call, then we finish if "function_call" not in last_message.additional_kwargs: return "end" - # Otherwise if there is, we need to check what type of function call it is + # Otherwise if there is, we continue else: - if response_format is None: - return "continue" - elif last_message.additional_kwargs["function_call"]["name"] == response_format.__name__: - return "end" - else: - return "continue" + return "continue" # Define the function that calls the model def call_model(state): diff --git a/langgraph/prebuilt/tool_executor.py b/langgraph/prebuilt/tool_executor.py index 7f860f256..121ef0855 100644 --- a/langgraph/prebuilt/tool_executor.py +++ b/langgraph/prebuilt/tool_executor.py @@ -1,7 +1,9 @@ -from langchain_core.runnables import RunnableBinding, RunnableLambda -from typing import Sequence, Any -from langchain_core.tools import BaseTool +from typing import Any, Sequence + from langchain_core.agents import AgentAction +from langchain_core.runnables import RunnableBinding, RunnableLambda +from langchain_core.tools import BaseTool + INVALID_TOOL_MSG_TEMPLATE = ( "{requested_tool_name} is not a valid tool, " "try one of [{available_tool_names_str}]."