mirror of
https://github.com/hwchase17/langchain.git
synced 2025-08-11 13:55:03 +00:00
Add custom async generator example (#14299)
<img width="1172" alt="Screenshot 2023-12-05 at 11 19 16 PM" src="https://github.com/langchain-ai/langchain/assets/10000925/6b0fbd70-9f6b-4f91-b494-9e88676b4786">
This commit is contained in:
parent
63fdc6e818
commit
5a23608c41
@ -17,6 +17,13 @@
|
|||||||
"Let's implement a custom output parser for comma-separated lists."
|
"Let's implement a custom output parser for comma-separated lists."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Sync version"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 1,
|
||||||
@ -57,7 +64,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 8,
|
"execution_count": 3,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
@ -66,7 +73,7 @@
|
|||||||
"'lion, tiger, wolf, gorilla, panda'"
|
"'lion, tiger, wolf, gorilla, panda'"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 8,
|
"execution_count": 3,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"output_type": "execute_result"
|
"output_type": "execute_result"
|
||||||
}
|
}
|
||||||
@ -152,12 +159,81 @@
|
|||||||
"list_chain.invoke({\"animal\": \"bear\"})"
|
"list_chain.invoke({\"animal\": \"bear\"})"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Async version"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 8,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"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": {
|
"metadata": {
|
||||||
@ -176,7 +252,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.9.1"
|
"version": "3.11.5"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
Loading…
Reference in New Issue
Block a user