From a99909ac5c375a5cb2c3e919d39b74698b23327a Mon Sep 17 00:00:00 2001 From: MaxRobinsonTheGreat Date: Tue, 9 Jan 2024 13:42:39 -0600 Subject: [PATCH] basic execute_action functionality added --- src/agent/coder.js | 101 +++++++++++++------------------------ src/agent/history.js | 2 +- src/agent/skill-library.js | 2 +- 3 files changed, 37 insertions(+), 68 deletions(-) diff --git a/src/agent/coder.js b/src/agent/coder.js index 2446f08..25e1379 100644 --- a/src/agent/coder.js +++ b/src/agent/coder.js @@ -1,6 +1,7 @@ import { writeFile, readFile, mkdirSync } from 'fs'; import { sendRequest, embed, cosineSimilarity } from '../utils/gpt.js'; import { stringifyTurns } from '../utils/text.js'; +import { getSkillDocs } from './skill-library.js'; export class Coder { @@ -54,91 +55,59 @@ export class Coder { }); } - async loadExamples() { - let examples = []; - try { - const data = readFileSync('./src/examples.json', 'utf8'); - examples = JSON.parse(data); - } catch (err) { - console.log('No history examples found.'); - } - - this.examples = []; - for (let example of examples) { - let context = ''; - for (let turn of example.conversation) { - context += turn.content + '\n'; - } - context = context.trim(); - const embedding = await embed(context); - this.examples.push({'embedding': embedding, 'turns': example}); - } - - await this.setExamples(); - } - - async sortExamples(messages) { - let context = ''; - for (let turn of messages) { - context += turn.content + '\n'; - } - context = context.trim(); - const embedding = await embed(context); - this.examples.sort((a, b) => { - return cosineSimilarity(a.embedding, embedding) - cosineSimilarity(b.embedding, embedding); - }); - } async generateCode(agent_history) { - let system_message = "You are a minecraft bot that plays minecraft by writing javascript. Given the conversation between you and the user, use the provided skills and world queries to write your code. You will then be given a response to your code. If you are satisfied with the response, return output without writing any additional code. If you want to try again, output the code you want to try."; + let system_message = "You are a minecraft bot that plays minecraft by writing javascript codeblocks. Given the conversation between you and the user, use the provided skills and world queries to write your code in a codeblock. Example response: ``` // your code here ``` You will then be given a response to your code. If you are satisfied with the response, respond without a codeblock in a conversational way. If something went wrong, write another codeblock and try to fix the problem."; system_message += getSkillDocs(); - let messages = []; - this.sortExamples(agent_history.turns); - for (let example of this.examples.slice(-this.fewshot)) { - messages.push({ - role: 'user', - content: stringifyTurns(example.conversation) - }); - for (let i = 0; i < example.coder.length; i++) { - messages.push({ - role: i % 2 == 0 ? 'assistant' : 'user', - content: example.coder[i] - }); - } - } - messages.push({ - role: 'user', - content: stringifyTurns(agent_history.turns), - }); + system_message += "\n\nExamples:\nUser zZZn98: come here \nAssistant: I am going to navigate to zZZn98. ```\nawait skills.goToPlayer(bot, 'zZZn98');```\nSystem: Code execution finished successfully.\nAssistant: Done."; - let final_message = 'No code generated.'; + let messages = agent_history.getHistory(false); + + let code_return = null; + let failures = 0; for (let i=0; i<5; i++) { - + console.log(messages) let res = await sendRequest(messages, system_message); + console.log('Code generation response:', res) + let contains_code = res.indexOf('```') !== -1; + if (!contains_code) { + if (code_return) { + agent_history.add('system', code_return.message); + agent_history.add(this.agent.name, res); + this.agent.bot.chat(res); + return; + } + if (failures >= 1) { + agent_history.add('system', 'Action failed, agent would not write code.'); + return; + } + messages.push({ + role: 'system', + content: 'Error: no code provided. Write code in codeblock in your response. ``` // example ```'} + ); + failures++; + continue; + } let code = res.substring(res.indexOf('```')+3, res.lastIndexOf('```')); - if (!code) - break; + this.queueCode(code); + code_return = await this.execute(); - agent.coder.queueCode(code); - let code_return = await agent.coder.execute(); - - if (code_return.interrupted && !custom_return.timedout) - break; + if (code_return.interrupted && !code_return.timedout) + return; messages.push({ role: 'assistant', content: res }); messages.push({ - role: 'user', + role: 'system', content: code_return.message }); - final_message = code_return.message; } - - return final_message; + + return } // returns {success: bool, message: string, interrupted: bool, timedout: false} diff --git a/src/agent/history.js b/src/agent/history.js index d26a97d..9dcb6b7 100644 --- a/src/agent/history.js +++ b/src/agent/history.js @@ -38,7 +38,7 @@ export class History { } getSystemMessage() { - let system_message = `You are a playful Minecraft bot named '${this.name}' that can communicate with players, see, move, mine, build, and interact with the world by writing and executing code. Act human-like as if you were a typical Minecraft player, rather than an AI. Be very brief in your responses, omit needless words, and do not give instructions unless asked.`; + let system_message = `You are a playful Minecraft bot named '${this.name}' that can communicate with players, see, move, mine, build, and interact with the world by writing and executing code. Act human-like as if you were a typical Minecraft player, rather than an AI. Be very brief in your responses, use actions often, and do not give instructions unless asked.`; system_message += getQueryDocs(); system_message += getCommandDocs(); if (this.bio != '') diff --git a/src/agent/skill-library.js b/src/agent/skill-library.js index 246e3ab..3bca956 100644 --- a/src/agent/skill-library.js +++ b/src/agent/skill-library.js @@ -2,7 +2,7 @@ import * as skills from './skills.js'; import * as world from './world.js'; export function getSkillDocs() { - let docstring = "\n*SKILL DOCS\nThese skills are javascript functions that can be called with a js function by writing a code block. Ex: '```// write description comment and code here```' \nYour code block should return a bool indicating if the task was completed successfully. It will return true if you don't write a return statement.\n"; + let docstring = "\n*SKILL DOCS\nThese skills are javascript functions that can be called when writing actions and skills.\n"; docstring += docHelper(Object.values(skills), 'skills'); docstring += docHelper(Object.values(world), 'world'); return docstring + '*\n';