该组件为基于 Go 构建的高性能 HTTP 反向代理,专用于将客户端请求透明转发至兼容 OpenAI 或 Anthropic 协议的上游服务。在转发的同时,代理以无侵入方式完整捕获每次会话的请求与响应,包括流式传输中产生的所有数据分片,并以 JSONL 格式实时持久化至本地存储。这一机制使每一轮交互均可被精确追溯,从而满足合规审计、行为分析及训练语料构建等需求。系统还提供可选的按日历日合并导出功能,可将当日全部会话记录重组织为统一的行式数据集,直接服务于监督微调、偏好对齐等训练数据的准备工作
本文档基于业界主流 LLM 代理服务(Ollama、vLLM、llama.cpp、LiteLLM、Open WebUI)的功能对标,详细列出 proxy-llm 当前缺失或需要完善的功能模块。
现状:当前项目仅通过请求体中的 model 字段选择后端,不支持同一模型的多节点负载分担。
竞品参考:
需要实现的功能:
# 建议的配置格式
load_balancing:
strategy: "round_robin" # round_robin, least_connections, weighted, random
health_check_interval: 30s
failover_timeout: 10s
nodes:
- url: "http://backend1:8080"
weight: 1
priority: 1
- url: "http://backend2:8080"
weight: 1
priority: 2 # 备用节点
实现要点:
现状:配置中有 max_retries 字段,但代理层未实现重试逻辑。
需要实现的功能:
retry_delay = base_delay * 2^attempt现状:proxy.rate_limit 配置项存在,值为 0(无限制),但代码中无任何限流逻辑。
竞品参考:
需要实现的功能:
rate_limiting:
global:
requests_per_minute: 100
tokens_per_minute: 50000
per_user:
enabled: true
requests_per_minute: 30
tokens_per_minute: 10000
per_model:
enabled: true
gpt-4:
requests_per_minute: 50
claude-3:
requests_per_minute: 40
strategies:
overflow: "reject" # reject, queue, fallback
queue_max_size: 1000
queue_timeout: 30s
实现要点:
Retry-After 头现状:HTTP Transport 配置了 MaxIdleConns 和 MaxIdleConnsPerHost,但无并发请求限制。
需要实现的功能:
现状:每次请求都会转发到上游,无任何缓存机制。
竞品参考:
需要实现的功能:
cache:
enabled: true
backend: "memory" # memory, redis, memcached
redis:
url: "redis://localhost:6379"
db: 0
ttl: 3600 # 秒
max_size: 10000 # 条目数
key_generation:
# 缓存键生成策略:基于请求内容
include_headers: ["Authorization"]
normalize: true # 归一化请求参数
scope:
cache_by_model: true
cache_by_system_prompt: true
cache_by_tools: true
hit_response:
headers:
X-Cache: "HIT"
latency_improvement_tracking: true
缓存键生成逻辑:
model + messages + system_prompt + temperature + max_tokensstream 参数(缓存原始结果)request_id 等唯一标识命中缓存时:
X-Cache: HIT 响应头现状:每次请求从头开始生成。
需要实现的功能:
--context 参数支持)现状:仅支持简单的 Bearer Token 认证,不支持 OAuth、API Key 轮换等。
需要实现的功能:
auth:
enabled: true
providers:
- type: "api_key"
header: "X-API-Key"
keys:
- key: "${API_KEY_1}"
user_id: "user_1"
permissions: ["chat", "completions"]
rate_limit: 100
- key: "${API_KEY_2}"
user_id: "user_2"
permissions: ["chat"]
rate_limit: 50
- type: "jwt"
secret: "${JWT_SECRET}"
issuer: "proxy-llm"
audience: "clients"
expiration: 24h
- type: "oauth2"
provider: "github" # github, google, oidc
client_id: "${OAUTH_CLIENT_ID}"
client_secret: "${OAUTH_CLIENT_SECRET}"
scopes: ["openid", "profile", "email"]
mfa:
enabled: false
provider: "totp" # totp, sms
现状:请求体直接透传,无输入验证。
需要实现的功能:
现状:请求/响应被记录到 JSONL,但无独立的安全审计日志。
需要实现的功能:
现状:有 request_id 和 call_id,但未集成分布式追踪。
需要实现的功能:
tracing:
enabled: true
provider: "opentelemetry" # opentelemetry, jaeger, zipkin
opentelemetry:
endpoint: "http://otel-collector:4317"
sampling_rate: 0.1
resource_attributes:
service.name: "proxy-llm"
service.version: "1.0.0"
span_attributes:
include_request_id: true
include_model: true
include_tokens: true
追踪维度:
现状:基本的请求计数和延迟指标。
需要补充的指标:
| 指标类型 | 指标名称 | 说明 |
|---|---|---|
| 缓存 | cache_hits_total |
缓存命中次数 |
| 缓存 | cache_misses_total |
缓存未命中次数 |
| 缓存 | cache_hit_ratio |
缓存命中率 |
| 速率限制 | rate_limit_rejected_total |
被速率限制拒绝的请求数 |
| 重试 | retries_total |
重试次数 |
| 重试 | retries_by_error_total |
按错误类型统计的重试 |
| 并发 | active_requests_gauge |
当前活跃请求数 |
| 连接 | upstream_connection_errors_total |
上游连接错误数 |
| 流式 | streaming_duration_seconds |
流式响应持续时间 |
| 格式转换 | format_conversion_errors_total |
格式转换错误数 |
现状:仅返回 OK 字符串。
需要实现的功能:
health:
endpoints:
- path: "/health"
type: "liveness"
- path: "/ready"
type: "readiness"
- path: "/health/verbose"
type: "verbose"
upstream:
check_interval: 60s
timeout: 10s
failure_threshold: 3
success_threshold: 2
verbose:
show_upstream_status: true
show_cache_status: true
show_rate_limit_status: true
就绪检查:
现状:SSE 流直接透传,无缓冲控制。
需要实现的功能:
现状:流式数据直接转发。
需要实现的功能:
X-Stream-Status)现状:仅支持本地文件系统 JSONL 存储。
需要实现的功能:
storage:
backend: "filesystem" # filesystem, s3, postgres, elasticsearch, mongodb
filesystem:
directory: "./data"
compress: true
rotate: "daily"
s3:
bucket: "llm-data"
region: "us-east-1"
prefix: "logs/"
credentials:
access_key: "${AWS_ACCESS_KEY_ID}"
secret_key: "${AWS_SECRET_ACCESS_KEY}"
elasticsearch:
url: "http://localhost:9200"
index: "proxy-llm-{date}"
bulk_size: 1000
flush_interval: 5s
postgres:
dsn: "${DATABASE_URL}"
table: "request_logs"
batch_size: 500
flush_interval: 10s
现状:通过 rotate 按日期组织,但无自动清理。
需要实现的功能:
现状:每日导出为单一 JSONL 文件。
需要实现的功能:
现状:模型配置需要在启动时静态配置。
需要实现的功能:
现状:简单通过 model 字段匹配。
需要实现的功能:
model_routing:
enabled: true
strategies:
- name: "cost_optimized"
description: "根据成本选择最优模型"
rules:
- condition: "prompt_tokens < 1000"
prefer: "fast_cheaper_model"
- condition: "requires_reasoning"
prefer: "strong_reasoning_model"
- name: "latency_optimized"
description: "根据延迟要求选择模型"
rules:
- condition: "max_latency < 1s"
prefer: "fastest_model"
fallback:
enabled: true
default_model: "fallback_model"
retry_on_failure: true
现状:无模型性能对比。
需要实现的功能:
现状:仅有 CORS 和认证中间件。
需要实现的功能:
middleware:
- name: "request_modifier"
enabled: true
config:
add_headers:
X-Proxy-Version: "1.0.0"
modify_model:
rules:
- pattern: "gpt-4-turbo"
replace: "gpt-4-1106-preview"
- name: "response_transformer"
enabled: true
config:
strip_fields: ["_meta", "debug_info"]
add_fields:
proxy_timestamp: "{now}"
- name: "usage_analytics"
enabled: true
config:
batch_size: 100
flush_interval: 30s
destination: "clickhouse"
- name: "content_filter"
enabled: true
config:
provider: "custom" # custom, openai_moderation, together_ai
model: "text-moderation-stable"
action: "block" # block, warn, replace
现状:无事件钩子。
需要实现的功能:
before_request:请求前处理(修改、验证、缓存检查)after_request:请求后处理(日志、通知、缓存存储)on_error:错误处理钩子on_stream_chunk:流式分块处理钩子现状:/usage 和 /api/usage/summary 提供基本的 Token 使用统计。
需要补充的功能:
| 功能 | 优先级 | 说明 |
|---|---|---|
| 实时请求监控 | P0 | 显示当前正在处理的请求 |
| 请求详情查看 | P0 | 点击查看完整请求/响应 |
| 模型性能对比 | P1 | 不同模型的延迟/质量对比 |
| 费用趋势图 | P1 | 按时间维度的费用趋势 |
| 异常检测 | P1 | 自动检测异常请求模式 |
| 用户行为分析 | P2 | 不同用户的使用模式 |
| 模型推荐 | P2 | 基于使用场景推荐模型 |
| 请求回放 | P2 | 重新发送历史请求 |
现状:单实例设计,无集群支持。
需要实现的功能:
现状:无 K8s 原生支持。
需要实现的功能:
现状:配置修改需要重启服务。
需要实现的功能:
现状:每个模型创建独立的 HTTP Client,但无连接池监控。
需要实现的功能:
现状:请求体完整读入内存。
需要实现的功能:
现状:日志写入为同步操作。
需要实现的功能:
现状:支持 TLS 但配置简单。
需要实现的功能:
现状:存储的数据无加密。
需要实现的功能:
现状:无标准可观测性集成。
需要实现的功能:
现状:支持 OpenAI 和 Anthropic 格式。
需要补充的功能:
| 功能模块 | 优先级 | 工作量估计 | 业务价值 |
|---|---|---|---|
| 速率限制实现 | P0 | 中 | 高 |
| 响应缓存 | P0 | 高 | 高 |
| 分布式追踪 | P1 | 中 | 高 |
| 指标增强 | P1 | 低 | 高 |
| 输入验证 | P1 | 中 | 高 |
| 存储后端多样化 | P1 | 高 | 中 |
| 模型路由策略 | P2 | 高 | 中 |
| 插件系统 | P2 | 高 | 中 |
| 多实例协调 | P2 | 高 | 中 |
| Kubernetes 支持 | P2 | 中 | 中 |
| KV Cache 复用 | P3 | 高 | 低 |
| 流式预处理 | P3 | 中 | 低 |
| 功能 | proxy-llm | LiteLLM | Ollama | vLLM | Open WebUI |
|---|---|---|---|---|---|
| 多提供商支持 | ✅ | ✅ | ❌ | ❌ | ✅ |
| 格式转换 | ✅ | ✅ | ❌ | ❌ | ✅ |
| 流式处理 | ✅ | ✅ | ✅ | ✅ | ✅ |
| 速率限制 | ❌ | ✅ | ❌ | ❌ | ✅ |
| 响应缓存 | ❌ | ❌ | ✅ | ✅ | ✅ |
| 负载均衡 | ❌ | ✅ | ❌ | ✅ | ❌ |
| 分布式追踪 | ❌ | ✅ | ❌ | ❌ | ❌ |
| 认证体系 | 基础 | ✅ | ❌ | ❌ | ✅ |
| 数据导出 | ✅ | ✅ | ❌ | ❌ | ✅ |
| Web UI | 基础 | ❌ | ❌ | ❌ | ✅ |
| 插件系统 | ❌ | ✅ | ❌ | ❌ | ❌ |
| Kubernetes | ❌ | ✅ | ❌ | ✅ | ❌ |