yxmiler commited on
Commit
096ce4f
·
verified ·
1 Parent(s): 784e43a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -11
app.py CHANGED
@@ -217,7 +217,39 @@ class AuthTokenManager:
217
  except Exception as error:
218
  logger.error(f"令牌删除失败: {str(error)}")
219
  return False
220
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
  def get_next_token_for_model(self, model_id):
222
  normalized_model = self.normalize_model_name(model_id)
223
 
@@ -411,12 +443,17 @@ class Utils:
411
 
412
  if proxy:
413
  logger.info(f"使用代理: {proxy}", "Server")
414
- proxy_options["proxies"] = {"https": proxy, "http": proxy}
415
-
416
  if proxy.startswith("socks5://"):
 
 
 
 
 
 
 
 
417
  proxy_options["proxies"] = {"https": proxy, "http": proxy}
418
- proxy_options["proxy_type"] = "socks5"
419
-
420
  return proxy_options
421
 
422
  class GrokApiClient:
@@ -569,7 +606,7 @@ class GrokApiClient:
569
  last_role = role
570
 
571
  return {
572
- "temporary": CONFIG["API"]["IS_TEMP_CONVERSATION"],
573
  "modelName": self.model_id,
574
  "message": messages.strip(),
575
  "fileAttachments": file_attachments[:4],
@@ -956,6 +993,7 @@ def chat_completions():
956
  retry_count = 0
957
  grok_client = GrokApiClient(model)
958
  request_payload = grok_client.prepare_chat_request(data)
 
959
 
960
  while retry_count < CONFIG["RETRY"]["MAX_ATTEMPTS"]:
961
  retry_count += 1
@@ -985,6 +1023,7 @@ def chat_completions():
985
  stream=True,
986
  **proxy_options)
987
  if response.status_code == 200:
 
988
  logger.info("请求成功", "Server")
989
  logger.info(
990
  f"当前{model}剩余可用令牌数: {token_manager.get_token_count_for_model(model)}",
@@ -1011,8 +1050,15 @@ def chat_completions():
1011
  model, CONFIG["API"]["SIGNATURE_COOKIE"])
1012
  if token_manager.get_token_count_for_model(model) == 0:
1013
  raise ValueError(f"{model} 次数已达上限,请切换其他模型或者重新对话")
1014
-
 
 
 
 
 
1015
  elif response.status_code == 429:
 
 
1016
  if CONFIG["API"]["IS_CUSTOM_SSO"]:
1017
  raise ValueError(f"自定义SSO令牌当前模型{model}的请求次数已失效")
1018
 
@@ -1038,8 +1084,10 @@ def chat_completions():
1038
  if CONFIG["API"]["IS_CUSTOM_SSO"]:
1039
  raise
1040
  continue
1041
-
1042
- raise ValueError('当前模型所有令牌都已耗尽')
 
 
1043
 
1044
  except Exception as error:
1045
  logger.error(str(error), "ChatAPI")
@@ -1047,7 +1095,7 @@ def chat_completions():
1047
  {"error": {
1048
  "message": str(error),
1049
  "type": "server_error"
1050
- }}), 500
1051
 
1052
  @app.route('/', defaults={'path': ''})
1053
  @app.route('/<path:path>')
@@ -1062,4 +1110,4 @@ if __name__ == '__main__':
1062
  host='0.0.0.0',
1063
  port=CONFIG["SERVER"]["PORT"],
1064
  debug=False
1065
- )
 
217
  except Exception as error:
218
  logger.error(f"令牌删除失败: {str(error)}")
219
  return False
220
+ def reduce_token_request_count(self, model_id, count):
221
+ try:
222
+ normalized_model = self.normalize_model_name(model_id)
223
+
224
+ if normalized_model not in self.token_model_map:
225
+ logger.error(f"模型 {normalized_model} 不存在", "TokenManager")
226
+ return False
227
+
228
+ if not self.token_model_map[normalized_model]:
229
+ logger.error(f"模型 {normalized_model} 没有可用的token", "TokenManager")
230
+ return False
231
+
232
+ token_entry = self.token_model_map[normalized_model][0]
233
+
234
+ # 确保RequestCount不会小于0
235
+ new_count = max(0, token_entry["RequestCount"] - count)
236
+ reduction = token_entry["RequestCount"] - new_count
237
+
238
+ token_entry["RequestCount"] = new_count
239
+
240
+ # 更新token状态
241
+ if token_entry["token"]:
242
+ sso = token_entry["token"].split("sso=")[1].split(";")[0]
243
+ if sso in self.token_status_map and normalized_model in self.token_status_map[sso]:
244
+ self.token_status_map[sso][normalized_model]["totalRequestCount"] = max(
245
+ 0,
246
+ self.token_status_map[sso][normalized_model]["totalRequestCount"] - reduction
247
+ )
248
+ return True
249
+
250
+ except Exception as error:
251
+ logger.error(f"重置校对token请求次数时发生错误: {str(error)}", "TokenManager")
252
+ return False
253
  def get_next_token_for_model(self, model_id):
254
  normalized_model = self.normalize_model_name(model_id)
255
 
 
443
 
444
  if proxy:
445
  logger.info(f"使用代理: {proxy}", "Server")
 
 
446
  if proxy.startswith("socks5://"):
447
+ proxy_options["proxy"] = proxy
448
+
449
+ if '@' in proxy:
450
+ auth_part = proxy.split('@')[0].split('://')[1]
451
+ if ':' in auth_part:
452
+ username, password = auth_part.split(':')
453
+ proxy_options["proxy_auth"] = (username, password)
454
+ else:
455
  proxy_options["proxies"] = {"https": proxy, "http": proxy}
456
+ print(proxy_options)
 
457
  return proxy_options
458
 
459
  class GrokApiClient:
 
606
  last_role = role
607
 
608
  return {
609
+ "temporary": CONFIG["API"].get("IS_TEMP_CONVERSATION", False),
610
  "modelName": self.model_id,
611
  "message": messages.strip(),
612
  "fileAttachments": file_attachments[:4],
 
993
  retry_count = 0
994
  grok_client = GrokApiClient(model)
995
  request_payload = grok_client.prepare_chat_request(data)
996
+ response_status_code = 500
997
 
998
  while retry_count < CONFIG["RETRY"]["MAX_ATTEMPTS"]:
999
  retry_count += 1
 
1023
  stream=True,
1024
  **proxy_options)
1025
  if response.status_code == 200:
1026
+ response_status_code = 200
1027
  logger.info("请求成功", "Server")
1028
  logger.info(
1029
  f"当前{model}剩余可用令牌数: {token_manager.get_token_count_for_model(model)}",
 
1050
  model, CONFIG["API"]["SIGNATURE_COOKIE"])
1051
  if token_manager.get_token_count_for_model(model) == 0:
1052
  raise ValueError(f"{model} 次数已达上限,请切换其他模型或者重新对话")
1053
+ elif response.status_code == 403:
1054
+ response_status_code = 403
1055
+ token_manager.reduce_token_request_count(model,1)#重置去除当前因为错误未成功请求的次数,确保不会因为错误未成功请求的次数导致次数上限
1056
+ if token_manager.get_token_count_for_model(model) == 0:
1057
+ raise ValueError(f"{model} 次数已达上限,请切换其他模型或者重新对话")
1058
+ raise ValueError(f"IP暂时被封黑无法破盾,请稍后重试或者更换ip")
1059
  elif response.status_code == 429:
1060
+ response_status_code = 429
1061
+ token_manager.reduce_token_request_count(model,1)
1062
  if CONFIG["API"]["IS_CUSTOM_SSO"]:
1063
  raise ValueError(f"自定义SSO令牌当前模型{model}的请求次数已失效")
1064
 
 
1084
  if CONFIG["API"]["IS_CUSTOM_SSO"]:
1085
  raise
1086
  continue
1087
+ if response_status_code == 403:
1088
+ raise ValueError('IP暂时被封黑无法破盾,请稍后重试或者更换ip')
1089
+ elif response_status_code == 500:
1090
+ raise ValueError('当前模型所有令牌暂无可用,请稍后重试')
1091
 
1092
  except Exception as error:
1093
  logger.error(str(error), "ChatAPI")
 
1095
  {"error": {
1096
  "message": str(error),
1097
  "type": "server_error"
1098
+ }}), response_status_code
1099
 
1100
  @app.route('/', defaults={'path': ''})
1101
  @app.route('/<path:path>')
 
1110
  host='0.0.0.0',
1111
  port=CONFIG["SERVER"]["PORT"],
1112
  debug=False
1113
+ )