@horribleprogram/sdk 0.1.6 → 0.1.8
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 +203 -26
- package/dist/chunk-UKIYZY2F.js +578 -0
- package/dist/index.cjs +352 -6
- package/dist/index.d.cts +61 -1
- package/dist/index.d.ts +61 -1
- package/dist/index.js +19 -3
- package/dist/main.cjs +6 -2
- package/dist/main.js +1 -1
- package/package.json +3 -2
- package/dist/chunk-45PYBUYK.js +0 -250
package/dist/chunk-45PYBUYK.js
DELETED
|
@@ -1,250 +0,0 @@
|
|
|
1
|
-
// src/index.ts
|
|
2
|
-
import { pipeline } from "@xenova/transformers";
|
|
3
|
-
|
|
4
|
-
// src/intents.ts
|
|
5
|
-
var INTENTS_TAXONOMY = {
|
|
6
|
-
"GIFTMINDER": {
|
|
7
|
-
"view_events": [
|
|
8
|
-
"I want to see events coming up"
|
|
9
|
-
],
|
|
10
|
-
"view_wishlists": [
|
|
11
|
-
"I want to see wishlists"
|
|
12
|
-
],
|
|
13
|
-
"buy_gifts": [
|
|
14
|
-
"I want to buy gifts for my loved ones"
|
|
15
|
-
]
|
|
16
|
-
},
|
|
17
|
-
"REMEMBOT": {
|
|
18
|
-
"remember": [
|
|
19
|
-
"I want to remember something"
|
|
20
|
-
],
|
|
21
|
-
"recall": [
|
|
22
|
-
"I want to recall something"
|
|
23
|
-
]
|
|
24
|
-
},
|
|
25
|
-
"RESCUEBIDS": {
|
|
26
|
-
"avoid_fee": [
|
|
27
|
-
"I want to avoid a bank fee"
|
|
28
|
-
],
|
|
29
|
-
"request_loan": [
|
|
30
|
-
"I need a small loan"
|
|
31
|
-
]
|
|
32
|
-
},
|
|
33
|
-
"FOODLINK": {
|
|
34
|
-
"request_food": [
|
|
35
|
-
"I'm hungry",
|
|
36
|
-
"I am hungry"
|
|
37
|
-
],
|
|
38
|
-
"share_food": [
|
|
39
|
-
"I have food to share",
|
|
40
|
-
"I want to share some food"
|
|
41
|
-
]
|
|
42
|
-
},
|
|
43
|
-
"BILLBRIDGE": {
|
|
44
|
-
"request_bill_help": [
|
|
45
|
-
"I need help with a bill"
|
|
46
|
-
]
|
|
47
|
-
},
|
|
48
|
-
"BLOODSHARE": {
|
|
49
|
-
"request_blood": [
|
|
50
|
-
"I need blood"
|
|
51
|
-
],
|
|
52
|
-
"share_blood": [
|
|
53
|
-
"I have blood to share"
|
|
54
|
-
]
|
|
55
|
-
},
|
|
56
|
-
"MATH": {
|
|
57
|
-
"compute_expression": [
|
|
58
|
-
"compute xplusonesquared",
|
|
59
|
-
"compute xsquaredplusone",
|
|
60
|
-
"Compute x squared plus one",
|
|
61
|
-
"Compute x plus one squared"
|
|
62
|
-
]
|
|
63
|
-
},
|
|
64
|
-
"SHOP_SAVVY": {
|
|
65
|
-
"add_item": [
|
|
66
|
-
"Add an item to my shopping list",
|
|
67
|
-
"I want to add items to my shopping list"
|
|
68
|
-
],
|
|
69
|
-
"remove_item": [
|
|
70
|
-
"Remove an item from my shopping list"
|
|
71
|
-
],
|
|
72
|
-
"view_list": [
|
|
73
|
-
"What's on my shopping list",
|
|
74
|
-
"I want to see my shopping list"
|
|
75
|
-
],
|
|
76
|
-
"edit_list": [
|
|
77
|
-
"I want to change my shopping list"
|
|
78
|
-
],
|
|
79
|
-
"mark_purchased": [
|
|
80
|
-
"I've purchased some items on my shopping list",
|
|
81
|
-
"I have purchased some items on my shopping list"
|
|
82
|
-
]
|
|
83
|
-
},
|
|
84
|
-
"BOT_STORE": {
|
|
85
|
-
"add_package": [
|
|
86
|
-
"I want to buy a package",
|
|
87
|
-
"I want to add a package"
|
|
88
|
-
],
|
|
89
|
-
"remove_package": [
|
|
90
|
-
"I want to stop using a package",
|
|
91
|
-
"I want to remove a package"
|
|
92
|
-
]
|
|
93
|
-
},
|
|
94
|
-
"TASKMASTERAI": {
|
|
95
|
-
"add_task": [
|
|
96
|
-
"I want to add a to do list item to my list",
|
|
97
|
-
"Add a to do list item",
|
|
98
|
-
"I want to add a to do list item"
|
|
99
|
-
],
|
|
100
|
-
"view_tasks": [
|
|
101
|
-
"I want to see my to do list",
|
|
102
|
-
"Show to do list",
|
|
103
|
-
"Show my to do list"
|
|
104
|
-
],
|
|
105
|
-
"edit_tasks": [
|
|
106
|
-
"I want to change my to do list",
|
|
107
|
-
"I want to edit my to do list"
|
|
108
|
-
],
|
|
109
|
-
"remove_task": [
|
|
110
|
-
"I want to remove items from my to do list"
|
|
111
|
-
]
|
|
112
|
-
},
|
|
113
|
-
"FLOWPLANNER": {
|
|
114
|
-
"create_flow_plan": [
|
|
115
|
-
"Create a flow plan for tomorrow"
|
|
116
|
-
]
|
|
117
|
-
},
|
|
118
|
-
"CGM": {
|
|
119
|
-
"find_company": [
|
|
120
|
-
"I need a company that...",
|
|
121
|
-
"I want a company that..."
|
|
122
|
-
]
|
|
123
|
-
},
|
|
124
|
-
"TIFFANY": {
|
|
125
|
-
"process_dental_patient": [
|
|
126
|
-
"I want to process the patient visit for <patient-name>"
|
|
127
|
-
]
|
|
128
|
-
},
|
|
129
|
-
"CAREMEDIC": {
|
|
130
|
-
"process_medical_patient": [
|
|
131
|
-
"I want to process the patient visit for <patient-name>"
|
|
132
|
-
]
|
|
133
|
-
},
|
|
134
|
-
"DISCRETION": {
|
|
135
|
-
"retrieval_augmented_generation": [
|
|
136
|
-
"I need to retrieve information from my documents"
|
|
137
|
-
]
|
|
138
|
-
}
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
// src/index.ts
|
|
142
|
-
var extractorPromise = null;
|
|
143
|
-
function getExtractor() {
|
|
144
|
-
if (!extractorPromise) {
|
|
145
|
-
extractorPromise = (async () => {
|
|
146
|
-
console.log("Loading local embedding model 'Xenova/all-MiniLM-L12-v2'...");
|
|
147
|
-
console.log("Note: On first run, this will download the model files (approx. 120MB).");
|
|
148
|
-
const extractor = await pipeline("feature-extraction", "Xenova/all-MiniLM-L12-v2");
|
|
149
|
-
console.log("Local embedding model loaded successfully.");
|
|
150
|
-
return extractor;
|
|
151
|
-
})();
|
|
152
|
-
}
|
|
153
|
-
return extractorPromise;
|
|
154
|
-
}
|
|
155
|
-
var cachedTaxonomyEmbeddingsPromise = null;
|
|
156
|
-
async function getTaxonomyEmbeddings() {
|
|
157
|
-
if (cachedTaxonomyEmbeddingsPromise) {
|
|
158
|
-
return cachedTaxonomyEmbeddingsPromise;
|
|
159
|
-
}
|
|
160
|
-
cachedTaxonomyEmbeddingsPromise = (async () => {
|
|
161
|
-
const extractor = await getExtractor();
|
|
162
|
-
const items = [];
|
|
163
|
-
for (const [domain, intents] of Object.entries(INTENTS_TAXONOMY)) {
|
|
164
|
-
for (const [intent, utterances] of Object.entries(intents)) {
|
|
165
|
-
for (const utterance of utterances) {
|
|
166
|
-
if (utterance.trim()) {
|
|
167
|
-
items.push({ domain, intent, utterance });
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
const texts = items.map((item) => item.utterance);
|
|
173
|
-
const output = await extractor(texts, { pooling: "mean", normalize: true });
|
|
174
|
-
const dim = output.dims[1];
|
|
175
|
-
const data = output.data;
|
|
176
|
-
const embeddings = [];
|
|
177
|
-
for (let i = 0; i < items.length; i++) {
|
|
178
|
-
const startIndex = i * dim;
|
|
179
|
-
const vector = Array.from(data.subarray(startIndex, startIndex + dim));
|
|
180
|
-
embeddings.push({
|
|
181
|
-
...items[i],
|
|
182
|
-
vector
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
return embeddings;
|
|
186
|
-
})();
|
|
187
|
-
return cachedTaxonomyEmbeddingsPromise;
|
|
188
|
-
}
|
|
189
|
-
function cosineSimilarity(v1, v2) {
|
|
190
|
-
let dotProduct = 0;
|
|
191
|
-
let norm1 = 0;
|
|
192
|
-
let norm2 = 0;
|
|
193
|
-
for (let i = 0; i < v1.length; i++) {
|
|
194
|
-
dotProduct += v1[i] * v2[i];
|
|
195
|
-
norm1 += v1[i] * v1[i];
|
|
196
|
-
norm2 += v2[i] * v2[i];
|
|
197
|
-
}
|
|
198
|
-
if (norm1 === 0 || norm2 === 0) return 0;
|
|
199
|
-
return dotProduct / (Math.sqrt(norm1) * Math.sqrt(norm2));
|
|
200
|
-
}
|
|
201
|
-
var Botcierge = class {
|
|
202
|
-
constructor(config = {}) {
|
|
203
|
-
}
|
|
204
|
-
async query_intent(utterance) {
|
|
205
|
-
const extractor = await getExtractor();
|
|
206
|
-
const output = await extractor(utterance, { pooling: "mean", normalize: true });
|
|
207
|
-
const queryVector = Array.from(output.data);
|
|
208
|
-
const taxEmbeddings = await getTaxonomyEmbeddings();
|
|
209
|
-
let bestSim = -1;
|
|
210
|
-
let bestItem = null;
|
|
211
|
-
for (const item of taxEmbeddings) {
|
|
212
|
-
const sim = cosineSimilarity(queryVector, item.vector);
|
|
213
|
-
if (sim > bestSim) {
|
|
214
|
-
bestSim = sim;
|
|
215
|
-
bestItem = item;
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
if (!bestItem || bestSim < 0.35) {
|
|
219
|
-
return {
|
|
220
|
-
domain: "unknown",
|
|
221
|
-
intent: "unknown",
|
|
222
|
-
confidence: "low",
|
|
223
|
-
scores: {}
|
|
224
|
-
};
|
|
225
|
-
}
|
|
226
|
-
let confidence = "low";
|
|
227
|
-
if (bestSim >= 0.7) {
|
|
228
|
-
confidence = "high";
|
|
229
|
-
} else if (bestSim >= 0.5) {
|
|
230
|
-
confidence = "medium";
|
|
231
|
-
}
|
|
232
|
-
return {
|
|
233
|
-
domain: bestItem.domain,
|
|
234
|
-
intent: bestItem.intent,
|
|
235
|
-
confidence,
|
|
236
|
-
scores: {
|
|
237
|
-
[bestItem.intent]: bestSim
|
|
238
|
-
}
|
|
239
|
-
};
|
|
240
|
-
}
|
|
241
|
-
};
|
|
242
|
-
var defaultClient = new Botcierge();
|
|
243
|
-
async function query_intent(utterance) {
|
|
244
|
-
return defaultClient.query_intent(utterance);
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
export {
|
|
248
|
-
Botcierge,
|
|
249
|
-
query_intent
|
|
250
|
-
};
|