mirror of
https://github.com/kolbytn/mindcraft.git
synced 2025-08-22 15:13:45 +02:00
basic execute_action functionality added
This commit is contained in:
parent
b3fc11cfec
commit
a99909ac5c
3 changed files with 37 additions and 68 deletions
|
@ -1,6 +1,7 @@
|
||||||
import { writeFile, readFile, mkdirSync } from 'fs';
|
import { writeFile, readFile, mkdirSync } from 'fs';
|
||||||
import { sendRequest, embed, cosineSimilarity } from '../utils/gpt.js';
|
import { sendRequest, embed, cosineSimilarity } from '../utils/gpt.js';
|
||||||
import { stringifyTurns } from '../utils/text.js';
|
import { stringifyTurns } from '../utils/text.js';
|
||||||
|
import { getSkillDocs } from './skill-library.js';
|
||||||
|
|
||||||
|
|
||||||
export class Coder {
|
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) {
|
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();
|
system_message += getSkillDocs();
|
||||||
|
|
||||||
let messages = [];
|
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.";
|
||||||
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),
|
|
||||||
});
|
|
||||||
|
|
||||||
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++) {
|
for (let i=0; i<5; i++) {
|
||||||
|
console.log(messages)
|
||||||
let res = await sendRequest(messages, system_message);
|
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('```'));
|
let code = res.substring(res.indexOf('```')+3, res.lastIndexOf('```'));
|
||||||
|
|
||||||
if (!code)
|
this.queueCode(code);
|
||||||
break;
|
code_return = await this.execute();
|
||||||
|
|
||||||
agent.coder.queueCode(code);
|
if (code_return.interrupted && !code_return.timedout)
|
||||||
let code_return = await agent.coder.execute();
|
return;
|
||||||
|
|
||||||
if (code_return.interrupted && !custom_return.timedout)
|
|
||||||
break;
|
|
||||||
|
|
||||||
messages.push({
|
messages.push({
|
||||||
role: 'assistant',
|
role: 'assistant',
|
||||||
content: res
|
content: res
|
||||||
});
|
});
|
||||||
messages.push({
|
messages.push({
|
||||||
role: 'user',
|
role: 'system',
|
||||||
content: code_return.message
|
content: code_return.message
|
||||||
});
|
});
|
||||||
final_message = code_return.message;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return final_message;
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns {success: bool, message: string, interrupted: bool, timedout: false}
|
// returns {success: bool, message: string, interrupted: bool, timedout: false}
|
||||||
|
|
|
@ -38,7 +38,7 @@ export class History {
|
||||||
}
|
}
|
||||||
|
|
||||||
getSystemMessage() {
|
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 += getQueryDocs();
|
||||||
system_message += getCommandDocs();
|
system_message += getCommandDocs();
|
||||||
if (this.bio != '')
|
if (this.bio != '')
|
||||||
|
|
|
@ -2,7 +2,7 @@ import * as skills from './skills.js';
|
||||||
import * as world from './world.js';
|
import * as world from './world.js';
|
||||||
|
|
||||||
export function getSkillDocs() {
|
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(skills), 'skills');
|
||||||
docstring += docHelper(Object.values(world), 'world');
|
docstring += docHelper(Object.values(world), 'world');
|
||||||
return docstring + '*\n';
|
return docstring + '*\n';
|
||||||
|
|
Loading…
Add table
Reference in a new issue