-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathagent.js
More file actions
117 lines (105 loc) · 2.5 KB
/
agent.js
File metadata and controls
117 lines (105 loc) · 2.5 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { model, systemPrompt } from "./llm.js";
import {
HumanMessage,
SystemMessage,
AIMessage,
ToolMessage
} from "@langchain/core/messages";
import { v4 as uuidv4 } from "uuid";
import {
START,
END,
MessagesAnnotation,
StateGraph,
MemorySaver,
} from "@langchain/langgraph";
import { ToolNode } from "@langchain/langgraph/prebuilt";
import * as cheerio from 'cheerio';
import {
multiply,
divide,
add,
subtract,
random,
insult,
qoute
} from "./tools.js";
const callModel = async (state) => {
const response = await model.invoke(state.messages);
return { messages: response };
};
const tools = [
multiply,
divide,
add,
subtract,
random,
insult,
qoute
];
const toolNode = new ToolNode(tools);
const shouldContinue = (state) => {
const { messages } = state;
const lastMessage = messages[messages.length - 1];
if ("tool_calls" in lastMessage && Array.isArray(lastMessage.tool_calls) && lastMessage.tool_calls?.length) {
console.log("tool calls", lastMessage.tool_calls);
return "tool";
}
console.log(lastMessage)
return "__end__";
}
const workflow = new StateGraph(MessagesAnnotation)
// Define the (single) node in the graph
.addNode("model", callModel)
.addNode("tool", toolNode)
.addEdge(START, "model")
.addConditionalEdges("model", shouldContinue)
.addEdge("tool", "model")
.addEdge("model", END)
const config = {
configurable: {
thread_id: uuidv4()
}
};
export const app = workflow.compile({ checkpointer: new MemorySaver() });
export async function agent(messages, conf = config) {
const result = await app.invoke({ messages: [systemPrompt, ...messages] }, conf);
return result.messages[result.messages.length - 1]
}
export function extract(input, cheerio) {
const $ = cheerio.load(input);
const thinkTexts = [];
$('think').each((i, el) => {
thinkTexts.push($(el).text().trim());
});
$('think').remove();
const text = $.root().text().trim();
return {
think: thinkTexts,
text: text
};
}
export const AIMessageParser = (message) => {
var data = extract(message.content, cheerio);
var text = data.text;
var think = data.think[0] || `\n`;
var think = think.split('\n');
var response = ``;
for (var i = 0; i < think.length; i++) {
response += `> ${think[i]}\n`;
}
response += text;
if (think.length > 0) {
response = text;
}
return {
think: think,
response: response
}
};
console.log('Agent is ready!')
export default {
app,
AIMessageParser,
agent
}