Rework AI and add more stats
This commit is contained in:
parent
29832dfce3
commit
c8de53bfc7
14 changed files with 197 additions and 169 deletions
|
|
@ -3,6 +3,9 @@ import type { ICallbackStore } from "../types.js";
|
|||
import { config } from "../../config.js";
|
||||
import { getTextGemini, getImageGemini } from "../../services/ai/ai.js";
|
||||
import { alts } from "./alts.js";
|
||||
import type { IAdminInstructions } from "./types.js";
|
||||
import { getUserById } from "../../helpers.js";
|
||||
import { prices } from "./prices.js";
|
||||
|
||||
let client: MatrixClient;
|
||||
|
||||
|
|
@ -15,40 +18,58 @@ const registerModuleAI = (
|
|||
callbackStore.messageCallbacks.push({
|
||||
startConditions: [`${config.app.triggerPrefix}ai `],
|
||||
callbackFunc: onAI,
|
||||
allowedRoles: ["USER", "MODERATOR", "ADMIN"],
|
||||
});
|
||||
callbackStore.messageCallbacks.push({
|
||||
startConditions: [
|
||||
...alts.map((alt) => `${alt.key} when`),
|
||||
...alts.map((alt) => `${alt.key} ked`),
|
||||
`!img`,
|
||||
],
|
||||
startConditions: [`${config.app.triggerPrefix}img `],
|
||||
callbackFunc: onImageGen,
|
||||
allowedRoles: ["USER", "MODERATOR", "ADMIN"],
|
||||
});
|
||||
};
|
||||
|
||||
const onAI = async (text: string, roomId: string) => {
|
||||
const onAI = async (text: string, roomId: string, sender: string) => {
|
||||
const user = getUserById(sender);
|
||||
|
||||
if (text.trim().length < 5) {
|
||||
return;
|
||||
}
|
||||
|
||||
const responseAI = await getTextGemini(text.replace("!ai ", ""));
|
||||
client.sendTextMessage(roomId, responseAI);
|
||||
let textMod = text.replace("!ai", "").trim().toLowerCase();
|
||||
let instructions = {
|
||||
prefferedLanguages: ["english", "slovak"],
|
||||
adminText: "Be concise, try to keep text as short as possible",
|
||||
alts: alts,
|
||||
} as IAdminInstructions;
|
||||
|
||||
textMod = `Admin Instructions:\n${instructions}\nPrompt:\n${textMod}`;
|
||||
|
||||
const responseAI = await getTextGemini(textMod);
|
||||
|
||||
user.aiCost += responseAI.tokens * prices.text;
|
||||
|
||||
client.sendTextMessage(roomId, responseAI.text);
|
||||
};
|
||||
|
||||
const onImageGen = async (text: string, roomId: string) => {
|
||||
const onImageGen = async (text: string, roomId: string, sender: string) => {
|
||||
const user = getUserById(sender);
|
||||
|
||||
let textMod = text.replace("!img", "").trim().toLowerCase();
|
||||
alts.forEach((alt) => {
|
||||
textMod = textMod.replaceAll(alt.key, alt.alt);
|
||||
alt.keys.forEach((key) => {
|
||||
textMod = textMod.replaceAll(key, alt.alt);
|
||||
});
|
||||
});
|
||||
|
||||
const buffer = await getImageGemini(textMod);
|
||||
if (!buffer || buffer.length < 10) {
|
||||
const responseAI = await getImageGemini(textMod);
|
||||
|
||||
user.aiCost += responseAI.tokens * prices.image;
|
||||
if (!responseAI.image || responseAI.image.length < 10) {
|
||||
return;
|
||||
}
|
||||
|
||||
const imageName = `photo-img-gen.png`;
|
||||
|
||||
const uploadResult = await client.uploadContent(buffer, {
|
||||
const uploadResult = await client.uploadContent(responseAI.image, {
|
||||
type: "image/png",
|
||||
name: imageName,
|
||||
});
|
||||
|
|
@ -59,7 +80,7 @@ const onImageGen = async (text: string, roomId: string) => {
|
|||
url: uploadResult.content_uri,
|
||||
info: {
|
||||
mimetype: "image/png",
|
||||
size: buffer.length,
|
||||
size: responseAI.image.length,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue