Skip to content

Commit

Permalink
cr
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul committed Aug 22, 2024
1 parent 7109824 commit 421c549
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions examples/how-tos/define-state.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
"\n",
"## Getting started\n",
"\n",
"The `Annotation` function is the recommended way to define your graph state for new `StateGraph` graphs. The `Annotation` function has a `Root` function which is called to define the state. Then, each field of the object represents a channel in the graph. Each channel can optionally have `reducer` and `default` functions passed in. For more information on `reducers`, see the [reducers conceptual guide](https://langchain-ai.github.io/langgraphjs/concepts/low_level/#reducers). The below example shows how to define a simple graph state with one field `messages`."
"The `Annotation` function is the recommended way to define your graph state for new `StateGraph` graphs. The `Annotation.Root` function is used to create the top-level state object, where each field represents a channel in the graph.\n",
"\n",
"Here's an example of how to define a simple graph state with one channel called `messages`:"
]
},
{
Expand All @@ -36,8 +38,11 @@
"import { Annotation } from \"@langchain/langgraph\";\n",
"\n",
"const GraphAnnotation = Annotation.Root({\n",
" // Define a 'messages' channel to store an array of BaseMessage objects\n",
" messages: Annotation<BaseMessage[]>({\n",
" // Reducer function: Combines the current state with new messages\n",
" reducer: (currentState, updateValue) => currentState.concat(updateValue),\n",
" // Default function: Initialize the channel with an empty array\n",
" default: () => [],\n",
" })\n",
"});"
Expand All @@ -47,7 +52,11 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Each annotation value can contain a `reducer`, and `default` factory functions, however this is optional. Say you want to define a graph state with fields `question` and `answer` which do not need a reducer or default value. You can define the graph state as follows:"
"Each channel can optionally have `reducer` and `default` functions:\n",
"- The `reducer` function defines how new values are combined with the existing state.\n",
"- The `default` function provides an initial value for the channel.\n",
"\n",
"For more information on reducers, see the [reducers conceptual guide](https://langchain-ai.github.io/langgraphjs/concepts/low_level/#reducers)"
]
},
{
Expand Down

0 comments on commit 421c549

Please sign in to comment.