-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(graph): passthrough input types to invoke/stream #650
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,14 +20,9 @@ import { | |
} from "@langchain/core/language_models/base"; | ||
import { ChatPromptTemplate } from "@langchain/core/prompts"; | ||
import { All, BaseCheckpointSaver } from "@langchain/langgraph-checkpoint"; | ||
import { | ||
END, | ||
messagesStateReducer, | ||
START, | ||
StateGraph, | ||
} from "../graph/index.js"; | ||
import { END, START, StateGraph } from "../graph/index.js"; | ||
import { MessagesAnnotation } from "../graph/messages_annotation.js"; | ||
import { CompiledStateGraph, StateGraphArgs } from "../graph/state.js"; | ||
import { CompiledStateGraph } from "../graph/state.js"; | ||
import { ToolNode } from "./tool_node.js"; | ||
|
||
export interface AgentState { | ||
|
@@ -107,11 +102,12 @@ export type CreateReactAgentParams = { | |
* // Returns the messages in the state at each step of execution | ||
* ``` | ||
*/ | ||
|
||
export function createReactAgent( | ||
params: CreateReactAgentParams | ||
): CompiledStateGraph< | ||
AgentState, | ||
Partial<AgentState>, | ||
(typeof MessagesAnnotation)["State"], | ||
(typeof MessagesAnnotation)["Update"], | ||
typeof START | "agent" | "tools" | ||
> { | ||
const { | ||
|
@@ -122,12 +118,6 @@ export function createReactAgent( | |
interruptBefore, | ||
interruptAfter, | ||
} = params; | ||
const schema: StateGraphArgs<AgentState>["channels"] = { | ||
messages: { | ||
value: messagesStateReducer, | ||
default: () => [], | ||
}, | ||
}; | ||
|
||
let toolClasses: (StructuredToolInterface | DynamicTool | RunnableToolLike)[]; | ||
if (!Array.isArray(tools)) { | ||
|
@@ -160,9 +150,7 @@ export function createReactAgent( | |
return { messages: [await modelRunnable.invoke(messages, config)] }; | ||
}; | ||
|
||
const workflow = new StateGraph<AgentState>({ | ||
channels: schema, | ||
}) | ||
const workflow = new StateGraph(MessagesAnnotation) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is done primarily to get the correct input types (based off the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's good to make this change, the other syntax is deprecated |
||
.addNode("agent", callModel) | ||
.addNode("tools", new ToolNode<AgentState>(toolClasses)) | ||
.addEdge(START, "agent") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3541,6 +3541,7 @@ export function runPregelTests( | |
hello: "there", | ||
bye: "world", | ||
messages: ["hello"], | ||
// @ts-expect-error This should emit a TS error | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can now actually catch excessive keys! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ha nice! |
||
now: 345, // ignored because not in input schema | ||
}) | ||
).toEqual({ | ||
|
@@ -3553,6 +3554,7 @@ export function runPregelTests( | |
hello: "there", | ||
bye: "world", | ||
messages: ["hello"], | ||
// @ts-expect-error This should emit a TS error | ||
now: 345, // ignored because not in input schema | ||
}) | ||
) | ||
|
@@ -3712,6 +3714,7 @@ export function runPregelTests( | |
}; | ||
const res = await app.invoke( | ||
{ | ||
// @ts-expect-error Messages is not in schema | ||
messages: ["initial input"], | ||
}, | ||
config | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a little strange that
RunInput = StateType<SD>
, I think we should change it to Update?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea I agree, this looks to be the other way around, no? input should be update, output should be based on stream mode (sometimes update, sometimes state, etc)