From c8a9fe87f6ca2f315b7fbbff796834691aa5ef5b Mon Sep 17 00:00:00 2001 From: osi Date: Mon, 12 May 2025 17:31:08 +0800 Subject: [PATCH 1/2] :bug: fixed gemini proxy --- llm_adapters.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/llm_adapters.py b/llm_adapters.py index d517625..d53fba3 100644 --- a/llm_adapters.py +++ b/llm_adapters.py @@ -3,7 +3,8 @@ import logging from typing import Optional from langchain_openai import ChatOpenAI, AzureChatOpenAI -import google.generativeai as genai +from google import genai +from google.genai import types from azure.ai.inference import ChatCompletionsClient from azure.core.credentials import AzureKeyCredential from azure.ai.inference.models import SystemMessage, UserMessage @@ -97,25 +98,25 @@ class GeminiAdapter(BaseLLMAdapter): """ 适配 Google Gemini (Google Generative AI) 接口 """ - def __init__(self, api_key: str, model_name: str, max_tokens: int, temperature: float = 0.7, timeout: Optional[int] = 600): + def __init__(self, api_key: str, base_url: str, model_name: str, max_tokens: int, temperature: float = 0.7, timeout: Optional[int] = 600): self.api_key = api_key self.model_name = model_name self.max_tokens = max_tokens self.temperature = temperature self.timeout = timeout - self._client = genai.Client(api_key=self.api_key) + self._client = genai.Client(api_key=self.api_key,http_options=types.HttpOptions(base_url=base_url)) def invoke(self, prompt: str) -> str: try: response = self._client.models.generate_content( model = self.model_name, contents = prompt, - config = genai.types.GenerateContentConfig( + config = types.GenerateContentConfig( max_output_tokens=self.max_tokens, temperature=self.temperature, ), - timeout=self.timeout # 添加超时参数 + # timeout=self.timeout # 添加超时参数 ) if response and response.text: return response.text @@ -364,8 +365,7 @@ def create_llm_adapter( elif fmt == "ml studio": return MLStudioAdapter(api_key, base_url, model_name, max_tokens, temperature, timeout) elif fmt == "gemini": - # base_url 对 Gemini 暂无用处,可忽略 - return GeminiAdapter(api_key, model_name, max_tokens, temperature, timeout) + return GeminiAdapter(api_key, base_url, model_name, max_tokens, temperature, timeout) elif fmt == "阿里云百炼": return OpenAIAdapter(api_key, base_url, model_name, max_tokens, temperature, timeout) elif fmt == "火山引擎": From f15b00d15991de4530c454018282981151b30335 Mon Sep 17 00:00:00 2001 From: osi Date: Tue, 13 May 2025 16:18:49 +0800 Subject: [PATCH 2/2] :art: gemini timeout params --- llm_adapters.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/llm_adapters.py b/llm_adapters.py index d53fba3..141bc53 100644 --- a/llm_adapters.py +++ b/llm_adapters.py @@ -103,9 +103,10 @@ class GeminiAdapter(BaseLLMAdapter): self.model_name = model_name self.max_tokens = max_tokens self.temperature = temperature - self.timeout = timeout + # gemini超时时间是毫秒 + self.timeout = timeout * 1000 - self._client = genai.Client(api_key=self.api_key,http_options=types.HttpOptions(base_url=base_url)) + self._client = genai.Client(api_key=self.api_key,http_options=types.HttpOptions(base_url=base_url,timeout=self.timeout)) def invoke(self, prompt: str) -> str: try: @@ -116,7 +117,6 @@ class GeminiAdapter(BaseLLMAdapter): max_output_tokens=self.max_tokens, temperature=self.temperature, ), - # timeout=self.timeout # 添加超时参数 ) if response and response.text: return response.text