mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-29 14:43:56 +08:00
gemini独立代理模式
在代理模式下手动组成request,无论 Steam 是否为 True,返回的均是非steam值。 不设置代理时自动切换为google的官方api,走 stream模式。 全局代理容易让系统中其他模块也使用代理,造成无法访问的问题。 测试最新的 gemini flash 2.0可以使用。
This commit is contained in:
@@ -85,22 +85,48 @@ class LLMProvider(LLMProviderBase):
|
|||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
}
|
}
|
||||||
|
|
||||||
# 发送POST请求,经测试手动 request 无法使用 stream 模式,但是文本消息其实也很快,stream 与否也无所谓
|
# 发送POST请求,经测试手动 request 无法使用 stream 模式
|
||||||
if self.proxies:
|
if self.proxies:
|
||||||
|
logger.bind(tag=TAG).info(f"Gemini response mode ")
|
||||||
response = requests.post(url, headers=headers, json=request_body, stream=False, proxies=self.proxies)
|
response = requests.post(url, headers=headers, json=request_body, stream=False, proxies=self.proxies)
|
||||||
|
try:
|
||||||
|
data = response.json() # 直接解析JSON
|
||||||
|
if 'candidates' in data and data['candidates']:
|
||||||
|
yield data['candidates'][0]['content']['parts'][0]['text']
|
||||||
|
else:
|
||||||
|
yield "未找到候选回复。"
|
||||||
|
except json.JSONDecodeError as e:
|
||||||
|
yield f"JSON解码错误:{e}"
|
||||||
|
except Exception as e:
|
||||||
|
yield f"发生错误:{e}"
|
||||||
else:
|
else:
|
||||||
response = requests.post(url, headers=headers, json=request_body, stream=False)
|
logger.bind(tag=TAG).info(f"Gemini stream mode ")
|
||||||
response.raise_for_status()
|
chat = self.model.start_chat(history=chat_history)
|
||||||
try:
|
|
||||||
data = response.json() # 直接解析JSON
|
# 发送消息并获取流式响应
|
||||||
if 'candidates' in data and data['candidates']:
|
response = chat.send_message(
|
||||||
yield data['candidates'][0]['content']['parts'][0]['text']
|
current_msg,
|
||||||
else:
|
stream=True,
|
||||||
yield "未找到候选回复。"
|
generation_config=self.generation_config
|
||||||
except json.JSONDecodeError as e:
|
)
|
||||||
yield f"JSON解码错误:{e}"
|
# 处理流式响应
|
||||||
except Exception as e:
|
for chunk in response:
|
||||||
yield f"发生错误:{e}"
|
if hasattr(chunk, 'text') and chunk.text:
|
||||||
|
yield chunk.text
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
error_msg = str(e)
|
||||||
|
logger.bind(tag=TAG).error(f"Gemini响应生成错误: {error_msg}")
|
||||||
|
|
||||||
|
# 针对不同错误返回友好提示
|
||||||
|
if "Rate limit" in error_msg:
|
||||||
|
yield "【Gemini服务请求太频繁,请稍后再试】"
|
||||||
|
elif "Invalid API key" in error_msg:
|
||||||
|
yield "【Gemini API key无效】"
|
||||||
|
else:
|
||||||
|
yield f"【Gemini服务响应异常: {error_msg}】"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
|
|||||||
Reference in New Issue
Block a user