mindcraft/src/agent/prompter.js

215 lines
9 KiB
JavaScript
Raw Normal View History

import { readFileSync, mkdirSync, writeFileSync} from 'fs';
import { Examples } from '../utils/examples.js';
import { getCommandDocs } from './commands/index.js';
import { getSkillDocs } from './library/index.js';
import { stringifyTurns } from '../utils/text.js';
import { getCommand } from './commands/index.js';
2024-03-23 11:15:53 -05:00
import { Gemini } from '../models/gemini.js';
import { GPT } from '../models/gpt.js';
import { Claude } from '../models/claude.js';
2024-05-06 00:18:04 -05:00
import { ReplicateAPI } from '../models/replicate.js';
import { Local } from '../models/local.js';
2024-08-31 15:29:34 -07:00
import { GroqCloudAPI } from '../models/groq.js';
export class Prompter {
constructor(agent, fp) {
2024-04-24 11:28:04 -07:00
this.agent = agent;
2024-06-03 18:23:29 -05:00
this.profile = JSON.parse(readFileSync(fp, 'utf8'));
2024-04-24 11:28:04 -07:00
this.convo_examples = null;
this.coding_examples = null;
2024-06-03 18:23:29 -05:00
let name = this.profile.name;
let chat = this.profile.model;
2024-08-31 15:29:34 -07:00
// try to get "max_tokens" parameter, else null
let max_tokens = null;
if (this.profile.max_tokens)
max_tokens = this.profile.max_tokens;
2024-04-24 11:28:04 -07:00
if (typeof chat === 'string' || chat instanceof String) {
chat = {model: chat};
if (chat.model.includes('gemini'))
chat.api = 'google';
else if (chat.model.includes('gpt') || chat.model.includes('o1'))
2024-04-24 11:28:04 -07:00
chat.api = 'openai';
else if (chat.model.includes('claude'))
chat.api = 'anthropic';
2024-05-06 00:18:04 -05:00
else if (chat.model.includes('meta/') || chat.model.includes('mistralai/') || chat.model.includes('replicate/'))
chat.api = 'replicate';
2024-08-31 15:29:34 -07:00
else if (chat.model.includes("groq/") || chat.model.includes("groqcloud/"))
chat.api = 'groq';
2024-04-24 11:28:04 -07:00
else
chat.api = 'ollama';
}
console.log('Using chat settings:', chat);
if (chat.api == 'google')
this.chat_model = new Gemini(chat.model, chat.url);
else if (chat.api == 'openai')
this.chat_model = new GPT(chat.model, chat.url);
else if (chat.api == 'anthropic')
this.chat_model = new Claude(chat.model, chat.url);
2024-05-06 00:18:04 -05:00
else if (chat.api == 'replicate')
this.chat_model = new ReplicateAPI(chat.model, chat.url);
2024-04-24 11:28:04 -07:00
else if (chat.api == 'ollama')
this.chat_model = new Local(chat.model, chat.url);
2024-08-31 15:29:34 -07:00
else if (chat.api == 'groq') {
this.chat_model = new GroqCloudAPI(chat.model.replace('groq/', '').replace('groqcloud/', ''), chat.url, max_tokens ? max_tokens : 8192);
}
2024-04-24 11:28:04 -07:00
else
throw new Error('Unknown API:', api);
2024-06-03 18:23:29 -05:00
let embedding = this.profile.embedding;
2024-06-01 16:23:14 -05:00
if (embedding === undefined) {
if (chat.api !== 'ollama')
embedding = {api: chat.api};
else
embedding = {api: 'none'};
}
2024-04-24 11:28:04 -07:00
else if (typeof embedding === 'string' || embedding instanceof String)
embedding = {api: embedding};
console.log('Using embedding settings:', embedding);
if (embedding.api == 'google')
this.embedding_model = new Gemini(embedding.model, embedding.url);
else if (embedding.api == 'openai')
this.embedding_model = new GPT(embedding.model, embedding.url);
2024-05-06 00:18:04 -05:00
else if (embedding.api == 'replicate')
this.embedding_model = new ReplicateAPI(embedding.model, embedding.url);
2024-04-24 11:28:04 -07:00
else if (embedding.api == 'ollama')
this.embedding_model = new Local(embedding.model, embedding.url);
else {
this.embedding_model = null;
console.log('Unknown embedding: ', embedding ? embedding.api : '[NOT SPECIFIED]', '. Using word overlap.');
}
mkdirSync(`./bots/${name}`, { recursive: true });
2024-06-03 18:23:29 -05:00
writeFileSync(`./bots/${name}/last_profile.json`, JSON.stringify(this.profile, null, 4), (err) => {
if (err) {
throw err;
}
console.log("Copy profile saved.");
});
}
getName() {
2024-06-03 18:23:29 -05:00
return this.profile.name;
}
getInitModes() {
return this.profile.modes;
}
async initExamples() {
// Using Promise.all to implement concurrent processing
// Create Examples instances
2024-04-24 11:28:04 -07:00
this.convo_examples = new Examples(this.embedding_model);
this.coding_examples = new Examples(this.embedding_model);
// Use Promise.all to load examples concurrently
await Promise.all([
this.convo_examples.load(this.profile.conversation_examples),
this.coding_examples.load(this.profile.coding_examples),
]);
}
2024-04-24 13:34:09 -07:00
async replaceStrings(prompt, messages, examples=null, prev_memory=null, to_summarize=[], last_goals=null) {
prompt = prompt.replaceAll('$NAME', this.agent.name);
if (prompt.includes('$STATS')) {
let stats = await getCommand('!stats').perform(this.agent);
prompt = prompt.replaceAll('$STATS', stats);
}
if (prompt.includes('$INVENTORY')) {
let inventory = await getCommand('!inventory').perform(this.agent);
prompt = prompt.replaceAll('$INVENTORY', inventory);
}
if (prompt.includes('$COMMAND_DOCS'))
prompt = prompt.replaceAll('$COMMAND_DOCS', getCommandDocs());
if (prompt.includes('$CODE_DOCS'))
prompt = prompt.replaceAll('$CODE_DOCS', getSkillDocs());
if (prompt.includes('$EXAMPLES') && examples !== null)
prompt = prompt.replaceAll('$EXAMPLES', await examples.createExampleMessage(messages));
if (prompt.includes('$MEMORY'))
prompt = prompt.replaceAll('$MEMORY', prev_memory ? prev_memory : 'None.');
if (prompt.includes('$TO_SUMMARIZE'))
prompt = prompt.replaceAll('$TO_SUMMARIZE', stringifyTurns(to_summarize));
2024-04-24 13:34:09 -07:00
if (prompt.includes('$CONVO'))
prompt = prompt.replaceAll('$CONVO', 'Recent conversation:\n' + stringifyTurns(messages));
if (prompt.includes('$SELF_PROMPT')) {
let self_prompt = this.agent.self_prompter.on ? `Use this self-prompt to guide your behavior: "${this.agent.self_prompter.prompt}"\n` : '';
prompt = prompt.replaceAll('$SELF_PROMPT', self_prompt);
}
2024-04-24 13:34:09 -07:00
if (prompt.includes('$LAST_GOALS')) {
let goal_text = '';
for (let goal in last_goals) {
if (last_goals[goal])
goal_text += `You recently successfully completed the goal ${goal}.\n`
else
goal_text += `You recently failed to complete the goal ${goal}.\n`
}
prompt = prompt.replaceAll('$LAST_GOALS', goal_text.trim());
}
if (prompt.includes('$BLUEPRINTS')) {
if (this.agent.npc.constructions) {
let blueprints = '';
for (let blueprint in this.agent.npc.constructions) {
blueprints += blueprint + ', ';
}
prompt = prompt.replaceAll('$BLUEPRINTS', blueprints.slice(0, -2));
}
}
// check if there are any remaining placeholders with syntax $<word>
let remaining = prompt.match(/\$[A-Z_]+/g);
if (remaining !== null) {
console.warn('Unknown prompt placeholders:', remaining.join(', '));
}
return prompt;
}
async promptConvo(messages) {
2024-06-03 18:23:29 -05:00
let prompt = this.profile.conversing;
prompt = await this.replaceStrings(prompt, messages, this.convo_examples);
2024-04-24 11:28:04 -07:00
return await this.chat_model.sendRequest(messages, prompt);
}
async promptCoding(messages) {
2024-06-03 18:23:29 -05:00
let prompt = this.profile.coding;
prompt = await this.replaceStrings(prompt, messages, this.coding_examples);
2024-04-24 11:28:04 -07:00
return await this.chat_model.sendRequest(messages, prompt);
}
async promptMemSaving(prev_mem, to_summarize) {
2024-06-03 18:23:29 -05:00
let prompt = this.profile.saving_memory;
prompt = await this.replaceStrings(prompt, null, null, prev_mem, to_summarize);
2024-04-24 11:28:04 -07:00
return await this.chat_model.sendRequest([], prompt);
}
2024-04-23 20:47:01 -07:00
async promptGoalSetting(messages, last_goals) {
2024-06-03 18:23:29 -05:00
let system_message = this.profile.goal_setting;
2024-04-24 13:34:09 -07:00
system_message = await this.replaceStrings(system_message, messages);
let user_message = 'Use the below info to determine what goal to target next\n\n';
user_message += '$LAST_GOALS\n$STATS\n$INVENTORY\n$CONVO'
user_message = await this.replaceStrings(user_message, messages, null, null, null, last_goals);
let user_messages = [{role: 'user', content: user_message}];
2024-04-30 13:31:51 -07:00
let res = await this.chat_model.sendRequest(user_messages, system_message);
2024-04-24 13:34:09 -07:00
let goal = null;
try {
let data = res.split('```')[1].replace('json', '').trim();
goal = JSON.parse(data);
} catch (err) {
console.log('Failed to parse goal:', res, err);
}
if (!goal || !goal.name || !goal.quantity || isNaN(parseInt(goal.quantity))) {
console.log('Failed to set goal:', res);
return null;
}
goal.quantity = parseInt(goal.quantity);
return goal;
2024-04-23 20:47:01 -07:00
}
2024-04-30 13:31:51 -07:00
}