|
| 1 | +import { GoogleGenAI } from '@google/genai'; |
| 2 | +import { BaseEmbedding, TEmbeddings } from './BaseEmbedding'; |
| 3 | +import { AccessCandidate } from '@sre/Security/AccessControl/AccessCandidate.class'; |
| 4 | +import { getLLMCredentials } from '@sre/LLMManager/LLM.service/LLMCredentials.helper'; |
| 5 | +import { TLLMCredentials, TLLMModel, BasicCredentials } from '@sre/types/LLM.types'; |
| 6 | + |
| 7 | +const DEFAULT_MODEL = 'gemini-embedding-001'; |
| 8 | + |
| 9 | +export class GoogleEmbeds extends BaseEmbedding { |
| 10 | + protected client: GoogleGenAI; |
| 11 | + |
| 12 | + // Keep in sync with Gemini API supported embedding models |
| 13 | + public static models = ['gemini-embedding-001', 'text-embedding-005', 'text-multilingual-embedding-002']; |
| 14 | + public canSpecifyDimensions = true; |
| 15 | + |
| 16 | + constructor(private settings?: Partial<TEmbeddings>) { |
| 17 | + super({ model: settings?.model ?? DEFAULT_MODEL, ...settings }); |
| 18 | + } |
| 19 | + |
| 20 | + async embedTexts(texts: string[], candidate: AccessCandidate): Promise<number[][]> { |
| 21 | + const batches = this.chunkArr(this.processTexts(texts), this.chunkSize); |
| 22 | + |
| 23 | + const batchRequests = batches.map((batch) => { |
| 24 | + return this.embed(batch, candidate); |
| 25 | + }); |
| 26 | + const batchResponses = await Promise.all(batchRequests); |
| 27 | + |
| 28 | + const embeddings: number[][] = []; |
| 29 | + for (let i = 0; i < batchResponses.length; i += 1) { |
| 30 | + const batch = batches[i]; |
| 31 | + const batchResponse = batchResponses[i]; |
| 32 | + for (let j = 0; j < batch.length; j += 1) { |
| 33 | + embeddings.push(batchResponse[j]); |
| 34 | + } |
| 35 | + } |
| 36 | + return embeddings; |
| 37 | + } |
| 38 | + |
| 39 | + async embedText(text: string, candidate: AccessCandidate): Promise<number[]> { |
| 40 | + const processedText = this.processTexts([text])[0]; |
| 41 | + const embeddings = await this.embed([processedText], candidate); |
| 42 | + return embeddings[0]; |
| 43 | + } |
| 44 | + |
| 45 | + protected async embed(texts: string[], candidate: AccessCandidate): Promise<number[][]> { |
| 46 | + let apiKey: string | undefined; |
| 47 | + |
| 48 | + // Try to get from credentials first |
| 49 | + try { |
| 50 | + const modelInfo: TLLMModel = { |
| 51 | + provider: 'GoogleAI', |
| 52 | + modelId: this.model, |
| 53 | + credentials: this.settings?.credentials as unknown as TLLMCredentials, |
| 54 | + }; |
| 55 | + const credentials = await getLLMCredentials(candidate, modelInfo); |
| 56 | + apiKey = (credentials as BasicCredentials)?.apiKey; |
| 57 | + } catch (e) { |
| 58 | + // If credential system fails, fall back to environment variable |
| 59 | + } |
| 60 | + |
| 61 | + // Fall back to environment variable if not found in credentials |
| 62 | + if (!apiKey) { |
| 63 | + apiKey = process.env.GOOGLE_AI_API_KEY; |
| 64 | + } |
| 65 | + |
| 66 | + if (!apiKey) { |
| 67 | + throw new Error('Please provide an API key for Google AI embeddings via credentials or GOOGLE_AI_API_KEY environment variable'); |
| 68 | + } |
| 69 | + |
| 70 | + if (!this.client) { |
| 71 | + this.client = new GoogleGenAI({ apiKey }); |
| 72 | + } |
| 73 | + |
| 74 | + try { |
| 75 | + const outputDimensionality = this.dimensions && Number.isFinite(this.dimensions) ? this.dimensions : undefined; |
| 76 | + |
| 77 | + // Batch request using the new SDK |
| 78 | + const res = await this.client.models.embedContent({ |
| 79 | + model: this.model, |
| 80 | + contents: texts, |
| 81 | + ...(outputDimensionality ? { outputDimensionality } : {}), |
| 82 | + }); |
| 83 | + |
| 84 | + // The SDK can return either { embedding } for single or { embeddings } for batch |
| 85 | + const vectors: number[][] = Array.isArray((res as any).embeddings) |
| 86 | + ? (res as any).embeddings.map((e: any) => e.values as number[]) |
| 87 | + : [((res as any).embedding?.values as number[]) || []]; |
| 88 | + |
| 89 | + // Enforce dimensions and normalization when requested or when non-3072 |
| 90 | + const targetDim = outputDimensionality; |
| 91 | + const processed = vectors.map((v) => this.postProcessEmbedding(v, targetDim)); |
| 92 | + |
| 93 | + return processed; |
| 94 | + } catch (e) { |
| 95 | + throw new Error(`Google Embeddings API error: ${e.message || e}`); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private postProcessEmbedding(values: number[], targetDim?: number): number[] { |
| 100 | + let v = Array.isArray(values) ? values.slice() : []; |
| 101 | + if (targetDim && targetDim > 0) { |
| 102 | + if (v.length > targetDim) { |
| 103 | + // SDK ignored smaller dimension: truncate |
| 104 | + v = v.slice(0, targetDim); |
| 105 | + } else if (v.length < targetDim) { |
| 106 | + // SDK returned shorter vector: pad with zeros |
| 107 | + v = v.concat(Array(targetDim - v.length).fill(0)); |
| 108 | + } |
| 109 | + } |
| 110 | + // Normalize for non-default 3072 dims (recommended by Google docs) |
| 111 | + const needNormalize = (targetDim && targetDim !== 3072) || (!targetDim && v.length !== 3072); |
| 112 | + if (needNormalize && v.length > 0) { |
| 113 | + const norm = Math.sqrt(v.reduce((acc, x) => acc + x * x, 0)); |
| 114 | + if (norm > 0) v = v.map((x) => x / norm); |
| 115 | + } |
| 116 | + return v; |
| 117 | + } |
| 118 | +} |
0 commit comments