feat: add support for GEMINI_API_KEY and improve error messages in AI service
This commit is contained in:
3
.env
3
.env
@@ -19,7 +19,8 @@ APPWRITE_COLLECTION_AI_CONVERSATIONS_ID="aiconversation"
|
|||||||
# ===========================================
|
# ===========================================
|
||||||
TOKEN=OTkxMDYyNzUxNjMzODgzMTM2.Gjzppb.OsdqEDhl_tiQmw4KL7ITbEZ1e-s9VeoF_xJvQQ
|
TOKEN=OTkxMDYyNzUxNjMzODgzMTM2.Gjzppb.OsdqEDhl_tiQmw4KL7ITbEZ1e-s9VeoF_xJvQQ
|
||||||
guildTest=1316592320954630144
|
guildTest=1316592320954630144
|
||||||
GOOGLE_AI_API_KEY=AIzaSyDNTHsqpbiYaEpe5AwykSqtgWGjsZcc_RA #AIzaSyDcqOndCJw02xFs305iQE7KVptBoBH8aPk
|
GOOGLE_AI_API_KEY=AIzaSyDNTHsqpbiYaEpe5AwykSqtgWGjsZcc_RA
|
||||||
|
GEMINI_API_KEY=AIzaSyDcqOndCJw02xFs305iQE7KVptBoBH8aPk
|
||||||
CLIENT=991062751633883136
|
CLIENT=991062751633883136
|
||||||
|
|
||||||
# ===========================================
|
# ===========================================
|
||||||
|
|||||||
@@ -4,16 +4,13 @@ import logger from '../../../core/lib/logger';
|
|||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'image',
|
name: 'image',
|
||||||
|
aliases: ['imagen', 'img', 'aiimage'],
|
||||||
description: 'Genera una imagen usando IA',
|
description: 'Genera una imagen usando IA',
|
||||||
type: 'message',
|
|
||||||
aliases: ['imagen', 'img', 'draw', 'dibuja'],
|
|
||||||
category: 'AI',
|
|
||||||
usage: 'imagen <descripción>',
|
|
||||||
cooldown: 10,
|
cooldown: 10,
|
||||||
async run(message: Message, args: string[]) {
|
async run(message: Message, args: string[]) {
|
||||||
// Verificar que hay un prompt
|
// Verificar que hay un prompt
|
||||||
if (!args || args.length === 0) {
|
if (!args || args.length === 0) {
|
||||||
await message.reply('❌ **Error**: Debes proporcionar una descripción para generar la imagen.\n\n**Ejemplo**: `imagen un gato espacial flotando entre estrellas`');
|
await message.reply('❌ **Error**: Debes proporcionar una descripción para generar la imagen.\n\n**Ejemplo**: `!image un gato espacial flotando entre estrellas`');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { GoogleGenerativeAI, HarmCategory, HarmBlockThreshold } from "@google/generative-ai";
|
import { GoogleGenerativeAI, HarmCategory, HarmBlockThreshold } from "@google/generative-ai";
|
||||||
// New: modern GenAI SDK for image generation
|
|
||||||
import { GoogleGenAI, PersonGeneration } from "@google/genai";
|
import { GoogleGenAI, PersonGeneration } from "@google/genai";
|
||||||
import logger from "../lib/logger";
|
import logger from "../lib/logger";
|
||||||
import { Collection } from "discord.js";
|
import { Collection } from "discord.js";
|
||||||
@@ -116,9 +115,9 @@ export class AIService {
|
|||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
const apiKey = process.env.GOOGLE_AI_API_KEY;
|
const apiKey = process.env.GOOGLE_AI_API_KEY || process.env.GEMINI_API_KEY;
|
||||||
if (!apiKey) {
|
if (!apiKey) {
|
||||||
throw new Error('GOOGLE_AI_API_KEY no está configurada');
|
throw new Error('Falta la clave de Google AI. Define GOOGLE_AI_API_KEY o GEMINI_API_KEY en las variables de entorno.');
|
||||||
}
|
}
|
||||||
|
|
||||||
this.genAI = new GoogleGenerativeAI(apiKey);
|
this.genAI = new GoogleGenerativeAI(apiKey);
|
||||||
@@ -131,6 +130,13 @@ export class AIService {
|
|||||||
this.genAIv2 = null;
|
this.genAIv2 = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Permitir override de modelo por variable de entorno
|
||||||
|
const envImageModel = process.env.GENAI_IMAGE_MODEL;
|
||||||
|
if (envImageModel && envImageModel.trim()) {
|
||||||
|
this.imageModelName = envImageModel.trim();
|
||||||
|
logger.info({ model: this.imageModelName }, 'Modelo de imágenes fijado por GENAI_IMAGE_MODEL');
|
||||||
|
}
|
||||||
|
|
||||||
this.startQueueProcessor();
|
this.startQueueProcessor();
|
||||||
this.startCleanupService();
|
this.startCleanupService();
|
||||||
this.detectImageModel();
|
this.detectImageModel();
|
||||||
@@ -145,10 +151,14 @@ export class AIService {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lista de candidatos de modelos de imagen ordenados por preferencia (actualizada según Google AI Studio)
|
// Lista de candidatos de modelos de imagen ordenados por preferencia (actualizada + retrocompatibilidad)
|
||||||
const candidates = [
|
const candidates = [
|
||||||
'models/imagen-4.0-generate-001',
|
'models/imagen-4.0-generate-001',
|
||||||
'imagen-4.0-generate-001',
|
'imagen-4.0-generate-001',
|
||||||
|
'models/imagen-3.0-fast',
|
||||||
|
'imagen-3.0-fast',
|
||||||
|
'models/imagen-3.0',
|
||||||
|
'imagen-3.0',
|
||||||
'models/gemini-2.5-flash-image',
|
'models/gemini-2.5-flash-image',
|
||||||
'gemini-2.5-flash-image',
|
'gemini-2.5-flash-image',
|
||||||
];
|
];
|
||||||
@@ -198,8 +208,7 @@ export class AIService {
|
|||||||
// Fallback: probar modelos uno por uno
|
// Fallback: probar modelos uno por uno
|
||||||
for (const candidate of candidates) {
|
for (const candidate of candidates) {
|
||||||
try {
|
try {
|
||||||
// Probar con la nueva API de generateImages
|
await (this.genAIv2 as any).models.generateImages({
|
||||||
const testRes: any = await (this.genAIv2 as any).models.generateImages({
|
|
||||||
model: candidate,
|
model: candidate,
|
||||||
prompt: 'test',
|
prompt: 'test',
|
||||||
config: {
|
config: {
|
||||||
|
|||||||
Reference in New Issue
Block a user