new
This commit is contained in:
@ -1,6 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
const conf = require('../conf');
|
||||
const {sleep} = require('../utils');
|
||||
|
||||
|
||||
const {
|
||||
GoogleGenerativeAI,
|
||||
@ -8,23 +10,23 @@ const {
|
||||
HarmBlockThreshold,
|
||||
} = require("@google/generative-ai");
|
||||
|
||||
const genAI = new GoogleGenerativeAI(conf.ai.key);
|
||||
|
||||
const model = genAI.getGenerativeModel({
|
||||
model: "gemini-1.5-flash",
|
||||
});
|
||||
|
||||
const generationConfig = {
|
||||
temperature: 1,
|
||||
topP: 0.95,
|
||||
topK: 64,
|
||||
maxOutputTokens: 8192,
|
||||
responseMimeType: "application/json",
|
||||
};
|
||||
class Ai{
|
||||
constructor(prompt){
|
||||
this.prompt = prompt;
|
||||
this.start();
|
||||
}
|
||||
|
||||
async function run(name, interval, players) {
|
||||
const chatSession = model.startChat({
|
||||
generationConfig,
|
||||
__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
|
||||
{
|
||||
@ -44,11 +46,11 @@ async function run(name, interval, players) {
|
||||
threshold: HarmBlockThreshold.BLOCK_NONE,
|
||||
},
|
||||
],
|
||||
history: [
|
||||
history: history || [
|
||||
{
|
||||
role: "user",
|
||||
parts: [
|
||||
{text: conf.ai.prompt(name, interval, players)},
|
||||
{text: this.prompt},
|
||||
],
|
||||
},
|
||||
{
|
||||
@ -58,10 +60,48 @@ async function run(name, interval, players) {
|
||||
],
|
||||
}
|
||||
],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return chatSession;
|
||||
start(history){
|
||||
const genAI = new GoogleGenerativeAI(conf.ai.key);
|
||||
|
||||
const model = genAI.getGenerativeModel({
|
||||
model: "gemini-1.5-flash",
|
||||
});
|
||||
|
||||
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();
|
Reference in New Issue
Block a user