yarn-ng/bots/LLMBot.js

54 lines
1.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const fs = require('fs');
const path = require('path');
const { LLM } = require('@themaximalist/llm.js'); // or wherever your LLM package lives
// read optional config file
const configPath = path.join(__dirname, '..', 'llm_config.json');
const llmOptions = fs.existsSync(configPath)
? JSON.parse(fs.readFileSync(configPath, 'utf8'))
: {};
class LLMBot {
constructor() {
// One instance keeps the whole conversation in memory
this.llm = new LLM(llmOptions);
// Prompt fragments well reuse
this.writePrompt =
`You are playing a collaborative storytelling game.
Continue the following story with exactly ONE short, creative sentence.
Do NOT repeat whats already written.`;
this.votePrompt =
`You are playing a collaborative storytelling game.
Below is the story so far, followed by a list of possible next sentences.
Reply with ONLY the number (0-indexed) of the sentence you like best.`;
this.banterPrompt =
`You are a playful, slightly sarcastic AI taking part in a writing-game chat.
Keep it short, fun, and on-topic.`;
}
async Write(story_so_far) {
const msg = `${this.writePrompt}\n\nStory so far:\n${story_so_far}`;
const llm_response = await this.llm.chat(msg);
console.log(`Received response from LLM: ${llm_response}`)
return llm_response.trim();
}
async Vote(story_so_far, choices) {
const choicesBlock = choices.map((c, i) => `${i}: ${c}`).join('\n');
const msg = `${this.votePrompt}\n\nStory:\n${story_so_far}\n\nChoices:\n${choicesBlock}`;
const reply = (await this.llm.chat(msg)).trim();
// extract first digit found, fallback to 0
const match = reply.match(/\d+/);
return match ? parseInt(match[0], 10) : 0;
}
async Banter(chat_so_far) {
const msg = `${this.banterPrompt}\n\nChat so far:\n${chat_so_far}`;
return (await this.llm.chat(msg)).trim();
}
}
module.exports = LLMBot;