chore(deps)!: migrate from @google/generative-ai to @google/genai
The @google/generative-ai SDK has been deprecated by Google. This switches to the successor @google/genai package and rewrites the Gemini provider against the new stateless models/chats API. BREAKING CHANGE: GeminiConfig.safetySettings now uses the SafetySetting type from @google/genai. Consumers passing this field must update their import from '@google/generative-ai' to '@google/genai'. The shape of the type is similar but not identical. Notable simplifications enabled by the new SDK: - No per-model client caching: generateContent specifies model per-call - Single code path for both single- and multi-turn (full contents array is passed to generateContent directly; no more startChat branching) - Stream chunks expose .text as a property (was .text() method) - Stream iteration is direct on the response (no .stream sub-property) Default model bumped from gemini-1.5-flash to gemini-2.5-flash.
This commit is contained in:
@@ -1,32 +1,26 @@
|
||||
/**
|
||||
* Google Gemini Provider Implementation
|
||||
*
|
||||
* This module provides integration with Google's Gemini models through the official
|
||||
* Generative AI SDK. Gemini offers cutting-edge AI capabilities including multimodal
|
||||
* understanding, advanced reasoning, and efficient text generation across various tasks.
|
||||
*
|
||||
*
|
||||
* Integrates with Google's Gemini models through the official @google/genai SDK
|
||||
* (the successor to the deprecated @google/generative-ai package).
|
||||
*
|
||||
* Key Features:
|
||||
* - Support for all Gemini model variants (Gemini 1.5 Pro, Flash, Pro Vision)
|
||||
* - Advanced streaming support with real-time token delivery
|
||||
* - Native multimodal capabilities (text, images, video)
|
||||
* - Sophisticated safety settings and content filtering
|
||||
* - Flexible generation configuration with fine-grained control
|
||||
*
|
||||
* Gemini-Specific Considerations:
|
||||
* - Uses "candidates" for response variants and "usageMetadata" for token counts
|
||||
* - Supports system instructions as separate parameter (not in conversation)
|
||||
* - Has sophisticated safety filtering with customizable thresholds
|
||||
* - Provides extensive generation configuration options
|
||||
* - Supports both single-turn and multi-turn conversations
|
||||
* - Offers advanced reasoning and coding capabilities
|
||||
*
|
||||
* - Support for Gemini 1.5 / 2.0 / 2.5 model families
|
||||
* - Streaming responses with real-time chunk delivery
|
||||
* - Multimodal capabilities and configurable safety filtering
|
||||
* - System instruction handling separate from conversation flow
|
||||
*
|
||||
* @author Jan-Marlon Leibl
|
||||
* @version 1.0.0
|
||||
* @see https://ai.google.dev/docs
|
||||
*/
|
||||
|
||||
import { GoogleGenerativeAI, GenerativeModel } from '@google/generative-ai';
|
||||
import type { Content, Part, GenerationConfig, SafetySetting } from '@google/generative-ai';
|
||||
import { GoogleGenAI } from '@google/genai';
|
||||
import type {
|
||||
Content,
|
||||
GenerateContentConfig,
|
||||
GenerateContentResponse,
|
||||
SafetySetting
|
||||
} from '@google/genai';
|
||||
import type {
|
||||
AIProviderConfig,
|
||||
CompletionParams,
|
||||
@@ -44,84 +38,39 @@ import { AIProviderError, AIErrorType } from '../types/index.js';
|
||||
|
||||
/**
|
||||
* Configuration interface for Gemini provider with Google-specific options.
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const config: GeminiConfig = {
|
||||
* apiKey: process.env.GOOGLE_API_KEY!,
|
||||
* defaultModel: 'gemini-1.5-pro',
|
||||
* safetySettings: [
|
||||
* {
|
||||
* category: HarmCategory.HARM_CATEGORY_HARASSMENT,
|
||||
* threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
|
||||
* }
|
||||
* ],
|
||||
* generationConfig: {
|
||||
* temperature: 0.7,
|
||||
* topP: 0.8,
|
||||
* topK: 40,
|
||||
* maxOutputTokens: 2048
|
||||
* }
|
||||
* };
|
||||
* ```
|
||||
*/
|
||||
export interface GeminiConfig extends AIProviderConfig {
|
||||
/**
|
||||
/**
|
||||
* Default Gemini model to use for requests.
|
||||
*
|
||||
*
|
||||
* Recommended models:
|
||||
* - 'gemini-1.5-pro': Flagship model, best overall performance and multimodal
|
||||
* - 'gemini-1.5-flash': Faster and cheaper, good for simple tasks
|
||||
* - 'gemini-1.0-pro': Previous generation, cost-effective
|
||||
* - 'gemini-pro-vision': Specialized for vision tasks (legacy)
|
||||
*
|
||||
* @default 'gemini-1.5-flash'
|
||||
* - 'gemini-2.5-flash': Latest fast model, great default
|
||||
* - 'gemini-2.5-pro': Flagship reasoning model
|
||||
* - 'gemini-2.0-flash': Previous generation fast model
|
||||
* - 'gemini-1.5-pro': Long-context flagship from the 1.5 generation
|
||||
*
|
||||
* @default 'gemini-2.5-flash'
|
||||
*/
|
||||
defaultModel?: string;
|
||||
|
||||
/**
|
||||
* Safety settings for content filtering and harm prevention.
|
||||
*
|
||||
* Gemini includes built-in safety filtering across multiple categories:
|
||||
* - Harassment and bullying
|
||||
* - Hate speech and discrimination
|
||||
* - Sexually explicit content
|
||||
* - Dangerous or harmful activities
|
||||
*
|
||||
* Each category can be configured with different blocking thresholds.
|
||||
*
|
||||
* @see https://ai.google.dev/docs/safety_setting
|
||||
*/
|
||||
/** Safety settings for content filtering and harm prevention. */
|
||||
safetySettings?: SafetySetting[];
|
||||
|
||||
/**
|
||||
* Generation configuration for controlling output characteristics.
|
||||
*
|
||||
* This allows fine-tuned control over the generation process including
|
||||
* creativity, diversity, length, and stopping conditions.
|
||||
*
|
||||
* @see https://ai.google.dev/docs/concepts#generation_configuration
|
||||
*/
|
||||
/** Default generation configuration applied to every request. */
|
||||
generationConfig?: {
|
||||
/** Controls randomness in generation (0.0 to 1.0) */
|
||||
temperature?: number;
|
||||
/** Controls nucleus sampling for diversity (0.0 to 1.0) */
|
||||
topP?: number;
|
||||
/** Controls top-k sampling for diversity (positive integer) */
|
||||
topK?: number;
|
||||
/** Maximum number of tokens to generate */
|
||||
maxOutputTokens?: number;
|
||||
/** Sequences that will stop generation when encountered */
|
||||
stopSequences?: string[];
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Processed messages structure for Gemini's conversation format.
|
||||
* Separates system instructions from conversational content.
|
||||
*/
|
||||
interface ProcessedMessages {
|
||||
/** System instruction for model behavior (if any) */
|
||||
/** System instruction string (if any) */
|
||||
systemInstruction?: string;
|
||||
/** Conversation content in Gemini's format */
|
||||
contents: Content[];
|
||||
@@ -132,80 +81,24 @@ interface ProcessedMessages {
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Google Gemini provider implementation.
|
||||
*
|
||||
* This class handles all interactions with Google's Gemini models through their
|
||||
* official Generative AI SDK. It provides optimized handling of Gemini's unique
|
||||
* features including multimodal inputs, safety filtering, and advanced generation control.
|
||||
*
|
||||
* Usage Pattern:
|
||||
* 1. Create instance with Google API key and configuration
|
||||
* 2. Call initialize() to set up client and validate credentials
|
||||
* 3. Use complete() or stream() for text generation
|
||||
* 4. Handle any AIProviderError exceptions appropriately
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const gemini = new GeminiProvider({
|
||||
* apiKey: process.env.GOOGLE_API_KEY!,
|
||||
* defaultModel: 'gemini-1.5-pro',
|
||||
* generationConfig: {
|
||||
* temperature: 0.7,
|
||||
* maxOutputTokens: 2048
|
||||
* }
|
||||
* });
|
||||
*
|
||||
* await gemini.initialize();
|
||||
*
|
||||
* const response = await gemini.complete({
|
||||
* messages: [
|
||||
* { role: 'system', content: 'You are a helpful research assistant.' },
|
||||
* { role: 'user', content: 'Explain quantum computing.' }
|
||||
* ],
|
||||
* maxTokens: 1000,
|
||||
* temperature: 0.8
|
||||
* });
|
||||
* ```
|
||||
* Google Gemini provider implementation backed by @google/genai.
|
||||
*
|
||||
* The new SDK is stateless w.r.t. model selection — each `generateContent` call
|
||||
* specifies the model directly, so this class only holds a single client.
|
||||
*/
|
||||
export class GeminiProvider extends BaseAIProvider {
|
||||
// ========================================================================
|
||||
// INSTANCE PROPERTIES
|
||||
// ========================================================================
|
||||
|
||||
/** Google Generative AI client instance (initialized during doInitialize) */
|
||||
private client: GoogleGenerativeAI | null = null;
|
||||
|
||||
/** Default model instance for requests */
|
||||
private model: GenerativeModel | null = null;
|
||||
|
||||
/** Default model identifier for requests */
|
||||
private client: GoogleGenAI | null = null;
|
||||
private readonly defaultModel: string;
|
||||
|
||||
/** Safety settings for content filtering */
|
||||
private readonly safetySettings?: SafetySetting[];
|
||||
|
||||
/** Generation configuration defaults */
|
||||
private readonly generationConfig?: any;
|
||||
private readonly defaultGenerationConfig?: GeminiConfig['generationConfig'];
|
||||
|
||||
// ========================================================================
|
||||
// CONSTRUCTOR
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* Creates a new Gemini provider instance.
|
||||
*
|
||||
* @param config - Gemini-specific configuration options
|
||||
* @throws {AIProviderError} If configuration validation fails
|
||||
*/
|
||||
constructor(config: GeminiConfig) {
|
||||
super(config);
|
||||
|
||||
// Set Gemini-specific defaults
|
||||
this.defaultModel = config.defaultModel || 'gemini-1.5-flash';
|
||||
|
||||
this.defaultModel = config.defaultModel || 'gemini-2.5-flash';
|
||||
this.safetySettings = config.safetySettings;
|
||||
this.generationConfig = config.generationConfig;
|
||||
|
||||
// Validate model name format
|
||||
this.defaultGenerationConfig = config.generationConfig;
|
||||
|
||||
this.validateModelName(this.defaultModel);
|
||||
}
|
||||
|
||||
@@ -213,38 +106,14 @@ export class GeminiProvider extends BaseAIProvider {
|
||||
// PROTECTED TEMPLATE METHOD IMPLEMENTATIONS
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* Initializes the Gemini provider by setting up the client and model.
|
||||
*
|
||||
* This method:
|
||||
* 1. Creates the Google Generative AI client with API key
|
||||
* 2. Sets up the default model with safety and generation settings
|
||||
* 3. Tests the connection with a minimal API call
|
||||
* 4. Validates API key permissions and model access
|
||||
*
|
||||
* @protected
|
||||
* @throws {Error} If client creation or connection validation fails
|
||||
*/
|
||||
protected async doInitialize(): Promise<void> {
|
||||
try {
|
||||
// Create Google Generative AI client
|
||||
this.client = new GoogleGenerativeAI(this.config.apiKey);
|
||||
|
||||
// Set up default model with configuration
|
||||
this.model = this.client.getGenerativeModel({
|
||||
model: this.defaultModel,
|
||||
safetySettings: this.safetySettings,
|
||||
generationConfig: this.generationConfig
|
||||
});
|
||||
this.client = new GoogleGenAI({ apiKey: this.config.apiKey });
|
||||
|
||||
// Validate connection and permissions
|
||||
await this.validateConnection();
|
||||
|
||||
} catch (error) {
|
||||
// Clean up on failure
|
||||
this.client = null;
|
||||
this.model = null;
|
||||
|
||||
|
||||
throw new AIProviderError(
|
||||
`Failed to initialize Gemini provider: ${(error as Error).message}`,
|
||||
AIErrorType.AUTHENTICATION,
|
||||
@@ -254,113 +123,43 @@ export class GeminiProvider extends BaseAIProvider {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a text completion using Gemini's generation API.
|
||||
*
|
||||
* This method:
|
||||
* 1. Converts messages to Gemini's format with system instructions
|
||||
* 2. Chooses between single-turn and multi-turn conversation modes
|
||||
* 3. Makes API call with comprehensive error handling
|
||||
* 4. Formats response to standard interface
|
||||
*
|
||||
* @protected
|
||||
* @param params - Validated completion parameters
|
||||
* @returns Promise resolving to formatted completion response
|
||||
* @throws {Error} If API request fails
|
||||
*/
|
||||
protected async doComplete(params: CompletionParams): Promise<CompletionResponse<string>> {
|
||||
if (!this.client || !this.model) {
|
||||
if (!this.client) {
|
||||
throw new AIProviderError('Gemini client not initialized', AIErrorType.INVALID_REQUEST);
|
||||
}
|
||||
|
||||
try {
|
||||
// Get the model for this request (might be different from default)
|
||||
const model = this.getModelForRequest(params);
|
||||
const { systemInstruction, contents } = this.convertMessages(params.messages);
|
||||
|
||||
// Choose appropriate generation method based on conversation length
|
||||
if (contents.length > 1) {
|
||||
// Multi-turn conversation - use chat session
|
||||
const chat = model.startChat({
|
||||
history: contents.slice(0, -1),
|
||||
systemInstruction,
|
||||
generationConfig: this.buildGenerationConfig(params)
|
||||
});
|
||||
|
||||
const lastMessage = contents[contents.length - 1];
|
||||
if (!lastMessage) {
|
||||
throw new AIProviderError('No valid messages provided', AIErrorType.INVALID_REQUEST);
|
||||
}
|
||||
|
||||
const result = await chat.sendMessage(lastMessage.parts);
|
||||
return this.formatCompletionResponse(result.response, params.model || this.defaultModel);
|
||||
|
||||
} else {
|
||||
// Single message - use generateContent
|
||||
const result = await model.generateContent({
|
||||
contents,
|
||||
systemInstruction,
|
||||
generationConfig: this.buildGenerationConfig(params)
|
||||
});
|
||||
|
||||
return this.formatCompletionResponse(result.response, params.model || this.defaultModel);
|
||||
}
|
||||
const model = params.model || this.defaultModel;
|
||||
|
||||
const response = await this.client.models.generateContent({
|
||||
model,
|
||||
contents,
|
||||
config: this.buildConfig(params, systemInstruction)
|
||||
});
|
||||
|
||||
return this.formatCompletionResponse(response, model);
|
||||
} catch (error) {
|
||||
throw this.handleGeminiError(error as Error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a streaming text completion using Gemini's streaming API.
|
||||
*
|
||||
* This method:
|
||||
* 1. Sets up appropriate streaming mode based on conversation type
|
||||
* 2. Handles real-time stream chunks from Gemini
|
||||
* 3. Tracks token usage throughout the stream
|
||||
* 4. Yields formatted chunks with proper completion tracking
|
||||
*
|
||||
* @protected
|
||||
* @param params - Validated completion parameters
|
||||
* @returns AsyncIterable yielding completion chunks
|
||||
* @throws {Error} If streaming request fails
|
||||
*/
|
||||
protected async *doStream<T = any>(params: CompletionParams<T>): AsyncIterable<CompletionChunk> {
|
||||
if (!this.client || !this.model) {
|
||||
if (!this.client) {
|
||||
throw new AIProviderError('Gemini client not initialized', AIErrorType.INVALID_REQUEST);
|
||||
}
|
||||
|
||||
try {
|
||||
// Get the model for this request
|
||||
const model = this.getModelForRequest(params);
|
||||
const { systemInstruction, contents } = this.convertMessages(params.messages);
|
||||
|
||||
// Set up streaming based on conversation type
|
||||
let stream;
|
||||
if (contents.length > 1) {
|
||||
// Multi-turn conversation
|
||||
const chat = model.startChat({
|
||||
history: contents.slice(0, -1),
|
||||
systemInstruction,
|
||||
generationConfig: this.buildGenerationConfig(params)
|
||||
});
|
||||
|
||||
const lastMessage = contents[contents.length - 1];
|
||||
if (!lastMessage) {
|
||||
throw new AIProviderError('No valid messages provided', AIErrorType.INVALID_REQUEST);
|
||||
}
|
||||
stream = await chat.sendMessageStream(lastMessage.parts);
|
||||
} else {
|
||||
// Single message
|
||||
stream = await model.generateContentStream({
|
||||
contents,
|
||||
systemInstruction,
|
||||
generationConfig: this.buildGenerationConfig(params)
|
||||
});
|
||||
}
|
||||
const model = params.model || this.defaultModel;
|
||||
|
||||
const stream = await this.client.models.generateContentStream({
|
||||
model,
|
||||
contents,
|
||||
config: this.buildConfig(params, systemInstruction)
|
||||
});
|
||||
|
||||
// Process stream chunks
|
||||
yield* this.processStreamChunks(stream);
|
||||
|
||||
} catch (error) {
|
||||
throw this.handleGeminiError(error as Error);
|
||||
}
|
||||
@@ -370,35 +169,28 @@ export class GeminiProvider extends BaseAIProvider {
|
||||
// PUBLIC INTERFACE METHODS
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* Returns comprehensive information about the Gemini provider.
|
||||
*
|
||||
* @returns Provider information including models, capabilities, and limits
|
||||
*/
|
||||
public getInfo(): ProviderInfo {
|
||||
return {
|
||||
name: 'Gemini',
|
||||
version: '1.0.0',
|
||||
version: '2.0.0',
|
||||
models: [
|
||||
'gemini-1.5-pro', // Latest flagship model
|
||||
'gemini-1.5-flash', // Fast and efficient variant
|
||||
'gemini-1.0-pro', // Previous generation
|
||||
'gemini-pro-vision', // Vision-specialized (legacy)
|
||||
'gemini-1.5-pro-vision', // Latest vision model
|
||||
'gemini-1.0-pro-latest', // Latest 1.0 variant
|
||||
'gemini-1.5-pro-latest' // Latest 1.5 variant
|
||||
'gemini-2.5-pro',
|
||||
'gemini-2.5-flash',
|
||||
'gemini-2.0-flash',
|
||||
'gemini-1.5-pro',
|
||||
'gemini-1.5-flash'
|
||||
],
|
||||
maxContextLength: 1048576, // ~1M tokens for Gemini 1.5
|
||||
maxContextLength: 1048576,
|
||||
supportsStreaming: true,
|
||||
capabilities: {
|
||||
vision: true, // Advanced multimodal capabilities
|
||||
functionCalling: true, // Tool use and function calling
|
||||
jsonMode: true, // Structured JSON output
|
||||
systemMessages: true, // System instructions support
|
||||
reasoning: true, // Strong reasoning capabilities
|
||||
codeGeneration: true, // Excellent at programming tasks
|
||||
multimodal: true, // Text, image, video understanding
|
||||
safetyFiltering: true // Built-in content safety
|
||||
vision: true,
|
||||
functionCalling: true,
|
||||
jsonMode: true,
|
||||
systemMessages: true,
|
||||
reasoning: true,
|
||||
codeGeneration: true,
|
||||
multimodal: true,
|
||||
safetyFiltering: true
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -407,26 +199,18 @@ export class GeminiProvider extends BaseAIProvider {
|
||||
// PRIVATE UTILITY METHODS
|
||||
// ========================================================================
|
||||
|
||||
/**
|
||||
* Validates the connection by making a minimal test request.
|
||||
*
|
||||
* @private
|
||||
* @throws {AIProviderError} If connection validation fails
|
||||
*/
|
||||
private async validateConnection(): Promise<void> {
|
||||
if (!this.model) {
|
||||
throw new Error('Model not initialized');
|
||||
if (!this.client) {
|
||||
throw new Error('Client not initialized');
|
||||
}
|
||||
|
||||
try {
|
||||
// Make minimal request to test connection and permissions
|
||||
await this.model.generateContent({
|
||||
await this.client.models.generateContent({
|
||||
model: this.defaultModel,
|
||||
contents: [{ role: 'user', parts: [{ text: 'Hi' }] }],
|
||||
generationConfig: { maxOutputTokens: 1 }
|
||||
config: { maxOutputTokens: 1 }
|
||||
});
|
||||
|
||||
} catch (error: any) {
|
||||
// Handle specific validation errors
|
||||
if (error.message?.includes('API key')) {
|
||||
throw new AIProviderError(
|
||||
'Invalid Google API key. Please verify your API key from https://aistudio.google.com/app/apikey',
|
||||
@@ -434,7 +218,7 @@ export class GeminiProvider extends BaseAIProvider {
|
||||
error.status
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
if (error.message?.includes('quota') || error.message?.includes('billing')) {
|
||||
throw new AIProviderError(
|
||||
'API quota exceeded or billing issue. Please check your Google Cloud billing and API quotas.',
|
||||
@@ -442,7 +226,7 @@ export class GeminiProvider extends BaseAIProvider {
|
||||
error.status
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
if (error.message?.includes('model')) {
|
||||
throw new AIProviderError(
|
||||
`Model '${this.defaultModel}' is not available. Please check the model name or your API access level.`,
|
||||
@@ -450,19 +234,11 @@ export class GeminiProvider extends BaseAIProvider {
|
||||
error.status
|
||||
);
|
||||
}
|
||||
|
||||
// For other errors during validation, log but don't fail initialization
|
||||
|
||||
console.warn('Gemini connection validation warning:', error.message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates model name format for Gemini models.
|
||||
*
|
||||
* @private
|
||||
* @param modelName - Model name to validate
|
||||
* @throws {AIProviderError} If model name format is invalid
|
||||
*/
|
||||
private validateModelName(modelName: string): void {
|
||||
if (!modelName || typeof modelName !== 'string') {
|
||||
throw new AIProviderError(
|
||||
@@ -471,65 +247,30 @@ export class GeminiProvider extends BaseAIProvider {
|
||||
);
|
||||
}
|
||||
|
||||
// Gemini model names follow specific patterns
|
||||
const validPatterns = [
|
||||
/^gemini-1\.5-pro(?:-latest|-vision)?$/, // e.g., gemini-1.5-pro, gemini-1.5-pro-latest
|
||||
/^gemini-1\.5-flash(?:-latest)?$/, // e.g., gemini-1.5-flash, gemini-1.5-flash-latest
|
||||
/^gemini-1\.0-pro(?:-latest|-vision)?$/, // e.g., gemini-1.0-pro, gemini-pro-vision
|
||||
/^gemini-pro(?:-vision)?$/, // Legacy names: gemini-pro, gemini-pro-vision
|
||||
/^models\/gemini-.+$/ // Full model path format
|
||||
/^gemini-2\.5-(?:pro|flash)(?:-lite)?(?:-\d{4}-\d{2}-\d{2})?$/,
|
||||
/^gemini-2\.0-(?:pro|flash)(?:-lite)?(?:-\d{4}-\d{2}-\d{2})?$/,
|
||||
/^gemini-1\.5-pro(?:-latest|-vision)?$/,
|
||||
/^gemini-1\.5-flash(?:-latest|-8b)?$/,
|
||||
/^gemini-1\.0-pro(?:-latest|-vision)?$/,
|
||||
/^gemini-pro(?:-vision)?$/,
|
||||
/^models\/gemini-.+$/
|
||||
];
|
||||
|
||||
const isValid = validPatterns.some(pattern => pattern.test(modelName));
|
||||
|
||||
|
||||
if (!isValid) {
|
||||
console.warn(`Model name '${modelName}' doesn't match expected Gemini naming patterns. This may cause API errors.`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the appropriate model instance for a request.
|
||||
*
|
||||
* @private
|
||||
* @param params - Completion parameters
|
||||
* @returns GenerativeModel instance
|
||||
*/
|
||||
private getModelForRequest(params: CompletionParams): GenerativeModel {
|
||||
if (!this.client) {
|
||||
throw new Error('Client not initialized');
|
||||
}
|
||||
|
||||
// Use default model if no specific model requested
|
||||
if (!params.model || params.model === this.defaultModel) {
|
||||
return this.model!;
|
||||
}
|
||||
|
||||
// Create new model instance for different model
|
||||
return this.client.getGenerativeModel({
|
||||
model: params.model,
|
||||
safetySettings: this.safetySettings,
|
||||
generationConfig: this.buildGenerationConfig(params)
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts generic messages to Gemini's conversation format.
|
||||
*
|
||||
* Gemini handles system messages as separate system instructions
|
||||
* rather than part of the conversation flow.
|
||||
*
|
||||
* @private
|
||||
* @param messages - Input messages array
|
||||
* @returns Processed messages with system instruction separated
|
||||
*/
|
||||
private convertMessages(messages: AIMessage[]): ProcessedMessages {
|
||||
let systemInstruction: string | undefined;
|
||||
const contents: Content[] = [];
|
||||
|
||||
for (const message of messages) {
|
||||
if (message.role === 'system') {
|
||||
// Combine multiple system messages
|
||||
systemInstruction = systemInstruction
|
||||
systemInstruction = systemInstruction
|
||||
? `${systemInstruction}\n\n${message.content}`
|
||||
: message.content;
|
||||
} else {
|
||||
@@ -543,59 +284,61 @@ export class GeminiProvider extends BaseAIProvider {
|
||||
return { systemInstruction, contents };
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds generation configuration from completion parameters.
|
||||
*
|
||||
* @private
|
||||
* @param params - Completion parameters
|
||||
* @returns Gemini generation configuration
|
||||
*/
|
||||
private buildGenerationConfig(params: CompletionParams): GenerationConfig {
|
||||
return {
|
||||
temperature: params.temperature ?? this.generationConfig?.temperature ?? 0.7,
|
||||
topP: params.topP ?? this.generationConfig?.topP,
|
||||
topK: this.generationConfig?.topK,
|
||||
maxOutputTokens: params.maxTokens ?? this.generationConfig?.maxOutputTokens ?? 1000,
|
||||
stopSequences: params.stopSequences ?? this.generationConfig?.stopSequences
|
||||
private buildConfig(
|
||||
params: CompletionParams,
|
||||
systemInstruction?: string
|
||||
): GenerateContentConfig {
|
||||
const defaults = this.defaultGenerationConfig;
|
||||
|
||||
const config: GenerateContentConfig = {
|
||||
temperature: params.temperature ?? defaults?.temperature ?? 0.7,
|
||||
maxOutputTokens: params.maxTokens ?? defaults?.maxOutputTokens ?? 1000
|
||||
};
|
||||
|
||||
const topP = params.topP ?? defaults?.topP;
|
||||
if (topP !== undefined) config.topP = topP;
|
||||
|
||||
if (defaults?.topK !== undefined) config.topK = defaults.topK;
|
||||
|
||||
const stopSequences = params.stopSequences ?? defaults?.stopSequences;
|
||||
if (stopSequences) config.stopSequences = stopSequences;
|
||||
|
||||
if (systemInstruction) config.systemInstruction = systemInstruction;
|
||||
if (this.safetySettings) config.safetySettings = this.safetySettings;
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes streaming response chunks from Gemini API.
|
||||
*
|
||||
* @private
|
||||
* @param stream - Gemini streaming response
|
||||
* @returns AsyncIterable of formatted completion chunks
|
||||
*/
|
||||
private async *processStreamChunks(stream: any): AsyncIterable<CompletionChunk> {
|
||||
let fullText = '';
|
||||
const requestId = `gemini-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
||||
private async *processStreamChunks(
|
||||
stream: AsyncGenerator<GenerateContentResponse>
|
||||
): AsyncIterable<CompletionChunk> {
|
||||
const requestId = `gemini-${Date.now()}-${Math.random().toString(36).slice(2, 11)}`;
|
||||
let lastUsage: GenerateContentResponse['usageMetadata'] | undefined;
|
||||
|
||||
try {
|
||||
// Process streaming chunks
|
||||
for await (const chunk of stream.stream) {
|
||||
const chunkText = chunk.text();
|
||||
fullText += chunkText;
|
||||
|
||||
yield {
|
||||
content: chunkText,
|
||||
isComplete: false,
|
||||
id: requestId
|
||||
};
|
||||
for await (const chunk of stream) {
|
||||
if (chunk.usageMetadata) {
|
||||
lastUsage = chunk.usageMetadata;
|
||||
}
|
||||
|
||||
const text = chunk.text;
|
||||
if (text) {
|
||||
yield {
|
||||
content: text,
|
||||
isComplete: false,
|
||||
id: requestId
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Final chunk with usage information
|
||||
const finalResponse = await stream.response;
|
||||
const usageMetadata = finalResponse.usageMetadata;
|
||||
|
||||
yield {
|
||||
content: '',
|
||||
isComplete: true,
|
||||
id: requestId,
|
||||
usage: {
|
||||
promptTokens: usageMetadata?.promptTokenCount || 0,
|
||||
completionTokens: usageMetadata?.candidatesTokenCount || 0,
|
||||
totalTokens: usageMetadata?.totalTokenCount || 0
|
||||
promptTokens: lastUsage?.promptTokenCount || 0,
|
||||
completionTokens: lastUsage?.candidatesTokenCount || 0,
|
||||
totalTokens: lastUsage?.totalTokenCount || 0
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
@@ -608,38 +351,11 @@ export class GeminiProvider extends BaseAIProvider {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats Gemini's response to our standard interface.
|
||||
*
|
||||
* @private
|
||||
* @param response - Raw Gemini API response
|
||||
* @param model - Model used for generation
|
||||
* @returns Formatted completion response
|
||||
* @throws {AIProviderError} If response format is unexpected
|
||||
*/
|
||||
private formatCompletionResponse(response: any, model: string): CompletionResponse<string> {
|
||||
// Handle multiple text parts in the response
|
||||
const candidate = response.candidates?.[0];
|
||||
if (!candidate) {
|
||||
throw new AIProviderError(
|
||||
'No candidates found in Gemini response',
|
||||
AIErrorType.UNKNOWN
|
||||
);
|
||||
}
|
||||
|
||||
const content = candidate.content;
|
||||
if (!content || !content.parts) {
|
||||
throw new AIProviderError(
|
||||
'No content found in Gemini response',
|
||||
AIErrorType.UNKNOWN
|
||||
);
|
||||
}
|
||||
|
||||
// Combine all text parts
|
||||
const text = content.parts
|
||||
.filter((part: any) => part.text)
|
||||
.map((part: any) => part.text)
|
||||
.join('');
|
||||
private formatCompletionResponse(
|
||||
response: GenerateContentResponse,
|
||||
model: string
|
||||
): CompletionResponse<string> {
|
||||
const text = response.text;
|
||||
|
||||
if (!text) {
|
||||
throw new AIProviderError(
|
||||
@@ -648,9 +364,11 @@ export class GeminiProvider extends BaseAIProvider {
|
||||
);
|
||||
}
|
||||
|
||||
const candidate = response.candidates?.[0];
|
||||
|
||||
return {
|
||||
content: text,
|
||||
model: model,
|
||||
model,
|
||||
usage: {
|
||||
promptTokens: response.usageMetadata?.promptTokenCount || 0,
|
||||
completionTokens: response.usageMetadata?.candidatesTokenCount || 0,
|
||||
@@ -658,23 +376,13 @@ export class GeminiProvider extends BaseAIProvider {
|
||||
},
|
||||
id: `gemini-${Date.now()}`,
|
||||
metadata: {
|
||||
finishReason: candidate.finishReason,
|
||||
safetyRatings: candidate.safetyRatings,
|
||||
citationMetadata: candidate.citationMetadata
|
||||
finishReason: candidate?.finishReason,
|
||||
safetyRatings: candidate?.safetyRatings,
|
||||
citationMetadata: candidate?.citationMetadata
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles and transforms Gemini-specific errors.
|
||||
*
|
||||
* This method maps Gemini's error responses to our standardized
|
||||
* error format, providing helpful context and actionable suggestions.
|
||||
*
|
||||
* @private
|
||||
* @param error - Original error from Gemini API
|
||||
* @returns Normalized AIProviderError
|
||||
*/
|
||||
private handleGeminiError(error: any): AIProviderError {
|
||||
if (error instanceof AIProviderError) {
|
||||
return error;
|
||||
@@ -683,7 +391,6 @@ export class GeminiProvider extends BaseAIProvider {
|
||||
const message = error.message || 'Unknown Gemini API error';
|
||||
const status = error.status || error.statusCode;
|
||||
|
||||
// Map Gemini-specific error patterns
|
||||
if (message.includes('API key')) {
|
||||
return new AIProviderError(
|
||||
'Invalid Google API key. Please verify your API key from https://aistudio.google.com/app/apikey',
|
||||
@@ -747,7 +454,6 @@ export class GeminiProvider extends BaseAIProvider {
|
||||
);
|
||||
}
|
||||
|
||||
// Handle HTTP status codes
|
||||
switch (status) {
|
||||
case 400:
|
||||
return new AIProviderError(
|
||||
@@ -801,4 +507,4 @@ export class GeminiProvider extends BaseAIProvider {
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user