-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
408 lines (366 loc) · 13.1 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
const baseUrl = 'https://api-beta.codegpt.co/api/v1'
const JUDINI_TUTORIAL = 'https://api-beta.codegpt.co/api/v1/docs'
export class CodeGPTPlus {
constructor({ apiKey, orgId }) {
this.headers = {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + apiKey,
source: 'api',
channel: 'sdk-js',
...(orgId && { 'CodeGPT-Org-Id': orgId })
}
this.isStreaming = false
}
isLoading() {
return this.isStreaming
}
stopStreaming() {
this.isStreaming = false
}
/**
* Initiates a chat with the specified agent and handles the streaming of responses.
*
* @param {Object} params - The parameters for the chat.
* @param {Array<Object>} params.messages - An array of message objects to be sent to the agent. Each object should have a `role` (which can be 'system', 'user', or 'assistant') and `content` which is the actual message. * @param {string} params.agentId - The ID of the agent to chat with.
* @param {Function} [callback=(chunk) => {}] - An optional callback function to handle streaming responses.
* @returns {Promise<string>} The full response from the chat.
* @throws {Error} If the API response is not ok.
*/
async chatCompletion({ messages, agentId }, callback = () => {}) {
if (messages.length === 0) {
throw new Error('JUDINI: messages array should not be empty')
}
if (!agentId) {
throw new Error('JUDINI: agentId should not be empty')
}
this.isStreaming = true
// eslint-disable-next-line no-async-promise-executor
return new Promise(async (resolve) => {
let fullResponse = ''
const body = {
agentId,
stream: true,
format: 'json',
messages
}
const response = await fetch(`${baseUrl}/chat/completions`, {
method: 'POST',
headers: this.headers,
body: JSON.stringify(body)
})
if (!response.ok) {
const errorMessage = `JUDINI: API Response was: ${response.status} ${response.statusText} ${JUDINI_TUTORIAL}`
throw new Error(errorMessage)
}
const reader = response.body.getReader()
const decoder = new TextDecoder('utf-8')
while (true) {
const { done, value } = await reader.read()
if (done) {
this.isStreaming = false
resolve(fullResponse)
break
}
const decoded = decoder.decode(value)
const data = JSON.parse(decoded)
const text = data.choices[0].delta.content
callback(text)
fullResponse += text
// if (text.includes('data: [DONE]')) {
// this.isStreaming = false
// resolve(fullResponse)
// break
// }
// const datas = text.split('\n\n')
// for (let i = 0; i < datas.length; i++) {
// try {
// const data = JSON.parse(datas[i].replace('data: ', ''))
// const text = data.choices[0].delta.content
// callback(text)
// fullResponse += text
// } catch {}
// }
}
})
}
/**
* Initiates a chat with the specified agent and handles the streaming of responses using a ReadableStream.
*
* @param {Object} params - The parameters for the chat.
* @param {Array<Object>} params.messages - An array of message objects to be sent to the agent. Each object should have a `role` (which can be 'system', 'user', or 'assistant') and `content` which is the actual message.
* @param {string} params.agentId - The ID of the agent to chat with.
* @returns {ReadableStream} A ReadableStream that emits the responses from the chat.
* @throws {Error} If the API response is not ok.
*/
async experimental_AIStream({ messages, agentId }) {
if (messages.length === 0) {
throw new Error('JUDINI: messages array should not be empty')
}
if (!agentId) {
throw new Error('JUDINI: agentId should not be empty')
}
this.isStreaming = true
const body = {
agentId,
stream: true,
format: 'json',
messages
}
const response = await fetch(`${baseUrl}/chat/completions`, {
method: 'POST',
headers: this.headers,
body: JSON.stringify(body)
})
if (!response.ok) {
const errorMessage = `JUDINI: API Response was: ${response.status} ${response.statusText} ${JUDINI_TUTORIAL}`
throw new Error(errorMessage)
}
const reader = response.body.getReader()
const decoder = new TextDecoder('utf-8')
const encoder = new TextEncoder()
return new ReadableStream({
async start(controller) {
while (true) {
const { done, value } = await reader.read()
if (done) {
controller.close()
return
}
const data = decoder.decode(value)
const json = JSON.parse(data)
const message = json?.choices?.[0]?.delta?.content
if (message) {
controller.enqueue(encoder.encode(message))
}
// const chunks = data.split('\n\n')
// for (const chunk of chunks) {
// try {
// if (!chunk) continue
// const json = JSON.parse(chunk.trim().replace('data: ', ''))
// const message = json?.choices?.[0]?.delta?.content
// if (message) {
// controller.enqueue(encoder.encode(message))
// }
// } catch {}
// }
}
},
cancel() {
reader.releaseLock()
}
})
}
/**
* Retrieves a list of all the agents from the CodeGPTPlus API.
*
* @returns {Promise<Array<{
* status: string,
* name: string,
* documentId: string[],
* description: string,
* prompt: string,
* topk: number,
* model: string,
* welcome: string,
* maxTokens: number,
* id: string,
* user_created: string,
* date_created: string
* }>>} An array of objects, each representing an agent.
*/
async getAgents() {
const response = await fetch(`${baseUrl}/agent`, {
method: 'GET',
headers: this.headers
})
if (!response.ok) {
const errorMessage = `JUDINI: API Response was: ${response.status} ${response.statusText} ${JUDINI_TUTORIAL}`
throw new Error(errorMessage)
}
return await response.json()
}
/**
* Retrieves a specific agent from the CodeGPTPlus API.
*
* @param {string} agentId - The ID of the agent you want to retrieve from the CodeGPTPlus API.
* @returns {Promise<{
* status: string,
* name: string,
* documentId: string[],
* description: string,
* prompt: string,
* topk: number,
* model: string,
* welcome: string,
* maxTokens: number,
* id: string,
* user_created: string,
* date_created: string
* }>} An object containing details about the agent.
*/
async getAgent(agentId) {
const response = await fetch(`${baseUrl}/agent/${agentId}`, {
method: 'GET',
headers: this.headers
})
if (!response.ok) {
const errorMessage = `JUDINI: API Response was: ${response.status} ${response.statusText} ${JUDINI_TUTORIAL}`
throw new Error(errorMessage)
}
return await response.json()
}
/**
* Creates a new agent in the CodeGPTPlus API.
*
* @param {Object} agent - The agent object to be created.
* @param {string} agent.name - The name of the agent.
* @param {number} agent.topk - The number of tokens to consider for each step.
* @param {string} agent.model - The model to be used by the agent. For example, 'gpt-3.5-turbo'.
* @param {string} agent.welcome - The welcome message of the agent.
* @param {string} agent.prompt - The prompt of the agent.
* @returns {Promise<Object>} The created agent object.
* @throws {Error} If the API response is not ok.
*/
async createAgent(agent) {
const response = await fetch(`${baseUrl}/agent`, {
method: 'POST',
headers: this.headers,
body: JSON.stringify(agent)
})
if (!response.ok) {
const errorMessage = `JUDINI: API Response was: ${response.status} ${response.statusText} ${JUDINI_TUTORIAL}`
throw new Error(errorMessage)
}
return await response.json()
}
/**
* Updates an existing agent in the CodeGPTPlus API.
*
* @param {string} agentId - The ID of the agent to be updated.
* @param {Object} agent - The agent object with updated values.
* @param {string} [agent.name] - The updated name of the agent.
* @param {string} [agent.model] - The updated model to be used by the agent. For example, 'gpt-3.5-turbo'.
* @param {string} [agent.prompt] - The updated prompt of the agent.
* @param {number} [agent.topk] - The updated number of tokens to consider for each step.
* @param {string} [agent.welcome] - The updated welcome message of the agent.
* @param {boolean} [agent.is_public] - The updated visibility of the agent. If true, the agent is public.
* @param {string} [agent.pincode] - The updated pincode of the agent.
* @returns {Promise<Object>} The updated agent object.
* @throws {Error} If the API response is not ok.
*/
async updateAgent(agentId, agent) {
const response = await fetch(`${baseUrl}/agent/${agentId}`, {
method: 'PATCH',
headers: this.headers,
body: JSON.stringify(agent)
})
if (!response.ok) {
const errorMessage = `JUDINI: API Response was: ${response.status} ${response.statusText} ${JUDINI_TUTORIAL}`
throw new Error(errorMessage)
}
return await response.json()
}
/**
* Deletes an existing agent in the CodeGPTPlus API.
*
* @param {string} agentId - The ID of the agent to be deleted.
* @returns {Promise<string>} A message indicating the deletion was successful.
* @throws {Error} If the API response is not ok.
*/
async deleteAgent(agentId) {
const response = await fetch(`${baseUrl}/agent/${agentId}`, {
method: 'DELETE',
headers: this.headers
})
if (!response.ok) {
const errorMessage = `JUDINI: API Response was: ${response.status} ${response.statusText} ${JUDINI_TUTORIAL}`
throw new Error(errorMessage)
}
return 'Agent deleted successfully'
}
/**
* Updates the documents of a specific agent in the CodeGPTPlus API.
*
* @param {string} agentId - The ID of the agent whose documents are to be updated.
* @param {Array<string>} documents - An array of document IDs to be associated with the agent.
* @returns {Promise<Object>} The updated agent object.
* @throws {Error} If the API response is not ok or if the agentId or documents are empty.
*/
async updateAgentDocuments(agentId, documents) {
if (!agentId || !documents) {
throw new Error('JUDINI: agentId and documents should not be empty')
}
const body = {
agent_documents: documents
}
const response = await fetch(`${baseUrl}/agent/${agentId}`, {
method: 'PATCH',
headers: this.headers,
body: JSON.stringify(body)
})
if (!response.ok) {
const errorMessage = `JUDINI: API Response was: ${response.status} ${response.statusText} ${JUDINI_TUTORIAL}`
throw new Error(errorMessage)
}
return await response.json()
}
/**
* Retrieves all documents from the CodeGPTPlus API.
*
* @returns {Promise<Array<Object>>} An array of document objects.
* @throws {Error} If the API response is not ok.
*/
async getDocuments() {
const response = await fetch(`${baseUrl}/document`, {
method: 'GET',
headers: this.headers
})
if (!response.ok) {
const errorMessage = `JUDINI: API Response was: ${response.status} ${response.statusText} ${JUDINI_TUTORIAL}`
throw new Error(errorMessage)
}
return await response.json()
}
/**
* Retrieves a specific document from the CodeGPTPlus API.
*
* @param {string} documentId - The ID of the document to be retrieved.
* @returns {Promise<Object>} The document object.
* @throws {Error} If the API response is not ok or if the documentId is empty.
*/
async getDocument(documentId) {
if (!documentId) {
throw new Error('JUDINI: documentId should not be empty')
}
const response = await fetch(`${baseUrl}/document/${documentId}`, {
method: 'GET',
headers: this.headers
})
if (!response.ok) {
const errorMessage = `JUDINI: API Response was: ${response.status} ${response.statusText} ${JUDINI_TUTORIAL}`
throw new Error(errorMessage)
}
return await response.json()
}
/**
* Deletes a specific document from the CodeGPTPlus API.
*
* @param {string} documentId - The ID of the document to be deleted.
* @returns {Promise<string>} A message indicating the deletion was successful.
* @throws {Error} If the API response is not ok or if the documentId is empty.
*/
async deleteDocument(documentId) {
if (!documentId) {
throw new Error('JUDINI: documentId should not be empty')
}
const response = await fetch(`${baseUrl}/document/${documentId}`, {
method: 'DELETE',
headers: this.headers
})
if (!response.ok) {
const errorMessage = `JUDINI: API Response was: ${response.status} ${response.statusText} ${JUDINI_TUTORIAL}`
throw new Error(errorMessage)
}
return 'Document deleted successfully'
}
}