File size: 2,711 Bytes
f2b63ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# -*- 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=["聊天"])


@router.post("/completions", response_model=None)
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"
                }
            }
        )