cursor icon indicating copy to clipboard operation
cursor copied to clipboard

Configure a BaseURL option for anthropic

Open seefs001 opened this issue 1 year ago • 8 comments

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

seefs001 avatar Aug 07 '24 07:08 seefs001

same problem

cuizheng0520 avatar Aug 09 '24 01:08 cuizheng0520

image 使用newapi来做中转,使用模型重定向到claude-3.5, 实测可行

jhs1873 avatar Aug 09 '24 03:08 jhs1873

image

使用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.

seefs001 avatar Aug 09 '24 03:08 seefs001

image 使用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.

requests always routed through the Cursor backend

jhs1873 avatar Aug 09 '24 03:08 jhs1873

image 使用newapi来做中转,使用模型重定向到claude-3.5, 实测可行

thanks,smart way

cuizheng0520 avatar Aug 09 '24 04:08 cuizheng0520

+1

edisonzf2020 avatar Aug 10 '24 08:08 edisonzf2020

#1647

labolado avatar Aug 20 '24 02:08 labolado

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'
      }
    })
  }
}

labolado avatar Sep 13 '24 04:09 labolado

image

使用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.

Hambaobao avatar Sep 19 '24 14:09 Hambaobao

https://makecoder.com/chat/share/dd48d1c6-ed4f-4bb3-8b96-96eb8fcb2874

  • 方案一:源码硬替换(Source Code Patching)
  • 方案二:网络劫持与中间人代理(DNS Spoofing + Reverse Proxy)
  • 方案三:利用 Cursor 的 "OpenAI" 兼容性 (The Switcheroo)

makecoderai avatar Nov 26 '25 14:11 makecoderai

any update?

naaive avatar Dec 16 '25 08:12 naaive

any update?

no way

makecoderai avatar Dec 16 '25 09:12 makecoderai