Add ai functionality
This commit is contained in:
parent
da66cf9001
commit
dd4da06753
19 changed files with 1118 additions and 52 deletions
44
src/services/ai/ai.ts
Normal file
44
src/services/ai/ai.ts
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import { GoogleGenAI } from "@google/genai";
|
||||
import { config } from "../../config.js";
|
||||
|
||||
const googleAI = new GoogleGenAI({
|
||||
apiKey: config.app.ai.api.key,
|
||||
});
|
||||
|
||||
const getTextGemini = async (input: string): Promise<string> => {
|
||||
const response = await googleAI.models.generateContent({
|
||||
model: "gemini-3-flash-preview",
|
||||
contents: input,
|
||||
});
|
||||
|
||||
return response.text ?? "AI Error";
|
||||
};
|
||||
|
||||
const getImageNanoBanana = async (
|
||||
input: string,
|
||||
): Promise<Buffer<ArrayBuffer> | undefined> => {
|
||||
const response = await googleAI.models.generateContent({
|
||||
model: "gemini-2.5-flash-image",
|
||||
contents: input,
|
||||
});
|
||||
|
||||
const firstCandidate = (response.candidates ?? [])[0];
|
||||
const parts = firstCandidate?.content?.parts ?? [];
|
||||
|
||||
let buffer: Buffer<ArrayBuffer> | undefined = undefined;
|
||||
|
||||
parts.forEach((part) => {
|
||||
if (part.inlineData) {
|
||||
const imageData = part.inlineData.data;
|
||||
if (!imageData) {
|
||||
return;
|
||||
}
|
||||
|
||||
buffer = Buffer.from(imageData, "base64");
|
||||
}
|
||||
});
|
||||
|
||||
return buffer;
|
||||
};
|
||||
|
||||
export { getTextGemini, getImageNanoBanana };
|
||||
Loading…
Add table
Add a link
Reference in a new issue