From f8c87afee1cd9822d8962e690a5bcf49568bf065 Mon Sep 17 00:00:00 2001 From: Vadym Barda Date: Thu, 11 Jul 2024 21:31:55 -0400 Subject: [PATCH] docs: update streaming from within the tool to use dispatch_custom_event (#1000) --- ...-from-within-tools-without-langchain.ipynb | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/examples/streaming-events-from-within-tools-without-langchain.ipynb b/examples/streaming-events-from-within-tools-without-langchain.ipynb index 928540cef..1c703a921 100644 --- a/examples/streaming-events-from-within-tools-without-langchain.ipynb +++ b/examples/streaming-events-from-within-tools-without-langchain.ipynb @@ -42,7 +42,7 @@ "metadata": {}, "outputs": [ { - "name": "stdout", + "name": "stdin", "output_type": "stream", "text": [ "OPENAI_API_KEY: ········\n" @@ -80,7 +80,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "d59234f9-173e-469d-a725-c13e0979663e", "metadata": {}, "outputs": [], @@ -181,19 +181,16 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "b90941d8-afe4-42ec-9262-9c3b87c3b1ec", "metadata": {}, "outputs": [], "source": [ "import json\n", - "from langchain_core.runnables import RunnableLambda\n", + "from langchain_core.callbacks import adispatch_custom_event\n", "\n", "async def get_items(place: str) -> str:\n", " \"\"\"Use this tool to look up which items are in the given place.\"\"\"\n", - " # NOTE: we need to define a special langchain runnable that we'll be using for logging the streaming outputs from within a tool\n", - " tool_logger = RunnableLambda(lambda inputs: inputs).with_config({\"tags\": [\"tool_call\"]})\n", - "\n", " # this can be replaced with any actual streaming logic that you might have\n", " def stream(place: str):\n", " if \"bed\" in place: # For under the bed\n", @@ -205,7 +202,13 @@ "\n", " tokens = []\n", " for token in stream(place):\n", - " tool_logger.invoke(token)\n", + " await adispatch_custom_event(\n", + " # this will allow you to filter events by name\n", + " \"tool_call_token_stream\",\n", + " {\"function_name\": \"get_items\", \"arguments\": {\"place\": place}, \"tool_output_token\": token},\n", + " # this will allow you to filter events by tags\n", + " config={\"tags\": [\"tool_call\"]}\n", + " )\n", " tokens.append(token)\n", "\n", " return \", \".join(tokens)\n", @@ -245,7 +248,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "id": "228260be-1f9a-4195-80e0-9604f8a5dba6", "metadata": {}, "outputs": [], @@ -284,10 +287,18 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 6, "id": "45c96a79-4147-42e3-89fd-d942b2b49f6c", "metadata": {}, "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Users/vadymbarda/.virtualenvs/langgraph/lib/python3.11/site-packages/langchain_core/_api/beta_decorator.py:87: LangChainBetaWarning: This API is in beta and may change in the future.\n", + " warn_beta(\n" + ] + }, { "name": "stdout", "output_type": "stream", @@ -301,8 +312,8 @@ "source": [ "async for event in graph.astream_events({\"messages\": [{\"role\": \"user\", \"content\": \"what's in the bedroom\"}]}, version=\"v2\"):\n", " tags = event.get(\"tags\", [])\n", - " if event[\"event\"] == \"on_chain_end\" and \"tool_call\" in tags:\n", - " print(\"Tool token\", event[\"data\"][\"output\"])" + " if event[\"event\"] == \"on_custom_event\" and \"tool_call\" in tags:\n", + " print(\"Tool token\", event[\"data\"][\"tool_output_token\"])" ] } ],