From 21c842baf6237c0aa35fb73f9e3d7606c340f151 Mon Sep 17 00:00:00 2001 From: Vadym Barda Date: Mon, 12 Aug 2024 17:11:07 -0400 Subject: [PATCH] docs: update streaming example (#1321) * docs: update streaming example --- examples/streaming-content.ipynb | 36 ++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/examples/streaming-content.ipynb b/examples/streaming-content.ipynb index aabd6fc97..ff182ca21 100644 --- a/examples/streaming-content.ipynb +++ b/examples/streaming-content.ipynb @@ -14,9 +14,24 @@ "Below is a simple toy example." ] }, + { + "cell_type": "markdown", + "id": "95301021-1db9-426f-807c-ec5b37bd5a9d", + "metadata": {}, + "source": [ + "
\n", + "

ASYNC IN PYTHON<=3.10

\n", + "

\n", + "Any Langchain RunnableLambda, a RunnableGenerator, or Tool that invokes other runnables and is running async in python<=3.10, will have to propagate callbacks to child objects manually. This is because LangChain cannot automatically propagate callbacks to child objects in this case.\n", + " \n", + "This is a common reason why you may fail to see events being emitted from custom runnables or tools.\n", + "

\n", + "
" + ] + }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 1, "id": "486a01a0", "metadata": {}, "outputs": [], @@ -51,7 +66,10 @@ " messages = []\n", " # Tagging a node makes it easy to filter out which events to include in your stream\n", " # It's completely optional, but useful if you have many functions with similar names\n", - " gen = RunnableGenerator(my_generator).with_config(tags=[\"should_stream\"])\n", + " gen = RunnableGenerator(my_generator).with_config(\n", + " tags=[\"should_stream\"],\n", + " callbacks=config.get(\"callbacks\", []) # <-- Propagate callbacks (Python <= 3.10)\n", + " )\n", " async for message in gen.astream(state):\n", " messages.append(message)\n", " return {\"messages\": [AIMessage(content=\" \".join(messages))]}\n", @@ -65,7 +83,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 2, "id": "ce773a40", "metadata": {}, "outputs": [ @@ -80,7 +98,7 @@ "name": "stderr", "output_type": "stream", "text": [ - "/Users/harrisonchase/.pyenv/versions/3.11.1/envs/permchain/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", + "/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" ] } @@ -89,7 +107,7 @@ "from langchain_core.messages import HumanMessage\n", "\n", "inputs = [HumanMessage(content=\"What are you thinking about?\")]\n", - "async for event in app.astream_events({\"messages\": inputs}, version=\"v1\"):\n", + "async for event in app.astream_events({\"messages\": inputs}, version=\"v2\"):\n", " kind = event[\"event\"]\n", " tags = event.get(\"tags\", [])\n", " if kind == \"on_chain_stream\" and \"should_stream\" in tags:\n", @@ -104,7 +122,7 @@ { "cell_type": "code", "execution_count": null, - "id": "2c7b7902-2d80-4bf9-91c1-737b749e58a3", + "id": "615cb9d2-bfa2-4f83-90b0-c6c2d1e6df95", "metadata": {}, "outputs": [], "source": [] @@ -112,9 +130,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "langgraph", "language": "python", - "name": "python3" + "name": "langgraph" }, "language_info": { "codemirror_mode": { @@ -126,7 +144,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.1" + "version": "3.11.9" } }, "nbformat": 4,