Files
llmproxy/app/models.py
Vertex-AI-Step-Builder f7508d915b feat: 优化 chat 接口并修复 function 消息处理
主要变更:
- 使用原生 Request 对象接收请求数据
- 先记录原始 client_request(完整 JSON)到数据库
- 然后解析为 IncomingRequest 对象进行验证
- 添加请求解析的错误处理

修复问题:
- ChatMessage 的 content 改为 Optional[str],支持空值
- 添加 name 字段支持 function 角色的工具名称
- 添加 tool_calls 字段支持 assistant 消息的工具调用
- 修复 function 类型消息 content 为空时报错的问题

优化改进:
- 保留完整的原始客户端请求
- 更好的数据完整性和可追溯性
- 代码清理:移除重复的 import 语句

测试验证:
- 多轮工具调用对话正常工作
- function 消息空 content 正常处理
- 所有单元测试通过 (20/20)
- 完全兼容 OpenAI API 消息格式

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-31 09:32:37 +00:00

50 lines
1.5 KiB
Python

from pydantic import BaseModel, Field
from typing import List, Dict, Any, Optional
# Models for incoming requests
class ChatMessage(BaseModel):
"""Represents a single message in the chat history."""
role: str
content: Optional[str] = None
name: Optional[str] = None # For function role messages
tool_calls: Optional[List[Dict[str, Any]]] = None # For assistant messages with tool calls
class Function(BaseModel):
"""Represents the function definition within a tool."""
name: str
description: str
parameters: Dict[str, Any]
class Tool(BaseModel):
"""Represents a tool definition provided by the user."""
type: str
function: Function
class IncomingRequest(BaseModel):
"""Defines the structure of the request from the client."""
messages: List[ChatMessage]
tools: Optional[List[Tool]] = None
stream: Optional[bool] = False
# Models for outgoing responses
class ToolCallFunction(BaseModel):
"""Function call details within a tool call."""
name: str
arguments: str # JSON string of arguments
class ToolCall(BaseModel):
"""Represents a tool call requested by the LLM."""
id: str
type: str = "function"
function: ToolCallFunction
class ResponseMessage(BaseModel):
"""The message part of the response from the proxy."""
role: str = "assistant"
content: Optional[str] = None
tool_calls: Optional[List[ToolCall]] = None
class ProxyResponse(BaseModel):
"""Defines the final structured response sent back to the client."""
message: ResponseMessage