diff --git a/src/api/customApi.ts b/src/api/customApi.ts
index 68aa1ee..98bb65f 100644
--- a/src/api/customApi.ts
+++ b/src/api/customApi.ts
@@ -25,45 +25,41 @@ export const getChatCompletion = async (
apiKey: string,
messages: MessageInterface[]
) => {
- try {
- const response = await fetch(endpoint, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- Authorization: `Bearer ${apiKey}`,
- },
- body: JSON.stringify({
- model: 'gpt-3.5-turbo',
- messages,
- }),
- });
- const data = await response.json();
- return data;
- } catch (error) {
- console.error('Error:', error);
- }
+ const response = await fetch(endpoint, {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ Authorization: `Bearer ${apiKey}`,
+ },
+ body: JSON.stringify({
+ model: 'gpt-3.5-turbo',
+ messages,
+ }),
+ });
+ if (!response.ok) throw new Error(await response.text());
+
+ const data = await response.json();
+ return data;
};
export const getChatCompletionStream = async (
apiKey: string,
messages: MessageInterface[]
) => {
- try {
- const response = await fetch(endpoint, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- Authorization: `Bearer ${apiKey}`,
- },
- body: JSON.stringify({
- model: 'gpt-3.5-turbo',
- messages,
- stream: true,
- }),
- });
- const stream = response.body;
- return stream;
- } catch (error) {
- console.error('Error:', error);
- }
+ const response = await fetch(endpoint, {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ Authorization: `Bearer ${apiKey}`,
+ },
+ body: JSON.stringify({
+ model: 'gpt-3.5-turbo',
+ messages,
+ stream: true,
+ }),
+ });
+ if (!response.ok) throw new Error(await response.text());
+
+ const stream = response.body;
+ return stream;
};
diff --git a/src/api/freeApi.ts b/src/api/freeApi.ts
index 3b09633..2835f79 100644
--- a/src/api/freeApi.ts
+++ b/src/api/freeApi.ts
@@ -3,40 +3,36 @@ import { MessageInterface } from '@type/chat';
export const endpoint = 'https://chatgpt-api.shn.hk/v1/';
export const getChatCompletion = async (messages: MessageInterface[]) => {
- try {
- const response = await fetch(endpoint, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- },
- body: JSON.stringify({
- model: 'gpt-3.5-turbo',
- messages,
- }),
- });
- const data = await response.json();
- return data;
- } catch (error) {
- console.error('Error:', error);
- }
+ const response = await fetch(endpoint, {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify({
+ model: 'gpt-3.5-turbo',
+ messages,
+ }),
+ });
+ if (!response.ok) throw new Error(await response.text());
+
+ const data = await response.json();
+ return data;
};
export const getChatCompletionStream = async (messages: MessageInterface[]) => {
- try {
- const response = await fetch(endpoint, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- },
- body: JSON.stringify({
- model: 'gpt-3.5-turbo',
- messages,
- stream: true,
- }),
- });
- const stream = response.body;
- return stream;
- } catch (error) {
- console.error('Error:', error);
- }
+ const response = await fetch(endpoint, {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify({
+ model: 'gpt-3.5-turbo',
+ messages,
+ stream: true,
+ }),
+ });
+ if (!response.ok) throw new Error(await response.text());
+
+ const stream = response.body;
+ return stream;
};
diff --git a/src/components/Chat/ChatContent/ChatContent.tsx b/src/components/Chat/ChatContent/ChatContent.tsx
index c6bc5bd..ece04b4 100644
--- a/src/components/Chat/ChatContent/ChatContent.tsx
+++ b/src/components/Chat/ChatContent/ChatContent.tsx
@@ -39,8 +39,8 @@ const ChatContent = () => {