Skip to content

Commit

Permalink
updated subgraph docs
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul committed Aug 19, 2024
1 parent 867496c commit 24d5409
Showing 1 changed file with 51 additions and 70 deletions.
121 changes: 51 additions & 70 deletions examples/how-tos/subgraph.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -81,39 +81,29 @@
"id": "38d1f06f",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[WARN]: You have enabled LangSmith tracing without backgrounding callbacks.\n",
"[WARN]: If you are not using a serverless environment where you must wait for tracing calls to finish,\n",
"[WARN]: we suggest setting \"process.env.LANGCHAIN_CALLBACKS_BACKGROUND=true\" to avoid additional latency.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" name: \u001b[32m'test'\u001b[39m,\n",
" name: 'test',\n",
" path: [\n",
" \u001b[32m'grandparent'\u001b[39m,\n",
" \u001b[32m'parent'\u001b[39m,\n",
" \u001b[32m'grandparent'\u001b[39m,\n",
" \u001b[32m'parent'\u001b[39m,\n",
" \u001b[32m'child_start'\u001b[39m,\n",
" \u001b[32m'child_middle'\u001b[39m,\n",
" \u001b[32m'child_end'\u001b[39m,\n",
" \u001b[32m'sibling'\u001b[39m,\n",
" \u001b[32m'fin'\u001b[39m\n",
" 'grandparent',\n",
" 'parent',\n",
" 'grandparent',\n",
" 'parent',\n",
" 'child_start',\n",
" 'child_middle',\n",
" 'child_end',\n",
" 'sibling',\n",
" 'fin'\n",
" ]\n",
"}\n"
]
}
],
"source": [
"import { END, START, StateGraph } from \"@langchain/langgraph\";\n",
"import { StateGraphArgs } from \"@langchain/langgraph\";\n",
"import { END, START, StateGraph, Annotation } from \"@langchain/langgraph\";\n",
"\n",
"function reduceList(\n",
" left?: string[] | string,\n",
Expand All @@ -132,27 +122,18 @@
" return [...left, ...right];\n",
"}\n",
"\n",
"// Define the state type\n",
"interface IState {\n",
" name: string;\n",
" path: string[];\n",
"}\n",
"\n",
"const graphState: StateGraphArgs<IState>[\"channels\"] = {\n",
" name: {\n",
"const GraphState = Annotation.Root({\n",
" name: Annotation<string>({\n",
" // Overwrite name if a new one is provided\n",
" value: (x: string, y?: string) => (y ? y : x),\n",
" reducer: (x, y) => y ?? x,\n",
" default: () => \"default\",\n",
" },\n",
" path: {\n",
" // Concatenate paths\n",
" value: reduceList,\n",
" default: () => [],\n",
" },\n",
"};\n",
" }),\n",
" path: Annotation<string[]>({\n",
" reducer: reduceList,\n",
" }),\n",
"});\n",
"\n",
"const childBuilder = new StateGraph<IState>({ channels: graphState });\n",
"childBuilder\n",
"const childBuilder = new StateGraph(GraphState)\n",
" .addNode(\"child_start\", (_state) => ({ path: [\"child_start\"] }))\n",
" .addEdge(START, \"child_start\")\n",
" .addNode(\"child_middle\", (_state) => ({ path: [\"child_middle\"] }))\n",
Expand All @@ -161,11 +142,7 @@
" .addEdge(\"child_middle\", \"child_end\")\n",
" .addEdge(\"child_end\", END);\n",
"\n",
"const builder = new StateGraph<IState>({\n",
" channels: graphState,\n",
"});\n",
"\n",
"builder\n",
"const builder = new StateGraph(GraphState)\n",
" .addNode(\"grandparent\", (_state) => ({ path: [\"grandparent\"] }))\n",
" .addEdge(START, \"grandparent\")\n",
" .addNode(\"parent\", (_state) => ({ path: [\"parent\"] }))\n",
Expand Down Expand Up @@ -213,10 +190,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "34a51908",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" name: 'test',\n",
" path: [\n",
" { val: 'grandparent', id: 'd0cb05ae-85d7-4cf6-adf3-b78259509cb4' },\n",
" { val: 'parent', id: 'b5adf2c8-d70b-4f5a-b87c-c2ae09a5d046' },\n",
" { val: 'child_start', id: '57ac546a-a709-4a7d-bd17-90f1a8d4d338' },\n",
" { val: 'child_middle', id: '237b9419-dd07-4602-8abb-4b959228e2a2' },\n",
" { val: 'child_end', id: 'c02960fa-0aff-4f21-a35b-07eb8870bd90' },\n",
" { val: 'sibling', id: 'b097055f-530c-47c7-b704-245f2b2edfcc' },\n",
" { val: 'fin', id: '4ca6eae6-f265-4780-ae1c-c854bf7939dc' }\n",
" ]\n",
"}\n"
]
}
],
"source": [
"import { v4 as uuidv4 } from \"uuid\";\n",
"\n",
Expand Down Expand Up @@ -266,29 +262,18 @@
" return merged;\n",
"}\n",
"\n",
"interface IStateWithIds {\n",
" name: string;\n",
" path: ValWithId[];\n",
"}\n",
"\n",
"const graphState2: StateGraphArgs<IStateWithIds>[\"channels\"] = {\n",
" name: {\n",
"const GraphStateWithIds = Annotation.Root({\n",
" name: Annotation<string>({\n",
" // Overwrite name if a new one is provided\n",
" value: (x: string, y?: string) => (y ? y : x),\n",
" reducer: (x, y) => y ?? x,\n",
" default: () => \"default\",\n",
" },\n",
" path: {\n",
" // Concatenate paths\n",
" value: reduceListWithIds,\n",
" default: () => [],\n",
" },\n",
"};\n",
"\n",
"const childBuilderWithIds = new StateGraph<IStateWithIds>({\n",
" channels: graphState2,\n",
" }),\n",
" path: Annotation<ValWithId[]>({\n",
" reducer: reduceListWithIds,\n",
" }),\n",
"});\n",
"\n",
"childBuilderWithIds\n",
"const childBuilderWithIds = new StateGraph(GraphStateWithIds)\n",
" .addNode(\"child_start\", (_state) => ({\n",
" path: [{ val: \"child_start\" }],\n",
" }))\n",
Expand All @@ -303,11 +288,7 @@
" .addEdge(\"child_middle\", \"child_end\")\n",
" .addEdge(\"child_end\", END);\n",
"\n",
"const builderWithIds = new StateGraph<IStateWithIds>({\n",
" channels: graphState2,\n",
"});\n",
"\n",
"builderWithIds\n",
"const builderWithIds = new StateGraph(GraphStateWithIds)\n",
" .addNode(\"grandparent\", (_state) => ({\n",
" path: [{ val: \"grandparent\" }],\n",
" }))\n",
Expand Down

0 comments on commit 24d5409

Please sign in to comment.