2023-11-12 14:53:23 -08:00
import { writeFileSync , readFileSync , mkdirSync } from 'fs' ;
2023-11-13 20:33:34 -08:00
import { getQueryDocs } from './queries.js' ;
import { getSkillDocs } from './skill_library.js' ;
2023-11-12 14:53:23 -08:00
import { sendRequest } from './gpt.js' ;
2023-11-07 12:00:55 -06:00
let history _examples = [
{ 'role' : 'user' , 'content' : 'miner_32: Hey! What are you up to?' } ,
2023-11-07 09:44:56 -06:00
{ 'role' : 'assistant' , 'content' : 'Nothing much miner_32, what do you need?' } ,
2023-11-07 22:23:42 -06:00
{ 'role' : 'user' , 'content' : 'grombo_Xx: What do you see?' } ,
2023-11-07 09:44:56 -06:00
{ 'role' : 'assistant' , 'content' : 'Let me see... !blocks' } ,
2023-11-12 13:57:22 -06:00
{ 'role' : 'system' , 'content' : 'NEARBY_BLOCKS\n- oak_log\n- dirt\n- cobblestone' } ,
2023-11-07 09:44:56 -06:00
{ 'role' : 'assistant' , 'content' : 'I see some oak logs, dirt, and cobblestone.' } ,
2023-11-07 12:00:55 -06:00
{ 'role' : 'user' , 'content' : 'zZZn98: come here' } ,
2023-11-12 23:19:58 -06:00
{ 'role' : 'assistant' , 'content' : '```// I am going to navigate to zZZn98.\nawait skills.goToPlayer(bot, "zZZn98");```' } ,
2023-11-12 13:57:22 -06:00
{ 'role' : 'system' , 'content' : 'Code execution finished successfully.' } ,
{ 'role' : 'assistant' , 'content' : 'Here!' } ,
2023-11-07 09:44:56 -06:00
2023-11-07 12:00:55 -06:00
{ 'role' : 'user' , 'content' : 'hanky: collect some sand for me please' } ,
2023-11-07 09:44:56 -06:00
{ 'role' : 'assistant' , 'content' : ' Collecting sand ... ` ` ` // I am going to collect 3 sand and give to hanky. \n \
2023-11-12 23:19:58 -06:00
await skills . collectBlock ( bot , "sand" ) ; \ nawait skills . giveToPlayer ( bot , "sand" , "hanky" ) ; ` ` ` '},
2023-11-12 13:57:22 -06:00
{ 'role' : 'system' , 'content' : 'Code Output:\nYou have reached player hanky.\nCode execution finished successfully.' } ,
{ 'role' : 'assistant' , 'content' : 'Here!' } ,
2023-11-07 09:44:56 -06:00
2023-11-12 13:57:22 -06:00
{ 'role' : 'user' , 'content' : 'sarah_O.o: can you fly up in the air?' } ,
{ 'role' : 'assistant' , 'content' : "I can't do that." } ,
2023-11-07 09:44:56 -06:00
2023-11-07 12:00:55 -06:00
{ 'role' : 'user' , 'content' : 'hanky: kill that zombie!' } ,
2023-11-07 09:44:56 -06:00
{ 'role' : 'assistant' , 'content' : " I 'm attacking! ```//I' m going to attack the nearest zombie . \ n \
2023-11-12 23:19:58 -06:00
await skills . attackMob ( bot , 'zombie' ) ; ` ` ` "},
2023-11-12 13:57:22 -06:00
{ 'role' : 'system' , 'content' : 'Code Output:\nNo zombie nearby\nCode execution failed!' } ,
{ 'role' : 'assistant' , 'content' : 'I could not find a zombie nearby.' } ,
2023-11-07 22:23:42 -06:00
2023-11-13 00:57:20 -06:00
{ 'role' : 'user' , 'content' : 'billybob: stop' } ,
2023-11-15 11:38:17 -08:00
{ 'role' : 'assistant' , 'content' : '```// I am going to write empty code to stop whatever I am doing\n```' }
2023-11-07 09:44:56 -06:00
]
export class History {
constructor ( agent ) {
2023-11-12 14:53:23 -08:00
this . name = agent . name ;
2023-11-07 12:00:55 -06:00
this . turns = history _examples ;
2023-11-12 14:53:23 -08:00
// These define an agent's long term memory
2023-11-13 20:33:34 -08:00
this . bio = '' ;
2023-11-12 14:53:23 -08:00
this . memory = '' ;
// Variables for controlling how often we summarize the agent's memory and knowledge
this . max _messages = 20 ;
2023-11-07 09:44:56 -06:00
}
2023-08-15 23:39:02 -07:00
2023-11-07 09:44:56 -06:00
getHistory ( ) {
return this . turns ;
}
2023-08-15 23:39:02 -07:00
2023-11-13 20:33:34 -08:00
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 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 = system _message + '\n\nBio:\n' + this . bio ;
2023-11-12 14:53:23 -08:00
}
2023-11-13 20:33:34 -08:00
if ( this . memory != '' ) {
system _message = system _message + '\n\nMemory:\n' + this . memory ;
}
return system _message ;
}
2023-11-12 14:53:23 -08:00
2023-11-13 20:33:34 -08:00
async storeMemories ( turns ) {
2023-11-15 11:38:17 -08:00
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.' ;
2023-11-13 20:33:34 -08:00
if ( this . memory != '' ) {
2023-11-15 11:38:17 -08:00
memory _prompt += ' Include information from your previous memory if it is still relevant. Your output will replace your previous memory.' ;
2023-11-13 20:33:34 -08:00
}
2023-11-15 11:38:17 -08:00
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' ;
2023-11-12 14:53:23 -08:00
for ( let turn of turns ) {
2023-11-13 20:33:34 -08:00
if ( turn . role === 'assistant' ) {
memory _prompt += ` \n \n You: ${ turn . content } ` ;
2023-11-12 14:53:23 -08:00
} else {
2023-11-13 20:33:34 -08:00
memory _prompt += ` \n \n ${ turn . content } ` ;
2023-11-12 14:53:23 -08:00
}
}
2023-11-13 20:33:34 -08:00
let memory _turns = [ { 'role' : 'user' , 'content' : memory _prompt } ]
this . memory = await sendRequest ( memory _turns , this . getSystemMessage ( ) ) ;
2023-11-12 14:53:23 -08:00
}
async add ( name , content ) {
2023-11-07 09:44:56 -06:00
let role = 'assistant' ;
2023-11-12 13:57:22 -06:00
if ( name === 'system' ) {
role = 'system' ;
}
2023-11-13 20:33:34 -08:00
else if ( name !== this . name ) {
2023-11-07 09:44:56 -06:00
role = 'user' ;
2023-11-07 12:00:55 -06:00
content = ` ${ name } : ${ content } ` ;
2023-08-17 00:00:57 -07:00
}
2023-11-07 09:44:56 -06:00
this . turns . push ( { role , content } ) ;
2023-11-12 14:53:23 -08:00
// Summarize older turns into memory
if ( this . turns . length >= this . max _messages ) {
2023-11-15 11:38:17 -08:00
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 ) ;
2023-11-12 14:53:23 -08:00
}
}
2023-11-15 11:38:17 -08:00
save ( save _path ) {
2023-11-12 14:53:23 -08:00
// save history object to json file
mkdirSync ( 'bots' , { recursive : true } ) ;
const data = JSON . stringify ( this , null , 4 ) ;
2023-11-15 11:38:17 -08:00
writeFileSync ( save _path , data , ( err ) => {
2023-11-12 14:53:23 -08:00
if ( err ) {
throw err ;
}
console . log ( "JSON data is saved." ) ;
} ) ;
}
2023-11-15 11:38:17 -08:00
load ( save _path ) {
2023-11-12 14:53:23 -08:00
try {
// load history object from json file
2023-11-15 11:38:17 -08:00
const data = readFileSync ( save _path , 'utf8' ) ;
2023-11-12 14:53:23 -08:00
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 + '.' ) ;
}
2023-08-17 00:00:57 -07:00
}
2023-11-07 09:44:56 -06:00
}