MCPcopy Create free account
hub / github.com/ZyphrZero/z.ai2api_python / _create_stream_response

Method _create_stream_response

app/core/upstream.py:1725–1900  ·  view source on GitHub ↗

创建流式响应,并在首包前支持双池重试。

(
        self,
        request: OpenAIRequest,
        transformed: Dict[str, Any]
    )

Source from the content-addressed store, hash-verified

1723 return handle_error(e, "请求处理")
1724
1725 async def _create_stream_response(
1726 self,
1727 request: OpenAIRequest,
1728 transformed: Dict[str, Any]
1729 ) -> AsyncGenerator[str, None]:
1730 """创建流式响应,并在首包前支持双池重试。"""
1731 max_attempts = self._get_total_retry_limit()
1732 excluded_tokens: Set[str] = set()
1733 excluded_guest_user_ids: Set[str] = set()
1734 current_token = str(transformed.get("token") or "")
1735
1736 try:
1737 proxies = self._get_proxy_config()
1738
1739 async with httpx.AsyncClient(
1740 timeout=self._build_timeout(read_timeout=180.0),
1741 http2=True,
1742 limits=self._build_limits(),
1743 proxy=proxies,
1744 ) as client:
1745 for attempt in range(max_attempts):
1746 self.logger.info(f"🎯 发送请求到上游: {transformed['url']}")
1747 async with client.stream(
1748 "POST",
1749 transformed["url"],
1750 json=transformed["body"],
1751 headers=transformed["headers"],
1752 ) as response:
1753 error_text = await response.aread() if response.status_code != 200 else b""
1754 error_msg = error_text.decode("utf-8", errors="ignore")
1755 error_code, parsed_error_message = (
1756 self._extract_upstream_error_details(
1757 response.status_code,
1758 error_msg,
1759 )
1760 if response.status_code != 200
1761 else (None, "")
1762 )
1763 is_concurrency_limited = self._is_concurrency_limited(
1764 response.status_code,
1765 error_code,
1766 parsed_error_message,
1767 )
1768
1769 if self._should_retry_guest_session(
1770 response.status_code,
1771 is_concurrency_limited,
1772 attempt,
1773 max_attempts,
1774 transformed,
1775 ):
1776 guest_user_id = str(
1777 transformed.get("guest_user_id")
1778 or transformed.get("user_id")
1779 or ""
1780 )
1781 if guest_user_id:
1782 excluded_guest_user_ids.add(guest_user_id)

Callers 1

chat_completionMethod · 0.95

Tested by

no test coverage detected