Skip to content

Commit

Permalink
manual git cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
cheeseonamonkey committed Apr 3, 2024
1 parent bae10ab commit cefa902
Showing 1 changed file with 85 additions and 53 deletions.
138 changes: 85 additions & 53 deletions src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,54 @@ import { ShareGPTSubmitBodyInterface } from '@type/api';
import { ConfigInterface, MessageInterface, ModelDefinition } from '@type/chat';
import { isAzureEndpoint, uuidv4 } from '@utils/api';

const getHeaders = (
export const getChatCompletion = async (
endpoint: string,
messages: MessageInterface[],
config: ConfigInterface,
modelDef: ModelDefinition,
apiKey?: string,
customHeaders?: Record<string, string>
): HeadersInit => {
) => {
const headers: HeadersInit = {
'Content-Type': 'application/json',
...customHeaders,
};
if (apiKey) headers.Authorization = `Bearer ${apiKey}`;
return headers;
};

const buildEndpoint = (
endpoint: string,
modelDef: ModelDefinition,
apiKey?: string
): string => {
if (isAzureEndpoint(endpoint) && apiKey) {
headers['api-key'] = apiKey;

const modelName = modelDef.model;

const apiVersion = '2023-03-15-preview';

const path = `openai/deployments/${modelName}/chat/completions?api-version=${apiVersion}`;
if (!endpoint.endsWith(path))
endpoint += endpoint.endsWith('/') ? path : `/${path}`;
}
return endpoint;
};

const handleErrorResponse = async (response: Response): Promise<never> => {
const text = await response.text();
if (response.status === 404 || response.status === 405) {
const errorMessage = text.includes('model_not_found')
? `${text}\nMessage from KoalaClient:\nPlease ensure that you have access to the GPT-4 API!`
: 'Message from KoalaClient:\nInvalid API endpoint! We recommend you to check your free API endpoint.';
throw new Error(errorMessage);
} else if (response.status === 429 || !response.ok) {
let error = text;
if (text.includes('insufficient_quota'))
error +=
'\nMessage from KoalaClient:\nWe recommend changing your API endpoint or API key';
if (response.status === 429) error += '\nRate limited!';
throw new Error(error);
if (!endpoint.endsWith(path)) {
if (!endpoint.endsWith('/')) {
endpoint += '/';
}
endpoint += path;
}
}
throw new Error(text);
};

export const getChatCompletion = async (
endpoint: string,
messages: MessageInterface[],
config: ConfigInterface,
modelDef: ModelDefinition,
apiKey?: string,
customHeaders?: Record<string, string>
) => {
const headers = getHeaders(apiKey, customHeaders);
const finalEndpoint = buildEndpoint(endpoint, modelDef, apiKey);

// todo: option in config
config.user = uuidv4();

delete (config as any).model_selection;

const response = await fetch(finalEndpoint, {
const response = await fetch(endpoint, {
method: 'POST',
headers,
body: JSON.stringify({ messages, ...config }),
body: JSON.stringify({
messages,
...config,
}),
});

if (!response.ok) throw new Error(await response.text());

return response.json();
const data = await response.json();
return data;
};

export const getChatCompletionStream = async (
Expand All @@ -80,28 +60,80 @@ export const getChatCompletionStream = async (
apiKey?: string,
customHeaders?: Record<string, string>
) => {
const headers = getHeaders(apiKey, customHeaders);
const finalEndpoint = buildEndpoint(endpoint, modelDef, apiKey);
const headers: HeadersInit = {
'Content-Type': 'application/json',
...customHeaders,
};

if (apiKey) headers.Authorization = `Bearer ${apiKey}`;
if (isAzureEndpoint(endpoint) && apiKey) {
headers['api-key'] = apiKey;

const modelName = modelDef.model;

const apiVersion = '2023-03-15-preview';

const path = `openai/deployments/${modelName}/chat/completions?api-version=${apiVersion}`;

if (!endpoint.endsWith(path)) {
if (!endpoint.endsWith('/')) {
endpoint += '/';
}
endpoint += path;
}
}

// todo: option in config
config.user = uuidv4();

delete (config as any).model_selection;

const response = await fetch(finalEndpoint, {
const response = await fetch(endpoint, {
method: 'POST',
headers,
body: JSON.stringify({ messages, ...config, stream: true }),
body: JSON.stringify({
messages,
...config,
stream: true,
}),
});
if (response.status === 404 || response.status === 405) {
const text = await response.text();
if (text.includes('model_not_found')) {
throw new Error(
text +
'\nMessage from KoalaClient:\nPlease ensure that you have access to the GPT-4 API!'
);
} else {
throw new Error(
'Message from KoalaClient:\nInvalid API endpoint! We recommend you to check your free API endpoint.'
);
}
}

if (!response.ok) await handleErrorResponse(response);
if (response.status === 429 || !response.ok) {
const text = await response.text();
let error = text;
if (text.includes('insufficient_quota')) {
error +=
'\nMessage from KoalaClient:\nWe recommend changing your API endpoint or API key';
} else if (response.status === 429) {
error += '\nRate limited!';
}
throw new Error(error);
}

return response.body;
const stream = response.body;
return stream;
};

export const submitShareGPT = async (body: ShareGPTSubmitBodyInterface) => {
const request = await fetch('https://sharegpt.com/api/conversations', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json',
},
method: 'POST',
});

const response = await request.json();
Expand Down

0 comments on commit cefa902

Please sign in to comment.