|
| 1 | +import { getEncoding, Tiktoken } from 'js-tiktoken'; |
| 2 | + |
| 3 | +import { GoogleGenAI } from '@google/genai'; |
| 4 | +import { PromptTemplate } from '@langchain/core/prompts'; |
| 5 | +import Together from 'together-ai'; |
| 6 | + |
| 7 | +import { AppError } from '../../../shared/errors'; |
| 8 | +import { env } from '../../../shared/config/environment'; |
| 9 | +import { logger } from '../../../shared/utils/logger'; |
| 10 | + |
| 11 | +import { AIGenerationModel, HigherTierImageGenerationRequest, LowerTierImageGenerationResponse } from '../types'; |
| 12 | +import { IMAGE_GENERATION_USER_PROMPT } from '../config/prompts'; |
| 13 | +import { IMAGE_GENERATION_MODEL_CONFIGS } from '../config/models'; |
| 14 | +import { AIProvider, checkEnvironmentVariables } from '../utils/ai.utils'; |
| 15 | +import { ImageGenerationStrategy } from '../strategy/generation.strategy'; |
| 16 | + |
| 17 | +const APP_ERROR_SOURCE = 'image.google.generation.service'; |
| 18 | + |
| 19 | +export class GeminiImageGenerationService implements ImageGenerationStrategy { |
| 20 | + private readonly defaultModelType: AIGenerationModel; |
| 21 | + private prompt: PromptTemplate; |
| 22 | + private encoding: Tiktoken; |
| 23 | + |
| 24 | + constructor() { |
| 25 | + checkEnvironmentVariables(APP_ERROR_SOURCE, AIProvider.GOOGLE); |
| 26 | + |
| 27 | + this.defaultModelType = AIGenerationModel.Gemini20FlashImageGenPreview; |
| 28 | + this.prompt = PromptTemplate.fromTemplate(IMAGE_GENERATION_USER_PROMPT); |
| 29 | + this.encoding = getEncoding('cl100k_base'); |
| 30 | + } |
| 31 | + |
| 32 | + setupGoogleGenAIClient(): GoogleGenAI { |
| 33 | + const genAI = new GoogleGenAI({ |
| 34 | + apiKey: process.env.GEMINI_API_KEY!, |
| 35 | + vertexai: process.env.GOOGLE_USE_VERTEX_AI === 'true' |
| 36 | + }); |
| 37 | + |
| 38 | + if (!genAI) { |
| 39 | + throw new AppError( |
| 40 | + 500, |
| 41 | + 'Failed to initialize Google GenAI client. Please check your environment variables.', |
| 42 | + APP_ERROR_SOURCE |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + return genAI; |
| 47 | + } |
| 48 | + |
| 49 | + async generate( |
| 50 | + request: LowerTierImageGenerationResponse | HigherTierImageGenerationRequest |
| 51 | + ): Promise<LowerTierImageGenerationResponse | HigherTierImageGenerationRequest> { |
| 52 | + const startTime = Date.now(); |
| 53 | + const modelType = request.options?.model || this.defaultModelType; |
| 54 | + |
| 55 | + try { |
| 56 | + const genAI = this.setupGoogleGenAIClient(); |
| 57 | + const formattedPrompt = await this.prompt.format({ |
| 58 | + text: request.text, |
| 59 | + }); |
| 60 | + |
| 61 | + const result = await genAI.models.generateImages({ |
| 62 | + model: this.defaultModelType, |
| 63 | + prompt: formattedPrompt, |
| 64 | + config: { |
| 65 | + ...IMAGE_GENERATION_MODEL_CONFIGS[modelType], |
| 66 | + } |
| 67 | + }); |
| 68 | + |
| 69 | + const imageUrl = result?.generatedImages?.[0]?.image?.imageBytes; |
| 70 | + if (!imageUrl) { |
| 71 | + throw new AppError( |
| 72 | + 500, |
| 73 | + 'Image generation was unsuccessful, there was no image returned for the request.', |
| 74 | + APP_ERROR_SOURCE |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + const endTime = Date.now(); |
| 79 | + const tokenCount = this.encoding.encode(formattedPrompt).length; |
| 80 | + const response: LowerTierImageGenerationResponse = {}; |
| 81 | + |
| 82 | + // TODO: Finish implementation for generate method in GeminiImageGenerationService |
| 83 | + |
| 84 | + } catch (error) { |
| 85 | + if (error instanceof AppError) { |
| 86 | + throw error; |
| 87 | + } else { |
| 88 | + logger.error( |
| 89 | + APP_ERROR_SOURCE, |
| 90 | + 'An unexpected error occurred during image generation.', |
| 91 | + error |
| 92 | + ); |
| 93 | + throw new AppError( |
| 94 | + 500, |
| 95 | + 'An unexpected error occurred during image generation. Please try again later.', |
| 96 | + APP_ERROR_SOURCE |
| 97 | + ); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments