mirror of
https://github.com/snailyp/gemini-balance.git
synced 2026-05-21 00:01:01 +08:00
主要更新: 添加图像模型支持 新增MODEL_IMAGE配置项 在模型列表中添加gemini-2.0-flash-exp-image模型 修改ModelService以支持图像模型 增强图像处理能力 添加PicGoUploader类用于图像上传 实现图像响应处理逻辑(_extract_image_data) 支持base64图像数据的解码与上传 优化请求与响应处理 为图像模型添加特殊处理逻辑 修改API客户端以支持图像模型 更新GeminiRequest默认值 安全性调整 将TOOLS_CODE_EXECUTION_ENABLED默认设置为false
56 lines
2.3 KiB
Python
56 lines
2.3 KiB
Python
# app/services/chat/api_client.py
|
|
|
|
from typing import Dict, Any, AsyncGenerator
|
|
import httpx
|
|
from abc import ABC, abstractmethod
|
|
|
|
|
|
class ApiClient(ABC):
|
|
"""API客户端基类"""
|
|
|
|
@abstractmethod
|
|
async def generate_content(self, payload: Dict[str, Any], model: str, api_key: str) -> Dict[str, Any]:
|
|
pass
|
|
|
|
@abstractmethod
|
|
async def stream_generate_content(self, payload: Dict[str, Any], model: str, api_key: str) -> AsyncGenerator[str, None]:
|
|
pass
|
|
|
|
|
|
class GeminiApiClient(ApiClient):
|
|
"""Gemini API客户端"""
|
|
|
|
def __init__(self, base_url: str, timeout: int = 300):
|
|
self.base_url = base_url
|
|
self.timeout = timeout
|
|
|
|
async def generate_content(self, payload: Dict[str, Any], model: str, api_key: str) -> Dict[str, Any]:
|
|
timeout = httpx.Timeout(self.timeout, read=self.timeout)
|
|
if model.endswith("-search"):
|
|
model = model[:-7]
|
|
if model.endswith("-image"):
|
|
model = model[:-6]
|
|
async with httpx.AsyncClient(timeout=timeout) as client:
|
|
url = f"{self.base_url}/models/{model}:generateContent?key={api_key}"
|
|
response = await client.post(url, json=payload)
|
|
if response.status_code != 200:
|
|
error_content = response.text
|
|
raise Exception(f"API call failed with status code {response.status_code}, {error_content}")
|
|
return response.json()
|
|
|
|
async def stream_generate_content(self, payload: Dict[str, Any], model: str, api_key: str) -> AsyncGenerator[str, None]:
|
|
timeout = httpx.Timeout(self.timeout, read=self.timeout)
|
|
if model.endswith("-search"):
|
|
model = model[:-7]
|
|
if model.endswith("-image"):
|
|
model = model[:-6]
|
|
async with httpx.AsyncClient(timeout=timeout) as client:
|
|
url = f"{self.base_url}/models/{model}:streamGenerateContent?alt=sse&key={api_key}"
|
|
async with client.stream(method="POST", url=url, json=payload) as response:
|
|
if response.status_code != 200:
|
|
error_content = await response.aread()
|
|
error_msg = error_content.decode("utf-8")
|
|
raise Exception(f"API call failed with status code {response.status_code}, {error_msg}")
|
|
async for line in response.aiter_lines():
|
|
yield line
|