mirror of
https://github.com/snailyp/gemini-balance.git
synced 2026-09-08 17:36:37 +08:00
Merge remote-tracking branch 'origin/main' into feat/AutoRoute
This commit is contained in:
@@ -45,7 +45,7 @@ class GenerationConfig(BaseModel):
|
||||
|
||||
class SystemInstruction(BaseModel):
|
||||
role: str = "system"
|
||||
parts: List[Dict[str, Any]] | Dict[str, Any]
|
||||
parts: Union[List[Dict[str, Any]], Dict[str, Any]]
|
||||
|
||||
|
||||
class GeminiContent(BaseModel):
|
||||
|
||||
@@ -1,23 +1,20 @@
|
||||
from typing import Union
|
||||
|
||||
|
||||
class ImageMetadata:
|
||||
def __init__(self, width: int, height: int, filename: str, size: int, url: str, delete_url: str | None = None):
|
||||
def __init__(self, width: int, height: int, filename: str, size: int, url: str, delete_url: Union[str, None] = None):
|
||||
self.width = width
|
||||
self.height = height
|
||||
self.filename = filename
|
||||
self.size = size
|
||||
self.url = url
|
||||
self.delete_url = delete_url
|
||||
|
||||
|
||||
class UploadResponse:
|
||||
def __init__(self, success: bool, code: str, message: str, data: ImageMetadata):
|
||||
self.success = success
|
||||
self.code = code
|
||||
self.message = message
|
||||
self.data = data
|
||||
|
||||
|
||||
class ImageUploader:
|
||||
def upload(self, file: bytes, filename: str) -> UploadResponse:
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import asyncio
|
||||
from itertools import cycle
|
||||
from typing import Dict
|
||||
from typing import Dict, Union
|
||||
|
||||
from app.config.config import settings
|
||||
from app.log.logger import get_key_manager_logger
|
||||
@@ -178,19 +178,20 @@ class KeyManager:
|
||||
if self.api_keys:
|
||||
return self.api_keys[0]
|
||||
if not self.api_keys:
|
||||
logger.warning("API key list is empty, cannot get first valid key.")
|
||||
logger.warning(
|
||||
"API key list is empty, cannot get first valid key.")
|
||||
return ""
|
||||
return self.api_keys[0]
|
||||
|
||||
|
||||
_singleton_instance = None
|
||||
_singleton_lock = asyncio.Lock()
|
||||
_preserved_failure_counts: Dict[str, int] | None = None
|
||||
_preserved_vertex_failure_counts: Dict[str, int] | None = None
|
||||
_preserved_old_api_keys_for_reset: list | None = None
|
||||
_preserved_vertex_old_api_keys_for_reset: list | None = None
|
||||
_preserved_next_key_in_cycle: str | None = None
|
||||
_preserved_vertex_next_key_in_cycle: str | None = None
|
||||
_preserved_failure_counts: Union[Dict[str, int], None] = None
|
||||
_preserved_vertex_failure_counts: Union[Dict[str, int], None] = None
|
||||
_preserved_old_api_keys_for_reset: Union[list, None] = None
|
||||
_preserved_vertex_old_api_keys_for_reset: Union[list, None] = None
|
||||
_preserved_next_key_in_cycle: Union[str, None] = None
|
||||
_preserved_vertex_next_key_in_cycle: Union[str, None] = None
|
||||
|
||||
|
||||
async def get_key_manager_instance(
|
||||
@@ -252,7 +253,8 @@ async def get_key_manager_instance(
|
||||
_singleton_instance.vertex_key_failure_counts = (
|
||||
current_vertex_failure_counts
|
||||
)
|
||||
logger.info("Inherited failure counts for applicable Vertex keys.")
|
||||
logger.info(
|
||||
"Inherited failure counts for applicable Vertex keys.")
|
||||
_preserved_vertex_failure_counts = None
|
||||
|
||||
# 2. 调整 key_cycle 的起始点
|
||||
@@ -429,7 +431,8 @@ async def reset_key_manager_instance():
|
||||
)
|
||||
_preserved_next_key_in_cycle = None
|
||||
except Exception as e:
|
||||
logger.error(f"Error preserving next key hint during reset: {e}")
|
||||
logger.error(
|
||||
f"Error preserving next key hint during reset: {e}")
|
||||
_preserved_next_key_in_cycle = None
|
||||
|
||||
# 4. 保存 vertex_key_cycle 的下一个 key 提示
|
||||
@@ -446,10 +449,10 @@ async def reset_key_manager_instance():
|
||||
)
|
||||
_preserved_vertex_next_key_in_cycle = None
|
||||
except Exception as e:
|
||||
logger.error(f"Error preserving next key hint during reset: {e}")
|
||||
logger.error(
|
||||
f"Error preserving next key hint during reset: {e}")
|
||||
_preserved_vertex_next_key_in_cycle = None
|
||||
|
||||
|
||||
_singleton_instance = None
|
||||
logger.info(
|
||||
"KeyManager instance has been reset. State (failure counts, old keys, next key hint) preserved for next instantiation."
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
# app/service/stats_service.py
|
||||
|
||||
import datetime
|
||||
from typing import Union
|
||||
|
||||
from sqlalchemy import and_, case, func, or_, select
|
||||
|
||||
@@ -195,10 +196,11 @@ class StatsService:
|
||||
return details
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get API call details for period '{period}': {e}")
|
||||
logger.error(
|
||||
f"Failed to get API call details for period '{period}': {e}")
|
||||
raise
|
||||
|
||||
async def get_key_usage_details_last_24h(self, key: str) -> dict | None:
|
||||
async def get_key_usage_details_last_24h(self, key: str) -> Union[dict, None]:
|
||||
"""
|
||||
获取指定 API 密钥在过去 24 小时内按模型统计的调用次数。
|
||||
|
||||
@@ -218,7 +220,8 @@ class StatsService:
|
||||
try:
|
||||
query = (
|
||||
select(
|
||||
RequestLog.model_name, func.count(RequestLog.id).label("call_count")
|
||||
RequestLog.model_name, func.count(
|
||||
RequestLog.id).label("call_count")
|
||||
)
|
||||
.where(
|
||||
RequestLog.api_key == key,
|
||||
@@ -237,7 +240,8 @@ class StatsService:
|
||||
)
|
||||
return {}
|
||||
|
||||
usage_details = {row["model_name"]: row["call_count"] for row in results}
|
||||
usage_details = {row["model_name"]: row["call_count"]
|
||||
for row in results}
|
||||
logger.info(
|
||||
f"Successfully fetched usage details for key ending in ...{key[-4:]}: {usage_details}"
|
||||
)
|
||||
|
||||
@@ -1535,14 +1535,14 @@ function createAndAppendBudgetMapItem(mapKey, mapValue, modelId) {
|
||||
valueInput.value = isNaN(intValue) ? 0 : intValue;
|
||||
valueInput.placeholder = "预算 (整数)";
|
||||
valueInput.className = `${MAP_VALUE_INPUT_CLASS} w-24 px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:border-primary-500 focus:ring focus:ring-primary-200 focus:ring-opacity-50`;
|
||||
valueInput.min = 0;
|
||||
valueInput.max = 24576;
|
||||
valueInput.min = -1;
|
||||
valueInput.max = 32767;
|
||||
valueInput.addEventListener("input", function () {
|
||||
let val = this.value.replace(/[^0-9]/g, "");
|
||||
let val = this.value.replace(/[^0-9-]/g, "");
|
||||
if (val !== "") {
|
||||
val = parseInt(val, 10);
|
||||
if (val < 0) val = 0;
|
||||
if (val > 24576) val = 24576;
|
||||
if (val < -1) val = -1;
|
||||
if (val > 32767) val = 32767;
|
||||
}
|
||||
this.value = val; // Corrected variable name
|
||||
});
|
||||
|
||||
@@ -1309,7 +1309,7 @@
|
||||
</div> -->
|
||||
<small class="text-gray-500 mt-1 block"
|
||||
>为每个思考模型设置预算(整数,最大值
|
||||
24576),此项与上方模型列表自动关联。</small
|
||||
32767),此项与上方模型列表自动关联。</small
|
||||
>
|
||||
</div>
|
||||
<!-- 安全设置 -->
|
||||
@@ -2365,7 +2365,7 @@
|
||||
let val = this.value.replace(/[^0-9]/g, "");
|
||||
if (val !== "") {
|
||||
val = parseInt(val, 10);
|
||||
if (val > 24576) val = 24576;
|
||||
if (val > 32767) val = 32767;
|
||||
}
|
||||
this.value = val;
|
||||
|
||||
@@ -2379,4 +2379,3 @@
|
||||
});
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
||||
Reference in New Issue
Block a user