2024-02-25 14:13:32 -06:00
|
|
|
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';
|
2024-04-10 15:54:40 +02:00
|
|
|
import { Local } from '../models/local.js';
|
2024-03-23 11:15:53 -05:00
|
|
|
|
2024-02-25 14:13:32 -06:00
|
|
|
|
|
|
|
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-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.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-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);
|
|
|
|
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.');
|
|
|
|
}
|
|
|
|
|
2024-02-25 14:13:32 -06:00
|
|
|
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) => {
|
2024-02-25 14:13:32 -06:00
|
|
|
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;
|
2024-02-25 14:13:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
async initExamples() {
|
|
|
|
console.log('Loading examples...')
|
2024-04-24 11:28:04 -07:00
|
|
|
this.convo_examples = new Examples(this.embedding_model);
|
2024-06-03 18:23:29 -05:00
|
|
|
await this.convo_examples.load(this.profile.conversation_examples);
|
2024-04-24 11:28:04 -07:00
|
|
|
this.coding_examples = new Examples(this.embedding_model);
|
2024-06-03 18:23:29 -05:00
|
|
|
await this.coding_examples.load(this.profile.coding_examples);
|
2024-02-25 14:13:32 -06:00
|
|
|
console.log('Examples loaded.');
|
|
|
|
}
|
|
|
|
|
2024-04-24 13:34:09 -07:00
|
|
|
async replaceStrings(prompt, messages, examples=null, prev_memory=null, to_summarize=[], last_goals=null) {
|
2024-02-25 14:13:32 -06:00
|
|
|
prompt = prompt.replaceAll('$NAME', this.agent.name);
|
|
|
|
|
|
|
|
if (prompt.includes('$STATS')) {
|
|
|
|
let stats = await getCommand('!stats').perform(this.agent);
|
|
|
|
prompt = prompt.replaceAll('$STATS', stats);
|
|
|
|
}
|
2024-04-02 19:18:13 -05:00
|
|
|
if (prompt.includes('$INVENTORY')) {
|
|
|
|
let inventory = await getCommand('!inventory').perform(this.agent);
|
|
|
|
prompt = prompt.replaceAll('$INVENTORY', inventory);
|
|
|
|
}
|
2024-02-25 14:13:32 -06:00
|
|
|
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('$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));
|
|
|
|
}
|
|
|
|
}
|
2024-02-25 14:13:32 -06:00
|
|
|
|
|
|
|
// check if there are any remaining placeholders with syntax $<word>
|
|
|
|
let remaining = prompt.match(/\$[A-Z_]+/g);
|
|
|
|
if (remaining !== null) {
|
2024-04-02 19:18:13 -05:00
|
|
|
console.warn('Unknown prompt placeholders:', remaining.join(', '));
|
2024-02-25 14:13:32 -06:00
|
|
|
}
|
|
|
|
return prompt;
|
|
|
|
}
|
|
|
|
|
|
|
|
async promptConvo(messages) {
|
2024-06-03 18:23:29 -05:00
|
|
|
let prompt = this.profile.conversing;
|
2024-02-25 14:13:32 -06:00
|
|
|
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);
|
2024-02-25 14:13:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
async promptCoding(messages) {
|
2024-06-03 18:23:29 -05:00
|
|
|
let prompt = this.profile.coding;
|
2024-02-25 14:13:32 -06:00
|
|
|
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);
|
2024-02-25 14:13:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
async promptMemSaving(prev_mem, to_summarize) {
|
2024-06-03 18:23:29 -05:00
|
|
|
let prompt = this.profile.saving_memory;
|
2024-02-25 14:13:32 -06:00
|
|
|
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-02-25 14:13:32 -06:00
|
|
|
}
|
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
|
|
|
}
|