Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi-handler.js
More file actions
Latest commit
112 lines (102 loc) · 2.66 KB
/
Copy pathapi-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
importaxiosfrom"axios";
import{SSE}from"sse.js";
exportconstlistAIs=async()=>{
try{
constresponse=awaitaxios({
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",
},
});
returnresponse.data;
}catch(error){
thrownewError(`API Error: ${error.message}`);
}
};
exportconstcreateAI=async(name,description,instructions,src)=>{
if(!instructions){
instructions=description;
}
if(!src){
src=
"https://res.cloudinary.com/dwyqkq1hq/image/upload/ro0duemq1iium4odnx6n.webp";
}
try{
constresponse=awaitaxios({
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",
},
});
returnresponse.data;
}catch(error){
thrownewError(`API Error: ${error.message}`);
}
};
exportconstcreateChat=async(aiId)=>{
try{
constresponse=awaitaxios({
method: "post",
url: `${process.env.API_URL}/api/v1/ai/${aiId}/chats`,
headers: {
"X-Authorization": `Bearer ${process.env.API_KEY}`,
"Content-Type": "application/json",
},
});
returnresponse.data;
}catch(error){
thrownewError(`API Error: ${error.message}`);
}
};
exportconstsendMessage=async(chatId,message)=>{
try{
consteventSource=newSSE(
`${process.env.API_URL}/api/v1/chats/${chatId}`,
{
start: false,
method: "POST",
withCredentials: true,
payload: JSON.stringify({
prompt: message,
date: newDate(),
}),
headers: {
"Content-Type": "application/json; charset=utf-8",
"X-Authorization": `Bearer ${process.env.API_KEY}`,
},
}
);
returneventSource;
}catch(error){
thrownewError(`API Error: ${error.message}`);
}
};
exportconstqueryAI=async(aiId,message)=>{
try{
constresponse=awaitaxios({
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",
},
});
returnresponse.data;
}catch(error){
thrownewError(`API Error: ${error.message}`);
}
};