-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
205 lines (205 loc) · 7.38 KB
/
index.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { randomUUID } from 'node:crypto';
import fs from 'node:fs/promises';
import { isUUID } from './utilities.js';
const apiUrl = 'https://platform.sectorflow.ai/api/v1';
export class SectorFlow {
#apiKey;
#models;
/**
* Creates a new SectorFlow API object.
* @param apiKey - The API key.
*/
constructor(apiKey) {
this.#apiKey = apiKey;
}
/**
* Retrieves the list of available LLMs.
* Once retrieved, they are cached to avoid added queries.
* @param forceRefresh - An optional parameter to bypass the cache.
* @returns A list of available LLMs.
*/
async getModels(forceRefresh = false) {
if (forceRefresh || this.#models === undefined) {
const response = await fetch(`${apiUrl}/models`, {
method: 'get',
headers: {
Authorization: `Bearer ${this.#apiKey}`
}
});
this.#models = await response.json();
}
return this.#models ?? [];
}
/**
* A helper function to retrieve a model id by keywords.
* i.e. getModelIdByKeywords('ChatGPT')
* @param spaceSeparatedKeywords - A string of space-separated keywords.
* @returns - The model id, if found.
*/
async getModelIdByKeywords(spaceSeparatedKeywords) {
const models = await this.getModels();
const keywords = spaceSeparatedKeywords.toLowerCase().split(' ');
const model = models.find((possibleModel) => {
const stringToSearch = `${possibleModel.name} ${possibleModel.baseModel}`.toLowerCase();
for (const keyword of keywords) {
if (!stringToSearch.includes(keyword)) {
return false;
}
}
return true;
});
if (model === undefined) {
return undefined;
}
return model.id;
}
/**
* Retrieves the list of projects.
* @returns A list of projects.
*/
async getProjects() {
const response = await fetch(`${apiUrl}/projects`, {
method: 'get',
headers: {
Authorization: `Bearer ${this.#apiKey}`
}
});
return await response.json();
}
/**
* Creates a new project.
* @param projectRequest - The settings for the new project.
* @returns The new project.
*/
async createProject(projectRequest) {
if (projectRequest.modelIds.length === 0) {
throw new Error('No modelIds available.');
}
for (const modelId of projectRequest.modelIds) {
if (!isUUID(modelId)) {
throw new Error(`modelId is not a valid UUID: ${modelId}`);
}
}
const response = await fetch(`${apiUrl}/projects`, {
method: 'post',
headers: {
Authorization: `Bearer ${this.#apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(projectRequest)
});
return await response.json();
}
/**
* Deletes a project.
* @param projectId - The project id.
* @returns `true` if the project was deleted.
*/
async deleteProject(projectId) {
if (!isUUID(projectId)) {
throw new Error(`projectId is not a valid UUID: ${projectId}`);
}
const response = await fetch(`${apiUrl}/projects/${projectId.toLowerCase()}`, {
method: 'delete',
headers: {
Authorization: `Bearer ${this.#apiKey}`,
'Content-Type': 'application/json'
}
});
return response.ok;
}
/**
* Uploads a file.
* @param projectId - The project id.
* @param filePath - The file path.
* @returns The upload response.
*/
async uploadFile(projectId, filePath) {
if (!isUUID(projectId)) {
throw new Error(`projectId is not a valid UUID: ${projectId}`);
}
// eslint-disable-next-line security/detect-non-literal-fs-filename
const fileBlob = new Blob([await fs.readFile(filePath)]);
const collectionName = randomUUID();
const fileName = filePath.split(/[/\\]/).at(-1);
const formData = new FormData();
formData.append('file', fileBlob, fileName);
formData.append('collection', collectionName);
const response = await fetch(`${apiUrl}/chat/${projectId.toLowerCase()}/add-file`, {
method: 'post',
headers: {
Authorization: `Bearer ${this.#apiKey}`
},
body: formData
});
const threadJson = await response.json();
threadJson.collectionName = collectionName;
threadJson.fileName = fileName;
return threadJson;
}
async getCollections(projectId) {
if (!isUUID(projectId)) {
throw new Error(`projectId is not a valid UUID: ${projectId}`);
}
const response = await fetch(`${apiUrl}/files/${projectId}/collections`, {
method: 'get',
headers: {
Authorization: `Bearer ${this.#apiKey}`
}
});
return await response.json();
}
/**
* Sends messages to a project, returning the responses.
* @param projectId - The project id.
* @param messagesRequest - The messages request.
* @returns The responses to the messages.
*/
async sendChatMessages(projectId, messagesRequest) {
if (!isUUID(projectId)) {
throw new Error(`projectId is not a valid UUID: ${projectId}`);
}
if (messagesRequest.threadId !== undefined &&
!isUUID(messagesRequest.threadId)) {
throw new Error(`threadId is not a valid UUID: ${projectId}`);
}
const response = await fetch(`${apiUrl}/chat/${projectId.toLowerCase()}/completions`, {
method: 'post',
headers: {
Authorization: `Bearer ${this.#apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(messagesRequest)
});
return await response.json();
}
/**
* Sends a message to a project, returning the responses.
* @param projectId - The project id.
* @param message - The message.
* @param options - Optional.
* @param options.threadId - The optional thread id, to continue a chain of messages.
* @param options.collectionName - The optional SectorFlow collection name that contains the file. Used with the fileName option.
* @param options.fileName - The optional SectorFlow file name. Used with the collectionName option.
* @returns The responses to the message.
*/
async sendChatMessage(projectId, message, options) {
let cleanMessage = message;
// eslint-disable-next-line @typescript-eslint/init-declarations
let ragSettings;
if (options?.collectionName !== undefined &&
options.fileName !== undefined) {
ragSettings = {
collectionName: options.collectionName,
fileNames: [options.fileName],
summarize: false
};
cleanMessage = cleanMessage.replaceAll(/ {2,}/g, ' ');
}
return await this.sendChatMessages(projectId, {
messages: [{ role: 'user', content: cleanMessage }],
threadId: options?.threadId,
ragSettings
});
}
}