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

Method chat_completion

app/core/upstream.py:1601–1723  ·  view source on GitHub ↗

聊天完成接口。

(
        self,
        request: OpenAIRequest,
        **kwargs
    )

Source from the content-addressed store, hash-verified

1599 }
1600
1601 async def chat_completion(
1602 self,
1603 request: OpenAIRequest,
1604 **kwargs
1605 ) -> Union[Dict[str, Any], AsyncGenerator[str, None]]:
1606 """聊天完成接口。"""
1607 self.logger.info(f"🔄 {self.name} 处理请求: {request.model}")
1608 self.logger.debug(f" 消息数量: {len(request.messages)}")
1609 self.logger.debug(f" 流式模式: {request.stream}")
1610
1611 try:
1612 transformed = await self.transform_request(request)
1613
1614 if request.stream:
1615 return self._create_stream_response(request, transformed)
1616
1617 proxies = self._get_proxy_config()
1618 max_attempts = self._get_total_retry_limit()
1619 excluded_tokens: Set[str] = set()
1620 excluded_guest_user_ids: Set[str] = set()
1621
1622 for attempt in range(max_attempts):
1623 async with httpx.AsyncClient(
1624 timeout=self._build_timeout(read_timeout=60.0),
1625 limits=self._build_limits(),
1626 proxy=proxies,
1627 ) as client:
1628 response = await client.post(
1629 transformed["url"],
1630 headers=transformed["headers"],
1631 json=transformed["body"],
1632 )
1633
1634 error_code, error_message = self._extract_upstream_error_details(
1635 response.status_code,
1636 response.text,
1637 )
1638 is_concurrency_limited = self._is_concurrency_limited(
1639 response.status_code,
1640 error_code,
1641 error_message,
1642 )
1643
1644 if self._should_retry_guest_session(
1645 response.status_code,
1646 is_concurrency_limited,
1647 attempt,
1648 max_attempts,
1649 transformed,
1650 ):
1651 guest_user_id = str(
1652 transformed.get("guest_user_id")
1653 or transformed.get("user_id")
1654 or ""
1655 )
1656 if guest_user_id:
1657 excluded_guest_user_ids.add(guest_user_id)
1658 transformed = await self._refresh_guest_request(

Calls 15

transform_requestMethod · 0.95
_get_proxy_configMethod · 0.95
_build_timeoutMethod · 0.95
_build_limitsMethod · 0.95
mark_token_failureMethod · 0.95