mirror of
https://github.com/zhouxiaoka/autoclip.git
synced 2026-09-02 22:15:18 +08:00
refactor: migrate Gemini provider to the new google-genai SDK
google-generativeai is deprecated and no longer maintained. Switch GeminiProvider to the unified google-genai SDK: - genai.configure() + GenerativeModel(...).generate_content(...) → genai.Client(api_key=...).models.generate_content(model=..., contents=...) - max-tokens hint now mapped onto types.GenerateContentConfig - requirements.txt: google-generativeai>=0.3.0 → google-genai>=1.0.0 - install_llm_dependencies.py: same package rename Verified the new SDK exposes Client + models.generate_content + GenerateContentConfig, and the module parses clean. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -331,25 +331,37 @@ class GeminiProvider(LLMProvider):
|
||||
def __init__(self, api_key: str, model_name: str = "gemini-2.5-flash", **kwargs):
|
||||
super().__init__(api_key, model_name, **kwargs)
|
||||
try:
|
||||
import google.generativeai as genai
|
||||
genai.configure(api_key=api_key)
|
||||
self.model = genai.GenerativeModel(model_name)
|
||||
# New unified Google GenAI SDK (replaces the deprecated
|
||||
# google-generativeai package).
|
||||
from google import genai
|
||||
self.client = genai.Client(api_key=api_key)
|
||||
except ImportError:
|
||||
raise ImportError("请安装google-generativeai: pip install google-generativeai")
|
||||
|
||||
raise ImportError("请安装google-genai: pip install google-genai")
|
||||
|
||||
def call(self, prompt: str, input_data: Any = None, **kwargs) -> LLMResponse:
|
||||
"""调用Gemini API"""
|
||||
try:
|
||||
full_input = self._build_full_input(prompt, input_data)
|
||||
|
||||
response = self.model.generate_content(full_input, **kwargs)
|
||||
|
||||
|
||||
# Map a max-tokens hint onto the new SDK's config object if present.
|
||||
config = None
|
||||
max_tokens = kwargs.get("max_tokens") or kwargs.get("max_output_tokens")
|
||||
if max_tokens:
|
||||
from google.genai import types
|
||||
config = types.GenerateContentConfig(max_output_tokens=max_tokens)
|
||||
|
||||
response = self.client.models.generate_content(
|
||||
model=self.model_name,
|
||||
contents=full_input,
|
||||
config=config,
|
||||
)
|
||||
|
||||
return LLMResponse(
|
||||
content=response.text,
|
||||
model=self.model_name,
|
||||
finish_reason=getattr(response, 'finish_reason', None)
|
||||
)
|
||||
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Gemini调用失败: {str(e)}")
|
||||
raise
|
||||
|
||||
@@ -23,7 +23,7 @@ def main():
|
||||
# 需要安装的包
|
||||
packages = [
|
||||
"openai>=1.0.0", # OpenAI
|
||||
"google-generativeai>=0.3.0", # Google Gemini
|
||||
"google-genai>=1.0.0", # Google Gemini (统一版 GenAI SDK)
|
||||
"requests>=2.25.0", # 硅基流动 (HTTP请求)
|
||||
"dashscope>=1.10.0", # 阿里通义千问 (如果还没有安装)
|
||||
]
|
||||
|
||||
@@ -29,5 +29,5 @@ pytz
|
||||
# desktop build shipped without them and any provider call failed. Bundle them
|
||||
# so the desktop app works out of the box.
|
||||
openai>=1.0.0
|
||||
google-generativeai>=0.3.0
|
||||
google-genai>=1.0.0
|
||||
dashscope>=1.10.0
|
||||
|
||||
Reference in New Issue
Block a user