Configure a BaseURL option for anthropic
Is your feature request related to a problem? Please describe. Accessing anthropic's API directly in China is not feasible and requires a proxy to forward requests.
Describe the solution you'd like Configure a BaseURL option for anthropic
Additional context None
same problem
使用newapi来做中转,使用模型重定向到claude-3.5, 实测可行
使用newapi来做中转,使用模型重定向到claude-3.5, 实测可行
Currently, it is being used temporarily in this way, but through observation, there is suspicion that the software backend might have optimizations for specific model names. The redirected model seems unable to achieve the same effects as the official one.
使用newapi来做中转,使用模型重定向到claude-3.5, 实测可行
Currently, it is being used temporarily in this way, but through observation, there is suspicion that the software backend might have optimizations for specific model names. The redirected model seems unable to achieve the same effects as the official one.
使用newapi来做中转,使用模型重定向到claude-3.5, 实测可行
thanks,smart way
+1
#1647
Use Cloudflare Workers to proxy your API and alias the model name.
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
const BASE_URL = 'https://my_api_url'
const MODEL_ALIASES = {
'mymodel': 'claude-3-5-sonnet-20240620'
}
async function handleRequest(request) {
const url = new URL(request.url)
const path = url.pathname
const queryParams = url.search
console.log(`Received request: ${request.method} ${path}`)
if (request.method === 'GET' && path === '/') {
return new Response("Claude API Proxy is running!", { status: 200 })
}
if ((request.method === 'POST' && path === '/v1/chat/completions') ||
(request.method === 'GET' && path === '/v1/models')) {
return await proxyRequest(request, path, queryParams)
}
console.log(`Unhandled route: ${path}`)
return new Response(JSON.stringify({ error: 'Not Found', path: path }), {
status: 404,
headers: { 'Content-Type': 'application/json' }
})
}
async function proxyRequest(request, path, queryParams) {
const authHeader = request.headers.get('Authorization')
if (!authHeader || !authHeader.startsWith('Bearer ')) {
console.log('Missing or invalid Authorization header')
return new Response(JSON.stringify({ error: 'Valid Authorization header is required' }), {
status: 401,
headers: { 'Content-Type': 'application/json' }
})
}
const apiKey = authHeader.split(' ')[1]
const url = BASE_URL + path + queryParams
const headers = {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
}
let body = request.body
if (path === '/v1/chat/completions' && request.method === 'POST') {
const requestData = await request.json()
console.log(`Request data: ${JSON.stringify(requestData)}`)
if (requestData.model && MODEL_ALIASES[requestData.model]) {
requestData.model = MODEL_ALIASES[requestData.model]
}
body = JSON.stringify(requestData)
}
try {
console.log(`Sending request to: ${url}`)
const response = await fetch(url, {
method: request.method,
headers: headers,
body: body
})
const responseData = await response.text()
console.log(`Received response: ${responseData}`)
return new Response(responseData, {
status: response.status,
headers: {
'Content-Type': 'application/json'
}
})
} catch (error) {
console.error(`Error: ${error.message}`)
return new Response(JSON.stringify({ error: error.message }), {
status: 500,
headers: {
'Content-Type': 'application/json'
}
})
}
}
使用newapi来做中转,使用模型重定向到claude-3.5, 实测可行
Currently, it is being used temporarily in this way, but through observation, there is suspicion that the software backend might have optimizations for specific model names. The redirected model seems unable to achieve the same effects as the official one.
You are absolutely right. I have the same observation.
https://makecoder.com/chat/share/dd48d1c6-ed4f-4bb3-8b96-96eb8fcb2874
- 方案一:源码硬替换(Source Code Patching)
- 方案二:网络劫持与中间人代理(DNS Spoofing + Reverse Proxy)
- 方案三:利用 Cursor 的 "OpenAI" 兼容性 (The Switcheroo)
any update?
any update?
no way
使用newapi来做中转,使用模型重定向到claude-3.5, 实测可行
使用newapi来做中转,使用模型重定向到claude-3.5, 实测可行