cleaned init params

This commit is contained in:
Maximus 2023-12-21 20:42:01 -07:00
parent c0da4a58f0
commit 1a854fe0d5
8 changed files with 29 additions and 124 deletions

1
.gitignore vendored
View file

@ -4,3 +4,4 @@ package-lock.json
scratch.js scratch.js
!agent_code/template.js !agent_code/template.js
bots/**/action-code/** bots/**/action-code/**
bots/**/save.json

View file

@ -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."
}
]
}

View file

@ -1,3 +1,3 @@
import { AgentProcess } from './src/process/agent-process.js'; import { AgentProcess } from './src/process/agent-process.js';
new AgentProcess('andy').start(true, false); new AgentProcess('andy').start('assist');

View file

@ -8,16 +8,14 @@ import { Events } from './events.js';
export class Agent { export class Agent {
constructor(name, save_path, load_path=null, init_message=null) { constructor(name, profile=null, init_message=null) {
this.name = name; this.name = name;
this.bot = initBot(name); this.bot = initBot(name);
this.history = new History(this, save_path); this.history = new History(this);
this.history.loadExamples(); this.history.loadExamples();
this.coder = new Coder(this); this.coder = new Coder(this);
if (load_path) { this.history.load(profile);
this.history.load(load_path);
}
this.events = new Events(this, this.history.events) this.events = new Events(this, this.history.events)

View file

@ -1,4 +1,4 @@
import { writeFile, readFile, unlink } from 'fs'; import { writeFile, readFile, unlink, mkdirSync } from 'fs';
export class Coder { export class Coder {
constructor(agent) { constructor(agent) {
@ -17,6 +17,8 @@ export class Coder {
if (err) throw err; if (err) throw err;
this.code_template = data; this.code_template = data;
}); });
mkdirSync('.' + this.fp, { recursive: true });
} }
queueCode(code) { queueCode(code) {

View file

@ -5,9 +5,9 @@ import { sendRequest, embed, cosineSimilarity } from '../utils/gpt.js';
export class History { export class History {
constructor(agent, save_path) { constructor(agent) {
this.name = agent.name; this.name = agent.name;
this.save_path = save_path; this.save_path = `./bots/${this.name}/save.json`;
this.turns = []; this.turns = [];
// These define an agent's long term memory // These define an agent's long term memory
@ -148,12 +148,9 @@ export class History {
await this.setExamples(); await this.setExamples();
} }
save(save_path=null) { save() {
if (save_path == null)
save_path = this.save_path;
if (save_path === '' || save_path == null) return;
// save history object to json file // save history object to json file
mkdirSync('bots', { recursive: true }); mkdirSync(`./bots/${this.name}`, { recursive: true });
let data = { let data = {
'name': this.name, 'name': this.name,
'bio': this.bio, 'bio': this.bio,
@ -162,7 +159,7 @@ export class History {
'turns': this.turns 'turns': this.turns
}; };
const json_data = JSON.stringify(data, null, 4); const json_data = JSON.stringify(data, null, 4);
writeFileSync(save_path, json_data, (err) => { writeFileSync(this.save_path, json_data, (err) => {
if (err) { if (err) {
throw err; throw err;
} }
@ -170,10 +167,8 @@ export class History {
}); });
} }
load(load_path=null) { load(profile) {
if (load_path == null) const load_path = profile? `./bots/${this.name}/${profile}.json` : this.save_path;
load_path = this.save_path;
if (load_path === '' || load_path == null) return;
try { try {
// load history object from json file // load history object from json file
const data = readFileSync(load_path, 'utf8'); const data = readFileSync(load_path, 'utf8');
@ -183,8 +178,7 @@ export class History {
this.events = obj.events; this.events = obj.events;
this.turns = obj.turns; this.turns = obj.turns;
} catch (err) { } catch (err) {
console.log('No history file found for ' + this.name + '.'); console.error(`No file for profile '${load_path}' for agent ${this.name}.`);
console.log(load_path);
} }
} }
} }

View file

@ -4,13 +4,12 @@ export class AgentProcess {
constructor(name) { constructor(name) {
this.name = 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]; let args = ['src/process/init-agent.js', this.name];
args.push('-p', profile); if (profile)
if (clear_memory) args.push('-p', profile);
args.push('-c'); if (init_message)
if (autostart) args.push('-m', init_message);
args.push('-a');
const agentProcess = spawn('node', args, { const agentProcess = spawn('node', args, {
stdio: 'inherit', stdio: 'inherit',
@ -28,7 +27,7 @@ export class AgentProcess {
process.exit(1); process.exit(1);
} }
console.log('Restarting agent...'); 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(); last_restart = Date.now();
} }
}); });

View file

@ -3,7 +3,7 @@ import yargs from 'yargs';
const args = process.argv.slice(2); const args = process.argv.slice(2);
if (args.length < 1) { if (args.length < 1) {
console.log('Usage: node init_agent.js <agent_name> [-c] [-a]'); console.log('Usage: node init_agent.js <agent_name> [profile] [init_message]');
process.exit(1); process.exit(1);
} }
@ -13,20 +13,11 @@ const argv = yargs(args)
type: 'string', type: 'string',
description: 'profile to use for agent' description: 'profile to use for agent'
}) })
.option('clear_memory', { .option('init_message', {
alias: 'c', alias: 'm',
type: 'boolean', type: 'string',
description: 'restart memory from scratch'
})
.option('autostart', {
alias: 'a',
type: 'boolean',
description: 'automatically prompt the agent on startup' description: 'automatically prompt the agent on startup'
}).argv }).argv
const name = argv._[0]; const name = args[0];
const save_path = `./bots/${name}/memories.json`; new Agent(name, argv.profile, argv.init_message);
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);