Rework AI and add more stats
This commit is contained in:
parent
29832dfce3
commit
c8de53bfc7
14 changed files with 197 additions and 169 deletions
|
|
@ -1,22 +1,24 @@
|
|||
import { GoogleGenAI } from "@google/genai";
|
||||
import { config } from "../../config.js";
|
||||
import type { AIResponseImage, AIResponseText } from "./types.js";
|
||||
|
||||
const googleAI = new GoogleGenAI({
|
||||
apiKey: config.app.ai.api.key,
|
||||
});
|
||||
|
||||
const getTextGemini = async (input: string): Promise<string> => {
|
||||
const getTextGemini = async (input: string): Promise<AIResponseText> => {
|
||||
const response = await googleAI.models.generateContent({
|
||||
model: "gemini-3-flash-preview",
|
||||
contents: input,
|
||||
});
|
||||
|
||||
return response.text ?? "AI Error";
|
||||
return {
|
||||
text: response.text ?? "AI Error",
|
||||
tokens: response.usageMetadata?.totalTokenCount ?? 0,
|
||||
};
|
||||
};
|
||||
|
||||
const getImageGemini = async (
|
||||
input: string,
|
||||
): Promise<Buffer<ArrayBuffer> | undefined> => {
|
||||
const getImageGemini = async (input: string): Promise<AIResponseImage> => {
|
||||
const response = await googleAI.models.generateContent({
|
||||
model: "gemini-2.5-flash-image",
|
||||
contents: input,
|
||||
|
|
@ -38,7 +40,10 @@ const getImageGemini = async (
|
|||
}
|
||||
});
|
||||
|
||||
return buffer;
|
||||
return {
|
||||
image: buffer,
|
||||
tokens: response.usageMetadata?.totalTokenCount ?? 0,
|
||||
};
|
||||
};
|
||||
|
||||
export { getTextGemini, getImageGemini };
|
||||
|
|
|
|||
|
|
@ -1 +1,2 @@
|
|||
export * from "./ai.js";
|
||||
export * from "./types.js";
|
||||
|
|
|
|||
11
src/services/ai/types.ts
Normal file
11
src/services/ai/types.ts
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
interface AIResponseText {
|
||||
text: string;
|
||||
tokens: number;
|
||||
}
|
||||
|
||||
interface AIResponseImage {
|
||||
image: Buffer<ArrayBuffer> | undefined;
|
||||
tokens: number;
|
||||
}
|
||||
|
||||
export { type AIResponseText, type AIResponseImage };
|
||||
Loading…
Add table
Add a link
Reference in a new issue