2023-03-06 15:22:05 +01:00
|
|
|
import { ConfigInterface, MessageInterface } from '@type/chat';
|
2023-03-03 23:17:11 +01:00
|
|
|
|
|
|
|
export const endpoint = 'https://api.openai.com/v1/chat/completions';
|
|
|
|
|
|
|
|
export const validateApiKey = async (apiKey: string) => {
|
|
|
|
try {
|
|
|
|
const response = await fetch(endpoint, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Authorization: `Bearer ${apiKey}`,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
|
|
|
|
if (response.status === 401) return false;
|
|
|
|
else if (response.status === 400) return true;
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error:', error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getChatCompletion = async (
|
|
|
|
apiKey: string,
|
2023-03-06 15:22:05 +01:00
|
|
|
messages: MessageInterface[],
|
|
|
|
config: ConfigInterface
|
2023-03-03 23:17:11 +01:00
|
|
|
) => {
|
2023-03-04 16:35:38 +01:00
|
|
|
const response = await fetch(endpoint, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Authorization: `Bearer ${apiKey}`,
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
messages,
|
2023-03-06 15:22:05 +01:00
|
|
|
...config,
|
2023-03-04 16:35:38 +01:00
|
|
|
}),
|
|
|
|
});
|
|
|
|
if (!response.ok) throw new Error(await response.text());
|
|
|
|
|
|
|
|
const data = await response.json();
|
|
|
|
return data;
|
2023-03-03 23:17:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export const getChatCompletionStream = async (
|
|
|
|
apiKey: string,
|
2023-03-06 15:22:05 +01:00
|
|
|
messages: MessageInterface[],
|
|
|
|
config: ConfigInterface
|
2023-03-03 23:17:11 +01:00
|
|
|
) => {
|
2023-03-04 16:35:38 +01:00
|
|
|
const response = await fetch(endpoint, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Authorization: `Bearer ${apiKey}`,
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
messages,
|
2023-03-06 15:22:05 +01:00
|
|
|
...config,
|
2023-03-04 16:35:38 +01:00
|
|
|
stream: true,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
if (!response.ok) throw new Error(await response.text());
|
|
|
|
|
|
|
|
const stream = response.body;
|
|
|
|
return stream;
|
2023-03-03 23:17:11 +01:00
|
|
|
};
|