-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13_custom_langgraph.js
More file actions
67 lines (49 loc) 路 1.71 KB
/
Copy path13_custom_langgraph.js
File metadata and controls
67 lines (49 loc) 路 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { ChatGroq } from "@langchain/groq";
import { StateGraph, START, END, Annotation } from "@langchain/langgraph";
import "dotenv/config";
//1. setup llm
const llm = new ChatGroq({
apiKey: process.env.GROQ_API_KEY,
model: "openai/gpt-oss-120b",
temperature: 0.7,
});
//2. define state , graph's globle memory
const stateGraph = Annotation.Root({
topic: Annotation(),
draft: Annotation(),
review: Annotation()
});
//3. define node, worker
//A. writer
async function writerNode(state) {
console.log(`[Writer Node]: Drafting content for -> "${state.topic}."`);
const prompt = `Write a short, engaging fact about: ${state.topic}.`;
const response = await llm.invoke(prompt);
//update draft
return { draft: response.content }
}
//b. Reviewer node
async function reviewerNode(state) {
console.log(`[Reviewer Node]: checking the draft...\n`);
const prompt = `Review this draft and give a strict 1-line feedback (Good/Bad and why):\nDraft: ${state.draft}`;
const response = await llm.invoke(prompt);
//update reviewerNode
return { review: response.content }
}
//build custom graph
const workflow = new StateGraph(stateGraph)
.addNode("writer", writerNode)
.addNode("reviewer", reviewerNode)
.addEdge(START, "writer")
.addEdge("writer", "reviewer")
.addEdge("reviewer", END)
const app = workflow.compile();
async function main() {
const userInput = "Future of AI agents."
const result = await app.invoke({ topic: userInput });
console.log("\nFinal output...\n");
console.log(`Topic : ${result.topic}\n`);
console.log(`Draft : ${result.draft}\n`);
console.log(`Review : ${result.review}`);
}
main().catch(console.error);