forked from wmantly/mc-bot-town
		
	new
This commit is contained in:
		| @ -9,5 +9,12 @@ module.exports = { | |||||||
| 		// 	} | 		// 	} | ||||||
| 		// } | 		// } | ||||||
| 	}, | 	}, | ||||||
|  | 	'ai':{ | ||||||
|  | 		"key": "...", | ||||||
|  | 		"interval": 5, | ||||||
|  | 		"prompt0": (name, interval, currentPlayers)=>` | ||||||
|  | Some stuff here. | ||||||
|  | 		` | ||||||
|  | 	}, | ||||||
| 	"playerListDir": "/home/william/test_list/" | 	"playerListDir": "/home/william/test_list/" | ||||||
| } | } | ||||||
| @ -1,6 +1,8 @@ | |||||||
| 'use strict'; | 'use strict'; | ||||||
|  |  | ||||||
| const conf = require('../conf'); | const conf = require('../conf'); | ||||||
|  | const {sleep} = require('../utils'); | ||||||
|  |  | ||||||
|  |  | ||||||
| const { | const { | ||||||
| 	GoogleGenerativeAI, | 	GoogleGenerativeAI, | ||||||
| @ -8,23 +10,23 @@ const { | |||||||
| 	HarmBlockThreshold, | 	HarmBlockThreshold, | ||||||
| } = require("@google/generative-ai"); | } = require("@google/generative-ai"); | ||||||
|  |  | ||||||
| const genAI = new GoogleGenerativeAI(conf.ai.key); |  | ||||||
|  |  | ||||||
| const model = genAI.getGenerativeModel({ |  | ||||||
| 	model: "gemini-1.5-flash", |  | ||||||
| }); |  | ||||||
|  |  | ||||||
| const generationConfig = { | class Ai{ | ||||||
|  | 	constructor(prompt){ | ||||||
|  | 		this.prompt = prompt; | ||||||
|  | 		this.start(); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	__settings(history){ | ||||||
|  | 		return { | ||||||
|  | 		generationConfig: { | ||||||
| 			temperature: 1, | 			temperature: 1, | ||||||
| 			topP: 0.95, | 			topP: 0.95, | ||||||
| 			topK: 64, | 			topK: 64, | ||||||
| 			maxOutputTokens: 8192, | 			maxOutputTokens: 8192, | ||||||
| 			responseMimeType: "application/json", | 			responseMimeType: "application/json", | ||||||
| }; | 		}, | ||||||
|  |  | ||||||
| async function run(name, interval, players) { |  | ||||||
| 	const chatSession = model.startChat({ |  | ||||||
| 		generationConfig, |  | ||||||
| 		safetySettings:[ | 		safetySettings:[ | ||||||
| 			// See https://ai.google.dev/gemini-api/docs/safety-settings | 			// See https://ai.google.dev/gemini-api/docs/safety-settings | ||||||
| 			{ | 			{ | ||||||
| @ -44,11 +46,11 @@ async function run(name, interval, players) { | |||||||
| 				threshold: HarmBlockThreshold.BLOCK_NONE, | 				threshold: HarmBlockThreshold.BLOCK_NONE, | ||||||
| 			}, | 			}, | ||||||
| 		], | 		], | ||||||
| 		history: [ | 		history: history || [ | ||||||
| 			{ | 			{ | ||||||
| 				role: "user", | 				role: "user", | ||||||
| 				parts: [ | 				parts: [ | ||||||
| 					{text: conf.ai.prompt(name, interval, players)}, | 					{text: this.prompt}, | ||||||
| 				], | 				], | ||||||
| 			}, | 			}, | ||||||
| 			{ | 			{ | ||||||
| @ -58,10 +60,48 @@ async function run(name, interval, players) { | |||||||
| 				], | 				], | ||||||
| 			} | 			} | ||||||
| 		], | 		], | ||||||
|  | 	} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	start(history){ | ||||||
|  | 		const genAI = new GoogleGenerativeAI(conf.ai.key); | ||||||
|  |  | ||||||
|  | 		const model = genAI.getGenerativeModel({ | ||||||
|  | 			model: "gemini-1.5-flash", | ||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
| 	return chatSession; | 		this.session = model.startChat({ | ||||||
|  | 			...this.__settings(history), | ||||||
|  | 			// systemInstruction: this.prompt, | ||||||
|  | 		}); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	async chat(message, retryCount=0){ | ||||||
|  | 		console.log('chat', retryCount) | ||||||
|  | 		try{ | ||||||
|  | 			let result = await this.session.sendMessage(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(); | ||||||
|  | 			console.log('current history', this.session.params.history); | ||||||
|  | 			this.start(this.session.params.history); | ||||||
|  | 			return await this.chat(message, retryCount++) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| module.exports = {run}; |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  | module.exports = {Ai}; | ||||||
| // run(); | // run(); | ||||||
| @ -1,5 +1,6 @@ | |||||||
| 'use strict'; | 'use strict'; | ||||||
|  |  | ||||||
|  | const axios = require('axios'); | ||||||
|  |  | ||||||
| const {sleep} = require('../utils'); | const {sleep} = require('../utils'); | ||||||
| const conf = require('../conf'); | const conf = require('../conf'); | ||||||
| @ -8,7 +9,7 @@ const inventoryViewer = require('mineflayer-web-inventory'); | |||||||
|  |  | ||||||
| const commands = require('./commands'); | const commands = require('./commands'); | ||||||
| const {onJoin} = require('./player_list'); | const {onJoin} = require('./player_list'); | ||||||
| const ai = require('./ai'); | const {Ai} = require('./ai'); | ||||||
|  |  | ||||||
|  |  | ||||||
| for(let name in conf.mc.bots){ | for(let name in conf.mc.bots){ | ||||||
| @ -31,7 +32,10 @@ for(let name in conf.mc.bots){ | |||||||
| 			// setTimeout(()=> bot.bot.setControlState('jump', false), 5000); | 			// setTimeout(()=> bot.bot.setControlState('jump', false), 5000); | ||||||
| 			if(bot.hasAi){ | 			if(bot.hasAi){ | ||||||
| 				console.log(`${bot.bot.entity.username} has AI`); | 				console.log(`${bot.bot.entity.username} has AI`); | ||||||
| 				let messages = []; | 				let messages = ['']; | ||||||
|  |  | ||||||
|  | 				let bulbaItems = await axios.get('https://webstore.bulbastore.uk/api/listings'); | ||||||
|  | 				bulbaItems = bulbaItems.data.listings.map(i=>i.listing_name); | ||||||
|  |  | ||||||
| 				bot.bot.on('message', (message, type)=>{ | 				bot.bot.on('message', (message, type)=>{ | ||||||
| 					if(type === 'game_info') return; | 					if(type === 'game_info') return; | ||||||
| @ -42,18 +46,22 @@ for(let name in conf.mc.bots){ | |||||||
|  |  | ||||||
| 				await sleep(500); | 				await sleep(500); | ||||||
| 				 | 				 | ||||||
| 				let aiChat = await ai.run( | 				let aiChat = new Ai(conf.ai.prompt( | ||||||
| 					bot.bot.entity.username, | 					bot.bot.entity.username, | ||||||
| 					conf.ai.interval, | 					conf.ai.interval, | ||||||
| 					Object.values(bot.getPlayers()).map(player=>`<[${player.lvl}] ${player.username}>`).join('\n') | 					Object.values(bot.getPlayers()).map(player=>`<[${player.lvl}] ${player.username}>`).join('\n'), | ||||||
| 				); | 					bulbaItems | ||||||
|  | 				)) | ||||||
|  |  | ||||||
| 				 | 				 | ||||||
| 				setInterval(async ()=>{ | 				setInterval(async ()=>{ | ||||||
| 					let result; | 					let result; | ||||||
| 					if(messages.length ===0) return; | 					// if(messages.length ===0) return; | ||||||
| 					 | 					 | ||||||
| 					try{ | 					try{ | ||||||
| 						result = await aiChat.sendMessage(messages); | 						result = await aiChat.chat(JSON.stringify({ | ||||||
|  | 							messages, currentTime:Date.now()+1} | ||||||
|  | 						)); | ||||||
| 					}catch(error){ | 					}catch(error){ | ||||||
| 						console.log('error AI API', error, result); | 						console.log('error AI API', error, result); | ||||||
| 						messages = []; | 						messages = []; | ||||||
| @ -61,7 +69,7 @@ for(let name in conf.mc.bots){ | |||||||
| 					} | 					} | ||||||
|  |  | ||||||
| 					try{ | 					try{ | ||||||
| 						messages = []; | 						messages = ['']; | ||||||
| 						if(!result.response.text()) return; | 						if(!result.response.text()) return; | ||||||
|  |  | ||||||
| 						for(let message of JSON.parse(result.response.text())){ | 						for(let message of JSON.parse(result.response.text())){ | ||||||
| @ -73,6 +81,9 @@ for(let name in conf.mc.bots){ | |||||||
| 						} | 						} | ||||||
| 					}catch(error){ | 					}catch(error){ | ||||||
| 						console.log('Error in AI message loop', error, result); | 						console.log('Error in AI message loop', error, result); | ||||||
|  | 						if(result || result.response || result.response.text()){ | ||||||
|  | 							console.log(result.response.text()) | ||||||
|  | 						} | ||||||
| 					} | 					} | ||||||
| 				}, conf.ai.interval*1000); | 				}, conf.ai.interval*1000); | ||||||
| 			} | 			} | ||||||
|  | |||||||
| @ -277,11 +277,15 @@ class CJbot{ | |||||||
|  |  | ||||||
| 	async say(...messages){ | 	async say(...messages){ | ||||||
| 		for(let message of messages){ | 		for(let message of messages){ | ||||||
|  | 			console.log('next chat time:', this.nextChatTime > Date.now(), Date.now()+1, this.nextChatTime-Date.now()+1); | ||||||
|  | 			(async (message)=>{ | ||||||
| 				if(this.nextChatTime > Date.now()){ | 				if(this.nextChatTime > Date.now()){ | ||||||
|  | 					console.log('am sleeping'); | ||||||
| 					await sleep(this.nextChatTime-Date.now()+1) | 					await sleep(this.nextChatTime-Date.now()+1) | ||||||
| 				} | 				} | ||||||
|  |  | ||||||
| 				this.bot.chat(message); | 				this.bot.chat(message); | ||||||
|  | 			})(message); | ||||||
|  |  | ||||||
| 			this.nextChatTime = Date.now() + this.__chatCoolDown(); | 			this.nextChatTime = Date.now() + this.__chatCoolDown(); | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user