Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Aestheticsuraj234/d38a8c72d98cacc9e1a729c728f5fd09 to your computer and use it in GitHub Desktop.

Select an option

Save Aestheticsuraj234/d38a8c72d98cacc9e1a729c728f5fd09 to your computer and use it in GitHub Desktop.
import { StateGraph, Annotation } from "@langchain/langgraph";
import { ChatOpenAI } from "@langchain/openai";
import { z } from "zod";
const llm = new ChatOpenAI({
model: "gpt-4o-mini" // Fixed model name
});
// Define the state schema
const StateSchema = z.object({
messages: z.array(z.string())
});
// Create the graph annotation
const GraphAnnotation = Annotation.Root({
messages: Annotation({
reducer: (x, y) => x.concat(y), // Reducer to handle state updates
default: () => []
})
});
async function callOpenAI(state) {
console.log(`Inside callOpenAI`, state);
// Return the new messages to be added to state
return {
messages: ['Hey, I just added something to the state'],
};
}
// Create workflow with proper annotation
const workflow = new StateGraph(GraphAnnotation)
.addNode("callOpenAI", callOpenAI)
.addEdge("__start__", "callOpenAI")
.addEdge("callOpenAI", "__end__");
const graph = workflow.compile();
async function runGraph() {
const updatedState = await graph.invoke({ messages: [] });
return updatedState;
}
// Export for use
export { runGraph, graph };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment