diff --git a/src/api/api.ts b/src/api/api.ts index 505fd4b37..94a461a33 100644 --- a/src/api/api.ts +++ b/src/api/api.ts @@ -2,6 +2,36 @@ import { ShareGPTSubmitBodyInterface } from '@type/api'; import { ConfigInterface, MessageInterface, ModelOptions } from '@type/chat'; import { isAzureEndpoint } from '@utils/api'; +const modifyEndpointForAzure = ( + endpoint: string, + config: ConfigInterface, + apiKey?: string +): string => { + const pathPattern = new RegExp( + 'openai\\/deployments\\/.+\\/chat\\/completions\\?api-version=.+$' + ); + if (!pathPattern.test(endpoint)) { + const modelMapping: Partial> = { + 'gpt-3.5-turbo': 'gpt-35-turbo', + 'gpt-3.5-turbo-16k': 'gpt-35-turbo-16k', + 'gpt-3.5-turbo-1106': 'gpt-35-turbo-1106', + 'gpt-3.5-turbo-0125': 'gpt-35-turbo-0125', + }; + + const model = modelMapping[config.model] || config.model; + const apiVersion = '2024-02-15-preview' + const path = `openai/deployments/${model}/chat/completions?api-version=${apiVersion}`; + + if (!endpoint.endsWith(path)) { + return endpoint.endsWith('/') + ? `${endpoint}${path}` + : `${endpoint}/${path}`; + } + } + + return endpoint; +}; + export const getChatCompletion = async ( endpoint: string, messages: MessageInterface[], @@ -17,30 +47,7 @@ export const getChatCompletion = async ( if (isAzureEndpoint(endpoint) && apiKey) { headers['api-key'] = apiKey; - - const modelmapping: Partial> = { - 'gpt-3.5-turbo': 'gpt-35-turbo', - 'gpt-3.5-turbo-16k': 'gpt-35-turbo-16k', - 'gpt-3.5-turbo-1106': 'gpt-35-turbo-1106', - 'gpt-3.5-turbo-0125': 'gpt-35-turbo-0125', - }; - - const model = modelmapping[config.model] || config.model; - - // set api version to 2023-07-01-preview for gpt-4 and gpt-4-32k, otherwise use 2023-03-15-preview - const apiVersion = - model === 'gpt-4' || model === 'gpt-4-32k' - ? '2023-07-01-preview' - : '2023-03-15-preview'; - - const path = `openai/deployments/${model}/chat/completions?api-version=${apiVersion}`; - - if (!endpoint.endsWith(path)) { - if (!endpoint.endsWith('/')) { - endpoint += '/'; - } - endpoint += path; - } + endpoint = modifyEndpointForAzure(endpoint, config, apiKey); } const response = await fetch(endpoint, { @@ -73,27 +80,7 @@ export const getChatCompletionStream = async ( if (isAzureEndpoint(endpoint) && apiKey) { headers['api-key'] = apiKey; - - const modelmapping: Partial> = { - 'gpt-3.5-turbo': 'gpt-35-turbo', - 'gpt-3.5-turbo-16k': 'gpt-35-turbo-16k', - }; - - const model = modelmapping[config.model] || config.model; - - // set api version to 2023-07-01-preview for gpt-4 and gpt-4-32k, otherwise use 2023-03-15-preview - const apiVersion = - model === 'gpt-4' || model === 'gpt-4-32k' - ? '2023-07-01-preview' - : '2023-03-15-preview'; - const path = `openai/deployments/${model}/chat/completions?api-version=${apiVersion}`; - - if (!endpoint.endsWith(path)) { - if (!endpoint.endsWith('/')) { - endpoint += '/'; - } - endpoint += path; - } + endpoint = modifyEndpointForAzure(endpoint, config, apiKey); } const response = await fetch(endpoint, { @@ -116,7 +103,7 @@ export const getChatCompletionStream = async ( ); } else { throw new Error( - 'Message from Better ChatGPT:\nInvalid API endpoint! We recommend you to check your free API endpoint.' + 'Message from Better ChatGPT:\nInvalid API endpoint! We recommend you to check your free API endpoint.' ); } }