chatbot with context

This commit is contained in:
Flummi 2023-07-19 12:31:39 +02:00
parent a59e8f99aa
commit 9afd536a5e

View File

@ -1,7 +1,10 @@
import fetch from 'flumm-fetch-cookies';
//import fetch from 'flumm-fetch-cookies';
import fetch from './lib/fetch.mjs';
import oger from './lang/oger.mjs';
import cfg from '../config.mjs';
const conversationIDs = new Map();
export default async bot => {
return [{
@ -13,68 +16,34 @@ export default async bot => {
if(!cfg.apis.chatbot || e.user.prefix?.toLowerCase().startsWith('uwe'))
return console.log('wo config?');
let finish = false;
const body = {
wordLimit: 160,
query: e.message.replace(/uw(e|i)/gi, "")
};
const conversationId = conversationIDs.has(e.channel) ? conversationIDs.get(e.channel) : null;
if(conversationId)
body.conversationId = conversationId;
else {
body.query += '. Antworte von nun an immer auf Deutsch in Du-Form.';
}
for(const api of cfg.apis.chatbot) {
if(finish)
return;
const opts = {
method: 'POST',
headers: api.headers,
body: JSON.stringify({
wordLimit: 160,
query: e.message.replace(/uw(e|i)/gi, "") + '. Antworte auf Deutsch in Du-Form.'
})
body: JSON.stringify(body)
};
const res = await (await fetch(api.url, opts)).json();
if(res.conversationId)
conversationIDs.set(e.channel, res.conversationId);
if(res.response) {
finish = true;
return await e.reply(oger(res.response).trim());
}
}
}
}, {
name: "chatgpt",
call: /^(\.|\/)gpt/i,
set: "uwe",
active: false,
f: async e => {
if(!cfg.apis.chatgpt || e.user.prefix?.toLowerCase().startsWith('uwe'))
return console.log('wo config?');
let actcon = [];
const constr = `${e.network}::${e.channel}`;
if(conversations.has(constr))
actcon.push(...conversations.get(constr));
else
actcon.push(initial);
actcon.push({
role: 'user',
content: e.message.replace(/uw(e|i)/gi, "")
});
const opts = {
method: 'POST',
headers: cfg.apis.chatgpt.headers,
body: JSON.stringify({
model: 'gpt-3.5-turbo',
messages: actcon
})
};
const res = await (await fetch(cfg.apis.chatgpt.url, opts)).json();
if(res.choices[0]?.message) {
actcon.push(res.choices[0].message);
conversations.set(constr, actcon);
console.log(actcon);
return await e.reply(oger(res.choices[0].message.content));
}
}
}]
};