Skip to content

Commit 57ac976

Browse files
committed
fix(tiku): 在 SiliconFlow 连接测试中引入限频间隔等待以避免 API 触发频率限制
1 parent 0acedc7 commit 57ac976

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

api/answer.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,6 +1300,14 @@ def __init__(self, config_path: Optional[str] = None):
13001300
self.last_request_time = None
13011301
self._lock = threading.Lock()
13021302

1303+
def _wait_for_interval(self):
1304+
if self.last_request_time:
1305+
interval = time.time() - self.last_request_time
1306+
if interval < self.min_interval:
1307+
sleep_time = self.min_interval - interval
1308+
logger.debug(f"API请求间隔过短, 等待 {sleep_time} 秒")
1309+
time.sleep(sleep_time)
1310+
13031311
def _query(self, q_info: dict):
13041312
with self._lock:
13051313
return self._query_locked(q_info)
@@ -1350,11 +1358,7 @@ def remove_md_json_wrapper(md_str):
13501358
"response_format": {"type": "text"}
13511359
}
13521360

1353-
# 处理请求间隔
1354-
if self.last_request_time:
1355-
interval = time.time() - self.last_request_time
1356-
if interval < self.min_interval:
1357-
time.sleep(self.min_interval - interval)
1361+
self._wait_for_interval()
13581362

13591363
try:
13601364
response = requests.post(
@@ -1416,12 +1420,16 @@ def check_llm_connection(self) -> bool:
14161420
'response_format': {'type': 'text'}
14171421
}
14181422

1423+
# 在测试 API 连接时同样执行节流校验
1424+
self._wait_for_interval()
1425+
14191426
response = requests.post(
14201427
self.api_endpoint,
14211428
headers=headers,
14221429
json=payload,
14231430
timeout=30
14241431
)
1432+
self.last_request_time = time.time()
14251433

14261434
if response.status_code == 200:
14271435
result = response.json()

0 commit comments

Comments
 (0)