77 lines
2.4 KiB
Python
77 lines
2.4 KiB
Python
import logging
|
|
import uuid
|
|
import json
|
|
|
|
from typing import Any, Dict
|
|
from fastapi import APIRouter, HTTPException, status
|
|
from fastapi.responses import StreamingResponse
|
|
|
|
from models import ChatRequest
|
|
from utils import convert_prompt_to_messages, standard_sse_generator
|
|
from config import GLM_API_KEY, GLM_UPSTREAM_URL
|
|
|
|
router = APIRouter()
|
|
logger = logging.getLogger("proxy")
|
|
|
|
|
|
@router.post("/glm/chat")
|
|
async def proxy_glm_chat(request: ChatRequest):
|
|
|
|
request_id = f"glm-{uuid.uuid4()}"
|
|
|
|
if not GLM_API_KEY:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
|
detail="GLM API key is not configured.",
|
|
)
|
|
|
|
client_request_body = request.model_dump()
|
|
logger.debug(
|
|
f"[{request_id}] === GLM New Request ===\n"
|
|
f"[{request_id}] Client Request Body:\n"
|
|
f"{json.dumps(client_request_body, indent=2, ensure_ascii=False)}"
|
|
)
|
|
|
|
messages = convert_prompt_to_messages(request.prompt)
|
|
logger.debug(
|
|
f"[{request_id}] Agent Transformed Messages:\n"
|
|
f"{json.dumps(messages, indent=2, ensure_ascii=False)}"
|
|
)
|
|
|
|
# 基础的请求体
|
|
payload: Dict[str, Any] = {
|
|
"model": "glm-4-flash",
|
|
"messages": messages,
|
|
"stream": True,
|
|
"temperature": request.temperature,
|
|
"top_p": request.top_p,
|
|
}
|
|
|
|
# 如果请求中指定了 max_tokens ,则添加到 payload
|
|
if request.max_tokens:
|
|
payload["max_tokens"] = request.max_tokens
|
|
|
|
# 处理 GLM web_search 功能
|
|
if request.web_search:
|
|
tool = {"type": "web_search", "web_search": {}}
|
|
if request.web_search is True:
|
|
# 如果是简单的 true,则启用默认的网络搜索
|
|
tool["web_search"] = {"enable": True}
|
|
elif isinstance(request.web_search, dict):
|
|
# 如果是字典,则使用用户提供的配置
|
|
tool["web_search"] = request.web_search
|
|
# 确保 enable 标志为 True
|
|
tool["web_search"]["enable"] = True
|
|
payload["tools"] = [tool]
|
|
|
|
logger.debug(
|
|
f"[{request_id}] Upstream Payload:\n"
|
|
f"{json.dumps(payload, indent=2, ensure_ascii=False)}"
|
|
)
|
|
|
|
headers = {"Authorization": f"Bearer {GLM_API_KEY}"}
|
|
return StreamingResponse(
|
|
standard_sse_generator(GLM_UPSTREAM_URL, payload, headers, "GLM", request_id),
|
|
media_type="text/event-stream",
|
|
)
|