第一个版本

This commit is contained in:
2026-01-11 04:17:53 +08:00
commit c160320892
11 changed files with 2383 additions and 0 deletions

220
README.md Normal file
View File

@@ -0,0 +1,220 @@
# LLM Proxy - OpenAI API 代理和训练数据收集工具
一个透明的 HTTP 代理服务器,用于拦截和保存 LLM API 请求,自动导出为 JSONL 格式的训练数据。
## 功能特性
-**透明代理**:拦截所有 `/v1/` 开头的 LLM API 请求
-**零配置**:无需在代理中配置 API Key直接使用客户端的 Key
-**多提供商支持**:支持 OpenAI、Anthropic、GLM、OpenRouter 等所有 OpenAI 兼容的 API
-**智能解析**:自动识别和解析 LLM 请求,忽略其他请求
-**思考过程保存**自动保存模型的推理内容reasoning
-**多轮对话支持**:完整保存对话上下文
-**JSONL 导出**:一键导出为标准训练数据格式
-**SQLite 存储**:轻量级数据库,无需额外配置
## 安装
### 1. 克隆项目
```bash
git clone https://github.com/mitmproxy/mitmproxy.git
cd mitmproxy
```
### 2. 安装依赖
```bash
pip install -r requirements.txt
```
## 使用方法
### 启动代理服务器
```bash
python start_proxy.py
```
默认监听 `127.0.0.1:8080`
### 配置系统代理
#### Windows
1. 打开"设置" → "网络和 Internet" → "代理"
2. 开启"使用代理服务器"
3. 地址:`127.0.0.1`
4. 端口:`8080`
#### macOS
```bash
networksetup -setwebproxy Wi-Fi 127.0.0.1 8080
networksetup -setsecurewebproxy Wi-Fi 127.0.0.1 8080
```
#### Linux
在浏览器或系统设置中配置 HTTP/HTTPS 代理为 `127.0.0.1:8080`
### 使用客户端
#### Trae
1. 启动代理服务器
2. 配置系统代理(见上)
3. 在 Trae 中正常使用,配置任何 API 提供商和 Key
4. 所有请求自动被拦截和保存
#### CherryStudio
**方法 1配置自定义提供商**
1. 打开 CherryStudio 设置 → 模型服务
2. 添加自定义提供商
3. API 地址:`http://127.0.0.1:8080/v1`
4. API Key任意值代理会忽略
5. 添加你使用的模型
**方法 2使用系统代理**
1. 启动代理服务器
2. 配置系统代理(见上)
3. 在 CherryStudio 中正常使用
### 导出训练数据
```bash
# 导出所有对话(包含思考过程)
python export.py
# 导出指定文件
python export.py --output my_data.jsonl
# 不包含思考过程
python export.py --no-reasoning
# 包含元数据
python export.py --with-metadata
# 查看数据库统计
python export.py --stats
```
## 配置文件
编辑 `config.json` 来自定义配置:
```json
{
"proxy": {
"listen_port": 8080,
"listen_host": "127.0.0.1"
},
"database": {
"path": "llm_data.db"
},
"filter": {
"enabled": true,
"path_patterns": ["/v1/"],
"save_all_requests": false
},
"export": {
"output_dir": "exports",
"include_reasoning": true,
"include_metadata": false
}
}
```
## JSONL 格式
导出的 JSONL 文件格式:
```jsonl
{"messages": [{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Hello!"}, {"role": "assistant", "content": "Hi there!", "reasoning": "The user greeted me, so I should respond politely."}]}
{"messages": [{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "What is 2+2?"}, {"role": "assistant", "content": "2+2 equals 4.", "reasoning": "This is a simple arithmetic problem. 2+2 = 4."}]}
```
## 数据库结构
### conversations 表
- `id`: 主键
- `conversation_id`: 对话 ID
- `created_at`: 创建时间
- `updated_at`: 更新时间
### requests 表
- `id`: 主键
- `request_id`: 请求 ID
- `conversation_id`: 对话 ID外键
- `model`: 模型名称
- `messages`: 消息列表JSON
- `request_body`: 完整请求体JSON
- `created_at`: 创建时间
### responses 表
- `id`: 主键
- `request_id`: 请求 ID外键
- `response_body`: 完整响应体JSON
- `reasoning_content`: 思考过程
- `tokens_used`: 使用的 token 数量
- `created_at`: 创建时间
## 工作原理
1. **拦截请求**:代理拦截所有 `/v1/` 开头的 HTTP 请求
2. **智能解析**:尝试解析请求体,识别是否为 LLM API 请求
3. **保存请求**:将请求信息保存到 SQLite 数据库
4. **透明转发**:保持原始 Authorization header转发到目标服务器
5. **保存响应**:接收响应后,保存响应内容和思考过程
6. **导出数据**:随时导出为 JSONL 格式用于训练
## 注意事项
### HTTPS 证书
如果客户端使用 HTTPS 连接到 API`https://api.openai.com`),需要:
1. 安装 mitmproxy 证书到系统信任库
2. 或者在客户端配置中使用 HTTP`http://api.openai.com`
### 证书安装
首次运行代理时mitmproxy 会生成证书:
- Windows: `%USERPROFILE%\.mitmproxy\mitmproxy-ca-cert.pem`
- macOS/Linux: `~/.mitmproxy/mitmproxy-ca-cert.pem`
将证书安装到系统信任库即可。
### 隐私和安全
- 代理不会保存 API Key
- 所有数据存储在本地 SQLite 数据库
- 请妥善保管导出的训练数据
## 故障排除
### 请求没有被拦截
1. 检查系统代理是否正确配置
2. 检查代理服务器是否正在运行
3. 检查请求路径是否包含 `/v1/`
### HTTPS 请求失败
1. 安装 mitmproxy 证书到系统信任库
2. 或者在客户端配置中使用 HTTP 而不是 HTTPS
### 数据库错误
1. 检查数据库文件权限
2. 删除 `llm_data.db` 重新初始化
## 许可证
MIT License
## 贡献
欢迎提交 Issue 和 Pull Request