🪶 feat: Add Support for Azure OpenAI Base URL (#1596)

* refactor(extractBaseURL): add handling for all possible Cloudflare AI Gateway endpoints

* chore: added endpointoption todo for updating type and optimizing handling app-wide

* feat(azureUtils):
- `genAzureChatCompletion`: allow optional client pass to update azure property
- `constructAzureURL`: optionally replace placeholders for instance and deployment names of an azure baseURL
- add tests for module

* refactor(extractBaseURL): return entire input when cloudflare `azure-openai` suffix detected
- also add more tests for both construct and extract URL

* refactor(genAzureChatCompletion): only allow omitting instance name if baseURL is not set

* refactor(initializeClient): determine `reverseProxyUrl` based on endpoint (azure or openai)

* refactor: utitlize `constructAzureURL` when `AZURE_OPENAI_BASEURL` is set

* docs: update docs on `AZURE_OPENAI_BASEURL`

* fix(ci): update expected error message for `azureUtils` tests
This commit is contained in:
Danny Avila 2024-01-19 14:57:03 -05:00 committed by GitHub
parent 5c94f5330a
commit e73608ba46
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 532 additions and 47 deletions

View file

@ -1,13 +1,15 @@
/**
* Extracts a valid OpenAI baseURL from a given string, matching "url/v1," also an added suffix,
* ending with "/openai" (to allow the Cloudflare, LiteLLM pattern).
* Returns the original URL if no match is found.
* Extracts a valid OpenAI baseURL from a given string, matching "url/v1," followed by an optional suffix.
* The suffix can be one of several predefined values (e.g., 'openai', 'azure-openai', etc.),
* accommodating different proxy patterns like Cloudflare, LiteLLM, etc.
* Returns the original URL if no valid pattern is found.
*
* Examples:
* - `https://open.ai/v1/chat` -> `https://open.ai/v1`
* - `https://open.ai/v1/chat/completions` -> `https://open.ai/v1`
* - `https://open.ai/v1/ACCOUNT/GATEWAY/openai/completions` -> `https://open.ai/v1/ACCOUNT/GATEWAY/openai`
* - `https://gateway.ai.cloudflare.com/v1/account/gateway/azure-openai/completions` -> `https://gateway.ai.cloudflare.com/v1/account/gateway/azure-openai`
* - `https://open.ai/v1/hi/openai` -> `https://open.ai/v1/hi/openai`
* - `https://api.example.com/v1/replicate` -> `https://api.example.com/v1/replicate`
*
* @param {string} url - The URL to be processed.
* @returns {string} The matched pattern or input if no match is found.
@ -23,8 +25,27 @@ function extractBaseURL(url) {
// Extract the part of the URL up to and including '/v1'.
let baseUrl = url.substring(0, v1Index + 3);
const openai = 'openai';
// Find which suffix is present.
const suffixes = [
'azure-openai',
openai,
'replicate',
'huggingface',
'workers-ai',
'aws-bedrock',
];
const suffixUsed = suffixes.find((suffix) => url.includes(`/${suffix}`));
if (suffixUsed === 'azure-openai') {
return url.split(/\/(chat|completion)/)[0];
}
// Check if the URL has '/openai' immediately after '/v1'.
const openaiIndex = url.indexOf('/openai', v1Index + 3);
const openaiIndex = url.indexOf(`/${openai}`, v1Index + 3);
// Find which suffix is present in the URL, if any.
const suffixIndex =
suffixUsed === openai ? openaiIndex : url.indexOf(`/${suffixUsed}`, v1Index + 3);
// If '/openai' is found right after '/v1', include it in the base URL.
if (openaiIndex === v1Index + 3) {
@ -37,9 +58,9 @@ function extractBaseURL(url) {
// If there is a next slash, the base URL goes up to but not including the slash.
baseUrl = url.substring(0, nextSlashIndex);
}
} else if (openaiIndex > 0) {
// If '/openai' is present but not immediately after '/v1', we need to include the reverse proxy pattern.
baseUrl = url.substring(0, openaiIndex + 7);
} else if (suffixIndex > 0) {
// If a suffix is present but not immediately after '/v1', we need to include the reverse proxy pattern.
baseUrl = url.substring(0, suffixIndex + suffixUsed.length + 1);
}
return baseUrl;