用Cloudflare Workers彻底解决Flutter ChatBot不支持GitHub Models的Azure OpenAI模型的问题

本代码基于 @zhong_little 大佬的github models worker 分享 修改,解决了Flutter ChatBot不支持Azure OpenAI模型store参数的问题

user:addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  const { method, url } = request
  const originalUrl = new URL(url)

  if (!originalUrl.pathname.startsWith('/v1/')) {
    return new Response('Forbidden', { status: 403 })
  }

  const allowedMethods = ['OPTIONS', 'GET', 'POST']
  if (!allowedMethods.includes(method)) {
    return new Response('Method Not Allowed', { status: 405 })
  }

  if (method === 'OPTIONS') {
    return handleOptions()
  }

  const targetUrl = new URL(request.url)
  targetUrl.hostname = 'models.inference.ai.azure.com'
  targetUrl.pathname = originalUrl.pathname.replace(/^\/v1/, '')

  let newRequestBody = null
  if (method === 'POST') {
    const bodyJson = await request.json()
    
    if (bodyJson.store !== undefined) {
      delete bodyJson.store
    }
    newRequestBody = JSON.stringify(bodyJson)
  }

  const newRequest = new Request(targetUrl.toString(), {
    method: request.method,
    headers: request.headers,
    body: newRequestBody,
    redirect: 'follow'
  })

  try {
    const response = await fetch(newRequest)

    if (originalUrl.pathname === '/v1/models') {
      const data = await response.json()

      if (Array.isArray(data)) {
        let modifiedData = data.map(item => {
          const newItem = { ...item }
          if ('id' in newItem) {
            newItem.id_original = newItem.id
            delete newItem.id
          }
          if ('name' in newItem) {
            newItem.id = newItem.name
            delete newItem.name
          }
          return newItem
        })
        modifiedData = JSON.stringify(
          {'data': modifiedData}
        )
        
        return new Response(modifiedData, {
          status: response.status,
          statusText: response.statusText,
          headers: {
            ...response.headers,
            'Content-Type': 'application/json'
          }
        })
      } else {
        return response
      }
    }

    return response
  } catch (error) {
    return new Response('Internal Server Error', { status: 500 })
  }
}

function handleOptions() {
  return new Response(null, {
    status: 204,
    headers: {
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Methods': 'GET, POST, OPTIONS', 
      'Access-Control-Allow-Headers': 'Content-Type, Authorization',
      'Access-Control-Max-Age': '86400'
    }
  })
}
13 个赞

感谢分享大佬厉害啊

狠狠点赞了 可以onehub吗 都有啥模型

AI21-Jamba-1.5-Large, AI21-Jamba-1.5-Mini, Cohere-command-r, Cohere-command-r-08-2024, Cohere-command-r-plus, Cohere-command-r-plus-08-2024, Cohere-embed-v3-english, Cohere-embed-v3-multilingual, Llama-3.2-90B-Vision-Instruct, Llama-3.2-11B-Vision-Instruct, Meta-Llama-3.1-405B-Instruct, Meta-Llama-3.1-70B-Instruct, Meta-Llama-3.1-8B-Instruct, Meta-Llama-3-70B-Instruct, Meta-Llama-3-8B-Instruct, Mistral-large, Mistral-large-2407, Mistral-Nemo, Mistral-small, Ministral-3B, gpt-4o, gpt-4o-mini, text-embedding-3-large, text-embedding-3-small, Phi-3.5-MoE-instruct, Phi-3.5-vision-instruct, Phi-3.5-mini-instruct, Phi-3-medium-128k-instruct, Phi-3-medium-4k-instruct, Phi-3-mini-128k-instruct, Phi-3-mini-4k-instruct, Phi-3-small-128k-instruct, Phi-3-small-8k-instruct, jais-30b-chat

这么多? 就是GitHub 那个

有没有封号的风险

这边是v1/models刷的,实际上的数量比这个还多,我也搞不懂为什么微软这个模型列表,不是实时更新的

没有什么封号风险,它这个API本来国内的IP都能调用的,没啥限制。

我记得GitHub 的这个 onehub 项目适配了

这个oneAPI/newapi早都适配了,但是经过中转之后仍然不能在Flutter ChatBot使用,所以这次的主题是解决不能在Flutter ChatBot调用的问题。

1 个赞

佬友对chatbot情有独钟

太强了! @bbb 前来点赞

有点厉害支持~~