Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 33 additions & 46 deletions src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Record<ModelOptions, string>> = {
'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[],
Expand All @@ -17,30 +47,7 @@ export const getChatCompletion = async (

if (isAzureEndpoint(endpoint) && apiKey) {
headers['api-key'] = apiKey;

const modelmapping: Partial<Record<ModelOptions, string>> = {
'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, {
Expand Down Expand Up @@ -73,27 +80,7 @@ export const getChatCompletionStream = async (

if (isAzureEndpoint(endpoint) && apiKey) {
headers['api-key'] = apiKey;

const modelmapping: Partial<Record<ModelOptions, string>> = {
'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, {
Expand All @@ -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.'
);
}
}
Expand Down