Skip to content

Commit

Permalink
docs: clean up and standardize more how-tos (#1959)
Browse files Browse the repository at this point in the history
  • Loading branch information
vbarda authored Oct 3, 2024
1 parent f55586e commit 99fd0ee
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 111 deletions.
79 changes: 45 additions & 34 deletions docs/docs/how-tos/streaming-from-final-node.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -33,7 +57,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "c87e4a47-4099-4d1a-907c-a99fa857165a",
"metadata": {},
"outputs": [],
Expand All @@ -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>"
]
},
{
Expand All @@ -73,7 +97,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 4,
"id": "5e62618d-0e0c-483c-acd3-40a26e61894a",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -120,7 +144,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 5,
"id": "8c7339d2-1835-4b5a-a99c-a60e150280af",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -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": [
Expand All @@ -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()))"
]
},
{
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
26 changes: 25 additions & 1 deletion docs/docs/how-tos/subgraph.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,31 @@
"source": [
"# How to create subgraphs\n",
"\n",
"For more complex systems, subgraphs are a useful design principle. Subgraphs allow you to create and manage different states in different parts of your graph. This allows you build things like [multi-agent teams](https://langchain-ai.github.io/langgraph/tutorials/multi_agent/hierarchical_agent_teams/), where each team can track its own separate state.\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/low_level/#state\">\n",
" State\n",
" </a>\n",
" </li>\n",
" <li> \n",
" <a href=\"https://langchain-ai.github.io/langgraph/concepts/low_level/#reducers\">\n",
" Reducers\n",
" </a>\n",
" </li>\n",
" <li> \n",
" <a href=\"https://langchain-ai.github.io/langgraph/concepts/agentic_concepts/#sub-graphs\">\n",
" Subgraphs\n",
" </a>\n",
" </li>\n",
" </ul>\n",
" </p>\n",
"</div> \n",
"\n",
"For more complex systems, subgraphs are a useful design principle. Subgraphs allow you to create and manage different states in different parts of your graph. This allows you build things like [multi-agent teams](https://langchain-ai.github.io/langgraph/tutorials/multi_agent/hierarchical_agent_teams/), where each team can track its own separate state. This guide shows how you can add subgraphs to your graph.\n",
"\n",
"![Screenshot 2024-07-11 at 1.01.28 PM.png](attachment:71516aef-9c00-4730-a676-a54e90cb6472.png)"
]
Expand Down
78 changes: 2 additions & 76 deletions docs/docs/how-tos/visualization.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"source": [
"# How to visualize your graph\n",
"\n",
"This notebook walks through how to visualize the graphs you create. This works with ANY [Graph](https://langchain-ai.github.io/langgraph/reference/graphs/).\n",
"This guide walks through how to visualize the graphs you create. This works with ANY [Graph](https://langchain-ai.github.io/langgraph/reference/graphs/).\n",
"\n",
"## Setup\n",
"\n",
Expand All @@ -32,7 +32,7 @@
"source": [
"## Set up Graph\n",
"\n",
"You can visualize any arbitrary Graph, including StateGraph's and MessageGraph's. Let's have some fun by drawing fractals :)."
"You can visualize any arbitrary [Graph](https://langchain-ai.github.io/langgraph/reference/graphs/), including [StateGraph](https://langchain-ai.github.io/langgraph/reference/graphs/#langgraph.graph.state.StateGraph). Let's have some fun by drawing fractals :)."
]
},
{
Expand Down Expand Up @@ -109,80 +109,6 @@
"app = build_fractal_graph(3)"
]
},
{
"cell_type": "markdown",
"id": "f4fc9378-b141-4b65-b86c-3afba77f7161",
"metadata": {
"ExecuteTime": {
"end_time": "2024-04-18T12:18:30.605220Z",
"start_time": "2024-04-18T12:18:30.587191Z"
}
},
"source": [
"## Ascii\n",
"\n",
"We can easily visualize this graph in ascii"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a0b22e88-7f78-4215-afdd-4eedef9e2b9b",
"metadata": {},
"outputs": [],
"source": [
"%pip install --quiet grandalf"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "ca9b980d-1f0a-4286-9157-a870e3d55134",
"metadata": {
"ExecuteTime": {
"end_time": "2024-04-19T11:25:37.303260Z",
"start_time": "2024-04-19T11:25:37.273032Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" +-----------+ \n",
" | __start__ | \n",
" +-----------+ \n",
" * \n",
" * \n",
" * \n",
" .......+------------+******** \n",
" ................ *****.| entry_node |....... **************** \n",
" ................ ***********...... +------------+ ****............ **************** \n",
" ............... ************ ..... . ****** ............. *************** \n",
" ................ ************ ...... . ****** ............ **************** \n",
" ................ ******* ...... . ****** ............ **************** \n",
" ........ +-------------------+ .... . **** ........ ******** \n",
" . | node_entry_node_B |****** .. . * .. ** \n",
" ... +-------------------+ ************. . * ... *** \n",
" . **** *** ... *********** . * ... *** \n",
" ... **** ** .. ************ . * .. ** \n",
" . ** ** .. ****** . * .. ** \n",
"+--------------------------+ +--------------------------+ +--------------------------+ **** +-------------------+ \n",
"| node_node_entry_node_B_B |........ | node_node_entry_node_B_C | | node_node_entry_node_B_A | ****** ......| node_entry_node_A | \n",
"+--------------------------+ ...+--------------------------+........ +--------------------------+ ****** ............... +-------------------+ \n",
" ............... .......... . ******* ............. \n",
" ............. ......... . ****** ............... \n",
" ............... ..... . **** ............. \n",
" .....+---------+........ \n",
" | __end__ | \n",
" +---------+ \n"
]
}
],
"source": [
"app.get_graph().print_ascii()"
]
},
{
"cell_type": "markdown",
"id": "edcd9ad2",
Expand Down

0 comments on commit 99fd0ee

Please sign in to comment.