From 52fa56c1daad158677d03f533536c2404cca7ad2 Mon Sep 17 00:00:00 2001 From: Othman El houfi Date: Wed, 17 May 2023 18:04:06 +0200 Subject: [PATCH] Azure OpenAI API Fix (#297) * Azure OpenAI API Fix * improve code * improve code --------- Co-authored-by: Jing Hua --- src/api/api.ts | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/src/api/api.ts b/src/api/api.ts index 0989207..20073cf 100644 --- a/src/api/api.ts +++ b/src/api/api.ts @@ -14,7 +14,24 @@ export const getChatCompletion = async ( ...customHeaders, }; if (apiKey) headers.Authorization = `Bearer ${apiKey}`; - if (isAzureEndpoint(endpoint) && apiKey) headers['api-key'] = apiKey; + + if (isAzureEndpoint(endpoint) && apiKey) { + headers['api-key'] = apiKey; + + const gpt3forAzure = 'gpt-35-turbo'; + const model = + config.model === 'gpt-3.5-turbo' ? gpt3forAzure : config.model; + const apiVersion = '2023-03-15-preview'; + + const path = `openai/deployments/${model}/chat/completions?api-version=${apiVersion}`; + + if (!endpoint.endsWith(path)) { + if (!endpoint.endsWith('/')) { + endpoint += '/'; + } + endpoint += path; + } + } const response = await fetch(endpoint, { method: 'POST', @@ -43,7 +60,24 @@ export const getChatCompletionStream = async ( ...customHeaders, }; if (apiKey) headers.Authorization = `Bearer ${apiKey}`; - if (isAzureEndpoint(endpoint) && apiKey) headers['api-key'] = apiKey; + + if (isAzureEndpoint(endpoint) && apiKey) { + headers['api-key'] = apiKey; + + const gpt3forAzure = 'gpt-35-turbo'; + const model = + config.model === 'gpt-3.5-turbo' ? gpt3forAzure : config.model; + const apiVersion = '2023-03-15-preview'; + + const path = `openai/deployments/${model}/chat/completions?api-version=${apiVersion}`; + + if (!endpoint.endsWith(path)) { + if (!endpoint.endsWith('/')) { + endpoint += '/'; + } + endpoint += path; + } + } const response = await fetch(endpoint, { method: 'POST',