Fixed LLM bot support
This commit is contained in:
1 parent
792f12b925
commit
e1d059d72c
3 files changed
+50
-30
No files matched your search
+28
-10
@@ -1,17 +1,19 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { LLM } = require('@themaximalist/llm.js'); // or wherever your LLM package lives
|
||||
const OpenAI = require('openai');
|
||||
|
||||
// read optional config file
|
||||
const configPath = path.join(__dirname, '..', 'llm_config.json');
|
||||
const configPath = path.join(__dirname, 'llm_config.json');
|
||||
const llmOptions = fs.existsSync(configPath)
|
||||
? JSON.parse(fs.readFileSync(configPath, 'utf8'))
|
||||
: {};
|
||||
|
||||
console.log(`Read config from ${configPath}`);
|
||||
|
||||
class LLMBot {
|
||||
constructor() {
|
||||
// One instance keeps the whole conversation in memory
|
||||
this.llm = new LLM(llmOptions);
|
||||
// Instantiate the OpenAI client with the config options we just read
|
||||
this.llm = new OpenAI(llmOptions);
|
||||
|
||||
// Prompt fragments we’ll reuse
|
||||
this.writePrompt =
|
||||
@@ -31,23 +33,39 @@ 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();
|
||||
const response = await this.llm.chat.completions.create({
|
||||
model: llmOptions.model || 'meta-llama/Llama-3.2-3B-Instruct-Turbo',
|
||||
messages: [{ role: 'user', content: msg }],
|
||||
temperature: llmOptions.temperature ?? 0.8,
|
||||
max_tokens: llmOptions.max_tokens ?? 80,
|
||||
});
|
||||
console.log(`Received response from LLM: ${response.choices[0].message.content}`);
|
||||
return response.choices[0].message.content.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 response = await this.llm.chat.completions.create({
|
||||
model: llmOptions.model || 'meta-llama/Llama-3.2-3B-Instruct-Turbo',
|
||||
messages: [{ role: 'user', content: msg }],
|
||||
temperature: 0.2,
|
||||
max_tokens: 5,
|
||||
});
|
||||
const reply = response.choices[0].message.content.trim();
|
||||
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();
|
||||
const response = await this.llm.chat.completions.create({
|
||||
model: llmOptions.model || 'meta-llama/Llama-3.2-3B-Instruct-Turbo',
|
||||
messages: [{ role: 'user', content: msg }],
|
||||
temperature: llmOptions.temperature ?? 0.8,
|
||||
max_tokens: llmOptions.max_tokens ?? 60,
|
||||
});
|
||||
return response.choices[0].message.content.trim();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user