Skip to content

Commit

Permalink
fix(docs): use messagesStateReducer instead of concat to match Py…
Browse files Browse the repository at this point in the history
…thon

Related #695
  • Loading branch information
dqbd committed Dec 1, 2024
1 parent 72de160 commit 7c420f8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,18 @@ import { tool } from "@langchain/core/tools";
import { z } from "zod";
import { ChatAnthropic } from "@langchain/anthropic";
import { StateGraph } from "@langchain/langgraph";
import { MemorySaver, Annotation } from "@langchain/langgraph";
import { MemorySaver, Annotation, messagesStateReducer } from "@langchain/langgraph";
import { ToolNode } from "@langchain/langgraph/prebuilt";

// Define the graph state
// See here for more info: https://langchain-ai.github.io/langgraphjs/how-tos/define-state/
const StateAnnotation = Annotation.Root({
messages: Annotation<BaseMessage[]>({
reducer: (x, y) => x.concat(y),
})
})
// `messagesStateReducer` function defines how `messages` state key should be updated
// (in this case it appends new messages to the list and overwrites messages with the same ID)
reducer: messagesStateReducer,
}),
});

// Define the tools for the agent to use
const weatherTool = tool(async ({ query }) => {
Expand Down
10 changes: 6 additions & 4 deletions libs/langgraph/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,18 @@ import { tool } from "@langchain/core/tools";
import { z } from "zod";
import { ChatAnthropic } from "@langchain/anthropic";
import { StateGraph } from "@langchain/langgraph";
import { MemorySaver, Annotation } from "@langchain/langgraph";
import { MemorySaver, Annotation, messagesStateReducer } from "@langchain/langgraph";
import { ToolNode } from "@langchain/langgraph/prebuilt";

// Define the graph state
// See here for more info: https://langchain-ai.github.io/langgraphjs/how-tos/define-state/
const StateAnnotation = Annotation.Root({
messages: Annotation<BaseMessage[]>({
reducer: (x, y) => x.concat(y),
})
})
// `messagesStateReducer` function defines how `messages` state key should be updated
// (in this case it appends new messages to the list and overwrites messages with the same ID)
reducer: messagesStateReducer,
}),
});

// Define the tools for the agent to use
const weatherTool = tool(async ({ query }) => {
Expand Down

0 comments on commit 7c420f8

Please sign in to comment.