forked from wmantly/mc-bot-town
craft fix
This commit is contained in:
@@ -1,16 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
const axios = require('axios');
|
||||
|
||||
const conf = require('../conf');
|
||||
const {sleep} = require('../utils');
|
||||
|
||||
|
||||
const {
|
||||
GoogleGenerativeAI,
|
||||
HarmCategory,
|
||||
HarmBlockThreshold,
|
||||
} = require("@google/generative-ai");
|
||||
const { ProviderFactory } = require('./ai/providers');
|
||||
|
||||
|
||||
class Ai{
|
||||
@@ -21,6 +13,18 @@ class Ai{
|
||||
this.intervalLength = args.intervalLength || 30;
|
||||
this.intervalStop;
|
||||
this.messageListener;
|
||||
this.provider = null;
|
||||
|
||||
// Bot-specific AI config (overrides global config)
|
||||
this.botConfig = args.botConfig || {};
|
||||
}
|
||||
|
||||
// Get merged config: bot-specific settings override global settings
|
||||
__getConfig() {
|
||||
return {
|
||||
...conf.ai, // Global defaults
|
||||
...this.botConfig, // Bot-specific overrides
|
||||
};
|
||||
}
|
||||
|
||||
async init(){
|
||||
@@ -38,11 +42,10 @@ class Ai{
|
||||
console.log(`Message ${type}: ${message.toString()}`)
|
||||
messages.push('>', message.toString());
|
||||
});
|
||||
|
||||
|
||||
this.intervalStop = setInterval(async ()=>{
|
||||
let result;
|
||||
// if(messages.length ===0) return;
|
||||
|
||||
|
||||
try{
|
||||
result = await this.chat(JSON.stringify({
|
||||
messages, currentTime:Date.now()+1}
|
||||
@@ -55,9 +58,9 @@ class Ai{
|
||||
|
||||
try{
|
||||
messages = [''];
|
||||
if(!result.response.text()) return;
|
||||
if(!this.provider.getResponse(result)) return;
|
||||
|
||||
for(let message of JSON.parse(result.response.text())){
|
||||
for(let message of JSON.parse(this.provider.getResponse(result))){
|
||||
console.log('toSay', message.delay, message.text);
|
||||
if(message.text === '___') return;
|
||||
setTimeout(async (message)=>{
|
||||
@@ -66,12 +69,16 @@ class Ai{
|
||||
}
|
||||
}catch(error){
|
||||
console.log('Error in AI message loop', error, result);
|
||||
if(result || result.response || result.response.text()){
|
||||
console.log(result.response.text())
|
||||
try {
|
||||
if(result && this.provider.getResponse(result)){
|
||||
console.log(this.provider.getResponse(result))
|
||||
}
|
||||
} catch(e) {
|
||||
// Ignore
|
||||
}
|
||||
}
|
||||
}, this.intervalLength*1000);
|
||||
|
||||
|
||||
}catch(error){
|
||||
console.log('error in onReady', error);
|
||||
}
|
||||
@@ -83,107 +90,56 @@ class Ai{
|
||||
clearInterval(this.intervalStop);
|
||||
this.intervalStop = undefined;
|
||||
}
|
||||
this.messageListener();
|
||||
|
||||
if(this.messageListener){
|
||||
this.messageListener();
|
||||
}
|
||||
if(this.provider){
|
||||
await this.provider.close();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
__settings(history){
|
||||
return {
|
||||
generationConfig: {
|
||||
temperature: 1,
|
||||
topP: 0.95,
|
||||
topK: 64,
|
||||
maxOutputTokens: 8192,
|
||||
responseMimeType: "application/json",
|
||||
},
|
||||
safetySettings:[
|
||||
// See https://ai.google.dev/gemini-api/docs/safety-settings
|
||||
{
|
||||
category: HarmCategory.HARM_CATEGORY_HARASSMENT,
|
||||
threshold: HarmBlockThreshold.BLOCK_NONE,
|
||||
},
|
||||
{
|
||||
category: HarmCategory.HARM_CATEGORY_HATE_SPEECH,
|
||||
threshold: HarmBlockThreshold.BLOCK_NONE,
|
||||
},
|
||||
{
|
||||
category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
|
||||
threshold: HarmBlockThreshold.BLOCK_NONE,
|
||||
},
|
||||
{
|
||||
category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
|
||||
threshold: HarmBlockThreshold.BLOCK_NONE,
|
||||
},
|
||||
],
|
||||
history: history || [
|
||||
{
|
||||
role: "user",
|
||||
parts: [
|
||||
{text: this.prompt},
|
||||
],
|
||||
},
|
||||
{
|
||||
role: "model",
|
||||
parts: [
|
||||
{text: "Chat stuff"},
|
||||
],
|
||||
}
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
async start(history){
|
||||
const genAI = new GoogleGenerativeAI(conf.ai.key);
|
||||
const config = this.__getConfig();
|
||||
let bulbaItems = {};
|
||||
|
||||
const model = genAI.getGenerativeModel({
|
||||
model: "gemini-1.5-flash",
|
||||
console.log(`${this.bot.name} AI config:`, {
|
||||
provider: config.provider,
|
||||
model: config.model,
|
||||
promptName: this.promptName,
|
||||
baseUrl: config.baseUrl,
|
||||
});
|
||||
|
||||
let bulbaItems = await axios.get('https://webstore.bulbastore.uk/api/listings');
|
||||
bulbaItems = bulbaItems.data.listings.map(i=>i.listing_name);
|
||||
|
||||
console.log('AI for prompts', conf.ai.prompts)
|
||||
|
||||
this.prompt = conf.ai.prompts[this.promptName](
|
||||
const prompt = conf.ai.prompts[this.promptName](
|
||||
this.bot.bot.entity.username,
|
||||
conf.ai.interval,
|
||||
config.interval,
|
||||
Object.values(this.bot.getPlayers()).map(player=>`<[${player.lvl}] ${player.username}>`).join('\n'),
|
||||
bulbaItems,
|
||||
this.prompCustom,
|
||||
);
|
||||
|
||||
this.session = model.startChat({
|
||||
...this.__settings(history),
|
||||
// systemInstruction: this.prompt,
|
||||
// Create the provider instance with merged config and prompt
|
||||
this.provider = ProviderFactory.create({
|
||||
...config,
|
||||
prompt: prompt,
|
||||
});
|
||||
|
||||
await this.provider.start(history);
|
||||
console.log(`${this.bot.name} AI ${config.provider} provider started (model: ${config.model})`);
|
||||
}
|
||||
|
||||
async chat(message, retryCount=0){
|
||||
console.log('chat', retryCount)
|
||||
console.log(`chat ${this.bot.name}`, retryCount)
|
||||
try{
|
||||
let result = await this.session.sendMessage(message);
|
||||
|
||||
let result = await this.provider.chat(message);
|
||||
return result
|
||||
}catch(error){
|
||||
console.log('AI chat error', error)
|
||||
|
||||
if(retryCount > 3){
|
||||
console.log('hit retry count');
|
||||
return ;
|
||||
};
|
||||
await sleep(500);
|
||||
this.session.params.history.pop();
|
||||
this.start(this.session.params.history);
|
||||
return await this.chat(message, retryCount++)
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
module.exports = Ai;
|
||||
// run();
|
||||
module.exports = Ai;
|
||||
Reference in New Issue
Block a user