Skip to content

Commit

Permalink
Update api.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
AthanaD authored Dec 26, 2023
1 parent 7987a75 commit 519877b
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ShareGPTSubmitBodyInterface } from '@type/api';
import { ConfigInterface, MessageInterface } from '@type/chat';
import { ConfigInterface, MessageInterface, ModelOptions } from '@type/chat';
import { isAzureEndpoint } from '@utils/api';

export const getChatCompletion = async (
Expand All @@ -18,9 +18,18 @@ export const getChatCompletion = async (
if (isAzureEndpoint(endpoint) && apiKey) {
headers['api-key'] = apiKey;

const model = config.model === 'gpt-3.5-turbo' ? 'gpt-35-turbo' : config.model === 'gpt-3.5-turbo-16k' ? 'gpt-35-turbo-16k' : config.model;
const modelmapping: Partial<Record<ModelOptions, string>> = {
'gpt-3.5-turbo': 'gpt-35-turbo',
'gpt-3.5-turbo-16k': 'gpt-35-turbo-16k',
};

const apiVersion = '2023-03-15-preview';
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}`;

Expand Down Expand Up @@ -63,10 +72,18 @@ export const getChatCompletionStream = async (
if (isAzureEndpoint(endpoint) && apiKey) {
headers['api-key'] = apiKey;

const model = config.model === 'gpt-3.5-turbo' ? 'gpt-35-turbo' : config.model === 'gpt-3.5-turbo-16k' ? 'gpt-35-turbo-16k' : config.model;
const modelmapping: Partial<Record<ModelOptions, string>> = {
'gpt-3.5-turbo': 'gpt-35-turbo',
'gpt-3.5-turbo-16k': 'gpt-35-turbo-16k',
};

const apiVersion = '2023-03-15-preview';
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)) {
Expand All @@ -89,6 +106,7 @@ export const getChatCompletionStream = async (
});
if (response.status === 404 || response.status === 405) {
const text = await response.text();

if (text.includes('model_not_found')) {
throw new Error(
text +
Expand Down

0 comments on commit 519877b

Please sign in to comment.