cloudflare.ts 1.2 KB

1234567891011121314151617181920212223242526
  1. export function cloudflareAIGatewayUrl(fetchUrl: string) {
  2. // rebuild fetchUrl, if using cloudflare ai gateway
  3. // document: https://developers.cloudflare.com/ai-gateway/providers/openai/
  4. const paths = fetchUrl.split("/");
  5. if ("gateway.ai.cloudflare.com" == paths[2]) {
  6. // is cloudflare.com ai gateway
  7. // https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/azure-openai/{resource_name}/{deployment_name}/chat/completions?api-version=2023-05-15'
  8. if ("azure-openai" == paths[6]) {
  9. // is azure gateway
  10. return paths.slice(0, 8).concat(paths.slice(-3)).join("/"); // rebuild ai gateway azure_url
  11. }
  12. // https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai/chat/completions
  13. if ("openai" == paths[6]) {
  14. // is openai gateway
  15. return paths.slice(0, 7).concat(paths.slice(-2)).join("/"); // rebuild ai gateway openai_url
  16. }
  17. // https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/anthropic/v1/messages \
  18. if ("anthropic" == paths[6]) {
  19. // is anthropic gateway
  20. return paths.slice(0, 7).concat(paths.slice(-2)).join("/"); // rebuild ai gateway anthropic_url
  21. }
  22. // TODO: Amazon Bedrock, Groq, HuggingFace...
  23. }
  24. return fetchUrl;
  25. }