-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path10_conversationalagents.js
132 lines (103 loc) · 3.33 KB
/
10_conversationalagents.js
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { config } from "dotenv";
config();
import { BufferMemory } from "langchain/memory";
import { Calculator } from "@langchain/community/tools/calculator";
import { DynamicTool } from "@langchain/community/tools/dynamic";
import { RunnableSequence } from "@langchain/core/runnables";
import { AgentExecutor } from "langchain/agents";
import { formatToOpenAIFunctionMessages } from "langchain/agents/format_scratchpad";
import { OpenAIFunctionsAgentOutputParser } from "langchain/agents/openai/output_parser";
import {
ChatPromptTemplate,
MessagesPlaceholder,
} from "@langchain/core/prompts";
import { ChatOpenAI, formatToOpenAIFunction } from "@langchain/openai";
process.env.LANGCHAIN_HANDLER = "langchain";
function getTodayDateTime() {
const timeZone = 'America/Chicago';
const options = {
timeZone: timeZone,
year: 'numeric',
month: 'long',
day: 'numeric',
weekday: 'long',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: true
};
const today = new Date();
const formattedDate = today.toLocaleString('en-US', options);
const result = {
"formattedDate": formattedDate,
"timezone": timeZone
};
return JSON.stringify(result);
}
const dateTool = new DynamicTool({
name: "todays_date_time",
description:
"Useful to get current day, date and time.",
func: async () => getTodayDateTime(),
});
const tools = [new Calculator(), dateTool];
const prompt = ChatPromptTemplate.fromMessages([
["system", "You are very powerful assistant"],
new MessagesPlaceholder("history"),
["human", "{input}"],
new MessagesPlaceholder("agent_scratchpad"),
]);
/**
* Define your chat model to use.
*/
const model = new ChatOpenAI({
temperature: 0,
});
const modelWithFunctions = model.bind({
functions: tools.map((tool) => formatToOpenAIFunction(tool)),
});
// Default "inputKey", "outputKey", and "memoryKey values would work here
// but we specify them for clarity.
const memory = new BufferMemory({
returnMessages: true,
inputKey: "input",
outputKey: "output",
memoryKey: "history",
});
console.log(await memory.loadMemoryVariables({}));
const runnableAgent = RunnableSequence.from([
{
input: (i) => i.input,
memory: () => memory.loadMemoryVariables({}),
agent_scratchpad: (i) => formatToOpenAIFunctionMessages(i.steps),
},
{
input: (previousOutput) => previousOutput.input,
agent_scratchpad: (previousOutput) => previousOutput.agent_scratchpad,
history: (previousOutput) => previousOutput.memory.history,
},
prompt,
modelWithFunctions,
new OpenAIFunctionsAgentOutputParser()
]);
const executor = AgentExecutor.fromAgentAndTools({
agent: runnableAgent,
tools,
});
const input0 = { input: "Harry Potter's birthday Jan 01, 2000" };
const result0 = await executor.invoke(input0);
console.log(result0);
// Save to History
await memory.saveContext(input0, {
output: result0.output,
});
console.log(await memory.loadMemoryVariables({}));
const input1 = { input: "What is his age today? show how you calculated it." };
const result1 = await executor.invoke(input1);
console.log(result1);
await memory.saveContext(input1, {
output: result1.output,
});
const input2 = { input: "Multiply his age raised to the second power then add 5? show your calculation}" };
const result2 = await executor.invoke(input2);
console.log(result2);