流式请求入口。 body 格式: {"model": str, "request": {...gemini payload...}} Yields: Response 对象(错误时)或 str/bytes(SSE 格式数据)
(
body: Dict[str, Any],
native: bool = False,
)
| 593 | # ==================== 核心请求函数 ==================== |
| 594 | |
| 595 | async def stream_request( |
| 596 | body: Dict[str, Any], |
| 597 | native: bool = False, |
| 598 | ): |
| 599 | """ |
| 600 | 流式请求入口。 |
| 601 | |
| 602 | body 格式: {"model": str, "request": {...gemini payload...}} |
| 603 | |
| 604 | Yields: |
| 605 | Response 对象(错误时)或 str/bytes(SSE 格式数据) |
| 606 | """ |
| 607 | if not WREQ_AVAILABLE: |
| 608 | yield Response( |
| 609 | content='{"error":{"code":503,"message":"wreq not installed, vertex channel unavailable","status":"UNAVAILABLE"}}', |
| 610 | status_code=503, |
| 611 | media_type="application/json", |
| 612 | ) |
| 613 | return |
| 614 | |
| 615 | model = body.get("model", "") |
| 616 | gemini_payload = body.get("request", {}) |
| 617 | |
| 618 | max_retries = 3 |
| 619 | recaptcha_token: Optional[str] = None |
| 620 | is_first_auth = True |
| 621 | |
| 622 | for attempt in range(max_retries + 1): |
| 623 | log.debug(f"[VERTEX STREAM] attempt {attempt + 1}/{max_retries + 1}, model={model}") |
| 624 | |
| 625 | if not recaptcha_token: |
| 626 | recaptcha_token = await fetch_recaptcha_token() |
| 627 | is_first_auth = True |
| 628 | |
| 629 | if not recaptcha_token: |
| 630 | if attempt >= max_retries: |
| 631 | yield Response( |
| 632 | content=json.dumps({"error": {"code": 401, "message": "Could not fetch reCAPTCHA token", "status": "UNAUTHENTICATED"}}), |
| 633 | status_code=401, |
| 634 | media_type="application/json", |
| 635 | ) |
| 636 | return |
| 637 | await asyncio.sleep(1) |
| 638 | continue |
| 639 | |
| 640 | payload = _build_request_payload(model, gemini_payload, recaptcha_token) |
| 641 | headers = _batch_graphql_headers() |
| 642 | |
| 643 | content_yielded = False |
| 644 | need_retry = False |
| 645 | auth_retry = False |
| 646 | quota_retry = False |
| 647 | |
| 648 | try: |
| 649 | resp = await wreq.post( |
| 650 | BATCH_GRAPHQL_URL, |
| 651 | json=payload, |
| 652 | headers=headers, |
no test coverage detected