Merge pull request #8 from kolbytn/memories

Memories
This commit is contained in:
Kolby Nottingham 2023-11-15 17:17:51 -08:00 committed by GitHub
commit 725b3e3b8a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 268 additions and 55 deletions

1
.gitignore vendored
View file

@ -1,5 +1,6 @@
.vscode/
node_modules/
bots/
package-lock.json
temp.js
scratch.js

View file

@ -2,33 +2,33 @@ import { initBot } from './utils/mcdata.js';
import { sendRequest } from './utils/gpt.js';
import { History } from './utils/history.js';
import { Coder } from './utils/coder.js';
import { getQuery, containsQuery, getQueryDocs } from './utils/queries.js';
import { getSkillDocs, containsCodeBlock } from './utils/skill_library.js';
import { getQuery, containsQuery } from './utils/queries.js';
import { containsCodeBlock } from './utils/skill_library.js';
export class Agent {
constructor(name) {
constructor(name, save_path) {
this.name = name;
this.system_message = `You are a playful Minecraft bot named '${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 brief in your responses, omit needless words, and do not give instructions unless asked.`;
this.system_message += getQueryDocs();
this.system_message += getSkillDocs();
this.bot = initBot(name);
this.history = new History(this);
this.history = new History(this, save_path);
this.coder = new Coder(this);
this.history.load();
this.bot.on('login', () => {
this.bot.chat('Hello world! I am ' + this.name);
console.log(`${this.name} logged in.`);
});
}
async start() {
await this.history.loadExamples();
this.bot.on('chat', (username, message) => {
if (username === this.name) return;
console.log('received message from', username, ':', message);
this.respond(username, message);
this.history.save();
});
this.bot.on('finished_executing', () => {
@ -41,9 +41,9 @@ export class Agent {
}
async respond(username, message) {
this.history.add(username, message);
await this.history.add(username, message);
for (let i=0; i<5; i++) {
let res = await sendRequest(this.history.getHistory(), this.system_message);
let res = await sendRequest(this.history.getHistory(), this.history.getSystemMessage());
this.history.add(this.name, res);
let query_cmd = containsQuery(res);
if (query_cmd) { // contains query

View file

@ -1,3 +1,4 @@
import { Agent } from './agent.js';
new Agent('andy');
let agent = new Agent('andy', 'bots/andy.json');
agent.start();

49
utils/examples.json Normal file
View file

@ -0,0 +1,49 @@
[
[
{"role": "user", "content": "miner_32: Hey! What are you up to?"},
{"role": "assistant", "content": "Nothing much miner_32, what do you need?"}
],
[
{"role": "user", "content": "grombo_Xx: What do you see?"},
{"role": "assistant", "content": "Let me see... !blocks"},
{"role": "system", "content": "NEARBY_BLOCKS\n- oak_log\n- dirt\n- cobblestone"},
{"role": "assistant", "content": "I see some oak logs, dirt, and cobblestone."}
],
[
{"role": "user", "content": "zZZn98: come here"},
{"role": "assistant", "content": "```\n// I am going to navigate to zZZn98.\nawait skills.goToPlayer(bot, 'zZZn98');\n```"},
{"role": "system", "content": "Code execution finished successfully."},
{"role": "assistant", "content": "Here!"}
],
[
{"role": "user", "content": "hanky: collect some sand for me please"},
{"role": "assistant", "content": "Collecting sand...\n```\n// I am going to collect 3 sand and give to hanky.\nawait skills.collectBlock(bot, 'sand');\nawait skills.giveToPlayer(bot, 'sand', 'hanky');\n```"},
{"role": "system", "content": "Code Output:\nYou have reached player hanky.\nCode execution finished successfully."},
{"role": "assistant", "content": "Here!"}
],
[
{"role": "user", "content": "sarah_O.o: can you fly up in the air?"},
{"role": "assistant", "content": "I can't do that."}
],
[
{"role": "user", "content": "joe: Follow me!"},
{"role": "assistant", "content": "Sure!\n```\n// I am going to follow joe.\nawait skills.followPlayer(bot, 'joe');\n```"}
],
[
{"role": "user", "content": "hanky: kill that zombie!"},
{"role": "assistant", "content": "I'm attacking!\n```\n//I'm going to attack the nearest zombie.\nawait skills.attackMob(bot, 'zombie');\n```"},
{"role": "system", "content": "Code Output:\nNo zombie nearby\nCode execution failed!"},
{"role": "assistant", "content": "I could not find a zombie nearby."}
],
[
{"role": "user", "content": "billybob: stop"},
{"role": "assistant", "content": "```\n// I am going to write empty code to stop whatever I am doing\n```"}
]
]

View file

@ -22,15 +22,44 @@ export async function sendRequest(turns, systemMessage, stop_seq='***') {
let res = null;
try {
let completion = await openai.chat.completions.create({
model: 'gpt-3.5-turbo',
model: 'gpt-3.5-turbo-1106',
messages: messages,
stop: stop_seq,
});
res = completion.choices[0].message.content;
}
catch (err) {
console.log(err);
res = 'My brain disconnected, try again.';
if (err.code == 'context_length_exceeded' && turns.length > 1) {
console.log('Context length exceeded, trying again with shorter context.');
return await sendRequest(turns.slice(1), systemMessage, stop_seq);
} else {
console.log(err);
res = 'My brain disconnected, try again.';
}
}
return res;
}
export async function embed(text) {
const embedding = await openai.embeddings.create({
model: "text-embedding-ada-002",
input: text,
encoding_format: "float",
});
return embedding.data[0].embedding;
}
export function cosineSimilarity(a, b) {
let dotProduct = 0;
let magnitudeA = 0;
let magnitudeB = 0;
for (let i = 0; i < a.length; i++) {
dotProduct += a[i] * b[i]; // calculate dot product
magnitudeA += Math.pow(a[i], 2); // calculate magnitude of a
magnitudeB += Math.pow(b[i], 2); // calculate magnitude of b
}
magnitudeA = Math.sqrt(magnitudeA);
magnitudeB = Math.sqrt(magnitudeB);
return dotProduct / (magnitudeA * magnitudeB); // calculate cosine similarity
}

View file

@ -1,57 +1,171 @@
let history_examples = [
{'role': 'user', 'content': 'miner_32: Hey! What are you up to?'},
{'role': 'assistant', 'content': 'Nothing much miner_32, what do you need?'},
import { writeFileSync, readFileSync, mkdirSync } from 'fs';
import { getQueryDocs } from './queries.js';
import { getSkillDocs } from './skill_library.js';
import { sendRequest, embed, cosineSimilarity } from './gpt.js';
{'role': 'user', 'content': 'grombo_Xx: What do you see?'},
{'role': 'assistant', 'content': 'Let me see... !blocks'},
{'role': 'system', 'content': 'NEARBY_BLOCKS\n- oak_log\n- dirt\n- cobblestone'},
{'role': 'assistant', 'content': 'I see some oak logs, dirt, and cobblestone.'},
{'role': 'user', 'content': 'zZZn98: come here'},
{'role': 'assistant', 'content': '```// I am going to navigate to zZZn98.\nawait skills.goToPlayer(bot, "zZZn98");```'},
{'role': 'system', 'content': 'Code execution finished successfully.'},
{'role': 'assistant', 'content': 'Here!'},
{'role': 'user', 'content': 'hanky: collect some sand for me please'},
{'role': 'assistant', 'content': 'Collecting sand...```// I am going to collect 3 sand and give to hanky.\n\
await skills.collectBlock(bot, "sand");\nawait skills.giveToPlayer(bot, "sand", "hanky");```'},
{'role': 'system', 'content': 'Code Output:\nYou have reached player hanky.\nCode execution finished successfully.'},
{'role': 'assistant', 'content': 'Here!'},
{'role': 'user', 'content': 'sarah_O.o: can you fly up in the air?'},
{'role': 'assistant', 'content': "I can't do that."},
{'role': 'user', 'content': 'hanky: kill that zombie!'},
{'role': 'assistant', 'content': "I'm attacking! ```//I'm going to attack the nearest zombie.\n\
await skills.attackMob(bot, 'zombie');```"},
{'role': 'system', 'content': 'Code Output:\nNo zombie nearby\nCode execution failed!'},
{'role': 'assistant', 'content': 'I could not find a zombie nearby.'},
{'role': 'user', 'content': 'billybob: stop'},
{'role': 'assistant', 'content': '```// I am going to write empty code to stop whatever I am doing\n```'},
]
export class History {
constructor(agent) {
this.agent = agent;
this.turns = history_examples;
constructor(agent, save_path) {
this.name = agent.name;
this.save_path = save_path;
this.turns = [];
// These define an agent's long term memory
this.bio = '';
this.memory = '';
// Variables for controlling the agent's memory and knowledge
this.max_messages = 20;
this.fewshot = 5;
this.examples = [];
this.selected_examples = [];
}
getHistory() {
return this.turns;
}
add(name, content) {
getSystemMessage(include_examples=true) {
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 brief in your responses, omit needless words, and do not give instructions unless asked.`;
system_message += getQueryDocs();
system_message += getSkillDocs();
if (this.bio != '')
system_message += '\n\nBio:\n' + this.bio;
if (this.memory != '')
system_message += '\n\nMemory:\n' + this.memory;
if (include_examples && this.selected_examples.length > 0) {
for (let i = 0; i < this.selected_examples.length; i++) {
system_message += '\n\nExample ' + (i+1) + ':\n\n';
system_message += this.stringifyTurns(this.selected_examples[i].turns);
}
}
return system_message;
}
stringifyTurns(turns) {
let res = '';
for (let turn of turns) {
if (turn.role === 'assistant') {
res += `\n\nYour output:\n${turn.content}`;
} else if (turn.role === 'system') {
res += `\n\nSystem output: ${turn.content}`;
} else {
res += `\n\nUser input: ${turn.content}`;
}
}
return res.trim();
}
async storeMemories(turns) {
let memory_prompt = 'Update your "Memory" with the following conversation. Your "Memory" is for storing information that will help you improve as a Minecraft bot. Include details about your interactions with other players that you may need to remember for later. Also include things that you have learned through player feedback or by executing code. Do not include information found in your Docs or that you got right on the first try.';
if (this.memory != '') {
memory_prompt += ' Include information from your previous memory if it is still relevant. Your output will replace your previous memory.';
}
memory_prompt += ' Your output should use one of the following formats:\n';
memory_prompt += '- When the player... output...\n';
memory_prompt += '- I learned that player [name]...\n';
memory_prompt += this.stringifyTurns(turns);
let memory_turns = [{'role': 'user', 'content': memory_prompt}]
this.memory = await sendRequest(memory_turns, this.getSystemMessage(false));
}
async loadExamples() {
let examples = [];
try {
const data = readFileSync('utils/examples.json', 'utf8');
examples = JSON.parse(data);
} catch (err) {
console.log('No history examples found.');
}
this.examples = [];
for (let example of examples) {
let messages = '';
for (let turn of example) {
if (turn.role == 'user')
messages += turn.content.substring(turn.content.indexOf(':')+1).trim() + '\n';
}
messages = messages.trim();
const embedding = await embed(messages);
this.examples.push({'embedding': embedding, 'turns': example});
}
}
async setExamples() {
let messages = '';
for (let turn of this.turns) {
if (turn.role == 'user')
messages += turn.content.substring(turn.content.indexOf(':')+1).trim() + '\n';
}
messages = messages.trim();
const embedding = await embed(messages);
this.examples.sort((a, b) => {
return cosineSimilarity(a.embedding, embedding) - cosineSimilarity(b.embedding, embedding);
});
this.selected_examples = this.examples.slice(-this.fewshot);
for (let example of this.selected_examples) {
console.log('selected example: ', example.turns[0].content);
}
}
async add(name, content) {
let role = 'assistant';
if (name === 'system') {
role = 'system';
}
else if (name !== this.agent.name) {
else if (name !== this.name) {
role = 'user';
content = `${name}: ${content}`;
}
this.turns.push({role, content});
// Summarize older turns into memory
if (this.turns.length >= this.max_messages) {
console.log('summarizing memory')
let to_summarize = [this.turns.shift()];
while (this.turns[0].role != 'user' && this.turns.length > 0)
to_summarize.push(this.turns.shift());
await this.storeMemories(to_summarize);
}
if (role === 'user')
await this.setExamples();
}
save() {
if (this.save_path === '' || this.save_path == null) return;
// save history object to json file
mkdirSync('bots', { recursive: true });
let data = {
'name': this.name,
'bio': this.bio,
'memory': this.memory,
'turns': this.turns
};
const json_data = JSON.stringify(data, null, 4);
writeFileSync(this.save_path, json_data, (err) => {
if (err) {
throw err;
}
console.log("JSON data is saved.");
});
}
load() {
if (this.save_path === '' || this.save_path == null) return;
try {
// load history object from json file
const data = readFileSync(this.save_path, 'utf8');
const obj = JSON.parse(data);
this.turns = obj.turns;
this.bio = obj.bio;
this.memory = obj.memory;
this.num_saved_turns = obj.num_saved_turns;
} catch (err) {
console.log('No history file found for ' + this.name + '.');
}
}
}

View file

@ -181,3 +181,22 @@ export function getNearbyBlockTypes(bot) {
}
return found;
}
export function getNearestBlockPosition(bot, blockType) {
/**
* Get the position of the nearest block of the given type.
* @param {Bot} bot - The bot to get the nearest block for.
* @param {string} blockType - The type of the block to search for.
* @returns {Vec3} - The position of the nearest block of the given type if found else null.
* @example
* let position = world.getNearestBlockPosition(bot, 'coal_ore');
**/
let blocks = getNearbyBlocks(bot, 16);
for (let i = 0; i < blocks.length; i++) {
if (blocks[i].name == blockType) {
return blocks[i].position;
}
}
return null;
}