fix: support Azure Cognitive Services and Azure OpenAI endpoints
Azure endpoints require two changes vs standard OpenAI:
1. Auth header: `api-key: {key}` instead of `Authorization: Bearer {key}`
2. URL path: `/openai/deployments/{model}/chat/completions?api-version={version}`
instead of `/chat/completions`
Detection is automatic when OPENAI_BASE_URL contains
`cognitiveservices.azure.com` or `openai.azure.com`.
The api-version defaults to `2024-12-01-preview` and can be overridden
via the AZURE_OPENAI_API_VERSION env var.
Handles all common Azure base URL formats:
- https://{resource}.cognitiveservices.azure.com/
- https://{resource}.cognitiveservices.azure.com/openai/v1
- https://{resource}.openai.azure.com/openai/v1
- https://{resource}.cognitiveservices.azure.com/openai/deployments/{model}/v1
Fixes #79
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -682,11 +682,40 @@ class OpenAIShimMessages {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const apiKey = process.env.OPENAI_API_KEY ?? ''
|
const apiKey = process.env.OPENAI_API_KEY ?? ''
|
||||||
|
const isAzure = /cognitiveservices\.azure\.com|openai\.azure\.com/.test(request.baseUrl)
|
||||||
|
|
||||||
if (apiKey) {
|
if (apiKey) {
|
||||||
headers.Authorization = `Bearer ${apiKey}`
|
if (isAzure) {
|
||||||
|
// Azure uses api-key header instead of Bearer token
|
||||||
|
headers['api-key'] = apiKey
|
||||||
|
} else {
|
||||||
|
headers.Authorization = `Bearer ${apiKey}`
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await fetch(`${request.baseUrl}/chat/completions`, {
|
// Build the chat completions URL
|
||||||
|
// Azure Cognitive Services / Azure OpenAI require a deployment-specific path
|
||||||
|
// and an api-version query parameter.
|
||||||
|
// Standard format: {base}/openai/deployments/{model}/chat/completions?api-version={version}
|
||||||
|
// Non-Azure: {base}/chat/completions
|
||||||
|
let chatCompletionsUrl: string
|
||||||
|
if (isAzure) {
|
||||||
|
const apiVersion = process.env.AZURE_OPENAI_API_VERSION ?? '2024-12-01-preview'
|
||||||
|
const deployment = request.resolvedModel ?? process.env.OPENAI_MODEL ?? 'gpt-4o'
|
||||||
|
// If base URL already contains /deployments/, use it as-is with api-version
|
||||||
|
if (/\/deployments\//i.test(request.baseUrl)) {
|
||||||
|
const base = request.baseUrl.replace(/\/+$/, '')
|
||||||
|
chatCompletionsUrl = `${base}/chat/completions?api-version=${apiVersion}`
|
||||||
|
} else {
|
||||||
|
// Strip trailing /v1 or /openai/v1 if present, then build Azure path
|
||||||
|
const base = request.baseUrl.replace(/\/(openai\/)?v1\/?$/, '').replace(/\/+$/, '')
|
||||||
|
chatCompletionsUrl = `${base}/openai/deployments/${deployment}/chat/completions?api-version=${apiVersion}`
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
chatCompletionsUrl = `${request.baseUrl}/chat/completions`
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await fetch(chatCompletionsUrl, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers,
|
headers,
|
||||||
body: JSON.stringify(body),
|
body: JSON.stringify(body),
|
||||||
|
|||||||
Reference in New Issue
Block a user