docs: clean up and standardize more how-tos (#1959)

This commit is contained in:
Vadym Barda
2024-10-03 13:40:34 +00:00
committed by GitHub
parent f55586ea23
commit 99fd0eedbd
3 changed files with 72 additions and 111 deletions
@@ -13,7 +13,31 @@
"id": "964686a6-8fed-4360-84d2-958c48186008",
"metadata": {},
"source": [
"A common use case is streaming from an agent is to stream LLM tokens from inside the final node. This guide demonstrates how you can do this.\n",
"<div class=\"admonition tip\">\n",
" <p class=\"admonition-title\">Prerequisites</p>\n",
" <p>\n",
" This guide assumes familiarity with the following:\n",
" <ul>\n",
" <li> \n",
" <a href=\"https://langchain-ai.github.io/langgraph/concepts/streaming/\">\n",
" Streaming\n",
" </a>\n",
" </li>\n",
" <li>\n",
" <a href=\"https://python.langchain.com/docs/concepts/#chat-models/\">\n",
" Chat Models\n",
" </a>\n",
" </li>\n",
" <li>\n",
" <a href=\"https://python.langchain.com/docs/concepts/#tools\">\n",
" Tools\n",
" </a>\n",
" </li>\n",
" </ul>\n",
" </p>\n",
"</div> \n",
"\n",
"A common use case when streaming from an agent is to stream LLM tokens from inside the final node. This guide demonstrates how you can do this.\n",
"\n",
"## Setup\n",
"\n",
@@ -33,7 +57,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "c87e4a47-4099-4d1a-907c-a99fa857165a",
"metadata": {},
"outputs": [],
@@ -60,7 +84,7 @@
" <p style=\"padding-top: 5px;\">\n",
" Sign up for LangSmith to quickly spot issues and improve the performance of your LangGraph projects. LangSmith lets you use trace data to debug, test, and monitor your LLM apps built with LangGraph — read more about how to get started <a href=\"https://docs.smith.langchain.com\">here</a>. \n",
" </p>\n",
"</div> "
"</div>"
]
},
{
@@ -73,7 +97,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 4,
"id": "5e62618d-0e0c-483c-acd3-40a26e61894a",
"metadata": {},
"outputs": [],
@@ -120,7 +144,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 5,
"id": "8c7339d2-1835-4b5a-a99c-a60e150280af",
"metadata": {},
"outputs": [],
@@ -164,28 +188,28 @@
" return {\"messages\": [response]}\n",
"\n",
"\n",
"workflow = StateGraph(MessagesState)\n",
"builder = StateGraph(MessagesState)\n",
"\n",
"workflow.add_node(\"agent\", call_model)\n",
"workflow.add_node(\"tools\", tool_node)\n",
"builder.add_node(\"agent\", call_model)\n",
"builder.add_node(\"tools\", tool_node)\n",
"# add a separate final node\n",
"workflow.add_node(\"final\", call_final_model)\n",
"builder.add_node(\"final\", call_final_model)\n",
"\n",
"workflow.add_edge(START, \"agent\")\n",
"workflow.add_conditional_edges(\n",
"builder.add_edge(START, \"agent\")\n",
"builder.add_conditional_edges(\n",
" \"agent\",\n",
" should_continue,\n",
")\n",
"\n",
"workflow.add_edge(\"tools\", \"agent\")\n",
"workflow.add_edge(\"final\", END)\n",
"builder.add_edge(\"tools\", \"agent\")\n",
"builder.add_edge(\"final\", END)\n",
"\n",
"app = workflow.compile()"
"graph = builder.compile()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 6,
"id": "2ab6d079-ba06-48ba-abe5-e72df24407af",
"metadata": {},
"outputs": [
@@ -203,7 +227,7 @@
"source": [
"from IPython.display import display, Image\n",
"\n",
"display(Image(app.get_graph().draw_mermaid_png()))"
"display(Image(graph.get_graph().draw_mermaid_png()))"
]
},
{
@@ -214,19 +238,6 @@
"## Stream outputs from the final node"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "84d65cbe-4cfe-44f8-b49e-b37632887c91",
"metadata": {},
"outputs": [],
"source": [
"import warnings\n",
"from langchain_core._api import LangChainBetaWarning\n",
"\n",
"warnings.filterwarnings(\"ignore\", category=LangChainBetaWarning)"
]
},
{
"cell_type": "markdown",
"id": "5cfaeb64-5506-4546-96c0-4891e6288ad9",
@@ -260,8 +271,8 @@
"source": [
"from langchain_core.messages import HumanMessage\n",
"\n",
"inputs = [HumanMessage(content=\"what is the weather in sf\")]\n",
"async for msg, metadata in app.astream({\"messages\": inputs}, stream_mode=\"messages\"):\n",
"inputs = {\"messages\": [HumanMessage(content=\"what is the weather in sf\")]}\n",
"for msg, metadata in graph.stream(inputs, stream_mode=\"messages\"):\n",
" if (\n",
" msg.content\n",
" and not isinstance(msg, HumanMessage)\n",
@@ -296,13 +307,13 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Well| folks|,| looks| like| we|'ve| got| some| cloudy| skies| in| the| Big| Apple| today|.| So| grab| your| umbrella| just| in| case|,| and| don|'t| let| those| clouds| rain| on| your| parade|!|"
"Looks| like| we|'ve| got| some| clouds| roll|in|'| in| over| the| Big| Apple| today|,| folks|!| Keep| an| eye| out| for| some| over|cast| skies| in| NYC|.|"
]
}
],
"source": [
"inputs = {\"messages\": [(\"human\", \"what's the weather in nyc?\")]}\n",
"async for event in app.astream_events(inputs, version=\"v2\"):\n",
"inputs = {\"messages\": [HumanMessage(content=\"what's the weather in nyc?\")]}\n",
"async for event in graph.astream_events(inputs, version=\"v2\"):\n",
" kind = event[\"event\"]\n",
" tags = event.get(\"tags\", [])\n",
" # filter on the custom tag\n",