Merge branch 'fork/lottshin/pr/text-stream-auto-rotate'
Browse files- services/account_service.py +30 -6
- services/protocol/conversation.py +26 -6
services/account_service.py
CHANGED
|
@@ -278,13 +278,37 @@ class AccountService:
|
|
| 278 |
f"status={account.get('status') if account else 'unknown'}"
|
| 279 |
)
|
| 280 |
|
| 281 |
-
def get_text_access_token(self) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
with self._lock:
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 288 |
|
| 289 |
def remove_invalid_token(self, access_token: str, event: str) -> bool:
|
| 290 |
if not config.auto_remove_invalid_accounts:
|
|
|
|
| 278 |
f"status={account.get('status') if account else 'unknown'}"
|
| 279 |
)
|
| 280 |
|
| 281 |
+
def get_text_access_token(self, excluded_tokens: set[str] | None = None) -> str:
|
| 282 |
+
excluded = {self._clean_token(token) for token in (excluded_tokens or set()) if self._clean_token(token)}
|
| 283 |
+
with self._lock:
|
| 284 |
+
candidates = [
|
| 285 |
+
token
|
| 286 |
+
for account in self._accounts
|
| 287 |
+
if self._clean_token(account.get("status")) not in {"禁用", "异常", "限流"}
|
| 288 |
+
and (token := self._clean_token(account.get("access_token")))
|
| 289 |
+
and token not in excluded
|
| 290 |
+
]
|
| 291 |
+
if not candidates:
|
| 292 |
+
return ""
|
| 293 |
+
access_token = candidates[self._index % len(candidates)]
|
| 294 |
+
self._index += 1
|
| 295 |
+
return access_token
|
| 296 |
+
|
| 297 |
+
def mark_text_used(self, access_token: str) -> None:
|
| 298 |
+
access_token = self._clean_token(access_token)
|
| 299 |
+
if not access_token:
|
| 300 |
+
return
|
| 301 |
with self._lock:
|
| 302 |
+
index = self._find_account_index(access_token)
|
| 303 |
+
if index < 0:
|
| 304 |
+
return
|
| 305 |
+
next_item = dict(self._accounts[index])
|
| 306 |
+
next_item["last_used_at"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 307 |
+
account = self._normalize_account(next_item)
|
| 308 |
+
if account is None:
|
| 309 |
+
return
|
| 310 |
+
self._accounts[index] = account
|
| 311 |
+
self._save_accounts()
|
| 312 |
|
| 313 |
def remove_invalid_token(self, access_token: str, event: str) -> bool:
|
| 314 |
if not config.auto_remove_invalid_accounts:
|
services/protocol/conversation.py
CHANGED
|
@@ -451,12 +451,32 @@ def text_backend() -> OpenAIBackendAPI:
|
|
| 451 |
|
| 452 |
|
| 453 |
def stream_text_deltas(backend: OpenAIBackendAPI, request: ConversationRequest) -> Iterator[str]:
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
if
|
| 459 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 460 |
|
| 461 |
|
| 462 |
def collect_text(backend: OpenAIBackendAPI, request: ConversationRequest) -> str:
|
|
|
|
| 451 |
|
| 452 |
|
| 453 |
def stream_text_deltas(backend: OpenAIBackendAPI, request: ConversationRequest) -> Iterator[str]:
|
| 454 |
+
attempted_tokens: set[str] = set()
|
| 455 |
+
emitted = False
|
| 456 |
+
while True:
|
| 457 |
+
token = account_service.get_text_access_token(attempted_tokens)
|
| 458 |
+
if not token:
|
| 459 |
+
token = getattr(backend, "access_token", "")
|
| 460 |
+
if not token or token in attempted_tokens:
|
| 461 |
+
raise RuntimeError("no available text account")
|
| 462 |
+
attempted_tokens.add(token)
|
| 463 |
+
try:
|
| 464 |
+
active_backend = OpenAIBackendAPI(access_token=token)
|
| 465 |
+
for event in conversation_events(active_backend, messages=request.messages, model=request.model, prompt=request.prompt):
|
| 466 |
+
if event.get("type") != "conversation.delta":
|
| 467 |
+
continue
|
| 468 |
+
delta = str(event.get("delta") or "")
|
| 469 |
+
if delta:
|
| 470 |
+
emitted = True
|
| 471 |
+
yield delta
|
| 472 |
+
account_service.mark_text_used(token)
|
| 473 |
+
return
|
| 474 |
+
except Exception as exc:
|
| 475 |
+
error_message = str(exc)
|
| 476 |
+
if not emitted and is_token_invalid_error(error_message):
|
| 477 |
+
account_service.remove_invalid_token(token, "text_stream")
|
| 478 |
+
continue
|
| 479 |
+
raise
|
| 480 |
|
| 481 |
|
| 482 |
def collect_text(backend: OpenAIBackendAPI, request: ConversationRequest) -> str:
|