{ "cells": [ { "cell_type": "markdown", "id": "51466c8d-8ce4-4b3d-be4e-18fdbeda5f53", "metadata": {}, "source": [ "# Chat Agent Executor using prebuilt Tool Node\n", "\n", "\n", "In this example we will build a ReAct Agent that uses tool calling and the prebuilt ToolNode." ] }, { "cell_type": "markdown", "id": "7cbd446a-808f-4394-be92-d45ab818953c", "metadata": {}, "source": [ "## Setup\n", "\n", "First we need to install the packages required" ] }, { "cell_type": "code", "execution_count": 1, "id": "af4ce0ba-7596-4e5f-8bf8-0b0bd6e62833", "metadata": {}, "outputs": [], "source": ["%%capture --no-stderr\n%pip install --quiet -U langgraph langchain langchain_openai tavily-python"] }, { "cell_type": "markdown", "id": "0abe11f4-62ed-4dc4-8875-3db21e260d1d", "metadata": {}, "source": [ "Next, we need to set API keys for OpenAI (the LLM we will use) and Tavily (the search tool we will use)" ] }, { "cell_type": "code", "execution_count": null, "id": "c903a1cf-2977-4e2d-ad7d-8b3946821d89", "metadata": {}, "outputs": [], "source": ["import getpass\nimport os\n\nos.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key:\")\nos.environ[\"TAVILY_API_KEY\"] = getpass.getpass(\"Tavily API Key:\")"] }, { "cell_type": "markdown", "id": "f0ed46a8-effe-4596-b0e1-a6a29ee16f5c", "metadata": {}, "source": [ "Optionally, we can set API key for [LangSmith tracing](https://smith.langchain.com/), which will give us best-in-class observability." ] }, { "cell_type": "code", "execution_count": null, "id": "95e25aec-7c9f-4a63-b143-225d0e9a79c3", "metadata": {}, "outputs": [], "source": ["os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\nos.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass(\"LangSmith API Key:\")"] }, { "cell_type": "markdown", "id": "21ac643b-cb06-4724-a80c-2862ba4773f1", "metadata": {}, "source": [ "## Set up the tools\n", "\n", "We will first define the tools we want to use.\n", "For this simple example, we will use a built-in search tool via Tavily.\n", "However, it is really easy to create your own tools - see documentation [here](https://python.langchain.com/v0.2/docs/how_to/custom_tools) on how to do that.\n", "\n", "**MODIFICATION**\n", "\n", "We don't need a ToolExecutor when using ToolNode.\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "d7ef57dd-5d6e-4ad3-9377-a92201c1310e", "metadata": {}, "outputs": [], "source": ["from langchain_community.tools.tavily_search import TavilySearchResults\n\ntools = [TavilySearchResults(max_results=1)]"] }, { "cell_type": "markdown", "id": "5497ed70-fce3-47f1-9cad-46f912bad6a5", "metadata": {}, "source": [ "## Set up the model\n", "\n", "Now we need to load the chat model we want to use.\n", "Importantly, this should satisfy two criteria:\n", "\n", "1. It should work with messages. We will represent all agent state in the form of messages, so it needs to be able to work well with them.\n", "2. It should work with tool calling. This means it should be a model that implements `.bind_tools()`.\n", "\n", "Note: these model requirements are not requirements for using LangGraph - they are just requirements for this one example.\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "892b54b9-75f0-4804-9ed0-88b5e5532989", "metadata": {}, "outputs": [], "source": ["from langchain_openai import ChatOpenAI\n\nmodel = ChatOpenAI(temperature=0)"] }, { "cell_type": "markdown", "id": "a77995c0-bae2-4cee-a036-8688a90f05b9", "metadata": {}, "source": [ "\n", "After we've done this, we should make sure the model knows that it has these tools available to call.\n", "We can do this by converting the LangChain tools into the format for OpenAI function calling, and then bind them to the model class.\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "cd3cbae5-d92c-4559-a4aa-44721b80d107", "metadata": {}, "outputs": [], "source": ["model = model.bind_tools(tools)"] }, { "cell_type": "markdown", "id": "8e8b9211-93d0-4ad5-aa7a-9c09099c53ff", "metadata": {}, "source": [ "## Define the agent state\n", "\n", "The main type of graph in `langgraph` is the [StateGraph](https://langchain-ai.github.io/langgraph/reference/graphs/#langgraph.graph.StateGraph).\n", "This graph is parameterized by a state object that it passes around to each node.\n", "Each node then returns operations to update that state.\n", "These operations can either SET specific attributes on the state (e.g. overwrite the existing values) or ADD to the existing attribute.\n", "Whether to set or add is denoted by annotating the state object you construct the graph with.\n", "\n", "For this example, the state we will track will just be a list of messages.\n", "We want each node to just add messages to that list.\n", "Therefore, we will use a `TypedDict` with one key (`messages`) and annotate it so that the `messages` attribute is always added to.\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "ea793afa-2eab-4901-910d-6eed90cd6564", "metadata": {}, "outputs": [], "source": ["import operator\nfrom typing import Annotated, Sequence, TypedDict\n\nfrom langchain_core.messages import BaseMessage\n\n\nclass AgentState(TypedDict):\n messages: Annotated[Sequence[BaseMessage], operator.add]"] }, { "cell_type": "markdown", "id": "e03c5094-9297-4d19-a04e-3eedc75cefb4", "metadata": {}, "source": [ "## Define the nodes\n", "\n", "We now need to define a few different nodes in our graph.\n", "In `langgraph`, a node can be either a function or a [runnable](https://python.langchain.com/v0.2/docs/concepts/#langchain-expression-language-lcel).\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. **MODIFICATION** The prebuilt ToolNode, given the list of tools. This will take tool calls from the most recent AIMessage, execute them, and return the result as ToolMessages.\n", "\n", "We will also need to define some edges.\n", "Some of these edges may be conditional.\n", "The reason they are conditional is that based on the output of a node, one of several paths may be taken.\n", "The path that is taken is not known until that node is run (the LLM decides).\n", "\n", "1. Conditional Edge: after the agent is called, we should either:\n", " a. If the agent said to take an action, then the function to invoke tools should be called\n", " b. If the agent said that it was finished, then it should finish\n", "2. Normal Edge: after the tools are invoked, it should always go back to the agent to decide what to do next\n", "\n", "Let's define the nodes, as well as a function to decide how what conditional edge to take.\n" ] }, { "cell_type": "code", "execution_count": 10, "id": "3b541bb9-900c-40d0-964d-7b5dfee30667", "metadata": {}, "outputs": [], "source": ["from langgraph.prebuilt import ToolNode\n\n\n# Define the function that determines whether to continue or not\ndef should_continue(state):\n messages = state[\"messages\"]\n last_message = messages[-1]\n # If there are no tool calls, then we finish\n if not last_message.tool_calls:\n return \"end\"\n # Otherwise if there is, we continue\n else:\n return \"continue\"\n\n\n# Define the function that calls the model\ndef call_model(state):\n messages = state[\"messages\"]\n response = model.invoke(messages)\n # We return a list, because this will get added to the existing list\n return {\"messages\": [response]}\n\n\n# Define the function to execute tools\ntool_node = ToolNode(tools)"] }, { "cell_type": "markdown", "id": "ffd6e892-946c-4899-8cc0-7c9291c1f73b", "metadata": {}, "source": [ "## Define the graph\n", "\n", "We can now put it all together and define the graph!" ] }, { "cell_type": "code", "execution_count": 6, "id": "813ae66c-3b58-4283-a02a-36da72a2ab90", "metadata": {}, "outputs": [], "source": ["from langgraph.graph import END, StateGraph, START\n\n# Define a new graph\nworkflow = StateGraph(AgentState)\n\n# Define the two nodes we will cycle between\nworkflow.add_node(\"agent\", call_model)\nworkflow.add_node(\"action\", tool_node)\n\n# Set the entrypoint as `agent`\n# This means that this node is the first one called\nworkflow.add_edge(START, \"agent\")\n\n# We now add a conditional edge\nworkflow.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.\nworkflow.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\napp = workflow.compile()"] }, { "cell_type": "markdown", "id": "547c3931-3dae-4281-ad4e-4b51305594d4", "metadata": {}, "source": [ "## Use it!\n", "\n", "We can now use it!\n", "This now exposes the [same interface](https://python.langchain.com/v0.2/docs/concepts/#langchain-expression-language-lcel) as all other LangChain runnables." ] }, { "cell_type": "code", "execution_count": 7, "id": "8edb04b9-40b6-46f1-a7a8-4b2d8aba7752", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'messages': [HumanMessage(content='what is the weather in sf'),\n", " AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_HGOi2cCxKKVWnz8WMuOCWnZx', 'function': {'arguments': '{\"query\":\"weather in San Francisco\"}', 'name': 'tavily_search_results_json'}, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 21, 'prompt_tokens': 87, 'total_tokens': 108}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_b28b39ffa8', 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-df061477-a815-432b-a69f-9951d4c6edfa-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'weather in San Francisco'}, 'id': 'call_HGOi2cCxKKVWnz8WMuOCWnZx'}]),\n", " ToolMessage(content='[{\"url\": \"https://www.weatherapi.com/\", \"content\": \"{\\'location\\': {\\'name\\': \\'San Francisco\\', \\'region\\': \\'California\\', \\'country\\': \\'United States of America\\', \\'lat\\': 37.78, \\'lon\\': -122.42, \\'tz_id\\': \\'America/Los_Angeles\\', \\'localtime_epoch\\': 1712852407, \\'localtime\\': \\'2024-04-11 9:20\\'}, \\'current\\': {\\'last_updated_epoch\\': 1712852100, \\'last_updated\\': \\'2024-04-11 09:15\\', \\'temp_c\\': 15.0, \\'temp_f\\': 59.0, \\'is_day\\': 1, \\'condition\\': {\\'text\\': \\'Partly cloudy\\', \\'icon\\': \\'//cdn.weatherapi.com/weather/64x64/day/116.png\\', \\'code\\': 1003}, \\'wind_mph\\': 3.8, \\'wind_kph\\': 6.1, \\'wind_degree\\': 350, \\'wind_dir\\': \\'N\\', \\'pressure_mb\\': 1015.0, \\'pressure_in\\': 29.97, \\'precip_mm\\': 0.0, \\'precip_in\\': 0.0, \\'humidity\\': 78, \\'cloud\\': 25, \\'feelslike_c\\': 15.8, \\'feelslike_f\\': 60.4, \\'vis_km\\': 16.0, \\'vis_miles\\': 9.0, \\'uv\\': 4.0, \\'gust_mph\\': 8.3, \\'gust_kph\\': 13.3}}\"}]', name='tavily_search_results_json', tool_call_id='call_HGOi2cCxKKVWnz8WMuOCWnZx'),\n", " AIMessage(content='The current weather in San Francisco is as follows:\\n- Temperature: 15.0°C (59.0°F)\\n- Condition: Partly cloudy\\n- Wind: 3.8 mph from the North\\n- Humidity: 78%\\n- Visibility: 16.0 km (9.0 miles)\\n- UV Index: 4.0\\n\\nFor more details, you can visit [Weather API](https://www.weatherapi.com/).', response_metadata={'token_usage': {'completion_tokens': 93, 'prompt_tokens': 465, 'total_tokens': 558}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_b28b39ffa8', 'finish_reason': 'stop', 'logprobs': None}, id='run-923bcbd2-3c79-4696-8f9e-5142b50b20cf-0')]}" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": ["from langchain_core.messages import HumanMessage\n\ninputs = {\"messages\": [HumanMessage(content=\"what is the weather in sf\")]}\napp.invoke(inputs)"] }, { "cell_type": "markdown", "id": "5a9e8155-70c5-4973-912c-dc55104b2acf", "metadata": {}, "source": [ "This may take a little bit - it's making a few calls behind the scenes.\n", "In order to start seeing some intermediate results as they happen, we can use streaming - see below for more information on that.\n", "\n", "## Streaming\n", "\n", "LangGraph has support for several different types of streaming.\n", "\n", "### Streaming Node Output\n", "\n", "One of the benefits of using LangGraph is that it is easy to stream output as it's produced by each node.\n" ] }, { "cell_type": "code", "execution_count": 8, "id": "f544977e-31f7-41f0-88c4-ec9c27b8cecb", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Output from node 'agent':\n", "---\n", "{'messages': [AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_3QXwm9UTKcfN2BuFhTDlLgIN', 'function': {'arguments': '{\"query\":\"weather in San Francisco\"}', 'name': 'tavily_search_results_json'}, 'type': 'function'}]}, response_metadata={'token_usage': {'completion_tokens': 21, 'prompt_tokens': 87, 'total_tokens': 108}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_b28b39ffa8', 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-9a2d6e22-873a-4afc-8ae2-0adf8176b1b2-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'weather in San Francisco'}, 'id': 'call_3QXwm9UTKcfN2BuFhTDlLgIN'}])]}\n", "\n", "---\n", "\n", "Output from node 'action':\n", "---\n", "{'messages': [ToolMessage(content='[{\"url\": \"https://www.weatherapi.com/\", \"content\": \"{\\'location\\': {\\'name\\': \\'San Francisco\\', \\'region\\': \\'California\\', \\'country\\': \\'United States of America\\', \\'lat\\': 37.78, \\'lon\\': -122.42, \\'tz_id\\': \\'America/Los_Angeles\\', \\'localtime_epoch\\': 1712852407, \\'localtime\\': \\'2024-04-11 9:20\\'}, \\'current\\': {\\'last_updated_epoch\\': 1712852100, \\'last_updated\\': \\'2024-04-11 09:15\\', \\'temp_c\\': 15.0, \\'temp_f\\': 59.0, \\'is_day\\': 1, \\'condition\\': {\\'text\\': \\'Partly cloudy\\', \\'icon\\': \\'//cdn.weatherapi.com/weather/64x64/day/116.png\\', \\'code\\': 1003}, \\'wind_mph\\': 3.8, \\'wind_kph\\': 6.1, \\'wind_degree\\': 350, \\'wind_dir\\': \\'N\\', \\'pressure_mb\\': 1015.0, \\'pressure_in\\': 29.97, \\'precip_mm\\': 0.0, \\'precip_in\\': 0.0, \\'humidity\\': 78, \\'cloud\\': 25, \\'feelslike_c\\': 15.8, \\'feelslike_f\\': 60.4, \\'vis_km\\': 16.0, \\'vis_miles\\': 9.0, \\'uv\\': 4.0, \\'gust_mph\\': 8.3, \\'gust_kph\\': 13.3}}\"}]', name='tavily_search_results_json', tool_call_id='call_3QXwm9UTKcfN2BuFhTDlLgIN')]}\n", "\n", "---\n", "\n", "Output from node 'agent':\n", "---\n", "{'messages': [AIMessage(content='The current weather in San Francisco is partly cloudy with a temperature of 59°F (15°C). The wind speed is 6.1 km/h coming from the north. The humidity is at 78%, and the visibility is 16.0 km.', response_metadata={'token_usage': {'completion_tokens': 53, 'prompt_tokens': 465, 'total_tokens': 518}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_b28b39ffa8', 'finish_reason': 'stop', 'logprobs': None}, id='run-8875456d-e31e-42b0-b2af-bdc1a9cfccfe-0')]}\n", "\n", "---\n", "\n" ] } ], "source": ["inputs = {\"messages\": [HumanMessage(content=\"what is the weather in sf\")]}\nfor output in app.stream(inputs):\n # stream() yields dictionaries with output keyed by node name\n for key, value in output.items():\n print(f\"Output from node '{key}':\")\n print(\"---\")\n print(value)\n print(\"\\n---\\n\")"] }, { "cell_type": "markdown", "id": "2a1b56c5-bd61-4192-8bdb-458a1e9f0159", "metadata": {}, "source": [ "### Streaming LLM Tokens\n", "\n", "You can also access the LLM tokens as they are produced by each node. \n", "In this case only the \"agent\" node produces LLM tokens.\n", "In order for this to work properly, you must be using an LLM that supports streaming as well as have set it when constructing the LLM (e.g. `ChatOpenAI(model=\"gpt-3.5-turbo-1106\", streaming=True)`)\n" ] }, { "cell_type": "code", "execution_count": 9, "id": "cfd140f0-a5a6-4697-8115-322242f197b5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "content='' additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_EdTLEVxQKMLRNv82Yqdcugdy', 'function': {'arguments': '', 'name': 'tavily_search_results_json'}, 'type': 'function'}]} id='run-acf76f4b-c5d0-46a1-a114-75021091719b' invalid_tool_calls=[{'name': 'tavily_search_results_json', 'args': '', 'id': 'call_EdTLEVxQKMLRNv82Yqdcugdy', 'error': 'Malformed args.'}] tool_call_chunks=[{'name': 'tavily_search_results_json', 'args': '', 'id': 'call_EdTLEVxQKMLRNv82Yqdcugdy', 'index': 0}]\n", "content='' additional_kwargs={'tool_calls': [{'index': 0, 'id': None, 'function': {'arguments': '{\"', 'name': None}, 'type': None}]} id='run-acf76f4b-c5d0-46a1-a114-75021091719b' tool_calls=[{'name': '', 'args': {}, 'id': None}] tool_call_chunks=[{'name': None, 'args': '{\"', 'id': None, 'index': 0}]\n", "content='' additional_kwargs={'tool_calls': [{'index': 0, 'id': None, 'function': {'arguments': 'query', 'name': None}, 'type': None}]} id='run-acf76f4b-c5d0-46a1-a114-75021091719b' invalid_tool_calls=[{'name': None, 'args': 'query', 'id': None, 'error': 'Malformed args.'}] tool_call_chunks=[{'name': None, 'args': 'query', 'id': None, 'index': 0}]\n", "content='' additional_kwargs={'tool_calls': [{'index': 0, 'id': None, 'function': {'arguments': '\":\"', 'name': None}, 'type': None}]} id='run-acf76f4b-c5d0-46a1-a114-75021091719b' invalid_tool_calls=[{'name': None, 'args': '\":\"', 'id': None, 'error': 'Malformed args.'}] tool_call_chunks=[{'name': None, 'args': '\":\"', 'id': None, 'index': 0}]\n", "content='' additional_kwargs={'tool_calls': [{'index': 0, 'id': None, 'function': {'arguments': 'weather', 'name': None}, 'type': None}]} id='run-acf76f4b-c5d0-46a1-a114-75021091719b' invalid_tool_calls=[{'name': None, 'args': 'weather', 'id': None, 'error': 'Malformed args.'}] tool_call_chunks=[{'name': None, 'args': 'weather', 'id': None, 'index': 0}]\n", "content='' additional_kwargs={'tool_calls': [{'index': 0, 'id': None, 'function': {'arguments': ' in', 'name': None}, 'type': None}]} id='run-acf76f4b-c5d0-46a1-a114-75021091719b' invalid_tool_calls=[{'name': None, 'args': ' in', 'id': None, 'error': 'Malformed args.'}] tool_call_chunks=[{'name': None, 'args': ' in', 'id': None, 'index': 0}]\n", "content='' additional_kwargs={'tool_calls': [{'index': 0, 'id': None, 'function': {'arguments': ' San', 'name': None}, 'type': None}]} id='run-acf76f4b-c5d0-46a1-a114-75021091719b' invalid_tool_calls=[{'name': None, 'args': ' San', 'id': None, 'error': 'Malformed args.'}] tool_call_chunks=[{'name': None, 'args': ' San', 'id': None, 'index': 0}]\n", "content='' additional_kwargs={'tool_calls': [{'index': 0, 'id': None, 'function': {'arguments': ' Francisco', 'name': None}, 'type': None}]} id='run-acf76f4b-c5d0-46a1-a114-75021091719b' invalid_tool_calls=[{'name': None, 'args': ' Francisco', 'id': None, 'error': 'Malformed args.'}] tool_call_chunks=[{'name': None, 'args': ' Francisco', 'id': None, 'index': 0}]\n", "content='' additional_kwargs={'tool_calls': [{'index': 0, 'id': None, 'function': {'arguments': '\"}', 'name': None}, 'type': None}]} id='run-acf76f4b-c5d0-46a1-a114-75021091719b' invalid_tool_calls=[{'name': None, 'args': '\"}', 'id': None, 'error': 'Malformed args.'}] tool_call_chunks=[{'name': None, 'args': '\"}', 'id': None, 'index': 0}]\n", "content='' response_metadata={'finish_reason': 'tool_calls'} id='run-acf76f4b-c5d0-46a1-a114-75021091719b'\n", "content='' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content='The' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' current' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' weather' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' in' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' San' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' Francisco' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' is' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' partly' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' cloudy' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' with' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' a' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' temperature' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' of' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' ' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content='59' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content='°F' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' (' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content='15' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content='°C' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=').' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' The' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' wind' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' speed' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' is' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' ' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content='3' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content='.' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content='8' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' mph' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' (' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content='6' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content='.' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content='1' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' k' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content='ph' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=')' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' coming' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' from' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' the' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' north' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content='.' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' The' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' humidity' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' is' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' at' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' ' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content='78' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content='%' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' with' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' a' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' visibility' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' of' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' ' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content='9' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content=' miles' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content='.' id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n", "content='' response_metadata={'finish_reason': 'stop'} id='run-bd561aa4-2af3-4d44-a110-b7991ec0d930'\n" ] } ], "source": ["inputs = {\"messages\": [HumanMessage(content=\"what is the weather in sf?\")]}\n\nasync for output in app.astream_log(inputs, include_types=[\"llm\"]):\n # astream_log() yields the requested logs (here LLMs) in JSONPatch format\n for op in output.ops:\n if op[\"path\"] == \"/streamed_output/-\":\n # this is the output from .stream()\n ...\n elif op[\"path\"].startswith(\"/logs/\") and op[\"path\"].endswith(\n \"/streamed_output/-\"\n ):\n # because we chose to only include LLMs, these are LLM tokens\n print(op[\"value\"])"] }, { "cell_type": "code", "execution_count": null, "id": "08ae8246-11d5-40e1-8567-361e5bef8917", "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.8" } }, "nbformat": 4, "nbformat_minor": 5 }