-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi-handler.js
More file actions
112 lines (102 loc) · 2.66 KB
/
api-handler.js
File metadata and controls
112 lines (102 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import axios from "axios";
import { SSE } from "sse.js";
export const listAIs = async () => {
try {
const response = await axios({
method: "get",
url: `${process.env.API_URL}/api/v1/me/ai?scope=OWNED`,
headers: {
"X-Authorization": `Bearer ${process.env.API_KEY}`,
"Content-Type": "application/json",
},
});
return response.data;
} catch (error) {
throw new Error(`API Error: ${error.message}`);
}
};
export const createAI = async (name, description, instructions, src) => {
if (!instructions) {
instructions = description;
}
if (!src) {
src =
"https://res.cloudinary.com/dwyqkq1hq/image/upload/ro0duemq1iium4odnx6n.webp";
}
try {
const response = await axios({
method: "post",
url: `${process.env.API_URL}/api/v1/ai`,
data: { name, description, instructions, src },
headers: {
"X-Authorization": `Bearer ${process.env.API_KEY}`,
"Content-Type": "application/json",
},
});
return response.data;
} catch (error) {
throw new Error(`API Error: ${error.message}`);
}
};
export const createChat = async (aiId) => {
try {
const response = await axios({
method: "post",
url: `${process.env.API_URL}/api/v1/ai/${aiId}/chats`,
headers: {
"X-Authorization": `Bearer ${process.env.API_KEY}`,
"Content-Type": "application/json",
},
});
return response.data;
} catch (error) {
throw new Error(`API Error: ${error.message}`);
}
};
export const sendMessage = async (chatId, message) => {
try {
const eventSource = new SSE(
`${process.env.API_URL}/api/v1/chats/${chatId}`,
{
start: false,
method: "POST",
withCredentials: true,
payload: JSON.stringify({
prompt: message,
date: new Date(),
}),
headers: {
"Content-Type": "application/json; charset=utf-8",
"X-Authorization": `Bearer ${process.env.API_KEY}`,
},
}
);
return eventSource;
} catch (error) {
throw new Error(`API Error: ${error.message}`);
}
};
export const queryAI = async (aiId, message) => {
try {
const response = await axios({
method: "post",
url: `${process.env.API_URL}/api/v1/chats/completions`,
data: {
messages: [
{
role: "user",
content: message,
},
],
model: aiId,
},
headers: {
"X-Authorization": `Bearer ${process.env.API_KEY}`,
"Content-Type": "application/json",
},
});
return response.data;
} catch (error) {
throw new Error(`API Error: ${error.message}`);
}
};