Spaces:
Running
Running
Download app/api/v1/chat.py from moole/grok2api_fastapi: direct link, hf CLI and curl.
- Browser
- Download file 2.71 kB
-
https://huggingface.co/spaces/moole/grok2api_fastapi/resolve/main/app/api/v1/chat.py
- Command line
-
hf download hf://spaces/moole/grok2api_fastapi/app/api/v1/chat.py
-
curl -L -o chat.py https://huggingface.co/spaces/moole/grok2api_fastapi/resolve/main/app/api/v1/chat.py
2.71 kB
| # -*- coding: utf-8 -*- | |
| """ | |
| 聊天API路由模块 | |
| 提供OpenAI兼容的聊天API接口,支持与Grok模型的交互。 | |
| """ | |
| from fastapi import APIRouter, Depends, HTTPException | |
| from typing import Optional | |
| from fastapi.responses import StreamingResponse | |
| from app.core.auth import auth_manager | |
| from app.core.exception import GrokApiException | |
| from app.core.logger import logger | |
| from app.services.grok.client import GrokClient | |
| from app.models.openai_schema import OpenAIChatRequest | |
| # 聊天路由 | |
| router = APIRouter(prefix="/chat", tags=["聊天"]) | |
| async def chat_completions( | |
| request: OpenAIChatRequest, | |
| authenticated: Optional[str] = Depends(auth_manager.verify) | |
| ): | |
| """ | |
| 创建聊天补全 | |
| 兼容OpenAI聊天API的端点,支持流式和非流式响应。 | |
| Args: | |
| request: OpenAI格式的聊天请求 | |
| authenticated: 认证状态(由依赖注入) | |
| Returns: | |
| OpenAIChatCompletionResponse: 非流式响应 | |
| StreamingResponse: 流式响应 | |
| Raises: | |
| HTTPException: 当请求处理失败时 | |
| """ | |
| try: | |
| logger.info(f"[Chat] 聊天请求 - 模型: {request.model}") | |
| # 调用Grok客户端处理请求 | |
| result = await GrokClient.openai_to_grok(request.model_dump()) | |
| # 如果是流式响应,GrokClient已经返回了Iterator,直接包装为StreamingResponse | |
| if request.stream: | |
| return StreamingResponse( | |
| content=result, | |
| media_type="text/event-stream", | |
| headers={ | |
| "Cache-Control": "no-cache", | |
| "Connection": "keep-alive", | |
| "X-Accel-Buffering": "no" | |
| } | |
| ) | |
| # 非流式响应直接返回 | |
| return result | |
| except GrokApiException as e: | |
| logger.error(f"[Chat] Grok API错误: {str(e)}", extra={"details": e.details}) | |
| raise HTTPException( | |
| status_code=500, | |
| detail={ | |
| "error": { | |
| "message": str(e), | |
| "type": e.error_code or "grok_api_error", | |
| "code": e.error_code or "unknown" | |
| } | |
| } | |
| ) | |
| except Exception as e: | |
| logger.error(f"[Chat] 聊天请求处理失败: {str(e)}", exc_info=True) | |
| raise HTTPException( | |
| status_code=500, | |
| detail={ | |
| "error": { | |
| "message": "服务器内部错误", | |
| "type": "internal_error", | |
| "code": "internal_server_error" | |
| } | |
| } | |
| ) | |