feat: add custom Gemini endpoint support

- Add GEMINI_BASE_URL configuration option in .env.example
- Implement custom endpoint support in GeminiModelProvider using HttpOptions
- Update registry to pass base_url parameter to Gemini provider
- Maintain backward compatibility - uses default Google endpoint when not configured
This commit is contained in:
dragonfsky
2025-08-22 18:01:00 +08:00
parent 4c87afd479
commit 462bce002e
3 changed files with 25 additions and 2 deletions

View File

@@ -12,6 +12,7 @@
# Option 1: Use native APIs (recommended for direct access)
# Get your Gemini API key from: https://makersuite.google.com/app/apikey
GEMINI_API_KEY=your_gemini_api_key_here
# GEMINI_BASE_URL= # Optional: Custom Gemini endpoint (defaults to Google's API)
# Get your OpenAI API key from: https://platform.openai.com/api-keys
OPENAI_API_KEY=your_openai_api_key_here

View File

@@ -117,15 +117,25 @@ class GeminiModelProvider(ModelProvider):
}
def __init__(self, api_key: str, **kwargs):
"""Initialize Gemini provider with API key."""
"""Initialize Gemini provider with API key and optional base URL."""
super().__init__(api_key, **kwargs)
self._client = None
self._token_counters = {} # Cache for token counting
self._base_url = kwargs.get('base_url', None) # Optional custom endpoint
@property
def client(self):
"""Lazy initialization of Gemini client."""
if self._client is None:
# Check if custom base URL is provided
if self._base_url:
# Use HttpOptions to set custom endpoint
from google.genai.types import HttpOptions
http_options = HttpOptions(baseUrl=self._base_url)
logger.debug(f"Initializing Gemini client with custom endpoint: {self._base_url}")
self._client = genai.Client(api_key=self.api_key, http_options=http_options)
else:
# Use default Google endpoint
self._client = genai.Client(api_key=self.api_key)
return self._client

View File

@@ -93,6 +93,18 @@ class ModelProviderRegistry:
api_key = api_key or ""
# Initialize custom provider with both API key and base URL
provider = provider_class(api_key=api_key, base_url=custom_url)
elif provider_type == ProviderType.GOOGLE:
# For Gemini, check if custom base URL is configured
if not api_key:
return None
gemini_base_url = os.getenv("GEMINI_BASE_URL")
if gemini_base_url:
# Initialize with custom endpoint
provider = provider_class(api_key=api_key, base_url=gemini_base_url)
logging.info(f"Initialized Gemini provider with custom endpoint: {gemini_base_url}")
else:
# Use default Google endpoint
provider = provider_class(api_key=api_key)
else:
if not api_key:
return None