vectocore4 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Readme.md +3 -0
- package/dist/cjs/index.js +478 -0
- package/dist/cjs/package.json +3 -0
- package/dist/index.d.ts +186 -0
- package/dist/mjs/index.js +438 -0
- package/dist/mjs/package.json +3 -0
- package/package.json +27 -0
package/Readme.md
ADDED
|
@@ -0,0 +1,478 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.AIMS = exports.Lens = exports.Vectocore = void 0;
|
|
16
|
+
const axios_1 = __importDefault(require("axios"));
|
|
17
|
+
class Vectocore {
|
|
18
|
+
/**
|
|
19
|
+
* API Client for interfacing with the Vectocore API.
|
|
20
|
+
*
|
|
21
|
+
* @param {string | undefined} [opts.tenantKey=process.env['VECTOCORE4_TENANT_KEY'] ?? undefined]
|
|
22
|
+
*/
|
|
23
|
+
constructor({ baseURL, tenantKey } = {}) {
|
|
24
|
+
if (!tenantKey) {
|
|
25
|
+
throw new Error("테넌트 키가 없습니다.");
|
|
26
|
+
}
|
|
27
|
+
this.tenantKey = tenantKey;
|
|
28
|
+
if (!baseURL) {
|
|
29
|
+
this.baseURL = "https://vectocore4-dev-apim.azure-api.net/external/post";
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
this.baseURL = baseURL;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
requestPost(requestBody) {
|
|
36
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
37
|
+
let config = {
|
|
38
|
+
headers: { "Content-Type": "application/json", "x-api-key": this.tenantKey },
|
|
39
|
+
};
|
|
40
|
+
const response = yield axios_1.default.post(this.baseURL, requestBody, config);
|
|
41
|
+
return response.data;
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
putIndex(_a) {
|
|
45
|
+
return __awaiter(this, arguments, void 0, function* ({ indexName, description }) {
|
|
46
|
+
const requestParam = {
|
|
47
|
+
command: "put_index",
|
|
48
|
+
index_name: indexName,
|
|
49
|
+
description: description !== null && description !== void 0 ? description : "",
|
|
50
|
+
};
|
|
51
|
+
const response = yield this.requestPost(requestParam);
|
|
52
|
+
return response;
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
deleteIndex(_a) {
|
|
56
|
+
return __awaiter(this, arguments, void 0, function* ({ indexName }) {
|
|
57
|
+
const requestParam = {
|
|
58
|
+
command: "delete_index",
|
|
59
|
+
index_name: indexName,
|
|
60
|
+
};
|
|
61
|
+
const response = yield this.requestPost(requestParam);
|
|
62
|
+
return response;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
listIndex() {
|
|
66
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
67
|
+
const requestParam = {
|
|
68
|
+
command: "list_index",
|
|
69
|
+
};
|
|
70
|
+
const response = yield this.requestPost(requestParam);
|
|
71
|
+
return response;
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
putExtreamData(_a) {
|
|
75
|
+
return __awaiter(this, arguments, void 0, function* ({ indexName, docBody }) {
|
|
76
|
+
const requestParam = {
|
|
77
|
+
command: "put_extream_data",
|
|
78
|
+
index_name: indexName,
|
|
79
|
+
doc_body: {
|
|
80
|
+
id: docBody.id,
|
|
81
|
+
keywords: docBody.keywords,
|
|
82
|
+
data: docBody.data,
|
|
83
|
+
doc_type: docBody.docType,
|
|
84
|
+
ref_only_text: docBody.refOnlyText,
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
if (docBody.imageUrl) {
|
|
88
|
+
requestParam.doc_body.image_url = docBody.imageUrl;
|
|
89
|
+
}
|
|
90
|
+
if (docBody.metadata) {
|
|
91
|
+
requestParam.doc_body.metadata = docBody.metadata;
|
|
92
|
+
}
|
|
93
|
+
const response = yield this.requestPost(requestParam);
|
|
94
|
+
return response;
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
getExtreamData(_a) {
|
|
98
|
+
return __awaiter(this, arguments, void 0, function* ({ indexName, documentId }) {
|
|
99
|
+
const requestParam = {
|
|
100
|
+
command: "get_extream_data",
|
|
101
|
+
index_name: indexName,
|
|
102
|
+
document_id: documentId,
|
|
103
|
+
};
|
|
104
|
+
const response = yield this.requestPost(requestParam);
|
|
105
|
+
return response;
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
listExtreamData(_a) {
|
|
109
|
+
return __awaiter(this, arguments, void 0, function* ({ indexName, lastKey }) {
|
|
110
|
+
const requestParam = {
|
|
111
|
+
command: "list_extream_data",
|
|
112
|
+
index_name: indexName,
|
|
113
|
+
};
|
|
114
|
+
if (lastKey !== undefined && lastKey !== null) {
|
|
115
|
+
requestParam.last_key = lastKey;
|
|
116
|
+
}
|
|
117
|
+
const response = yield this.requestPost(requestParam);
|
|
118
|
+
return response;
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
deleteExtreamData(_a) {
|
|
122
|
+
return __awaiter(this, arguments, void 0, function* ({ indexName, documentId }) {
|
|
123
|
+
const requestParam = {
|
|
124
|
+
command: "delete_extream_data",
|
|
125
|
+
index_name: indexName,
|
|
126
|
+
document_id: documentId,
|
|
127
|
+
};
|
|
128
|
+
const response = yield this.requestPost(requestParam);
|
|
129
|
+
return response;
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
extreamSearch(_a) {
|
|
133
|
+
return __awaiter(this, arguments, void 0, function* ({ indexName, question, maxCount, mode, webSearch, findIndexPrompt, docOnly, }) {
|
|
134
|
+
var _b, _c, _d;
|
|
135
|
+
const requestParam = {
|
|
136
|
+
command: "extream_search",
|
|
137
|
+
question: question,
|
|
138
|
+
};
|
|
139
|
+
if (indexName)
|
|
140
|
+
requestParam.index_name = indexName;
|
|
141
|
+
if (mode)
|
|
142
|
+
requestParam.mode = mode;
|
|
143
|
+
if (maxCount)
|
|
144
|
+
requestParam.max_count = maxCount;
|
|
145
|
+
if (findIndexPrompt)
|
|
146
|
+
requestParam.find_index_prompt = findIndexPrompt;
|
|
147
|
+
if (docOnly)
|
|
148
|
+
requestParam.doc_only = docOnly;
|
|
149
|
+
let __webSearch = {
|
|
150
|
+
model: (_b = webSearch === null || webSearch === void 0 ? void 0 : webSearch.model) !== null && _b !== void 0 ? _b : "bing",
|
|
151
|
+
with_image: (_c = webSearch === null || webSearch === void 0 ? void 0 : webSearch.withImage) !== null && _c !== void 0 ? _c : false,
|
|
152
|
+
with_video: (_d = webSearch === null || webSearch === void 0 ? void 0 : webSearch.withVideo) !== null && _d !== void 0 ? _d : false,
|
|
153
|
+
};
|
|
154
|
+
if (webSearch)
|
|
155
|
+
requestParam.web_search = __webSearch;
|
|
156
|
+
const response = yield this.requestPost(requestParam);
|
|
157
|
+
return response;
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
webSearch(_a) {
|
|
161
|
+
return __awaiter(this, arguments, void 0, function* ({ question }) {
|
|
162
|
+
const requestParam = {
|
|
163
|
+
command: "web_search",
|
|
164
|
+
question: question,
|
|
165
|
+
};
|
|
166
|
+
const response = yield this.requestPost(requestParam);
|
|
167
|
+
return response;
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
exports.Vectocore = Vectocore;
|
|
172
|
+
class Lens {
|
|
173
|
+
/**
|
|
174
|
+
* API Client for interfacing with the Lens API.
|
|
175
|
+
*
|
|
176
|
+
* @param {string | undefined} [opts.tenantKey=process.env['VECTOCORE_TENANT_KEY'] ?? undefined]
|
|
177
|
+
*/
|
|
178
|
+
constructor({ tenantKey, baseURL, role, rule, outputFormat, mode, webSearch, agentic, maxHisCount, sessionKey, numOfRef, customSystemPrompt, staticInfo, }) {
|
|
179
|
+
if (tenantKey === undefined) {
|
|
180
|
+
throw new Error("error tenantKey is undefined");
|
|
181
|
+
}
|
|
182
|
+
this.tenantKey = tenantKey;
|
|
183
|
+
if (!baseURL) {
|
|
184
|
+
this.baseURL = "https://vectocore4-dev-apim.azure-api.net/external/post";
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
this.baseURL = baseURL;
|
|
188
|
+
}
|
|
189
|
+
this.role = role;
|
|
190
|
+
this.rule = rule;
|
|
191
|
+
this.outputFormat = outputFormat;
|
|
192
|
+
this.mode = mode;
|
|
193
|
+
this.maxHisCount = maxHisCount;
|
|
194
|
+
this.sessionKey = sessionKey;
|
|
195
|
+
this.numOfRef = numOfRef;
|
|
196
|
+
this.customSystemPrompt = customSystemPrompt || undefined;
|
|
197
|
+
this.staticInfo = staticInfo || undefined;
|
|
198
|
+
this.webSearch = webSearch;
|
|
199
|
+
this.agentic = agentic;
|
|
200
|
+
}
|
|
201
|
+
requestStream(requestBody) {
|
|
202
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
203
|
+
let header = {
|
|
204
|
+
"x-api-key": this.tenantKey ? this.tenantKey : "",
|
|
205
|
+
};
|
|
206
|
+
const request = {
|
|
207
|
+
method: "POST",
|
|
208
|
+
headers: header,
|
|
209
|
+
body: JSON.stringify(requestBody),
|
|
210
|
+
};
|
|
211
|
+
const response = yield fetch(this.baseURL, request);
|
|
212
|
+
return response.body;
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
genStreamResponse(streamRes) {
|
|
216
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
217
|
+
const reader = streamRes === null || streamRes === void 0 ? void 0 : streamRes.getReader();
|
|
218
|
+
const decoder = new TextDecoder();
|
|
219
|
+
let done = false;
|
|
220
|
+
let content = "";
|
|
221
|
+
while (!done) {
|
|
222
|
+
const { value, done: doneReading } = yield reader.read();
|
|
223
|
+
done = doneReading; // 스트림 완료시 true
|
|
224
|
+
const chunkValue = decoder.decode(value);
|
|
225
|
+
content = content + chunkValue;
|
|
226
|
+
}
|
|
227
|
+
const payload = JSON.parse(content);
|
|
228
|
+
return payload;
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
chat(_a) {
|
|
232
|
+
return __awaiter(this, arguments, void 0, function* ({ question, imageUrls, fileUrls, stream, indexName, customData, model, textOnlyRef, isFirstMsg, withStatus, }) {
|
|
233
|
+
const requestParam = {
|
|
234
|
+
command: "lensChat",
|
|
235
|
+
question: question,
|
|
236
|
+
};
|
|
237
|
+
if (this.role)
|
|
238
|
+
requestParam.role = this.role;
|
|
239
|
+
if (this.rule)
|
|
240
|
+
requestParam.rule = this.rule;
|
|
241
|
+
if (this.outputFormat)
|
|
242
|
+
requestParam.outputFormat = this.outputFormat;
|
|
243
|
+
if (this.mode)
|
|
244
|
+
requestParam.mode = this.mode;
|
|
245
|
+
if (this.maxHisCount)
|
|
246
|
+
requestParam.maxHisCount = this.maxHisCount;
|
|
247
|
+
if (this.sessionKey)
|
|
248
|
+
requestParam.sessionKey = this.sessionKey;
|
|
249
|
+
if (this.numOfRef)
|
|
250
|
+
requestParam.numOfRef = this.numOfRef;
|
|
251
|
+
if (this.customSystemPrompt)
|
|
252
|
+
requestParam.customSystemPrompt = this.customSystemPrompt;
|
|
253
|
+
if (this.staticInfo)
|
|
254
|
+
requestParam.staticInfo = this.staticInfo;
|
|
255
|
+
if (this.webSearch)
|
|
256
|
+
requestParam.webSearch = this.webSearch;
|
|
257
|
+
if (this.agentic)
|
|
258
|
+
requestParam.agentic = this.agentic;
|
|
259
|
+
if (imageUrls)
|
|
260
|
+
requestParam.imageUrls = imageUrls;
|
|
261
|
+
if (fileUrls)
|
|
262
|
+
requestParam.fileUrls = fileUrls;
|
|
263
|
+
if (customData)
|
|
264
|
+
requestParam.customData = customData;
|
|
265
|
+
if (indexName)
|
|
266
|
+
requestParam.indexName = indexName;
|
|
267
|
+
if (model)
|
|
268
|
+
requestParam.model = model;
|
|
269
|
+
if (textOnlyRef)
|
|
270
|
+
requestParam.textOnlyRef = textOnlyRef;
|
|
271
|
+
if (isFirstMsg !== undefined) {
|
|
272
|
+
requestParam.isFirstMsg = isFirstMsg;
|
|
273
|
+
}
|
|
274
|
+
else {
|
|
275
|
+
requestParam.isFirstMsg = true;
|
|
276
|
+
}
|
|
277
|
+
requestParam.withStatus = withStatus ? withStatus : false;
|
|
278
|
+
let response = yield this.requestStream(requestParam);
|
|
279
|
+
if (stream) {
|
|
280
|
+
return response;
|
|
281
|
+
}
|
|
282
|
+
else {
|
|
283
|
+
const res = yield this.genStreamResponse(response);
|
|
284
|
+
return res;
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
history(_a) {
|
|
289
|
+
return __awaiter(this, arguments, void 0, function* ({ sessionKey, limit }) {
|
|
290
|
+
const requestParam = {
|
|
291
|
+
command: "lensChatHistory",
|
|
292
|
+
sessionKey: sessionKey,
|
|
293
|
+
};
|
|
294
|
+
if (limit)
|
|
295
|
+
requestParam.limit = limit;
|
|
296
|
+
let response = yield this.requestStream(requestParam);
|
|
297
|
+
const res = yield this.genStreamResponse(response);
|
|
298
|
+
return res;
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
jsonAutoComplete(jsonString) {
|
|
302
|
+
const dataCloseChar = {
|
|
303
|
+
"{": "}",
|
|
304
|
+
"[": "]",
|
|
305
|
+
'"': '"',
|
|
306
|
+
};
|
|
307
|
+
if (!jsonString)
|
|
308
|
+
return null;
|
|
309
|
+
let string = jsonString
|
|
310
|
+
.trim()
|
|
311
|
+
.replace(/(\r\n|\n|\r|\s{2,})/gm, "")
|
|
312
|
+
.replace(/(?<=:)([a-zA-Z]+)(?=\s*(?![,\}])(?:[,\}\s]|$))/g, " null");
|
|
313
|
+
let missingChars = [];
|
|
314
|
+
for (let i = 0; i < string.length; i++) {
|
|
315
|
+
const char = string[i];
|
|
316
|
+
if (char === missingChars[missingChars.length - 1]) {
|
|
317
|
+
missingChars.pop();
|
|
318
|
+
}
|
|
319
|
+
else if (dataCloseChar[char]) {
|
|
320
|
+
missingChars.push(dataCloseChar[char]);
|
|
321
|
+
if (char === "{") {
|
|
322
|
+
missingChars.push(":");
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
if (missingChars[missingChars.length - 1] === ":") {
|
|
327
|
+
if (string[string.length - 1] !== "{") {
|
|
328
|
+
missingChars[missingChars.length - 1] = ": null";
|
|
329
|
+
}
|
|
330
|
+
else {
|
|
331
|
+
missingChars.pop();
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
const missingCharsString = missingChars.reverse().join("");
|
|
335
|
+
const completeString = string + missingCharsString;
|
|
336
|
+
const cleanedString = completeString
|
|
337
|
+
.replace(/"":/g, "")
|
|
338
|
+
.replace(/":}|": }/g, '": null }')
|
|
339
|
+
.replace(/,""}|,}|,\"\w+\"}/g, "}")
|
|
340
|
+
.replace(/},]/g, "}]");
|
|
341
|
+
return cleanedString;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
exports.Lens = Lens;
|
|
345
|
+
class AIMS {
|
|
346
|
+
/**
|
|
347
|
+
* API Client for interfacing with the Vectocore API.
|
|
348
|
+
*
|
|
349
|
+
* @param {string | undefined} [opts.tenantKey=process.env['VECTOCORE_TENANT_KEY'] ?? undefined]
|
|
350
|
+
*/
|
|
351
|
+
constructor({ baseURL, tenantKey } = {}) {
|
|
352
|
+
if (tenantKey === undefined) {
|
|
353
|
+
throw new Error("error tenantKey is undefined");
|
|
354
|
+
}
|
|
355
|
+
this.tenantKey = tenantKey;
|
|
356
|
+
if (!baseURL) {
|
|
357
|
+
this.baseURL = "https://vectocore4-dev-apim.azure-api.net/external/post";
|
|
358
|
+
}
|
|
359
|
+
else {
|
|
360
|
+
this.baseURL = baseURL;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
requestStream(requestBody) {
|
|
364
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
365
|
+
let header = {
|
|
366
|
+
"x-api-key": this.tenantKey ? this.tenantKey : "",
|
|
367
|
+
};
|
|
368
|
+
const request = {
|
|
369
|
+
method: "POST",
|
|
370
|
+
headers: header,
|
|
371
|
+
body: JSON.stringify(requestBody),
|
|
372
|
+
};
|
|
373
|
+
const response = yield fetch(this.baseURL, request);
|
|
374
|
+
return response.body;
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
genStreamResponse(streamRes) {
|
|
378
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
379
|
+
const reader = streamRes === null || streamRes === void 0 ? void 0 : streamRes.getReader();
|
|
380
|
+
const decoder = new TextDecoder();
|
|
381
|
+
let done = false;
|
|
382
|
+
let content = "";
|
|
383
|
+
while (!done) {
|
|
384
|
+
const { value, done: doneReading } = yield reader.read();
|
|
385
|
+
done = doneReading; // 스트림 완료시 true
|
|
386
|
+
const chunkValue = decoder.decode(value);
|
|
387
|
+
content = content + chunkValue;
|
|
388
|
+
}
|
|
389
|
+
const payload = JSON.parse(content);
|
|
390
|
+
return payload;
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
keypoint(text) {
|
|
394
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
395
|
+
const requestParam = {
|
|
396
|
+
command: "keywords",
|
|
397
|
+
text: text,
|
|
398
|
+
};
|
|
399
|
+
const response = yield this.requestStream(requestParam);
|
|
400
|
+
const res = yield this.genStreamResponse(response);
|
|
401
|
+
return res;
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
imCap(imageUrl) {
|
|
405
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
406
|
+
const requestParam = {
|
|
407
|
+
command: "imCap",
|
|
408
|
+
imageUrl: imageUrl,
|
|
409
|
+
};
|
|
410
|
+
const response = yield this.requestStream(requestParam);
|
|
411
|
+
const res = yield this.genStreamResponse(response);
|
|
412
|
+
return res;
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
translate(_a) {
|
|
416
|
+
return __awaiter(this, arguments, void 0, function* ({ language, text, stream }) {
|
|
417
|
+
const requestParam = {
|
|
418
|
+
command: "translate",
|
|
419
|
+
language: language,
|
|
420
|
+
text: text,
|
|
421
|
+
};
|
|
422
|
+
let response = yield this.requestStream(requestParam);
|
|
423
|
+
if (stream) {
|
|
424
|
+
return response;
|
|
425
|
+
}
|
|
426
|
+
else {
|
|
427
|
+
const res = yield this.genStreamResponse(response);
|
|
428
|
+
return res;
|
|
429
|
+
}
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
jsonAutoComplete(jsonString) {
|
|
433
|
+
const dataCloseChar = {
|
|
434
|
+
"{": "}",
|
|
435
|
+
"[": "]",
|
|
436
|
+
'"': '"',
|
|
437
|
+
};
|
|
438
|
+
if (!jsonString)
|
|
439
|
+
return null;
|
|
440
|
+
let string = jsonString
|
|
441
|
+
.trim()
|
|
442
|
+
.replace(/(\r\n|\n|\r|\s{2,})/gm, "")
|
|
443
|
+
// 속성명이 따옴표 없이 나올 경우 → null 치환
|
|
444
|
+
.replace(/:\s*([a-zA-Z0-9_]+)(?=\s*[,\}])/g, ": null")
|
|
445
|
+
// 속성명이 따옴표 없이 시작하는 경우 → 강제로 따옴표 감싸기
|
|
446
|
+
.replace(/([{,])\s*([a-zA-Z0-9_]+)\s*:/g, '$1"$2":');
|
|
447
|
+
let missingChars = [];
|
|
448
|
+
for (let i = 0; i < string.length; i++) {
|
|
449
|
+
const char = string[i];
|
|
450
|
+
if (char === missingChars[missingChars.length - 1]) {
|
|
451
|
+
missingChars.pop();
|
|
452
|
+
}
|
|
453
|
+
else if (dataCloseChar[char]) {
|
|
454
|
+
missingChars.push(dataCloseChar[char]);
|
|
455
|
+
if (char === "{") {
|
|
456
|
+
missingChars.push(":");
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
if (missingChars[missingChars.length - 1] === ":") {
|
|
461
|
+
if (string[string.length - 1] !== "{") {
|
|
462
|
+
missingChars[missingChars.length - 1] = ": null";
|
|
463
|
+
}
|
|
464
|
+
else {
|
|
465
|
+
missingChars.pop();
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
const missingCharsString = missingChars.reverse().join("");
|
|
469
|
+
const completeString = string + missingCharsString;
|
|
470
|
+
const cleanedString = completeString
|
|
471
|
+
.replace(/"":/g, "")
|
|
472
|
+
.replace(/":}|": }/g, '": null }')
|
|
473
|
+
.replace(/,""}|,}|,\"\w+\"}/g, "}")
|
|
474
|
+
.replace(/},]/g, "}]");
|
|
475
|
+
return cleanedString;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
exports.AIMS = AIMS;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
export interface ClientOptions {
|
|
2
|
+
/**
|
|
3
|
+
* Vectocore4 사용을 위하여 부여받은 테넌트 키입니다.
|
|
4
|
+
*/
|
|
5
|
+
tenantKey?: string;
|
|
6
|
+
/**
|
|
7
|
+
* Vectocore4 실행 URL이 다른 경우 입력합니다.
|
|
8
|
+
* 보통의 경우 필요하지 않습니다.
|
|
9
|
+
* @example 'https://extra.example.com'
|
|
10
|
+
*/
|
|
11
|
+
baseURL?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface CustomDataParams {
|
|
14
|
+
type: "text" | "image";
|
|
15
|
+
text?: string;
|
|
16
|
+
image_url?: string;
|
|
17
|
+
}
|
|
18
|
+
export interface LensClientOptions {
|
|
19
|
+
tenantKey?: string;
|
|
20
|
+
baseURL?: string | null | undefined;
|
|
21
|
+
role?: string;
|
|
22
|
+
rule?: string;
|
|
23
|
+
outputFormat?: any;
|
|
24
|
+
mode?: "VECTOR" | "BOTH" | "WEB" | "CUSTOM";
|
|
25
|
+
webSearch?: WebSearchOption;
|
|
26
|
+
agentic?: AgenticOption;
|
|
27
|
+
maxHisCount?: number;
|
|
28
|
+
sessionKey?: string;
|
|
29
|
+
customData?: CustomDataParams;
|
|
30
|
+
customSystemPrompt?: string;
|
|
31
|
+
numOfRef?: number;
|
|
32
|
+
staticInfo?: any;
|
|
33
|
+
}
|
|
34
|
+
export interface PutIndexParams {
|
|
35
|
+
/**
|
|
36
|
+
* 생성할 인덱스의 이름입니다.
|
|
37
|
+
* ⚠️ 인덱스명은 다음 문자를 사용할 수 없습니다. ", *, \, <, |, ,, >, /, ?
|
|
38
|
+
*/
|
|
39
|
+
indexName: string;
|
|
40
|
+
/**
|
|
41
|
+
* 인덱스에 대한 설명입니다.
|
|
42
|
+
*/
|
|
43
|
+
description?: string;
|
|
44
|
+
}
|
|
45
|
+
export interface DeleteIndexParams {
|
|
46
|
+
indexName: string;
|
|
47
|
+
}
|
|
48
|
+
export interface PutExtreamDataParams {
|
|
49
|
+
indexName: string;
|
|
50
|
+
docBody: {
|
|
51
|
+
id: string;
|
|
52
|
+
keywords: string;
|
|
53
|
+
data: string;
|
|
54
|
+
docType: string;
|
|
55
|
+
imageUrl?: string | undefined;
|
|
56
|
+
metadata?: any;
|
|
57
|
+
refOnlyText?: boolean;
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
export interface ListExtreamDataParams {
|
|
61
|
+
indexName: string;
|
|
62
|
+
lastKey?: any | null;
|
|
63
|
+
}
|
|
64
|
+
export interface GetExtreamDataParams {
|
|
65
|
+
indexName: string;
|
|
66
|
+
documentId: string;
|
|
67
|
+
}
|
|
68
|
+
export interface DeleteExtreamDataParams {
|
|
69
|
+
indexName: string;
|
|
70
|
+
documentId: string;
|
|
71
|
+
}
|
|
72
|
+
export interface ExtreamSearchParams {
|
|
73
|
+
indexName?: string | string[] | undefined;
|
|
74
|
+
question: string;
|
|
75
|
+
maxCount?: number | undefined;
|
|
76
|
+
mode?: "VECTOR" | "BOTH" | "WEB" | undefined;
|
|
77
|
+
webSearch?: {
|
|
78
|
+
model?: "google" | "bing";
|
|
79
|
+
withImage?: boolean;
|
|
80
|
+
withVideo?: boolean;
|
|
81
|
+
};
|
|
82
|
+
findIndexPrompt?: string | undefined;
|
|
83
|
+
docOnly?: boolean;
|
|
84
|
+
}
|
|
85
|
+
export interface WebSearchParams {
|
|
86
|
+
question: string;
|
|
87
|
+
}
|
|
88
|
+
export type FileType = {
|
|
89
|
+
type: string;
|
|
90
|
+
name: string;
|
|
91
|
+
url: string;
|
|
92
|
+
extImgUrls?: string[];
|
|
93
|
+
};
|
|
94
|
+
export interface ChatParams {
|
|
95
|
+
question: string;
|
|
96
|
+
imageUrls?: string[];
|
|
97
|
+
fileUrls?: FileType[];
|
|
98
|
+
stream?: boolean | undefined;
|
|
99
|
+
indexName?: string | string[] | undefined;
|
|
100
|
+
customData?: CustomDataParams;
|
|
101
|
+
model?: string | undefined;
|
|
102
|
+
textOnlyRef?: boolean | undefined;
|
|
103
|
+
isFirstMsg?: boolean | undefined;
|
|
104
|
+
withStatus?: boolean | undefined;
|
|
105
|
+
}
|
|
106
|
+
export interface ChatHistoryParams {
|
|
107
|
+
sessionKey: string;
|
|
108
|
+
limit?: number;
|
|
109
|
+
}
|
|
110
|
+
export interface TranslateParams {
|
|
111
|
+
language: string;
|
|
112
|
+
text: string;
|
|
113
|
+
stream?: boolean | undefined;
|
|
114
|
+
}
|
|
115
|
+
type WebSearchOption = {
|
|
116
|
+
model?: "google" | "bing";
|
|
117
|
+
withImage?: boolean;
|
|
118
|
+
withVideo?: boolean;
|
|
119
|
+
};
|
|
120
|
+
type AgenticOption = {
|
|
121
|
+
enable?: boolean;
|
|
122
|
+
reasoning?: boolean;
|
|
123
|
+
};
|
|
124
|
+
export declare class Vectocore {
|
|
125
|
+
tenantKey: string | undefined;
|
|
126
|
+
baseURL: string;
|
|
127
|
+
/**
|
|
128
|
+
* API Client for interfacing with the Vectocore API.
|
|
129
|
+
*
|
|
130
|
+
* @param {string | undefined} [opts.tenantKey=process.env['VECTOCORE4_TENANT_KEY'] ?? undefined]
|
|
131
|
+
*/
|
|
132
|
+
constructor({ baseURL, tenantKey }?: ClientOptions);
|
|
133
|
+
protected requestPost(requestBody: any): Promise<any>;
|
|
134
|
+
putIndex({ indexName, description }: PutIndexParams): Promise<any>;
|
|
135
|
+
deleteIndex({ indexName }: DeleteIndexParams): Promise<any>;
|
|
136
|
+
listIndex(): Promise<any>;
|
|
137
|
+
putExtreamData({ indexName, docBody }: PutExtreamDataParams): Promise<any>;
|
|
138
|
+
getExtreamData({ indexName, documentId }: GetExtreamDataParams): Promise<any>;
|
|
139
|
+
listExtreamData({ indexName, lastKey }: ListExtreamDataParams): Promise<any>;
|
|
140
|
+
deleteExtreamData({ indexName, documentId }: DeleteExtreamDataParams): Promise<any>;
|
|
141
|
+
extreamSearch({ indexName, question, maxCount, mode, webSearch, findIndexPrompt, docOnly, }: ExtreamSearchParams): Promise<any>;
|
|
142
|
+
webSearch({ question }: WebSearchParams): Promise<any>;
|
|
143
|
+
}
|
|
144
|
+
export declare class Lens {
|
|
145
|
+
tenantKey: string | undefined;
|
|
146
|
+
baseURL: string;
|
|
147
|
+
role: string | undefined;
|
|
148
|
+
rule: string | undefined;
|
|
149
|
+
outputFormat: any;
|
|
150
|
+
mode: "VECTOR" | "BOTH" | "WEB" | "CUSTOM" | undefined;
|
|
151
|
+
webSearch: WebSearchOption | undefined;
|
|
152
|
+
agentic: AgenticOption | undefined;
|
|
153
|
+
maxHisCount: number | undefined;
|
|
154
|
+
sessionKey: string | undefined;
|
|
155
|
+
numOfRef: number | undefined;
|
|
156
|
+
customSystemPrompt: string | undefined;
|
|
157
|
+
staticInfo: any;
|
|
158
|
+
/**
|
|
159
|
+
* API Client for interfacing with the Lens API.
|
|
160
|
+
*
|
|
161
|
+
* @param {string | undefined} [opts.tenantKey=process.env['VECTOCORE_TENANT_KEY'] ?? undefined]
|
|
162
|
+
*/
|
|
163
|
+
constructor({ tenantKey, baseURL, role, rule, outputFormat, mode, webSearch, agentic, maxHisCount, sessionKey, numOfRef, customSystemPrompt, staticInfo, }: LensClientOptions);
|
|
164
|
+
protected requestStream(requestBody: any): Promise<ReadableStream<Uint8Array> | null>;
|
|
165
|
+
protected genStreamResponse(streamRes: ReadableStream): Promise<any>;
|
|
166
|
+
chat({ question, imageUrls, fileUrls, stream, indexName, customData, model, textOnlyRef, isFirstMsg, withStatus, }: ChatParams): Promise<any>;
|
|
167
|
+
history({ sessionKey, limit }: ChatHistoryParams): Promise<any>;
|
|
168
|
+
jsonAutoComplete(jsonString: string): string | null;
|
|
169
|
+
}
|
|
170
|
+
export declare class AIMS {
|
|
171
|
+
tenantKey: string | undefined;
|
|
172
|
+
baseURL: string;
|
|
173
|
+
/**
|
|
174
|
+
* API Client for interfacing with the Vectocore API.
|
|
175
|
+
*
|
|
176
|
+
* @param {string | undefined} [opts.tenantKey=process.env['VECTOCORE_TENANT_KEY'] ?? undefined]
|
|
177
|
+
*/
|
|
178
|
+
constructor({ baseURL, tenantKey }?: ClientOptions);
|
|
179
|
+
protected requestStream(requestBody: any): Promise<ReadableStream<Uint8Array> | null>;
|
|
180
|
+
protected genStreamResponse(streamRes: ReadableStream): Promise<any>;
|
|
181
|
+
keypoint(text: string): Promise<any>;
|
|
182
|
+
imCap(imageUrl: string): Promise<any>;
|
|
183
|
+
translate({ language, text, stream }: TranslateParams): Promise<any>;
|
|
184
|
+
jsonAutoComplete(jsonString: string): string | null;
|
|
185
|
+
}
|
|
186
|
+
export {};
|
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
export class Vectocore {
|
|
3
|
+
tenantKey;
|
|
4
|
+
baseURL;
|
|
5
|
+
/**
|
|
6
|
+
* API Client for interfacing with the Vectocore API.
|
|
7
|
+
*
|
|
8
|
+
* @param {string | undefined} [opts.tenantKey=process.env['VECTOCORE4_TENANT_KEY'] ?? undefined]
|
|
9
|
+
*/
|
|
10
|
+
constructor({ baseURL, tenantKey } = {}) {
|
|
11
|
+
if (!tenantKey) {
|
|
12
|
+
throw new Error("테넌트 키가 없습니다.");
|
|
13
|
+
}
|
|
14
|
+
this.tenantKey = tenantKey;
|
|
15
|
+
if (!baseURL) {
|
|
16
|
+
this.baseURL = "https://vectocore4-dev-apim.azure-api.net/external/post";
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
this.baseURL = baseURL;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
async requestPost(requestBody) {
|
|
23
|
+
let config = {
|
|
24
|
+
headers: { "Content-Type": "application/json", "x-api-key": this.tenantKey },
|
|
25
|
+
};
|
|
26
|
+
const response = await axios.post(this.baseURL, requestBody, config);
|
|
27
|
+
return response.data;
|
|
28
|
+
}
|
|
29
|
+
async putIndex({ indexName, description }) {
|
|
30
|
+
const requestParam = {
|
|
31
|
+
command: "put_index",
|
|
32
|
+
index_name: indexName,
|
|
33
|
+
description: description ?? "",
|
|
34
|
+
};
|
|
35
|
+
const response = await this.requestPost(requestParam);
|
|
36
|
+
return response;
|
|
37
|
+
}
|
|
38
|
+
async deleteIndex({ indexName }) {
|
|
39
|
+
const requestParam = {
|
|
40
|
+
command: "delete_index",
|
|
41
|
+
index_name: indexName,
|
|
42
|
+
};
|
|
43
|
+
const response = await this.requestPost(requestParam);
|
|
44
|
+
return response;
|
|
45
|
+
}
|
|
46
|
+
async listIndex() {
|
|
47
|
+
const requestParam = {
|
|
48
|
+
command: "list_index",
|
|
49
|
+
};
|
|
50
|
+
const response = await this.requestPost(requestParam);
|
|
51
|
+
return response;
|
|
52
|
+
}
|
|
53
|
+
async putExtreamData({ indexName, docBody }) {
|
|
54
|
+
const requestParam = {
|
|
55
|
+
command: "put_extream_data",
|
|
56
|
+
index_name: indexName,
|
|
57
|
+
doc_body: {
|
|
58
|
+
id: docBody.id,
|
|
59
|
+
keywords: docBody.keywords,
|
|
60
|
+
data: docBody.data,
|
|
61
|
+
doc_type: docBody.docType,
|
|
62
|
+
ref_only_text: docBody.refOnlyText,
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
if (docBody.imageUrl) {
|
|
66
|
+
requestParam.doc_body.image_url = docBody.imageUrl;
|
|
67
|
+
}
|
|
68
|
+
if (docBody.metadata) {
|
|
69
|
+
requestParam.doc_body.metadata = docBody.metadata;
|
|
70
|
+
}
|
|
71
|
+
const response = await this.requestPost(requestParam);
|
|
72
|
+
return response;
|
|
73
|
+
}
|
|
74
|
+
async getExtreamData({ indexName, documentId }) {
|
|
75
|
+
const requestParam = {
|
|
76
|
+
command: "get_extream_data",
|
|
77
|
+
index_name: indexName,
|
|
78
|
+
document_id: documentId,
|
|
79
|
+
};
|
|
80
|
+
const response = await this.requestPost(requestParam);
|
|
81
|
+
return response;
|
|
82
|
+
}
|
|
83
|
+
async listExtreamData({ indexName, lastKey }) {
|
|
84
|
+
const requestParam = {
|
|
85
|
+
command: "list_extream_data",
|
|
86
|
+
index_name: indexName,
|
|
87
|
+
};
|
|
88
|
+
if (lastKey !== undefined && lastKey !== null) {
|
|
89
|
+
requestParam.last_key = lastKey;
|
|
90
|
+
}
|
|
91
|
+
const response = await this.requestPost(requestParam);
|
|
92
|
+
return response;
|
|
93
|
+
}
|
|
94
|
+
async deleteExtreamData({ indexName, documentId }) {
|
|
95
|
+
const requestParam = {
|
|
96
|
+
command: "delete_extream_data",
|
|
97
|
+
index_name: indexName,
|
|
98
|
+
document_id: documentId,
|
|
99
|
+
};
|
|
100
|
+
const response = await this.requestPost(requestParam);
|
|
101
|
+
return response;
|
|
102
|
+
}
|
|
103
|
+
async extreamSearch({ indexName, question, maxCount, mode, webSearch, findIndexPrompt, docOnly, }) {
|
|
104
|
+
const requestParam = {
|
|
105
|
+
command: "extream_search",
|
|
106
|
+
question: question,
|
|
107
|
+
};
|
|
108
|
+
if (indexName)
|
|
109
|
+
requestParam.index_name = indexName;
|
|
110
|
+
if (mode)
|
|
111
|
+
requestParam.mode = mode;
|
|
112
|
+
if (maxCount)
|
|
113
|
+
requestParam.max_count = maxCount;
|
|
114
|
+
if (findIndexPrompt)
|
|
115
|
+
requestParam.find_index_prompt = findIndexPrompt;
|
|
116
|
+
if (docOnly)
|
|
117
|
+
requestParam.doc_only = docOnly;
|
|
118
|
+
let __webSearch = {
|
|
119
|
+
model: webSearch?.model ?? "bing",
|
|
120
|
+
with_image: webSearch?.withImage ?? false,
|
|
121
|
+
with_video: webSearch?.withVideo ?? false,
|
|
122
|
+
};
|
|
123
|
+
if (webSearch)
|
|
124
|
+
requestParam.web_search = __webSearch;
|
|
125
|
+
const response = await this.requestPost(requestParam);
|
|
126
|
+
return response;
|
|
127
|
+
}
|
|
128
|
+
async webSearch({ question }) {
|
|
129
|
+
const requestParam = {
|
|
130
|
+
command: "web_search",
|
|
131
|
+
question: question,
|
|
132
|
+
};
|
|
133
|
+
const response = await this.requestPost(requestParam);
|
|
134
|
+
return response;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
export class Lens {
|
|
138
|
+
tenantKey;
|
|
139
|
+
baseURL;
|
|
140
|
+
role;
|
|
141
|
+
rule;
|
|
142
|
+
outputFormat;
|
|
143
|
+
mode;
|
|
144
|
+
webSearch;
|
|
145
|
+
agentic;
|
|
146
|
+
maxHisCount;
|
|
147
|
+
sessionKey;
|
|
148
|
+
numOfRef;
|
|
149
|
+
customSystemPrompt;
|
|
150
|
+
staticInfo;
|
|
151
|
+
/**
|
|
152
|
+
* API Client for interfacing with the Lens API.
|
|
153
|
+
*
|
|
154
|
+
* @param {string | undefined} [opts.tenantKey=process.env['VECTOCORE_TENANT_KEY'] ?? undefined]
|
|
155
|
+
*/
|
|
156
|
+
constructor({ tenantKey, baseURL, role, rule, outputFormat, mode, webSearch, agentic, maxHisCount, sessionKey, numOfRef, customSystemPrompt, staticInfo, }) {
|
|
157
|
+
if (tenantKey === undefined) {
|
|
158
|
+
throw new Error("error tenantKey is undefined");
|
|
159
|
+
}
|
|
160
|
+
this.tenantKey = tenantKey;
|
|
161
|
+
if (!baseURL) {
|
|
162
|
+
this.baseURL = "https://vectocore4-dev-apim.azure-api.net/external/post";
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
this.baseURL = baseURL;
|
|
166
|
+
}
|
|
167
|
+
this.role = role;
|
|
168
|
+
this.rule = rule;
|
|
169
|
+
this.outputFormat = outputFormat;
|
|
170
|
+
this.mode = mode;
|
|
171
|
+
this.maxHisCount = maxHisCount;
|
|
172
|
+
this.sessionKey = sessionKey;
|
|
173
|
+
this.numOfRef = numOfRef;
|
|
174
|
+
this.customSystemPrompt = customSystemPrompt || undefined;
|
|
175
|
+
this.staticInfo = staticInfo || undefined;
|
|
176
|
+
this.webSearch = webSearch;
|
|
177
|
+
this.agentic = agentic;
|
|
178
|
+
}
|
|
179
|
+
async requestStream(requestBody) {
|
|
180
|
+
let header = {
|
|
181
|
+
"x-api-key": this.tenantKey ? this.tenantKey : "",
|
|
182
|
+
};
|
|
183
|
+
const request = {
|
|
184
|
+
method: "POST",
|
|
185
|
+
headers: header,
|
|
186
|
+
body: JSON.stringify(requestBody),
|
|
187
|
+
};
|
|
188
|
+
const response = await fetch(this.baseURL, request);
|
|
189
|
+
return response.body;
|
|
190
|
+
}
|
|
191
|
+
async genStreamResponse(streamRes) {
|
|
192
|
+
const reader = streamRes?.getReader();
|
|
193
|
+
const decoder = new TextDecoder();
|
|
194
|
+
let done = false;
|
|
195
|
+
let content = "";
|
|
196
|
+
while (!done) {
|
|
197
|
+
const { value, done: doneReading } = await reader.read();
|
|
198
|
+
done = doneReading; // 스트림 완료시 true
|
|
199
|
+
const chunkValue = decoder.decode(value);
|
|
200
|
+
content = content + chunkValue;
|
|
201
|
+
}
|
|
202
|
+
const payload = JSON.parse(content);
|
|
203
|
+
return payload;
|
|
204
|
+
}
|
|
205
|
+
async chat({ question, imageUrls, fileUrls, stream, indexName, customData, model, textOnlyRef, isFirstMsg, withStatus, }) {
|
|
206
|
+
const requestParam = {
|
|
207
|
+
command: "lensChat",
|
|
208
|
+
question: question,
|
|
209
|
+
};
|
|
210
|
+
if (this.role)
|
|
211
|
+
requestParam.role = this.role;
|
|
212
|
+
if (this.rule)
|
|
213
|
+
requestParam.rule = this.rule;
|
|
214
|
+
if (this.outputFormat)
|
|
215
|
+
requestParam.outputFormat = this.outputFormat;
|
|
216
|
+
if (this.mode)
|
|
217
|
+
requestParam.mode = this.mode;
|
|
218
|
+
if (this.maxHisCount)
|
|
219
|
+
requestParam.maxHisCount = this.maxHisCount;
|
|
220
|
+
if (this.sessionKey)
|
|
221
|
+
requestParam.sessionKey = this.sessionKey;
|
|
222
|
+
if (this.numOfRef)
|
|
223
|
+
requestParam.numOfRef = this.numOfRef;
|
|
224
|
+
if (this.customSystemPrompt)
|
|
225
|
+
requestParam.customSystemPrompt = this.customSystemPrompt;
|
|
226
|
+
if (this.staticInfo)
|
|
227
|
+
requestParam.staticInfo = this.staticInfo;
|
|
228
|
+
if (this.webSearch)
|
|
229
|
+
requestParam.webSearch = this.webSearch;
|
|
230
|
+
if (this.agentic)
|
|
231
|
+
requestParam.agentic = this.agentic;
|
|
232
|
+
if (imageUrls)
|
|
233
|
+
requestParam.imageUrls = imageUrls;
|
|
234
|
+
if (fileUrls)
|
|
235
|
+
requestParam.fileUrls = fileUrls;
|
|
236
|
+
if (customData)
|
|
237
|
+
requestParam.customData = customData;
|
|
238
|
+
if (indexName)
|
|
239
|
+
requestParam.indexName = indexName;
|
|
240
|
+
if (model)
|
|
241
|
+
requestParam.model = model;
|
|
242
|
+
if (textOnlyRef)
|
|
243
|
+
requestParam.textOnlyRef = textOnlyRef;
|
|
244
|
+
if (isFirstMsg !== undefined) {
|
|
245
|
+
requestParam.isFirstMsg = isFirstMsg;
|
|
246
|
+
}
|
|
247
|
+
else {
|
|
248
|
+
requestParam.isFirstMsg = true;
|
|
249
|
+
}
|
|
250
|
+
requestParam.withStatus = withStatus ? withStatus : false;
|
|
251
|
+
let response = await this.requestStream(requestParam);
|
|
252
|
+
if (stream) {
|
|
253
|
+
return response;
|
|
254
|
+
}
|
|
255
|
+
else {
|
|
256
|
+
const res = await this.genStreamResponse(response);
|
|
257
|
+
return res;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
async history({ sessionKey, limit }) {
|
|
261
|
+
const requestParam = {
|
|
262
|
+
command: "lensChatHistory",
|
|
263
|
+
sessionKey: sessionKey,
|
|
264
|
+
};
|
|
265
|
+
if (limit)
|
|
266
|
+
requestParam.limit = limit;
|
|
267
|
+
let response = await this.requestStream(requestParam);
|
|
268
|
+
const res = await this.genStreamResponse(response);
|
|
269
|
+
return res;
|
|
270
|
+
}
|
|
271
|
+
jsonAutoComplete(jsonString) {
|
|
272
|
+
const dataCloseChar = {
|
|
273
|
+
"{": "}",
|
|
274
|
+
"[": "]",
|
|
275
|
+
'"': '"',
|
|
276
|
+
};
|
|
277
|
+
if (!jsonString)
|
|
278
|
+
return null;
|
|
279
|
+
let string = jsonString
|
|
280
|
+
.trim()
|
|
281
|
+
.replace(/(\r\n|\n|\r|\s{2,})/gm, "")
|
|
282
|
+
.replace(/(?<=:)([a-zA-Z]+)(?=\s*(?![,\}])(?:[,\}\s]|$))/g, " null");
|
|
283
|
+
let missingChars = [];
|
|
284
|
+
for (let i = 0; i < string.length; i++) {
|
|
285
|
+
const char = string[i];
|
|
286
|
+
if (char === missingChars[missingChars.length - 1]) {
|
|
287
|
+
missingChars.pop();
|
|
288
|
+
}
|
|
289
|
+
else if (dataCloseChar[char]) {
|
|
290
|
+
missingChars.push(dataCloseChar[char]);
|
|
291
|
+
if (char === "{") {
|
|
292
|
+
missingChars.push(":");
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
if (missingChars[missingChars.length - 1] === ":") {
|
|
297
|
+
if (string[string.length - 1] !== "{") {
|
|
298
|
+
missingChars[missingChars.length - 1] = ": null";
|
|
299
|
+
}
|
|
300
|
+
else {
|
|
301
|
+
missingChars.pop();
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
const missingCharsString = missingChars.reverse().join("");
|
|
305
|
+
const completeString = string + missingCharsString;
|
|
306
|
+
const cleanedString = completeString
|
|
307
|
+
.replace(/"":/g, "")
|
|
308
|
+
.replace(/":}|": }/g, '": null }')
|
|
309
|
+
.replace(/,""}|,}|,\"\w+\"}/g, "}")
|
|
310
|
+
.replace(/},]/g, "}]");
|
|
311
|
+
return cleanedString;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
export class AIMS {
|
|
315
|
+
tenantKey;
|
|
316
|
+
baseURL;
|
|
317
|
+
/**
|
|
318
|
+
* API Client for interfacing with the Vectocore API.
|
|
319
|
+
*
|
|
320
|
+
* @param {string | undefined} [opts.tenantKey=process.env['VECTOCORE_TENANT_KEY'] ?? undefined]
|
|
321
|
+
*/
|
|
322
|
+
constructor({ baseURL, tenantKey } = {}) {
|
|
323
|
+
if (tenantKey === undefined) {
|
|
324
|
+
throw new Error("error tenantKey is undefined");
|
|
325
|
+
}
|
|
326
|
+
this.tenantKey = tenantKey;
|
|
327
|
+
if (!baseURL) {
|
|
328
|
+
this.baseURL = "https://vectocore4-dev-apim.azure-api.net/external/post";
|
|
329
|
+
}
|
|
330
|
+
else {
|
|
331
|
+
this.baseURL = baseURL;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
async requestStream(requestBody) {
|
|
335
|
+
let header = {
|
|
336
|
+
"x-api-key": this.tenantKey ? this.tenantKey : "",
|
|
337
|
+
};
|
|
338
|
+
const request = {
|
|
339
|
+
method: "POST",
|
|
340
|
+
headers: header,
|
|
341
|
+
body: JSON.stringify(requestBody),
|
|
342
|
+
};
|
|
343
|
+
const response = await fetch(this.baseURL, request);
|
|
344
|
+
return response.body;
|
|
345
|
+
}
|
|
346
|
+
async genStreamResponse(streamRes) {
|
|
347
|
+
const reader = streamRes?.getReader();
|
|
348
|
+
const decoder = new TextDecoder();
|
|
349
|
+
let done = false;
|
|
350
|
+
let content = "";
|
|
351
|
+
while (!done) {
|
|
352
|
+
const { value, done: doneReading } = await reader.read();
|
|
353
|
+
done = doneReading; // 스트림 완료시 true
|
|
354
|
+
const chunkValue = decoder.decode(value);
|
|
355
|
+
content = content + chunkValue;
|
|
356
|
+
}
|
|
357
|
+
const payload = JSON.parse(content);
|
|
358
|
+
return payload;
|
|
359
|
+
}
|
|
360
|
+
async keypoint(text) {
|
|
361
|
+
const requestParam = {
|
|
362
|
+
command: "keywords",
|
|
363
|
+
text: text,
|
|
364
|
+
};
|
|
365
|
+
const response = await this.requestStream(requestParam);
|
|
366
|
+
const res = await this.genStreamResponse(response);
|
|
367
|
+
return res;
|
|
368
|
+
}
|
|
369
|
+
async imCap(imageUrl) {
|
|
370
|
+
const requestParam = {
|
|
371
|
+
command: "imCap",
|
|
372
|
+
imageUrl: imageUrl,
|
|
373
|
+
};
|
|
374
|
+
const response = await this.requestStream(requestParam);
|
|
375
|
+
const res = await this.genStreamResponse(response);
|
|
376
|
+
return res;
|
|
377
|
+
}
|
|
378
|
+
async translate({ language, text, stream }) {
|
|
379
|
+
const requestParam = {
|
|
380
|
+
command: "translate",
|
|
381
|
+
language: language,
|
|
382
|
+
text: text,
|
|
383
|
+
};
|
|
384
|
+
let response = await this.requestStream(requestParam);
|
|
385
|
+
if (stream) {
|
|
386
|
+
return response;
|
|
387
|
+
}
|
|
388
|
+
else {
|
|
389
|
+
const res = await this.genStreamResponse(response);
|
|
390
|
+
return res;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
jsonAutoComplete(jsonString) {
|
|
394
|
+
const dataCloseChar = {
|
|
395
|
+
"{": "}",
|
|
396
|
+
"[": "]",
|
|
397
|
+
'"': '"',
|
|
398
|
+
};
|
|
399
|
+
if (!jsonString)
|
|
400
|
+
return null;
|
|
401
|
+
let string = jsonString
|
|
402
|
+
.trim()
|
|
403
|
+
.replace(/(\r\n|\n|\r|\s{2,})/gm, "")
|
|
404
|
+
// 속성명이 따옴표 없이 나올 경우 → null 치환
|
|
405
|
+
.replace(/:\s*([a-zA-Z0-9_]+)(?=\s*[,\}])/g, ": null")
|
|
406
|
+
// 속성명이 따옴표 없이 시작하는 경우 → 강제로 따옴표 감싸기
|
|
407
|
+
.replace(/([{,])\s*([a-zA-Z0-9_]+)\s*:/g, '$1"$2":');
|
|
408
|
+
let missingChars = [];
|
|
409
|
+
for (let i = 0; i < string.length; i++) {
|
|
410
|
+
const char = string[i];
|
|
411
|
+
if (char === missingChars[missingChars.length - 1]) {
|
|
412
|
+
missingChars.pop();
|
|
413
|
+
}
|
|
414
|
+
else if (dataCloseChar[char]) {
|
|
415
|
+
missingChars.push(dataCloseChar[char]);
|
|
416
|
+
if (char === "{") {
|
|
417
|
+
missingChars.push(":");
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
if (missingChars[missingChars.length - 1] === ":") {
|
|
422
|
+
if (string[string.length - 1] !== "{") {
|
|
423
|
+
missingChars[missingChars.length - 1] = ": null";
|
|
424
|
+
}
|
|
425
|
+
else {
|
|
426
|
+
missingChars.pop();
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
const missingCharsString = missingChars.reverse().join("");
|
|
430
|
+
const completeString = string + missingCharsString;
|
|
431
|
+
const cleanedString = completeString
|
|
432
|
+
.replace(/"":/g, "")
|
|
433
|
+
.replace(/":}|": }/g, '": null }')
|
|
434
|
+
.replace(/,""}|,}|,\"\w+\"}/g, "}")
|
|
435
|
+
.replace(/},]/g, "}]");
|
|
436
|
+
return cleanedString;
|
|
437
|
+
}
|
|
438
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "vectocore4",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"types": "dist/index.d.ts",
|
|
6
|
+
"main": "dis/cjs/index.js",
|
|
7
|
+
"module": "dist/mjs/index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/mjs/index.js",
|
|
11
|
+
"require": "./dist/cjs/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"type": "module",
|
|
15
|
+
"scripts": {
|
|
16
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
17
|
+
"build": "rm -fr dist/* && tsc -p tsconfig.json && tsc -p tsconfig-cjs.json && ./fixup.sh"
|
|
18
|
+
},
|
|
19
|
+
"author": "",
|
|
20
|
+
"license": "ISC",
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"typescript": "^5.6.3"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"axios": "^1.7.7"
|
|
26
|
+
}
|
|
27
|
+
}
|