forked from Asynchronus-ai/graph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.ts
More file actions
40 lines (33 loc) · 1.04 KB
/
agent.ts
File metadata and controls
40 lines (33 loc) · 1.04 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
import { HumanMessage } from "@langchain/core/messages";
import { MongoClient } from "mongodb";
import { MongoDBSaver } from "@langchain/langgraph-checkpoint-mongodb";
import { AgentController } from "./controllers/agentController";
import "dotenv/config";
export async function callAgent(
client: MongoClient,
query: string,
thread_id: string
) {
// Validate environment variables
AgentController.validateEnvironment();
// Create workflow
const workflow = await AgentController.createWorkflow();
// Initialize MongoDB checkpointer
const checkpointer = new MongoDBSaver({
client,
dbName: process.env.DB_NAME as string,
});
// Compile the workflow
const app = workflow.compile({ checkpointer });
// Execute the workflow
const finalState = await app.invoke(
{
messages: [new HumanMessage(query)],
},
{ recursionLimit: 15, configurable: { thread_id: thread_id } }
);
const finalMessage =
finalState.messages[finalState.messages.length - 1].content;
console.log(finalMessage);
return finalMessage;
}