From 1a854fe0d5c4483efa6d3a0f00c0b4e25754db1f Mon Sep 17 00:00:00 2001 From: Maximus Date: Thu, 21 Dec 2023 20:42:01 -0700 Subject: [PATCH] cleaned init params --- .gitignore | 3 +- bots/andy/memories.json | 80 ------------------------------------ main.js | 2 +- src/agent/agent.js | 8 ++-- src/agent/coder.js | 4 +- src/agent/history.js | 22 ++++------ src/process/agent-process.js | 13 +++--- src/process/init-agent.js | 21 +++------- 8 files changed, 29 insertions(+), 124 deletions(-) delete mode 100644 bots/andy/memories.json diff --git a/.gitignore b/.gitignore index 791d367..633ddad 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ node_modules/ package-lock.json scratch.js !agent_code/template.js -bots/**/action-code/** \ No newline at end of file +bots/**/action-code/** +bots/**/save.json \ No newline at end of file diff --git a/bots/andy/memories.json b/bots/andy/memories.json deleted file mode 100644 index a4d62b5..0000000 --- a/bots/andy/memories.json +++ /dev/null @@ -1,80 +0,0 @@ -{ - "name": "andy", - "bio": "You are playing minecraft and assisting other players in tasks.", - "memory": "", - "events": [], - "turns": [ - { - "role": "user", - "content": "sumixam0: come here andy" - }, - { - "role": "assistant", - "content": "```\n// I am going to navigate to sumixam0. \nawait skills.goToPlayer(bot, 'sumixam0');\n```" - }, - { - "role": "system", - "content": "Code output:\nYou have reached at 300.7930424155586, 78, -115.19151414785698.\nPlayer is now at (300.7930424155586, 78, -115.19151414785698).\nCode finished.\n" - }, - { - "role": "assistant", - "content": "I'm here, sumixam0! What can I do for you?" - }, - { - "role": "user", - "content": "sumixam0: follow me!" - }, - { - "role": "assistant", - "content": "Sure, I'll follow you, Sumixam0!\n```\n// I am going to follow sumixam0.\nawait skills.followPlayer(bot, 'sumixam0');\n```" - }, - { - "role": "user", - "content": "sumixam0: place some dirt" - }, - { - "role": "assistant", - "content": "Sure, I'll place some dirt.\n```\n// I am going to place dirt at my current position.\nlet position = world.getPosition(bot);\nawait skills.placeBlock(bot, 'dirt', position.x, position.y, position.z);\n```" - }, - { - "role": "system", - "content": "Code output:\nDon't have any dirt to place.\nCode finished.\n" - }, - { - "role": "assistant", - "content": "I apologize, but I don't have any dirt to place at the moment." - }, - { - "role": "user", - "content": "sumixam0: try again" - }, - { - "role": "assistant", - "content": "Sure, let me try again.\n```\n// I am going to place dirt at my current position.\nlet position = world.getPosition(bot);\nawait skills.placeBlock(bot, 'dirt', position.x, position.y - 1, position.z);\n```" - }, - { - "role": "system", - "content": "Code output:\nCannot place block at (303, 77, -116) because grass_block is in the way.\nCode finished.\n" - }, - { - "role": "assistant", - "content": "I apologize, but I am unable to place the dirt block here because there is grass in the way." - }, - { - "role": "user", - "content": "sumixam0: try placing one higher" - }, - { - "role": "assistant", - "content": "Alright, I'll try placing the dirt block one block higher.\n```\n// I am going to place dirt one block higher than my current position.\nlet position = world.getPosition(bot);\nawait skills.placeBlock(bot, 'dirt', position.x, position.y, position.z);\n```" - }, - { - "role": "system", - "content": "Code output:\nSuccessfully placed dirt at (303, 78, -116).\nCode finished.\n" - }, - { - "role": "assistant", - "content": "I have successfully placed a dirt block one block higher." - } - ] -} \ No newline at end of file diff --git a/main.js b/main.js index 1a73182..6d6e133 100644 --- a/main.js +++ b/main.js @@ -1,3 +1,3 @@ import { AgentProcess } from './src/process/agent-process.js'; -new AgentProcess('andy').start(true, false); \ No newline at end of file +new AgentProcess('andy').start('assist'); \ No newline at end of file diff --git a/src/agent/agent.js b/src/agent/agent.js index 2b8938d..d26c75c 100644 --- a/src/agent/agent.js +++ b/src/agent/agent.js @@ -8,16 +8,14 @@ import { Events } from './events.js'; export class Agent { - constructor(name, save_path, load_path=null, init_message=null) { + constructor(name, profile=null, init_message=null) { this.name = name; this.bot = initBot(name); - this.history = new History(this, save_path); + this.history = new History(this); this.history.loadExamples(); this.coder = new Coder(this); - if (load_path) { - this.history.load(load_path); - } + this.history.load(profile); this.events = new Events(this, this.history.events) diff --git a/src/agent/coder.js b/src/agent/coder.js index 0f3e8ce..23f3026 100644 --- a/src/agent/coder.js +++ b/src/agent/coder.js @@ -1,4 +1,4 @@ -import { writeFile, readFile, unlink } from 'fs'; +import { writeFile, readFile, unlink, mkdirSync } from 'fs'; export class Coder { constructor(agent) { @@ -17,6 +17,8 @@ export class Coder { if (err) throw err; this.code_template = data; }); + + mkdirSync('.' + this.fp, { recursive: true }); } queueCode(code) { diff --git a/src/agent/history.js b/src/agent/history.js index d8d469a..6f1cfd0 100644 --- a/src/agent/history.js +++ b/src/agent/history.js @@ -5,9 +5,9 @@ import { sendRequest, embed, cosineSimilarity } from '../utils/gpt.js'; export class History { - constructor(agent, save_path) { + constructor(agent) { this.name = agent.name; - this.save_path = save_path; + this.save_path = `./bots/${this.name}/save.json`; this.turns = []; // These define an agent's long term memory @@ -148,12 +148,9 @@ export class History { await this.setExamples(); } - save(save_path=null) { - if (save_path == null) - save_path = this.save_path; - if (save_path === '' || save_path == null) return; + save() { // save history object to json file - mkdirSync('bots', { recursive: true }); + mkdirSync(`./bots/${this.name}`, { recursive: true }); let data = { 'name': this.name, 'bio': this.bio, @@ -162,7 +159,7 @@ export class History { 'turns': this.turns }; const json_data = JSON.stringify(data, null, 4); - writeFileSync(save_path, json_data, (err) => { + writeFileSync(this.save_path, json_data, (err) => { if (err) { throw err; } @@ -170,10 +167,8 @@ export class History { }); } - load(load_path=null) { - if (load_path == null) - load_path = this.save_path; - if (load_path === '' || load_path == null) return; + load(profile) { + const load_path = profile? `./bots/${this.name}/${profile}.json` : this.save_path; try { // load history object from json file const data = readFileSync(load_path, 'utf8'); @@ -183,8 +178,7 @@ export class History { this.events = obj.events; this.turns = obj.turns; } catch (err) { - console.log('No history file found for ' + this.name + '.'); - console.log(load_path); + console.error(`No file for profile '${load_path}' for agent ${this.name}.`); } } } \ No newline at end of file diff --git a/src/process/agent-process.js b/src/process/agent-process.js index 48eb412..c81de04 100644 --- a/src/process/agent-process.js +++ b/src/process/agent-process.js @@ -4,13 +4,12 @@ export class AgentProcess { constructor(name) { this.name = name; } - start(clear_memory=false, autostart=false, profile='assist') { + start(profile='assist', init_message=null) { let args = ['src/process/init-agent.js', this.name]; - args.push('-p', profile); - if (clear_memory) - args.push('-c'); - if (autostart) - args.push('-a'); + if (profile) + args.push('-p', profile); + if (init_message) + args.push('-m', init_message); const agentProcess = spawn('node', args, { stdio: 'inherit', @@ -28,7 +27,7 @@ export class AgentProcess { process.exit(1); } console.log('Restarting agent...'); - this.start(false, true); + this.start('save', 'Agent process restarted. Notify the user and decide what to do.'); last_restart = Date.now(); } }); diff --git a/src/process/init-agent.js b/src/process/init-agent.js index 4421596..584f1bb 100644 --- a/src/process/init-agent.js +++ b/src/process/init-agent.js @@ -3,7 +3,7 @@ import yargs from 'yargs'; const args = process.argv.slice(2); if (args.length < 1) { - console.log('Usage: node init_agent.js [-c] [-a]'); + console.log('Usage: node init_agent.js [profile] [init_message]'); process.exit(1); } @@ -13,20 +13,11 @@ const argv = yargs(args) type: 'string', description: 'profile to use for agent' }) - .option('clear_memory', { - alias: 'c', - type: 'boolean', - description: 'restart memory from scratch' - }) - .option('autostart', { - alias: 'a', - type: 'boolean', + .option('init_message', { + alias: 'm', + type: 'string', description: 'automatically prompt the agent on startup' }).argv -const name = argv._[0]; -const save_path = `./bots/${name}/memories.json`; -const load_path = !!argv.clear_memory ? `./bots/${name}/${argv.profile}.json` : save_path; -const init_message = !!argv.autostart ? 'Agent process restarted. Notify the user and decide what to do.' : null; - -new Agent(name, save_path, load_path, init_message); +const name = args[0]; +new Agent(name, argv.profile, argv.init_message);