🪶 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

@ -53,4 +53,59 @@ describe('extractBaseURL', () => {
const url = 'https://open.ai/v1/hi/openai';
expect(extractBaseURL(url)).toBe('https://open.ai/v1/hi/openai');
});
test('should handle Azure OpenAI Cloudflare endpoint correctly', () => {
const url = 'https://gateway.ai.cloudflare.com/v1/account/gateway/azure-openai/completions';
expect(extractBaseURL(url)).toBe(
'https://gateway.ai.cloudflare.com/v1/account/gateway/azure-openai',
);
});
test('should include various suffixes in the extracted URL when present', () => {
const urls = [
'https://api.example.com/v1/azure-openai/something',
'https://api.example.com/v1/replicate/anotherthing',
'https://api.example.com/v1/huggingface/yetanotherthing',
'https://api.example.com/v1/workers-ai/differentthing',
'https://api.example.com/v1/aws-bedrock/somethingelse',
];
const expected = [
/* Note: exception for azure-openai to allow credential injection */
'https://api.example.com/v1/azure-openai/something',
'https://api.example.com/v1/replicate',
'https://api.example.com/v1/huggingface',
'https://api.example.com/v1/workers-ai',
'https://api.example.com/v1/aws-bedrock',
];
urls.forEach((url, index) => {
expect(extractBaseURL(url)).toBe(expected[index]);
});
});
test('should handle URLs with suffixes not immediately after /v1', () => {
const url = 'https://api.example.com/v1/some/path/azure-openai';
expect(extractBaseURL(url)).toBe('https://api.example.com/v1/some/path/azure-openai');
});
test('should handle URLs with complex paths after the suffix', () => {
const url = 'https://api.example.com/v1/replicate/deep/path/segment';
expect(extractBaseURL(url)).toBe('https://api.example.com/v1/replicate');
});
test('should leave a regular Azure OpenAI baseURL as is', () => {
const url = 'https://instance-name.openai.azure.com/openai/deployments/deployment-name';
expect(extractBaseURL(url)).toBe(url);
});
test('should leave a regular Azure OpenAI baseURL with placeholders as is', () => {
const url = 'https://${INSTANCE_NAME}.openai.azure.com/openai/deployments/${DEPLOYMENT_NAME}';
expect(extractBaseURL(url)).toBe(url);
});
test('should leave an alternate Azure OpenAI baseURL with placeholders as is', () => {
const url = 'https://${INSTANCE_NAME}.com/resources/deployments/${DEPLOYMENT_NAME}';
expect(extractBaseURL(url)).toBe(url);
});
});