-
Notifications
You must be signed in to change notification settings - Fork 0
/
agentProcessor.js
169 lines (144 loc) · 4.98 KB
/
agentProcessor.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const { Configuration, OpenAIApi } = require("openai");
const dJSON = require("dirty-json");
function formatToolList(toolList) {
let formattedToolList = "";
toolList.forEach((tool) => {
formattedToolList =
formattedToolList +
`${tool.name}:[${tool.arguments.toString()}], ${tool.description}\n`;
});
return formattedToolList;
}
function formatCOTTurns(cotTurns) {
let result = "";
cotTurns.forEach((cotTurn) => {
const { subAgentLog, ...extractedCOTTurn } = { ...cotTurn };
result += `${JSON.stringify(extractedCOTTurn)},\n`;
});
return result;
}
function formatExamples(examples) {
let formattedExampleList = "";
examples.forEach((example) => {
formattedExampleList = formattedExampleList + "\nEXAMPLE:\n";
formattedExampleList += formatCOTTurns(example);
});
return formattedExampleList;
}
async function buildPrompt(agent, agentLog) {
let prompt = `${agent.preamble} \n ${formatToolList(agent.tools)}\n${
agent.rules
}
${formatExamples(agent.examples)}
${agent.postAmble}
${formatCOTTurns(agentLog)}`;
return prompt;
}
exports.runNeuralSubAgent = async function (agent, query) {
const initialAgentLog = [...agent.cotPrompt];
console.log("** running neural sub agent", agent.name, query);
const logResult = await exports.processUserTurn({
agent: agent,
agentLog: initialAgentLog,
userReply: query,
});
console.log(`agent log ${agent.name}`, logResult);
// get last talk from the search result
let finalResult = "no result";
logResult.forEach((logElement) => {
if (logElement.type == "action" && logElement.actionType == "Talk") {
finalResult = logElement.message;
}
});
return { result: finalResult, log: logResult };
};
exports.processUserTurn = async function ({ agent, agentLog, userReply }) {
// add on to the log the user reply as an observation
let updatedLog = [
...agentLog,
{ type: "observation", from: "user", message: userReply },
];
//console.log("** PROMPT");
//console.log(prompt);
//console.log("** END PROMPT");
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
let isThinkingComplete = false;
let interationCount = 0;
while (!isThinkingComplete && interationCount < 5) {
interationCount += 1;
const prompt = await buildPrompt(agent, updatedLog);
const promptResult = await openai.createCompletion({
model: "text-davinci-003",
prompt: prompt,
max_tokens: 500,
temperature: 0,
stop: `{"type":"observation"`,
});
const parsedPromptResult = promptResult.data.choices[0].text;
console.log(parsedPromptResult);
const promptResults = parsedPromptResult.split(",\n");
const updatedLogElements = await Promise.all(
promptResults.map(async (p) => {
if (p.length > 0) {
let observations = [];
//const pp = JSON.parse(p);
//const correctedPromptResult = p.replace(/,\s*([}\]])/g, "$1");
//const correctedPromptResult = p.replace("},", "}");
//const pp = JSON.parse(correctedPromptResult);
let pp = null;
console.log("about to parse: ", p);
try {
pp = dJSON.parse(p);
} catch (ex) {
//const correctedPromptResult = p.replace(/,\s*([}\]])/g, "$1");
let correctedPromptResult = p.replace("},", "}");
console.log("... how about ", correctedPromptResult);
if (correctedPromptResult.charAt(0) != "{") {
correctedPromptResult = `{${correctedPromptResult}`;
}
pp = JSON.parse(correctedPromptResult);
}
if (pp.type == "action") {
// determine if we have a tool for the action type
const matchingTool = agent.tools.find(
(tool) => tool.name == pp.actionType
);
if (matchingTool && matchingTool.callback) {
const callbackResult = await matchingTool.callback(pp);
observations.push({
type: "observation",
from: matchingTool.name,
message: callbackResult.result,
subAgentLog: callbackResult.log,
});
}
}
return { event: pp, observations };
}
})
);
let filteredExpandedUpdatedLog = [];
updatedLogElements.forEach((logElement) => {
if (logElement && logElement.event) {
if (
logElement.event.type == "action" &&
logElement.event.actionType == "talk"
) {
// ready to speak to user, thinking complete
isThinkingComplete = true;
}
filteredExpandedUpdatedLog.push(logElement.event);
if (logElement.observations) {
logElement.observations.forEach((observation) => {
filteredExpandedUpdatedLog.push(observation);
});
}
}
});
updatedLog = [...updatedLog, ...filteredExpandedUpdatedLog];
}
return updatedLog;
};