-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomgpt.js
47 lines (39 loc) · 1.51 KB
/
customgpt.js
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
import EventSource from "eventsource";
const BASE_URL = 'https://app.customgpt.ai/api/v1/projects';
export class CustomGPTClient {
constructor (projectId, token) {
this.projectId = projectId;
this.token = token;
}
get baseUrl () {
return `${BASE_URL}/${this.projectId}`;
}
get authenticationHeader () {
return {
'authorization': `Bearer ${this.token}`,
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0'
}
}
async makeRequest (url, options) {
console.log(url, options);
const response = await fetch(url, options);
const json = await response.json();
console.log(json);
return json;
}
createConversation (conversationName) {
return this.makeRequest(`${this.baseUrl}/conversations`, {
headers: {...this.authenticationHeader, ...{ 'content-type': 'application/json', 'accept': 'application/json' }},
method: 'POST',
body: JSON.stringify({ name: 'coucou' })
});
}
async sendMessageToConversation (conversationData, messageContent) {
const url = `${this.baseUrl}/conversations/${conversationData.session_id}/messages?stream=0`;
return this.makeRequest(url, {
headers: {...this.authenticationHeader, ...{ 'content-type': 'application/json', 'accept': 'application/json' }},
method: 'POST',
body: JSON.stringify({ prompt: messageContent })
});
}
}