新增功能:
- 实现 ResponseParser 模块,支持解析 LLM 响应中的工具调用
- 支持双花括号格式的工具调用 {{...}}
- 工具调用智能解析,处理嵌套 JSON 结构
- 生成符合 OpenAI 规范的 tool_call ID
- 完善的数据库日志记录功能
核心特性:
- 低耦合高内聚的架构设计
- 完整的单元测试覆盖(23个测试全部通过)
- 100% 兼容 OpenAI REST API tools 字段行为
- 支持流式和非流式响应
- 支持 content + tool_calls 混合响应
技术实现:
- response_parser.py: 响应解析器模块
- services.py: 业务逻辑层(工具注入、响应处理)
- models.py: 数据模型定义
- main.py: API 端点和请求处理
- database.py: SQLite 数据库操作
测试覆盖:
- 工具调用解析(各种格式)
- 流式响应处理
- 原生 OpenAI 格式支持
- 边缘情况处理
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
48 lines
1.4 KiB
Python
48 lines
1.4 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: str
|
|
|
|
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
|