bibibi12345 commited on
Commit
c10b1dd
·
1 Parent(s): 9426190

changed proxy pool

Browse files
Files changed (1) hide show
  1. freeplay2api.py +127 -87
freeplay2api.py CHANGED
@@ -152,8 +152,9 @@ class FreeplayClient:
152
  self.proxy_pool.remove_proxy(proxy_info['ip'], proxy_info['port'])
153
  return 0.0
154
  except Exception as e:
 
155
  logging.warning(
156
- f"Failed to check balance for session_id ending in ...{session_id[-4:]}: {e}"
157
  )
158
  return 0.0
159
 
@@ -326,11 +327,14 @@ class AccountManager:
326
  def load_accounts(self):
327
  with self.lock:
328
  if not os.path.exists(self.filepath):
 
329
  self.accounts = []
330
  return
331
  with open(self.filepath, "r", encoding="utf-8") as f:
332
  self.accounts = [json.loads(line) for line in f if line.strip()]
333
  logging.info(f"Loaded {len(self.accounts)} accounts from {self.filepath}")
 
 
334
 
335
  def save_accounts(self):
336
  # This operation is disabled to ensure the application is stateless.
@@ -380,36 +384,54 @@ class KeyMaintainer(threading.Thread):
380
  try:
381
  logging.info("KeyMaintainer: Starting maintenance cycle.")
382
  accounts = self.manager.get_all_accounts()
 
383
 
384
  # Update balances
385
- for account in accounts:
386
- balance = self.client.check_balance(account["session_id"])
387
- if balance != account.get("balance"):
388
- account["balance"] = balance
389
- self.manager.update_account(account)
390
- logging.info(f"Account {account['email']} balance updated to ${balance:.4f}")
 
 
 
 
 
 
 
 
 
 
391
 
392
  # Check if new accounts are needed
393
- healthy_accounts = [
394
- acc for acc in self.manager.get_all_accounts()
395
- if acc.get("balance", 0) > self.config["LOW_BALANCE_THRESHOLD"]
396
- ]
397
- needed = self.config["ACTIVE_KEY_THRESHOLD"] - len(healthy_accounts)
398
-
399
- while needed > 0:
400
- logging.info(f"Healthy accounts ({len(healthy_accounts)}) below threshold. Need to register {needed} new accounts. Retrying immediately.")
401
- new_account = self.client.register()
402
- if new_account:
403
- self.manager.add_account(new_account)
404
-
405
  healthy_accounts = [
406
  acc for acc in self.manager.get_all_accounts()
407
  if acc.get("balance", 0) > self.config["LOW_BALANCE_THRESHOLD"]
408
  ]
409
  needed = self.config["ACTIVE_KEY_THRESHOLD"] - len(healthy_accounts)
410
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
411
  except Exception as e:
412
- logging.error(f"Error in KeyMaintainer cycle: {e}")
 
 
413
 
414
  time.sleep(self.config["CHECK_INTERVAL_SECONDS"])
415
 
@@ -587,88 +609,106 @@ async def chat_completions(
587
  message["content"] = new_content
588
 
589
  # 账户选择和重试逻辑
590
- max_retries = len(account_manager.get_all_accounts())
 
 
 
591
  for attempt in range(max_retries):
592
- account = account_manager.get_account()
593
- if not account:
594
- raise HTTPException(
595
- status_code=503, detail="No available accounts in the pool."
596
- )
597
-
598
  try:
599
- params = {
600
- "max_tokens": req.max_tokens,
601
- "temperature": req.temperature,
602
- "top_p": req.top_p,
603
- }
604
- response = freeplay_client.chat(
605
- account["session_id"],
606
- account["project_id"],
607
- model_config,
608
- messages_dict,
609
- params,
610
- )
611
 
612
- if response.status_code == 200:
613
- # 请求成功
614
- if req.stream:
615
- return StreamingResponse(
616
- stream_generator(response, req.model, account),
617
- media_type="text/event-stream",
618
- )
619
- else:
620
- full_content = ""
621
- for line in response.iter_lines(decode_unicode=True):
622
- if line and line.startswith("data: "):
623
- try:
624
- data = json.loads(line[6:])
625
- full_content += data.get("content", "")
626
- if data.get("cost") is not None:
627
- break
628
- except json.JSONDecodeError:
629
- continue
630
-
631
- # 更新余额
632
- new_balance = freeplay_client.check_balance(account["session_id"])
633
- account["balance"] = new_balance
634
- account_manager.update_account(account)
635
- logging.info(
636
- f"Post-chat balance update for {account['email']}: ${new_balance:.4f}"
637
- )
638
 
639
- return ChatCompletionResponse(
640
- model=req.model,
641
- choices=[
642
- ChatCompletionChoice(
643
- message=ChatMessage(
644
- role="assistant", content=full_content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
645
  )
646
- )
647
- ],
648
- )
649
 
650
- elif response.status_code in [401, 403, 404]:
651
- logging.warning(
652
- f"Account {account['email']} failed with status {response.status_code}. Disabling it."
653
- )
654
- account["balance"] = 0.0 # 禁用账户
655
- account_manager.update_account(account)
656
- continue # 重试下一个
657
- else:
658
- logging.error(
659
- f"API call failed with status {response.status_code}: {response.text}"
660
- )
661
- response.raise_for_status()
 
 
 
 
 
 
662
 
663
  except requests.exceptions.ProxyError:
664
  # Proxy error was already logged and handled in FreeplayClient
665
  logging.warning(f"Retrying request due to proxy error.")
666
  # Don't disable the account, just retry with a new proxy (and potentially new account)
667
  continue
 
668
  except Exception as e:
669
  logging.error(
670
- f"Error with account {account['email']}: {e}. Trying next account."
671
  )
 
 
672
  account["balance"] = 0.0 # 发生未知异常也禁用
673
  account_manager.update_account(account)
674
  continue
 
152
  self.proxy_pool.remove_proxy(proxy_info['ip'], proxy_info['port'])
153
  return 0.0
154
  except Exception as e:
155
+ session_suffix = session_id[-4:] if session_id and len(session_id) >= 4 else session_id or "None"
156
  logging.warning(
157
+ f"Failed to check balance for session_id ending in ...{session_suffix}: {e}"
158
  )
159
  return 0.0
160
 
 
327
  def load_accounts(self):
328
  with self.lock:
329
  if not os.path.exists(self.filepath):
330
+ logging.info(f"DEBUG: Accounts file {self.filepath} does not exist, initializing empty accounts list")
331
  self.accounts = []
332
  return
333
  with open(self.filepath, "r", encoding="utf-8") as f:
334
  self.accounts = [json.loads(line) for line in f if line.strip()]
335
  logging.info(f"Loaded {len(self.accounts)} accounts from {self.filepath}")
336
+ for i, acc in enumerate(self.accounts):
337
+ logging.info(f"DEBUG: Account {i}: email={acc.get('email')}, session_id type={type(acc.get('session_id'))}, session_id={repr(acc.get('session_id'))}")
338
 
339
  def save_accounts(self):
340
  # This operation is disabled to ensure the application is stateless.
 
384
  try:
385
  logging.info("KeyMaintainer: Starting maintenance cycle.")
386
  accounts = self.manager.get_all_accounts()
387
+ logging.info(f"DEBUG: KeyMaintainer got {len(accounts)} accounts")
388
 
389
  # Update balances
390
+ for i, account in enumerate(accounts):
391
+ try:
392
+ logging.info(f"DEBUG: Processing account {i}: {account.get('email')}")
393
+ logging.info(f"DEBUG: Account structure: {list(account.keys())}")
394
+ session_id = account.get("session_id")
395
+ logging.info(f"DEBUG: session_id type: {type(session_id)}, value: {repr(session_id)}")
396
+
397
+ balance = self.client.check_balance(session_id)
398
+ if balance != account.get("balance"):
399
+ account["balance"] = balance
400
+ self.manager.update_account(account)
401
+ logging.info(f"Account {account['email']} balance updated to ${balance:.4f}")
402
+ except Exception as e:
403
+ logging.error(f"DEBUG: Error processing account {i} ({account.get('email')}): {type(e).__name__}: {e}")
404
+ import traceback
405
+ logging.error(f"DEBUG: Account processing traceback: {traceback.format_exc()}")
406
 
407
  # Check if new accounts are needed
408
+ try:
 
 
 
 
 
 
 
 
 
 
 
409
  healthy_accounts = [
410
  acc for acc in self.manager.get_all_accounts()
411
  if acc.get("balance", 0) > self.config["LOW_BALANCE_THRESHOLD"]
412
  ]
413
  needed = self.config["ACTIVE_KEY_THRESHOLD"] - len(healthy_accounts)
414
 
415
+ while needed > 0:
416
+ logging.info(f"Healthy accounts ({len(healthy_accounts)}) below threshold. Need to register {needed} new accounts. Retrying immediately.")
417
+ new_account = self.client.register()
418
+ if new_account:
419
+ self.manager.add_account(new_account)
420
+
421
+ healthy_accounts = [
422
+ acc for acc in self.manager.get_all_accounts()
423
+ if acc.get("balance", 0) > self.config["LOW_BALANCE_THRESHOLD"]
424
+ ]
425
+ needed = self.config["ACTIVE_KEY_THRESHOLD"] - len(healthy_accounts)
426
+ except Exception as e:
427
+ logging.error(f"DEBUG: Error in account threshold check: {type(e).__name__}: {e}")
428
+ import traceback
429
+ logging.error(f"DEBUG: Threshold check traceback: {traceback.format_exc()}")
430
+
431
  except Exception as e:
432
+ logging.error(f"Error in KeyMaintainer cycle: {type(e).__name__}: {e}")
433
+ import traceback
434
+ logging.error(f"DEBUG: KeyMaintainer outer traceback: {traceback.format_exc()}")
435
 
436
  time.sleep(self.config["CHECK_INTERVAL_SECONDS"])
437
 
 
609
  message["content"] = new_content
610
 
611
  # 账户选择和重试逻辑
612
+ all_accounts = account_manager.get_all_accounts()
613
+ logging.info(f"DEBUG: chat_completions got {len(all_accounts)} total accounts")
614
+ max_retries = len(all_accounts)
615
+
616
  for attempt in range(max_retries):
 
 
 
 
 
 
617
  try:
618
+ account = account_manager.get_account()
619
+ if not account:
620
+ raise HTTPException(
621
+ status_code=503, detail="No available accounts in the pool."
622
+ )
 
 
 
 
 
 
 
623
 
624
+ logging.info(f"DEBUG: Using account {account.get('email')} for chat attempt {attempt}")
625
+ logging.info(f"DEBUG: Account keys: {list(account.keys())}")
626
+
627
+ try:
628
+ params = {
629
+ "max_tokens": req.max_tokens,
630
+ "temperature": req.temperature,
631
+ "top_p": req.top_p,
632
+ }
633
+ response = freeplay_client.chat(
634
+ account["session_id"],
635
+ account["project_id"],
636
+ model_config,
637
+ messages_dict,
638
+ params,
639
+ )
 
 
 
 
 
 
 
 
 
 
640
 
641
+ if response.status_code == 200:
642
+ # 请求成功
643
+ if req.stream:
644
+ return StreamingResponse(
645
+ stream_generator(response, req.model, account),
646
+ media_type="text/event-stream",
647
+ )
648
+ else:
649
+ full_content = ""
650
+ for line in response.iter_lines(decode_unicode=True):
651
+ if line and line.startswith("data: "):
652
+ try:
653
+ data = json.loads(line[6:])
654
+ content = data.get("content", "")
655
+ if content is not None:
656
+ full_content += content
657
+ if data.get("cost") is not None:
658
+ break
659
+ except json.JSONDecodeError:
660
+ continue
661
+
662
+ # 更新余额
663
+ new_balance = freeplay_client.check_balance(account["session_id"])
664
+ account["balance"] = new_balance
665
+ account_manager.update_account(account)
666
+ logging.info(
667
+ f"Post-chat balance update for {account['email']}: ${new_balance:.4f}"
668
+ )
669
+
670
+ return ChatCompletionResponse(
671
+ model=req.model,
672
+ choices=[
673
+ ChatCompletionChoice(
674
+ message=ChatMessage(
675
+ role="assistant", content=full_content
676
+ )
677
  )
678
+ ],
679
+ )
 
680
 
681
+ elif response.status_code in [401, 403, 404]:
682
+ logging.warning(
683
+ f"Account {account['email']} failed with status {response.status_code}. Disabling it."
684
+ )
685
+ account["balance"] = 0.0 # 禁用账户
686
+ account_manager.update_account(account)
687
+ continue # 重试下一个
688
+ else:
689
+ logging.error(
690
+ f"API call failed with status {response.status_code}: {response.text}"
691
+ )
692
+ response.raise_for_status()
693
+
694
+ except Exception as e:
695
+ logging.error(f"DEBUG: Error in chat request: {type(e).__name__}: {e}")
696
+ import traceback
697
+ logging.error(f"DEBUG: Chat request traceback: {traceback.format_exc()}")
698
+ raise # Re-raise to be caught by outer exception handler
699
 
700
  except requests.exceptions.ProxyError:
701
  # Proxy error was already logged and handled in FreeplayClient
702
  logging.warning(f"Retrying request due to proxy error.")
703
  # Don't disable the account, just retry with a new proxy (and potentially new account)
704
  continue
705
+
706
  except Exception as e:
707
  logging.error(
708
+ f"Error with account {account.get('email', 'unknown')}: {type(e).__name__}: {e}. Trying next account."
709
  )
710
+ import traceback
711
+ logging.error(f"DEBUG: Account error traceback: {traceback.format_exc()}")
712
  account["balance"] = 0.0 # 发生未知异常也禁用
713
  account_manager.update_account(account)
714
  continue