Skip to content

The agent engineering platform.

Add custom async generator example (#14299)

Bob Lin5a23608c4193
diff --git a/docs/docs/expression_language/how_to/generators.ipynb b/docs/docs/expression_language/how_to/generators.ipynb
index bf7319d273..8f0010c6c7 100644
--- a/docs/docs/expression_language/how_to/generators.ipynb
+++ b/docs/docs/expression_language/how_to/generators.ipynb
@@ -17,6 +17,13 @@
"Let's implement a custom output parser for comma-separated lists."
]
},
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Sync version"
+ ]
+ },
{
"cell_type": "code",
"execution_count": 1,
@@ -57,7 +64,7 @@
},
{
"cell_type": "code",
- "execution_count": 8,
+ "execution_count": 3,
"metadata": {},
"outputs": [
{
@@ -66,7 +73,7 @@
"'lion, tiger, wolf, gorilla, panda'"
]
},
- "execution_count": 8,
+ "execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
@@ -152,12 +159,81 @@
"list_chain.invoke({\"animal\": \"bear\"})"
]
},
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Async version"
+ ]
+ },
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 8,
"metadata": {},
"outputs": [],
- "source": []
+ "source": [
+ "from typing import AsyncIterator\n",
+ "\n",
+ "\n",
+ "async def asplit_into_list(\n",
+ " input: AsyncIterator[str]\n",
+ ") -> AsyncIterator[List[str]]: # async def\n",
+ " buffer = \"\"\n",
+ " async for (\n",
+ " chunk\n",
+ " ) in input: # `input` is a `async_generator` object, so use `async for`\n",
+ " buffer += chunk\n",
+ " while \",\" in buffer:\n",
+ " comma_index = buffer.index(\",\")\n",
+ " yield [buffer[:comma_index].strip()]\n",
+ " buffer = buffer[comma_index + 1 :]\n",
+ " yield [buffer.strip()]\n",
+ "\n",
+ "\n",
+ "list_chain = str_chain | asplit_into_list"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "['lion']\n",
+ "['tiger']\n",
+ "['wolf']\n",
+ "['gorilla']\n",
+ "['panda']\n"
+ ]
+ }
+ ],
+ "source": [
+ "async for chunk in list_chain.astream({\"animal\": \"bear\"}):\n",
+ " print(chunk, flush=True)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "['lion', 'tiger', 'wolf', 'gorilla', 'panda']"
+ ]
+ },
+ "execution_count": 10,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "await list_chain.ainvoke({\"animal\": \"bear\"})"
+ ]
}
],
"metadata": {
@@ -176,7 +252,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.9.1"
+ "version": "3.11.5"
}
},
"nbformat": 4,