aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/ChatBox/Agent.ts
diff options
context:
space:
mode:
authorA.J. Shulman <Shulman.aj@gmail.com>2024-08-20 18:32:08 -0400
committerA.J. Shulman <Shulman.aj@gmail.com>2024-08-20 18:32:08 -0400
commit79e4c4a3fba42b90ffa656db3ca435505f978afe (patch)
treec1515c97bf88a9e40c95f094c68d7bc0118e0522 /src/client/views/nodes/ChatBox/Agent.ts
parent57dcd9e29a9b622493f8a4246545675385223572 (diff)
supports multiple inputs
maybe also make it so web results cannot have overlap (no same url in websites returned by search) Also make sure it will cite multiple websites
Diffstat (limited to 'src/client/views/nodes/ChatBox/Agent.ts')
-rw-r--r--src/client/views/nodes/ChatBox/Agent.ts52
1 files changed, 47 insertions, 5 deletions
diff --git a/src/client/views/nodes/ChatBox/Agent.ts b/src/client/views/nodes/ChatBox/Agent.ts
index ae08271ee..43138bf94 100644
--- a/src/client/views/nodes/ChatBox/Agent.ts
+++ b/src/client/views/nodes/ChatBox/Agent.ts
@@ -55,13 +55,23 @@ export class Agent {
const systemPrompt = getReactPrompt(Object.values(this.tools), this._summaries, chatHistory);
this.interMessages = [{ role: 'system', content: systemPrompt }];
this.interMessages.push({ role: 'user', content: `<stage number="1" role="user"><query>${question}</query></stage>` });
- const parser = new XMLParser({ ignoreAttributes: false, attributeNamePrefix: '@_' });
+ const parser = new XMLParser({
+ ignoreAttributes: false,
+ attributeNamePrefix: '@_',
+ textNodeName: '_text',
+ isArray: (name, jpath, isLeafNode, isAttribute) => {
+ // Convert tags with the same name to arrays
+ return ['query', 'url'].indexOf(name) !== -1;
+ },
+ });
const builder = new XMLBuilder({ ignoreAttributes: false, attributeNamePrefix: '@_' });
+
let currentAction: string | undefined;
this.processingInfo = [];
for (let i = 2; i < maxTurns; i += 2) {
+ console.log(this.interMessages);
console.log(`Turn ${i}/${maxTurns}`);
const result = await this.execute(onUpdate);
@@ -102,11 +112,14 @@ export class Agent {
break;
}
} else if (key === 'action_input') {
- const actionInput = builder.build({ action_input: stage[key] });
- console.log(`Action input: ${actionInput}`);
+ const actionInput = stage[key];
+ console.log(`Action input:`, actionInput);
if (currentAction) {
try {
- const observation = await this.processAction(currentAction, stage[key].inputs);
+ // Parse the inputs
+ //const parsedInputs = this.parseActionInputs(actionInput.inputs);
+ //console.log(`Parsed inputs:`, parsedInputs);
+ const observation = await this.processAction(currentAction, actionInput.inputs);
const nextPrompt = [{ type: 'text', text: `<stage number="${i + 1}" role="user"> <observation>` }, ...observation, { type: 'text', text: '</observation></stage>' }];
console.log(observation);
this.interMessages.push({ role: 'user', content: nextPrompt });
@@ -198,9 +211,26 @@ export class Agent {
const tool = this.tools[action];
const args: Record<string, any> = {};
+
for (const paramName in tool.parameters) {
if (actionInput[paramName] !== undefined) {
- args[paramName] = actionInput[paramName];
+ if (Array.isArray(actionInput[paramName])) {
+ // If the input is already an array, use it as is
+ args[paramName] = actionInput[paramName];
+ } else if (typeof actionInput[paramName] === 'object' && actionInput[paramName] !== null) {
+ // If the input is an object, check if it has multiple of the same tag
+ const values = Object.values(actionInput[paramName]);
+ if (values.length > 1) {
+ // If there are multiple values, convert to an array
+ args[paramName] = values;
+ } else {
+ // If there's only one value, use it directly
+ args[paramName] = values[0];
+ }
+ } else {
+ // For single values, use them as is
+ args[paramName] = actionInput[paramName];
+ }
} else if (tool.parameters[paramName].required === 'true') {
throw new Error(`Missing required parameter '${paramName}' for action '${action}'`);
}
@@ -208,4 +238,16 @@ export class Agent {
return await tool.execute(args);
}
+
+ private parseActionInputs(inputs: any): Record<string, string | string[]> {
+ const parsedInputs: Record<string, string | string[]> = {};
+ for (const key in inputs) {
+ if (Array.isArray(inputs[key])) {
+ parsedInputs[key] = inputs[key].map((item: any) => item._text);
+ } else {
+ parsedInputs[key] = inputs[key]._text;
+ }
+ }
+ return parsedInputs;
+ }
}