Created
August 22, 2025 16:11
-
-
Save Aestheticsuraj234/d38a8c72d98cacc9e1a729c728f5fd09 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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