This commit is contained in:
Harrison Chase
2024-01-15 13:16:05 -08:00
parent ec219c4d49
commit da5e6d7319
24 changed files with 1679 additions and 2417 deletions
-104
View File
@@ -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
}
+274
View File
@@ -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
}
@@ -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
}
+302
View File
@@ -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
}
@@ -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
}
@@ -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
}
@@ -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"
]
}
-371
View File
@@ -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
}
-116
View File
@@ -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?"}))
File diff suppressed because it is too large Load Diff
-58
View File
@@ -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)
-132
View File
@@ -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())
-134
View File
@@ -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"]))