逆向百度文心助手 (chat.baidu.com) 的纯算法实现,支持 OpenAI 兼容 API。
POST https://chat.baidu.com/aichat/api/conversationtext/event-stream (SSE)aiTabFrameBaseData JSON 提取chat_token = base64("{token}|{MD5(query)}|{timestamp}|{lid}")-{lid}-3
token 和 lid 从页面初始化数据获取MD5 使用标准 spark-md5 算法对查询字符串哈希timestamp 为当前毫秒时间戳| OpenAI 模型名 | Baidu 模型 | 说明 |
|---|---|---|
baidu-smart |
smartMode |
默认智能模式 |
baidu-deepseek |
deepseek |
DeepSeek 深度思考 |
baidu-ds-v4 |
ds-v4 |
DeepSeek-V4 Pro |
deep_search=True 启用深度思考thinking / thinking_content / reasoning 字段reasoning_content 字段tools 后,服务端会自动追加系统提示词<tool_calls>
<tool_call>
<name>get_weather</name>
<arguments>{"city":"北京"}</arguments>
</tool_call>
</tool_calls>
message.tool_calls├── baidu_chat.py # 核心逆向客户端 (纯算法)
├── main.py # OpenAI 兼容 API 服务器
├── config.toml # 配置文件
├── requirements.txt # 依赖
└── config/ # 逆向分析产物 (JS 源码等)
pip install -r requirements.txt
编辑 config.toml,填入从浏览器复制的 Cookie:
[cookies]
value = "BAIDUID=xxx; BIDUPSID=xxx; ..."
python main.py --config config.toml
# 或指定端口
python main.py --port 8000
公网部署时建议在 config.toml 配置自定义密钥:
[auth]
api_keys = ["sk-your-secret-key"]
配置后,请求必须携带:
-H "Authorization: Bearer sk-your-secret-key"
Docker 一键部署:
docker compose up -d --build
多 Cookie 负载均衡:
[cookies]
values = [
"BAIDUID=xxx; BIDUPSID=xxx; ...",
"BAIDUID=yyy; BIDUPSID=yyy; ..."
]
服务端会为每个 Cookie 建立独立客户端,并按当前并发数均衡分发。某个 Cookie 不可用时会立即刷新当前会话并重试,仍失败则自动切到下一个 Cookie。
上下文压缩与新窗口:
[context]
fresh_conversation = true
max_chars = 12000
max_messages = 16
max_message_chars = 2000
每次请求都会使用新的百度对话窗口;OpenAI messages 会在本地压缩后作为单次 prompt 发送,避免百度侧上下文串线。
curl http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-your-secret-key" \
-d '{
"model": "baidu-deepseek",
"messages": [{"role": "user", "content": "1+1等于几"}],
"stream": true
}'
curl http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-your-secret-key" \
-d '{
"model": "baidu-smart",
"messages": [{"role": "user", "content": "北京天气怎么样"}],
"tools": [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get weather by city",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"]
}
}
}],
"stream": false
}'
返回中的工具调用:
{
"choices": [{
"message": {
"role": "assistant",
"content": "",
"tool_calls": [{
"id": "call_xxx",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"city\":\"北京\"}"
}
}]
},
"finish_reason": "tool_calls"
}]
}
python baidu_chat.py "1+1等于几" --model deepseek
python baidu_chat.py "hello" --model smart
aichat/api/conversation SSE 接口search-js.js, chat-main-pc.js, common.js, vendors.js 等 Vite 构建产物chat_token 格式和参数结构getToken$1 函数,确认使用 spark-md5 进行标准 MD5 哈希token 和 lid 存储在页面 HTML 的 <script name="aiTabFrameBaseData"> 中base64(token_val | MD5(query_str) | timestamp | lid) - lid - 3usedModel.modelName 和 isDeepseek header 控制basedata (初始数据), ping (心跳), message (内容块)token 和 lid 在会话期间有效,过期后需重新获取页面本项目仅供学习交流使用,请遵守百度相关服务条款。不得用于商业用途或非法用途。
$ claude mcp add baiduchat2api \
-- python -m otcore.mcp_server <graph>