@horribleprogram/sdk 0.1.5 → 0.1.7
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/dist/chunk-4TRAHT2X.js +577 -0
- package/dist/index.cjs +351 -6
- package/dist/index.d.cts +60 -1
- package/dist/index.d.ts +60 -1
- package/dist/index.js +19 -3
- package/dist/main.cjs +6 -2
- package/dist/main.js +1 -1
- package/package.json +14 -2
- package/dist/chunk-45PYBUYK.js +0 -250
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
8
|
var __export = (target, all) => {
|
|
7
9
|
for (var name in all)
|
|
@@ -15,16 +17,32 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
17
|
}
|
|
16
18
|
return to;
|
|
17
19
|
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
18
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
29
|
|
|
20
30
|
// src/index.ts
|
|
21
31
|
var index_exports = {};
|
|
22
32
|
__export(index_exports, {
|
|
23
33
|
Botcierge: () => Botcierge,
|
|
24
|
-
|
|
34
|
+
CompanyEmbeddingManager: () => CompanyEmbeddingManager,
|
|
35
|
+
addCompany: () => addCompany,
|
|
36
|
+
closeConnection: () => closeConnection,
|
|
37
|
+
companyToText: () => companyToText,
|
|
38
|
+
parseCSV: () => parseCSV,
|
|
39
|
+
queryCompanies: () => queryCompanies,
|
|
40
|
+
query_intent: () => query_intent,
|
|
41
|
+
removeCompany: () => removeCompany,
|
|
42
|
+
seedCompanies: () => seedCompanies
|
|
25
43
|
});
|
|
26
44
|
module.exports = __toCommonJS(index_exports);
|
|
27
|
-
var
|
|
45
|
+
var import_transformers2 = require("@xenova/transformers");
|
|
28
46
|
|
|
29
47
|
// src/intents.ts
|
|
30
48
|
var INTENTS_TAXONOMY = {
|
|
@@ -163,6 +181,325 @@ var INTENTS_TAXONOMY = {
|
|
|
163
181
|
}
|
|
164
182
|
};
|
|
165
183
|
|
|
184
|
+
// src/companyEmbeddings.ts
|
|
185
|
+
var fs = __toESM(require("fs"), 1);
|
|
186
|
+
var import_mongodb = require("mongodb");
|
|
187
|
+
var import_transformers = require("@xenova/transformers");
|
|
188
|
+
function parseCSV(content) {
|
|
189
|
+
const lines = [];
|
|
190
|
+
let currentLine = "";
|
|
191
|
+
let insideQuote = false;
|
|
192
|
+
for (let i = 0; i < content.length; i++) {
|
|
193
|
+
const char = content[i];
|
|
194
|
+
const nextChar = content[i + 1];
|
|
195
|
+
if (char === '"') {
|
|
196
|
+
insideQuote = !insideQuote;
|
|
197
|
+
} else if (char === "\r" || char === "\n") {
|
|
198
|
+
if (!insideQuote) {
|
|
199
|
+
if (currentLine || i === 0 || content[i - 1] === "\n" || content[i - 1] === "\r") {
|
|
200
|
+
lines.push(currentLine);
|
|
201
|
+
currentLine = "";
|
|
202
|
+
}
|
|
203
|
+
if (char === "\r" && nextChar === "\n") {
|
|
204
|
+
i++;
|
|
205
|
+
}
|
|
206
|
+
} else {
|
|
207
|
+
currentLine += char;
|
|
208
|
+
}
|
|
209
|
+
} else {
|
|
210
|
+
currentLine += char;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (currentLine) {
|
|
214
|
+
lines.push(currentLine);
|
|
215
|
+
}
|
|
216
|
+
if (lines.length === 0) return [];
|
|
217
|
+
const parseLine = (line) => {
|
|
218
|
+
const result = [];
|
|
219
|
+
let cell = "";
|
|
220
|
+
let inside = false;
|
|
221
|
+
for (let i = 0; i < line.length; i++) {
|
|
222
|
+
const char = line[i];
|
|
223
|
+
if (char === '"') {
|
|
224
|
+
if (inside && line[i + 1] === '"') {
|
|
225
|
+
cell += '"';
|
|
226
|
+
i++;
|
|
227
|
+
} else {
|
|
228
|
+
inside = !inside;
|
|
229
|
+
}
|
|
230
|
+
} else if (char === "," && !inside) {
|
|
231
|
+
result.push(cell.trim());
|
|
232
|
+
cell = "";
|
|
233
|
+
} else {
|
|
234
|
+
cell += char;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
result.push(cell.trim());
|
|
238
|
+
return result;
|
|
239
|
+
};
|
|
240
|
+
const headers = parseLine(lines[0]);
|
|
241
|
+
const records = [];
|
|
242
|
+
for (let i = 1; i < lines.length; i++) {
|
|
243
|
+
const cells = parseLine(lines[i]);
|
|
244
|
+
if (cells.length === 1 && cells[0] === "") continue;
|
|
245
|
+
const record = {};
|
|
246
|
+
for (let j = 0; j < headers.length; j++) {
|
|
247
|
+
record[headers[j]] = cells[j] || "";
|
|
248
|
+
}
|
|
249
|
+
records.push(record);
|
|
250
|
+
}
|
|
251
|
+
return records;
|
|
252
|
+
}
|
|
253
|
+
function companyToText(company) {
|
|
254
|
+
const parts = [];
|
|
255
|
+
const keys = [
|
|
256
|
+
"company_name",
|
|
257
|
+
"website",
|
|
258
|
+
"short_description",
|
|
259
|
+
"product_description",
|
|
260
|
+
"mapped_function",
|
|
261
|
+
"mapped_industry",
|
|
262
|
+
"match_keywords",
|
|
263
|
+
"aliases",
|
|
264
|
+
"active",
|
|
265
|
+
"priority",
|
|
266
|
+
"source",
|
|
267
|
+
"zone"
|
|
268
|
+
];
|
|
269
|
+
for (const key of keys) {
|
|
270
|
+
if (key in company) {
|
|
271
|
+
const val = String(company[key]).trim();
|
|
272
|
+
if (val) {
|
|
273
|
+
parts.push(`${key}: ${val}`);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
for (const [key, value] of Object.entries(company)) {
|
|
278
|
+
if (keys.includes(key) || key === "_id" || key === "embedding") continue;
|
|
279
|
+
const val = String(value).trim();
|
|
280
|
+
if (val) {
|
|
281
|
+
parts.push(`${key}: ${val}`);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return parts.join(" | ");
|
|
285
|
+
}
|
|
286
|
+
function cosineSimilarity(v1, v2) {
|
|
287
|
+
let dotProduct = 0;
|
|
288
|
+
let norm1 = 0;
|
|
289
|
+
let norm2 = 0;
|
|
290
|
+
for (let i = 0; i < v1.length; i++) {
|
|
291
|
+
dotProduct += v1[i] * v2[i];
|
|
292
|
+
norm1 += v1[i] * v1[i];
|
|
293
|
+
norm2 += v2[i] * v2[i];
|
|
294
|
+
}
|
|
295
|
+
if (norm1 === 0 || norm2 === 0) return 0;
|
|
296
|
+
return dotProduct / (Math.sqrt(norm1) * Math.sqrt(norm2));
|
|
297
|
+
}
|
|
298
|
+
var CompanyEmbeddingManager = class {
|
|
299
|
+
client = null;
|
|
300
|
+
config;
|
|
301
|
+
extractorPromise = null;
|
|
302
|
+
constructor(config = {}) {
|
|
303
|
+
this.config = {
|
|
304
|
+
uri: config.uri || process.env.MONGODB_URI || "mongodb://localhost:27017",
|
|
305
|
+
dbName: config.dbName || process.env.MONGODB_DB_NAME || "distance_metrics",
|
|
306
|
+
collectionName: config.collectionName || "companies",
|
|
307
|
+
openAIApiKey: config.openAIApiKey || process.env.OPENAI_API_KEY || "",
|
|
308
|
+
preferOpenAI: config.preferOpenAI ?? !!(config.openAIApiKey || process.env.OPENAI_API_KEY)
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
async getExtractor() {
|
|
312
|
+
if (!this.extractorPromise) {
|
|
313
|
+
this.extractorPromise = (async () => {
|
|
314
|
+
console.log("Loading local embedding model 'Xenova/all-MiniLM-L12-v2'...");
|
|
315
|
+
const extractor = await (0, import_transformers.pipeline)("feature-extraction", "Xenova/all-MiniLM-L12-v2");
|
|
316
|
+
console.log("Local embedding model loaded successfully.");
|
|
317
|
+
return extractor;
|
|
318
|
+
})();
|
|
319
|
+
}
|
|
320
|
+
return this.extractorPromise;
|
|
321
|
+
}
|
|
322
|
+
async connect() {
|
|
323
|
+
if (!this.client) {
|
|
324
|
+
this.client = new import_mongodb.MongoClient(this.config.uri);
|
|
325
|
+
await this.client.connect();
|
|
326
|
+
}
|
|
327
|
+
return this.client;
|
|
328
|
+
}
|
|
329
|
+
async getCollection() {
|
|
330
|
+
const client = await this.connect();
|
|
331
|
+
return client.db(this.config.dbName).collection(this.config.collectionName);
|
|
332
|
+
}
|
|
333
|
+
async disconnect() {
|
|
334
|
+
if (this.client) {
|
|
335
|
+
await this.client.close();
|
|
336
|
+
this.client = null;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
async getEmbedding(text) {
|
|
340
|
+
if (this.config.preferOpenAI && this.config.openAIApiKey) {
|
|
341
|
+
const embeddings = await this.getOpenAIBatchEmbeddings([text]);
|
|
342
|
+
return embeddings[0];
|
|
343
|
+
}
|
|
344
|
+
const extractor = await this.getExtractor();
|
|
345
|
+
const output = await extractor(text, { pooling: "mean", normalize: true });
|
|
346
|
+
return Array.from(output.data);
|
|
347
|
+
}
|
|
348
|
+
async getEmbeddings(texts) {
|
|
349
|
+
if (texts.length === 0) return [];
|
|
350
|
+
if (this.config.preferOpenAI && this.config.openAIApiKey) {
|
|
351
|
+
return this.getOpenAIBatchEmbeddings(texts);
|
|
352
|
+
}
|
|
353
|
+
const extractor = await this.getExtractor();
|
|
354
|
+
const output = await extractor(texts, { pooling: "mean", normalize: true });
|
|
355
|
+
const dim = output.dims[1];
|
|
356
|
+
const data = output.data;
|
|
357
|
+
const embeddings = [];
|
|
358
|
+
for (let i = 0; i < texts.length; i++) {
|
|
359
|
+
const startIndex = i * dim;
|
|
360
|
+
const vector = Array.from(data.subarray(startIndex, startIndex + dim));
|
|
361
|
+
embeddings.push(vector);
|
|
362
|
+
}
|
|
363
|
+
return embeddings;
|
|
364
|
+
}
|
|
365
|
+
async getOpenAIBatchEmbeddings(texts) {
|
|
366
|
+
const batchSize = 50;
|
|
367
|
+
const results = [];
|
|
368
|
+
for (let i = 0; i < texts.length; i += batchSize) {
|
|
369
|
+
const chunk = texts.slice(i, i + batchSize);
|
|
370
|
+
const response = await fetch("https://api.openai.com/v1/embeddings", {
|
|
371
|
+
method: "POST",
|
|
372
|
+
headers: {
|
|
373
|
+
"Content-Type": "application/json",
|
|
374
|
+
"Authorization": `Bearer ${this.config.openAIApiKey}`
|
|
375
|
+
},
|
|
376
|
+
body: JSON.stringify({
|
|
377
|
+
input: chunk,
|
|
378
|
+
model: "text-embedding-3-small"
|
|
379
|
+
})
|
|
380
|
+
});
|
|
381
|
+
if (!response.ok) {
|
|
382
|
+
const errorText = await response.text();
|
|
383
|
+
throw new Error(`OpenAI API error: ${response.status} - ${errorText}`);
|
|
384
|
+
}
|
|
385
|
+
const json = await response.json();
|
|
386
|
+
results.push(...json.data.map((item) => item.embedding));
|
|
387
|
+
}
|
|
388
|
+
return results;
|
|
389
|
+
}
|
|
390
|
+
async addCompany(company) {
|
|
391
|
+
const col = await this.getCollection();
|
|
392
|
+
let active = company.active;
|
|
393
|
+
if (typeof active === "string") {
|
|
394
|
+
const activeStr = active.toLowerCase().trim();
|
|
395
|
+
active = activeStr === "true" || activeStr === "yes" || activeStr === "1";
|
|
396
|
+
}
|
|
397
|
+
const cleanCompany = {
|
|
398
|
+
...company,
|
|
399
|
+
active: !!active
|
|
400
|
+
};
|
|
401
|
+
if (!cleanCompany.embedding) {
|
|
402
|
+
const text = companyToText(cleanCompany);
|
|
403
|
+
cleanCompany.embedding = await this.getEmbedding(text);
|
|
404
|
+
}
|
|
405
|
+
await col.updateOne(
|
|
406
|
+
{ company_name: cleanCompany.company_name },
|
|
407
|
+
{ $set: cleanCompany },
|
|
408
|
+
{ upsert: true }
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
async removeCompany(companyName) {
|
|
412
|
+
const col = await this.getCollection();
|
|
413
|
+
const result = await col.deleteOne({
|
|
414
|
+
company_name: { $regex: new RegExp(`^${this.escapeRegExp(companyName)}$`, "i") }
|
|
415
|
+
});
|
|
416
|
+
return (result.deletedCount ?? 0) > 0;
|
|
417
|
+
}
|
|
418
|
+
async queryCompanies(prompt, limit = 10) {
|
|
419
|
+
const col = await this.getCollection();
|
|
420
|
+
const queryVector = await this.getEmbedding(prompt);
|
|
421
|
+
const allDocs = await col.find({}).toArray();
|
|
422
|
+
const results = allDocs.filter((doc) => doc.embedding && Array.isArray(doc.embedding)).map((doc) => {
|
|
423
|
+
const similarity = cosineSimilarity(queryVector, doc.embedding);
|
|
424
|
+
return {
|
|
425
|
+
...doc,
|
|
426
|
+
similarity
|
|
427
|
+
};
|
|
428
|
+
});
|
|
429
|
+
results.sort((a, b) => b.similarity - a.similarity);
|
|
430
|
+
return results.slice(0, limit);
|
|
431
|
+
}
|
|
432
|
+
async seedFromCSV(csvPath) {
|
|
433
|
+
const csvContent = fs.readFileSync(csvPath, "utf8");
|
|
434
|
+
const records = parseCSV(csvContent);
|
|
435
|
+
if (records.length === 0) return 0;
|
|
436
|
+
const companies = records.map((rec) => {
|
|
437
|
+
const activeStr = String(rec.active).toLowerCase().trim();
|
|
438
|
+
const active = activeStr === "true" || activeStr === "yes" || activeStr === "1";
|
|
439
|
+
return {
|
|
440
|
+
company_name: rec.company_name || "",
|
|
441
|
+
website: rec.website || "",
|
|
442
|
+
short_description: rec.short_description || "",
|
|
443
|
+
product_description: rec.product_description || "",
|
|
444
|
+
mapped_function: rec.mapped_function || "",
|
|
445
|
+
mapped_industry: rec.mapped_industry || "",
|
|
446
|
+
match_keywords: rec.match_keywords || "",
|
|
447
|
+
aliases: rec.aliases || "",
|
|
448
|
+
active,
|
|
449
|
+
priority: rec.priority || "",
|
|
450
|
+
source: rec.source || "",
|
|
451
|
+
zone: rec.zone || ""
|
|
452
|
+
};
|
|
453
|
+
}).filter((c) => c.company_name);
|
|
454
|
+
const texts = companies.map((c) => companyToText(c));
|
|
455
|
+
console.log(`Generating embeddings for ${companies.length} companies...`);
|
|
456
|
+
const embeddings = await this.getEmbeddings(texts);
|
|
457
|
+
for (let i = 0; i < companies.length; i++) {
|
|
458
|
+
companies[i].embedding = embeddings[i];
|
|
459
|
+
}
|
|
460
|
+
const col = await this.getCollection();
|
|
461
|
+
await col.deleteMany({});
|
|
462
|
+
const batchSize = 100;
|
|
463
|
+
for (let i = 0; i < companies.length; i += batchSize) {
|
|
464
|
+
const batch = companies.slice(i, i + batchSize);
|
|
465
|
+
await col.insertMany(batch);
|
|
466
|
+
}
|
|
467
|
+
return companies.length;
|
|
468
|
+
}
|
|
469
|
+
escapeRegExp(string) {
|
|
470
|
+
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
471
|
+
}
|
|
472
|
+
};
|
|
473
|
+
var defaultManager = null;
|
|
474
|
+
function getDefaultManager(config) {
|
|
475
|
+
if (!defaultManager || config) {
|
|
476
|
+
defaultManager = new CompanyEmbeddingManager(config);
|
|
477
|
+
}
|
|
478
|
+
return defaultManager;
|
|
479
|
+
}
|
|
480
|
+
async function seedCompanies(csvPath, config) {
|
|
481
|
+
const manager = getDefaultManager(config);
|
|
482
|
+
return manager.seedFromCSV(csvPath);
|
|
483
|
+
}
|
|
484
|
+
async function addCompany(company, config) {
|
|
485
|
+
const manager = getDefaultManager(config);
|
|
486
|
+
return manager.addCompany(company);
|
|
487
|
+
}
|
|
488
|
+
async function removeCompany(companyName, config) {
|
|
489
|
+
const manager = getDefaultManager(config);
|
|
490
|
+
return manager.removeCompany(companyName);
|
|
491
|
+
}
|
|
492
|
+
async function queryCompanies(prompt, limit, config) {
|
|
493
|
+
const manager = getDefaultManager(config);
|
|
494
|
+
return manager.queryCompanies(prompt, limit);
|
|
495
|
+
}
|
|
496
|
+
async function closeConnection() {
|
|
497
|
+
if (defaultManager) {
|
|
498
|
+
await defaultManager.disconnect();
|
|
499
|
+
defaultManager = null;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
|
|
166
503
|
// src/index.ts
|
|
167
504
|
var extractorPromise = null;
|
|
168
505
|
function getExtractor() {
|
|
@@ -170,7 +507,7 @@ function getExtractor() {
|
|
|
170
507
|
extractorPromise = (async () => {
|
|
171
508
|
console.log("Loading local embedding model 'Xenova/all-MiniLM-L12-v2'...");
|
|
172
509
|
console.log("Note: On first run, this will download the model files (approx. 120MB).");
|
|
173
|
-
const extractor = await (0,
|
|
510
|
+
const extractor = await (0, import_transformers2.pipeline)("feature-extraction", "Xenova/all-MiniLM-L12-v2");
|
|
174
511
|
console.log("Local embedding model loaded successfully.");
|
|
175
512
|
return extractor;
|
|
176
513
|
})();
|
|
@@ -211,7 +548,7 @@ async function getTaxonomyEmbeddings() {
|
|
|
211
548
|
})();
|
|
212
549
|
return cachedTaxonomyEmbeddingsPromise;
|
|
213
550
|
}
|
|
214
|
-
function
|
|
551
|
+
function cosineSimilarity2(v1, v2) {
|
|
215
552
|
let dotProduct = 0;
|
|
216
553
|
let norm1 = 0;
|
|
217
554
|
let norm2 = 0;
|
|
@@ -234,7 +571,7 @@ var Botcierge = class {
|
|
|
234
571
|
let bestSim = -1;
|
|
235
572
|
let bestItem = null;
|
|
236
573
|
for (const item of taxEmbeddings) {
|
|
237
|
-
const sim =
|
|
574
|
+
const sim = cosineSimilarity2(queryVector, item.vector);
|
|
238
575
|
if (sim > bestSim) {
|
|
239
576
|
bestSim = sim;
|
|
240
577
|
bestItem = item;
|
|
@@ -271,5 +608,13 @@ async function query_intent(utterance) {
|
|
|
271
608
|
// Annotate the CommonJS export names for ESM import in node:
|
|
272
609
|
0 && (module.exports = {
|
|
273
610
|
Botcierge,
|
|
274
|
-
|
|
611
|
+
CompanyEmbeddingManager,
|
|
612
|
+
addCompany,
|
|
613
|
+
closeConnection,
|
|
614
|
+
companyToText,
|
|
615
|
+
parseCSV,
|
|
616
|
+
queryCompanies,
|
|
617
|
+
query_intent,
|
|
618
|
+
removeCompany,
|
|
619
|
+
seedCompanies
|
|
275
620
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,3 +1,62 @@
|
|
|
1
|
+
import { MongoClient, Collection } from 'mongodb';
|
|
2
|
+
|
|
3
|
+
interface Company {
|
|
4
|
+
company_name: string;
|
|
5
|
+
website: string;
|
|
6
|
+
short_description: string;
|
|
7
|
+
product_description: string;
|
|
8
|
+
mapped_function: string;
|
|
9
|
+
mapped_industry: string;
|
|
10
|
+
match_keywords: string;
|
|
11
|
+
aliases: string;
|
|
12
|
+
active: boolean | string;
|
|
13
|
+
priority: string;
|
|
14
|
+
source: string;
|
|
15
|
+
zone: string;
|
|
16
|
+
embedding?: number[];
|
|
17
|
+
[key: string]: any;
|
|
18
|
+
}
|
|
19
|
+
interface MongoEmbeddingConfig {
|
|
20
|
+
uri?: string;
|
|
21
|
+
dbName?: string;
|
|
22
|
+
collectionName?: string;
|
|
23
|
+
openAIApiKey?: string;
|
|
24
|
+
preferOpenAI?: boolean;
|
|
25
|
+
}
|
|
26
|
+
declare function parseCSV(content: string): Record<string, string>[];
|
|
27
|
+
declare function companyToText(company: Partial<Company>): string;
|
|
28
|
+
declare class CompanyEmbeddingManager {
|
|
29
|
+
private client;
|
|
30
|
+
private config;
|
|
31
|
+
private extractorPromise;
|
|
32
|
+
constructor(config?: MongoEmbeddingConfig);
|
|
33
|
+
private getExtractor;
|
|
34
|
+
connect(): Promise<MongoClient>;
|
|
35
|
+
getCollection(): Promise<Collection<Company>>;
|
|
36
|
+
disconnect(): Promise<void>;
|
|
37
|
+
getEmbedding(text: string): Promise<number[]>;
|
|
38
|
+
getEmbeddings(texts: string[]): Promise<number[][]>;
|
|
39
|
+
private getOpenAIBatchEmbeddings;
|
|
40
|
+
addCompany(company: Omit<Company, 'embedding'> & {
|
|
41
|
+
embedding?: number[];
|
|
42
|
+
}): Promise<void>;
|
|
43
|
+
removeCompany(companyName: string): Promise<boolean>;
|
|
44
|
+
queryCompanies(prompt: string, limit?: number): Promise<(Company & {
|
|
45
|
+
similarity: number;
|
|
46
|
+
})[]>;
|
|
47
|
+
seedFromCSV(csvPath: string): Promise<number>;
|
|
48
|
+
private escapeRegExp;
|
|
49
|
+
}
|
|
50
|
+
declare function seedCompanies(csvPath: string, config?: MongoEmbeddingConfig): Promise<number>;
|
|
51
|
+
declare function addCompany(company: Omit<Company, 'embedding'> & {
|
|
52
|
+
embedding?: number[];
|
|
53
|
+
}, config?: MongoEmbeddingConfig): Promise<void>;
|
|
54
|
+
declare function removeCompany(companyName: string, config?: MongoEmbeddingConfig): Promise<boolean>;
|
|
55
|
+
declare function queryCompanies(prompt: string, limit?: number, config?: MongoEmbeddingConfig): Promise<(Company & {
|
|
56
|
+
similarity: number;
|
|
57
|
+
})[]>;
|
|
58
|
+
declare function closeConnection(): Promise<void>;
|
|
59
|
+
|
|
1
60
|
interface IntentResult {
|
|
2
61
|
domain: string;
|
|
3
62
|
intent: string;
|
|
@@ -15,4 +74,4 @@ declare class Botcierge {
|
|
|
15
74
|
*/
|
|
16
75
|
declare function query_intent(utterance: string): Promise<IntentResult>;
|
|
17
76
|
|
|
18
|
-
export { Botcierge, type BotciergeConfig, type IntentResult, query_intent };
|
|
77
|
+
export { Botcierge, type BotciergeConfig, type Company, CompanyEmbeddingManager, type IntentResult, type MongoEmbeddingConfig, addCompany, closeConnection, companyToText, parseCSV, queryCompanies, query_intent, removeCompany, seedCompanies };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,62 @@
|
|
|
1
|
+
import { MongoClient, Collection } from 'mongodb';
|
|
2
|
+
|
|
3
|
+
interface Company {
|
|
4
|
+
company_name: string;
|
|
5
|
+
website: string;
|
|
6
|
+
short_description: string;
|
|
7
|
+
product_description: string;
|
|
8
|
+
mapped_function: string;
|
|
9
|
+
mapped_industry: string;
|
|
10
|
+
match_keywords: string;
|
|
11
|
+
aliases: string;
|
|
12
|
+
active: boolean | string;
|
|
13
|
+
priority: string;
|
|
14
|
+
source: string;
|
|
15
|
+
zone: string;
|
|
16
|
+
embedding?: number[];
|
|
17
|
+
[key: string]: any;
|
|
18
|
+
}
|
|
19
|
+
interface MongoEmbeddingConfig {
|
|
20
|
+
uri?: string;
|
|
21
|
+
dbName?: string;
|
|
22
|
+
collectionName?: string;
|
|
23
|
+
openAIApiKey?: string;
|
|
24
|
+
preferOpenAI?: boolean;
|
|
25
|
+
}
|
|
26
|
+
declare function parseCSV(content: string): Record<string, string>[];
|
|
27
|
+
declare function companyToText(company: Partial<Company>): string;
|
|
28
|
+
declare class CompanyEmbeddingManager {
|
|
29
|
+
private client;
|
|
30
|
+
private config;
|
|
31
|
+
private extractorPromise;
|
|
32
|
+
constructor(config?: MongoEmbeddingConfig);
|
|
33
|
+
private getExtractor;
|
|
34
|
+
connect(): Promise<MongoClient>;
|
|
35
|
+
getCollection(): Promise<Collection<Company>>;
|
|
36
|
+
disconnect(): Promise<void>;
|
|
37
|
+
getEmbedding(text: string): Promise<number[]>;
|
|
38
|
+
getEmbeddings(texts: string[]): Promise<number[][]>;
|
|
39
|
+
private getOpenAIBatchEmbeddings;
|
|
40
|
+
addCompany(company: Omit<Company, 'embedding'> & {
|
|
41
|
+
embedding?: number[];
|
|
42
|
+
}): Promise<void>;
|
|
43
|
+
removeCompany(companyName: string): Promise<boolean>;
|
|
44
|
+
queryCompanies(prompt: string, limit?: number): Promise<(Company & {
|
|
45
|
+
similarity: number;
|
|
46
|
+
})[]>;
|
|
47
|
+
seedFromCSV(csvPath: string): Promise<number>;
|
|
48
|
+
private escapeRegExp;
|
|
49
|
+
}
|
|
50
|
+
declare function seedCompanies(csvPath: string, config?: MongoEmbeddingConfig): Promise<number>;
|
|
51
|
+
declare function addCompany(company: Omit<Company, 'embedding'> & {
|
|
52
|
+
embedding?: number[];
|
|
53
|
+
}, config?: MongoEmbeddingConfig): Promise<void>;
|
|
54
|
+
declare function removeCompany(companyName: string, config?: MongoEmbeddingConfig): Promise<boolean>;
|
|
55
|
+
declare function queryCompanies(prompt: string, limit?: number, config?: MongoEmbeddingConfig): Promise<(Company & {
|
|
56
|
+
similarity: number;
|
|
57
|
+
})[]>;
|
|
58
|
+
declare function closeConnection(): Promise<void>;
|
|
59
|
+
|
|
1
60
|
interface IntentResult {
|
|
2
61
|
domain: string;
|
|
3
62
|
intent: string;
|
|
@@ -15,4 +74,4 @@ declare class Botcierge {
|
|
|
15
74
|
*/
|
|
16
75
|
declare function query_intent(utterance: string): Promise<IntentResult>;
|
|
17
76
|
|
|
18
|
-
export { Botcierge, type BotciergeConfig, type IntentResult, query_intent };
|
|
77
|
+
export { Botcierge, type BotciergeConfig, type Company, CompanyEmbeddingManager, type IntentResult, type MongoEmbeddingConfig, addCompany, closeConnection, companyToText, parseCSV, queryCompanies, query_intent, removeCompany, seedCompanies };
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,24 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Botcierge,
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
CompanyEmbeddingManager,
|
|
4
|
+
addCompany,
|
|
5
|
+
closeConnection,
|
|
6
|
+
companyToText,
|
|
7
|
+
parseCSV,
|
|
8
|
+
queryCompanies,
|
|
9
|
+
query_intent,
|
|
10
|
+
removeCompany,
|
|
11
|
+
seedCompanies
|
|
12
|
+
} from "./chunk-4TRAHT2X.js";
|
|
5
13
|
export {
|
|
6
14
|
Botcierge,
|
|
7
|
-
|
|
15
|
+
CompanyEmbeddingManager,
|
|
16
|
+
addCompany,
|
|
17
|
+
closeConnection,
|
|
18
|
+
companyToText,
|
|
19
|
+
parseCSV,
|
|
20
|
+
queryCompanies,
|
|
21
|
+
query_intent,
|
|
22
|
+
removeCompany,
|
|
23
|
+
seedCompanies
|
|
8
24
|
};
|
package/dist/main.cjs
CHANGED
|
@@ -26,7 +26,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
26
26
|
var readline = __toESM(require("readline"), 1);
|
|
27
27
|
|
|
28
28
|
// src/index.ts
|
|
29
|
-
var
|
|
29
|
+
var import_transformers2 = require("@xenova/transformers");
|
|
30
30
|
|
|
31
31
|
// src/intents.ts
|
|
32
32
|
var INTENTS_TAXONOMY = {
|
|
@@ -165,6 +165,10 @@ var INTENTS_TAXONOMY = {
|
|
|
165
165
|
}
|
|
166
166
|
};
|
|
167
167
|
|
|
168
|
+
// src/companyEmbeddings.ts
|
|
169
|
+
var import_mongodb = require("mongodb");
|
|
170
|
+
var import_transformers = require("@xenova/transformers");
|
|
171
|
+
|
|
168
172
|
// src/index.ts
|
|
169
173
|
var extractorPromise = null;
|
|
170
174
|
function getExtractor() {
|
|
@@ -172,7 +176,7 @@ function getExtractor() {
|
|
|
172
176
|
extractorPromise = (async () => {
|
|
173
177
|
console.log("Loading local embedding model 'Xenova/all-MiniLM-L12-v2'...");
|
|
174
178
|
console.log("Note: On first run, this will download the model files (approx. 120MB).");
|
|
175
|
-
const extractor = await (0,
|
|
179
|
+
const extractor = await (0, import_transformers2.pipeline)("feature-extraction", "Xenova/all-MiniLM-L12-v2");
|
|
176
180
|
console.log("Local embedding model loaded successfully.");
|
|
177
181
|
return extractor;
|
|
178
182
|
})();
|
package/dist/main.js
CHANGED
package/package.json
CHANGED
|
@@ -1,11 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@horribleprogram/sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"description": "SDK for Botcierge Intent Classification",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
7
7
|
"module": "./dist/index.mjs",
|
|
8
8
|
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
},
|
|
15
|
+
"./dist": {
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
|
+
},
|
|
18
|
+
"./dist/*": "./dist/*"
|
|
19
|
+
},
|
|
9
20
|
"files": [
|
|
10
21
|
"dist"
|
|
11
22
|
],
|
|
@@ -33,6 +44,7 @@
|
|
|
33
44
|
"vitest": "^4.1.6"
|
|
34
45
|
},
|
|
35
46
|
"dependencies": {
|
|
36
|
-
"@xenova/transformers": "^2.17.2"
|
|
47
|
+
"@xenova/transformers": "^2.17.2",
|
|
48
|
+
"mongodb": "^7.4.0"
|
|
37
49
|
}
|
|
38
50
|
}
|