前言
由于 Claude Code 和 Qwen 的请求格式有点不兼容,需要另外写一个代理来确保正常地请求
使用代理拦截和修改请求
javascript
const http = require('http');
const https = require('https');
const targetHost = 'dashscope.aliyuncs.com';
const targetPath = '/apps/anthropic/v1/messages';
const server = http.createServer((req, res) => {
let body = '';
req.on('data', chunk => { body += chunk; });
req.on('end', () => {
try {
const originalReq = JSON.parse(body);
// 修改 max_tokens
if (originalReq.max_tokens > 8192) {
originalReq.max_tokens = 8192;
}
// 确保 top_p 等参数也在范围内
if (originalReq.top_p && originalReq.top_p > 1) {
originalReq.top_p = 0.95;
}
const options = {
hostname: targetHost,
path: targetPath,
method: 'POST',
headers: {
...req.headers,
'host': targetHost,
'content-length': Buffer.byteLength(JSON.stringify(originalReq))
}
};
const proxyReq = https.request(options, (proxyRes) => {
res.writeHead(proxyRes.statusCode, proxyRes.headers);
proxyRes.pipe(res);
});
proxyReq.write(JSON.stringify(originalReq));
proxyReq.end();
} catch(e) {
console.error('Proxy error:', e);
res.writeHead(500);
res.end('Proxy error');
}
});
});
server.listen(8080, () => {
console.log('Proxy running on http://localhost:8080');
});
然后修改 settings.json:
json
{
"env": {
"ANTHROPIC_AUTH_TOKEN": "你的apikey",
"ANTHROPIC_BASE_URL": "http://localhost:8080",
"ANTHROPIC_MODEL": "qwen-max"
}
}
先用 node 运行 proxy.js 最后再启动 claude 就可以用了