mirror of
https://github.com/snailyp/gemini-balance.git
synced 2026-08-25 02:00:36 +08:00
feat: 新增请求超时配置及优化模型列表接口api_key获取方式
1. 新增功能:
- 在`.env.example`中添加`TIME_OUT=300`配置项(包含中文注释)
- 在`Settings`类中增加`TIME_OUT`字段(读取自`DEFAULT_TIMEOUT`)
2. 优化内容:
- 生成配置:
* 为`GenerationConfig`设置默认温度/TOP_P/TOP_K值
* 移除`maxOutputTokens`默认值,改为可选传递
- OpenAI请求:
* 移除`max_tokens`默认值
* 只有当`max_tokens`有值时才添加到请求payload
- 日志优化:
* 注释掉`stream_optimizer.py`中部分调试日志
3. 模型列表接口api_key获取方式
This commit is contained in:
@@ -89,6 +89,11 @@ def _get_safety_settings(model: str) -> List[Dict[str, str]]:
|
||||
def _build_payload(model: str, request: GeminiRequest) -> Dict[str, Any]:
|
||||
"""构建请求payload"""
|
||||
request_dict = request.model_dump()
|
||||
if request.generationConfig:
|
||||
if request.generationConfig.maxOutputTokens is None:
|
||||
# 如果未指定最大输出长度,则不传递该字段,解决截断的问题
|
||||
request_dict["generationConfig"].pop("maxOutputTokens")
|
||||
|
||||
payload = {
|
||||
"contents": request_dict.get("contents", []),
|
||||
"tools": _build_tools(model, request_dict),
|
||||
|
||||
@@ -115,7 +115,6 @@ def _build_payload(
|
||||
"contents": messages,
|
||||
"generationConfig": {
|
||||
"temperature": request.temperature,
|
||||
"maxOutputTokens": request.max_tokens,
|
||||
"stopSequences": request.stop,
|
||||
"topP": request.top_p,
|
||||
"topK": request.top_k,
|
||||
@@ -123,6 +122,8 @@ def _build_payload(
|
||||
"tools": _build_tools(request, messages),
|
||||
"safetySettings": _get_safety_settings(request.model),
|
||||
}
|
||||
if request.max_tokens is not None:
|
||||
payload["generationConfig"]["maxOutputTokens"] = request.max_tokens
|
||||
if request.model.endswith("-image") or request.model.endswith("-image-generation"):
|
||||
payload["generationConfig"]["responseModalities"] = ["Text", "Image"]
|
||||
|
||||
|
||||
@@ -81,6 +81,13 @@ class KeyManager:
|
||||
|
||||
return {"valid_keys": valid_keys, "invalid_keys": invalid_keys}
|
||||
|
||||
async def get_first_valid_key(self) -> str:
|
||||
"""获取第一个有效的API key"""
|
||||
async with self.failure_count_lock:
|
||||
for key in self.key_failure_counts:
|
||||
if self.key_failure_counts[key] < self.MAX_FAILURES:
|
||||
return key
|
||||
return self.api_keys[0]
|
||||
|
||||
_singleton_instance = None
|
||||
_singleton_lock = asyncio.Lock()
|
||||
|
||||
Reference in New Issue
Block a user