mirror of
https://github.com/handsomezhuzhu/QQuiz.git
synced 2026-02-20 20:10:14 +00:00
refactor: 重构 Gemini 为 REST API 调用以支持自定义代理服务
核心改进: - 🔄 完全重写 Gemini 实现方式 - 移除 google-genai SDK 依赖 - 改用 httpx 直接调用 Gemini REST API - 完全控制请求 URL 和参数 - 🌐 完美支持自定义 Base URL - 支持代理服务(如 https://load.zhuzihan.com/proxy/gemini-self) - 支持 API Key 轮训中转服务 - 兼容标准 Gemini API 格式(v1beta/models/{model}:generateContent) - 📄 保留完整的 PDF 处理能力 - 使用 inline_data 格式(base64 编码) - 原生 PDF 理解(最多 1000 页) - 完整保留图片、表格、公式等视觉元素 - ⚡ 优化性能和稳定性 - 统一使用 httpx.AsyncClient - 120 秒超时配置 - 连接池管理(max_keepalive_connections=5) - 完善的错误处理和日志输出 技术细节: - 移除依赖:google-genai==1.0.0 - 请求格式:标准 Gemini REST API - 响应解析:直接从 JSON 提取 candidates[0].content.parts[0].text - PDF 上传:inline_data with base64 encoding 影响范围: - 文本内容解析 ✅ - PDF 文档解析 ✅ - 简答题评分 ✅ - AI 参考答案生成 ✅ 🎉 现在 Gemini 可完美配合用户自建的代理/轮训服务使用! 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -113,16 +113,20 @@ async def generate_ai_reference_answer(
|
||||
|
||||
# Generate answer using LLM
|
||||
if llm_service.provider == "gemini":
|
||||
import asyncio
|
||||
# Use REST API for Gemini
|
||||
url = f"{llm_service.gemini_base_url}/v1beta/models/{llm_service.model}:generateContent"
|
||||
headers = {"Content-Type": "application/json"}
|
||||
params = {"key": llm_service.gemini_api_key}
|
||||
payload = {
|
||||
"contents": [{
|
||||
"parts": [{"text": prompt}]
|
||||
}]
|
||||
}
|
||||
|
||||
def _generate():
|
||||
return llm_service.client.models.generate_content(
|
||||
model=llm_service.model,
|
||||
contents=prompt
|
||||
)
|
||||
|
||||
response = await asyncio.to_thread(_generate)
|
||||
return response.text.strip()
|
||||
response = await llm_service.client.post(url, headers=headers, params=params, json=payload)
|
||||
response.raise_for_status()
|
||||
response_data = response.json()
|
||||
return response_data["candidates"][0]["content"]["parts"][0]["text"].strip()
|
||||
elif llm_service.provider == "anthropic":
|
||||
response = await llm_service.client.messages.create(
|
||||
model=llm_service.model,
|
||||
|
||||
Reference in New Issue
Block a user