@botpress/vai 0.0.1 → 0.0.2-beta.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/dist/index.js CHANGED
@@ -1,473 +1,8 @@
1
- var __defProp = Object.defineProperty;
2
- var __defProps = Object.defineProperties;
3
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
- var __typeError = (msg) => {
8
- throw TypeError(msg);
9
- };
10
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11
- var __spreadValues = (a, b) => {
12
- for (var prop in b || (b = {}))
13
- if (__hasOwnProp.call(b, prop))
14
- __defNormalProp(a, prop, b[prop]);
15
- if (__getOwnPropSymbols)
16
- for (var prop of __getOwnPropSymbols(b)) {
17
- if (__propIsEnum.call(b, prop))
18
- __defNormalProp(a, prop, b[prop]);
19
- }
20
- return a;
21
- };
22
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
23
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
24
- var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
25
- var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
26
- var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
27
- var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
28
-
29
- // src/utils/zui.ts
30
- import sdk from "@botpress/sdk";
31
- var z = sdk.z;
32
-
33
- // src/task/compare.ts
34
- import { createTaskCollector, getCurrentSuite } from "vitest/suite";
35
-
36
- // src/utils/deferred.ts
37
- var _Deferred = class _Deferred {
38
- constructor() {
39
- this.promise = new Promise((resolve, reject) => {
40
- this._resolve = resolve;
41
- this._reject = reject;
42
- });
43
- }
44
- resolve(value) {
45
- this._resolve(value);
46
- }
47
- reject(reason) {
48
- this._reject(reason);
49
- }
50
- };
51
- __name(_Deferred, "Deferred");
52
- var Deferred = _Deferred;
53
-
54
- // src/task/compare.ts
55
- var scenarioId = z.string().trim().min(1, "Scenario ID/name must not be empty").max(50, "Scenario ID/name is too long");
56
- var ScenarioLike = z.union([
57
- scenarioId,
58
- z.object({ name: scenarioId }).passthrough(),
59
- z.object({ id: scenarioId }).passthrough()
60
- ]);
61
- var getScenarioName = /* @__PURE__ */ __name((scenario) => typeof scenario === "string" ? scenario : "name" in scenario ? scenario == null ? void 0 : scenario.name : scenario == null ? void 0 : scenario.id, "getScenarioName");
62
- var scenarioArgs = z.array(ScenarioLike).min(2, "You need at least two scenarios to compare").max(10, "You can only compare up to 10 scenarios").refine((scenarios) => {
63
- const set = /* @__PURE__ */ new Set();
64
- scenarios.forEach((scenario) => set.add(getScenarioName(scenario)));
65
- return set.size === scenarios.length;
66
- }, "Scenarios names must be unique");
67
- function compare(name, scenarios, fn) {
68
- scenarios = scenarioArgs.parse(scenarios);
69
- return createTaskCollector((_name, fn2, timeout) => {
70
- const currentSuite = getCurrentSuite();
71
- let completedCount = 0;
72
- const finished = new Deferred();
73
- for (const scenario of scenarios) {
74
- const key = getScenarioName(scenario);
75
- currentSuite.task(key, {
76
- meta: {
77
- scenario: key,
78
- isVaiTest: true
79
- },
80
- handler: /* @__PURE__ */ __name(async (context) => {
81
- const extendedContext = Object.freeze({
82
- scenario
83
- });
84
- context.onTestFinished(() => {
85
- if (++completedCount === scenarios.length) {
86
- finished.resolve();
87
- }
88
- });
89
- await fn2(__spreadValues(__spreadValues({}, context), extendedContext));
90
- }, "handler"),
91
- timeout: timeout != null ? timeout : 1e4
92
- });
93
- }
94
- })(name, fn);
95
- }
96
- __name(compare, "compare");
97
-
98
- // src/context.ts
99
- import { onTestFinished } from "vitest";
100
- import { getCurrentTest } from "vitest/suite";
101
- var getTestMetadata = /* @__PURE__ */ __name(() => {
102
- var _a;
103
- const test = getCurrentTest();
104
- return (_a = test == null ? void 0 : test.meta) != null ? _a : {
105
- isVaiTest: false
106
- };
107
- }, "getTestMetadata");
108
- var _client, _wrapError;
109
- var _VaiContext = class _VaiContext {
110
- constructor() {
111
- __privateAdd(this, _client, null);
112
- __privateAdd(this, _wrapError, false);
113
- }
114
- get wrapError() {
115
- return __privateGet(this, _wrapError);
116
- }
117
- get client() {
118
- if (!__privateGet(this, _client)) {
119
- throw new Error("Botpress client is not set");
120
- }
121
- return __privateGet(this, _client);
122
- }
123
- get evaluatorModel() {
124
- var _a;
125
- return (_a = getTestMetadata().evaluatorModel) != null ? _a : "openai__gpt-4o-mini-2024-07-18";
126
- }
127
- get scenario() {
128
- return getTestMetadata().scenario;
129
- }
130
- get isVaiTest() {
131
- return getTestMetadata().isVaiTest;
132
- }
133
- setClient(cognitive) {
134
- __privateSet(this, _client, cognitive);
135
- }
136
- swallowErrors() {
137
- if (!getCurrentTest()) {
138
- throw new Error("cancelBail is a Vitest hook and must be called within a test");
139
- }
140
- __privateSet(this, _wrapError, true);
141
- onTestFinished(() => {
142
- __privateSet(this, _wrapError, false);
143
- });
144
- }
145
- };
146
- _client = new WeakMap();
147
- _wrapError = new WeakMap();
148
- __name(_VaiContext, "VaiContext");
149
- var VaiContext = _VaiContext;
150
- var Context = new VaiContext();
151
-
152
- // src/utils/asyncAssertion.ts
153
- import { expect } from "vitest";
154
- import { getCurrentTest as getCurrentTest2 } from "vitest/suite";
155
- var _AsyncExpectError = class _AsyncExpectError extends Error {
156
- constructor(message, output) {
157
- super(message);
158
- this.output = output;
159
- this.name = "AsyncExpectError";
160
- }
161
- };
162
- __name(_AsyncExpectError, "AsyncExpectError");
163
- var AsyncExpectError = _AsyncExpectError;
164
- var getErrorMessages = /* @__PURE__ */ __name((e) => {
165
- if (e instanceof Error) {
166
- return e.message;
167
- } else if (typeof e === "string") {
168
- return e;
169
- } else if (typeof e === "object" && e !== null) {
170
- return JSON.stringify(e);
171
- }
172
- return `Unknown error: ${e}`;
173
- }, "getErrorMessages");
174
- var asyncExpect = /* @__PURE__ */ __name((output, assertion) => {
175
- var _a, _b;
176
- const promise = output.then((x) => {
177
- try {
178
- assertion(expect(x.result, x.reason));
179
- } catch (e) {
180
- if (Context.wrapError) {
181
- return new AsyncExpectError(getErrorMessages(e), x);
182
- }
183
- throw e;
184
- }
185
- return x;
186
- });
187
- (_b = (_a = getCurrentTest2()).promises) != null ? _b : _a.promises = [];
188
- getCurrentTest2().promises.push(promise);
189
- return promise;
190
- }, "asyncExpect");
191
-
192
- // src/utils/predictJson.ts
193
- import JSON5 from "json5";
194
- var nonEmptyString = z.string().trim().min(1);
195
- var nonEmptyObject = z.object({}).passthrough().refine((value) => Object.keys(value).length > 0, {
196
- message: "Expected a non-empty object"
197
- });
198
- var Input = nonEmptyString.or(nonEmptyObject).or(z.array(z.any()));
199
- var Output = z.object({
200
- reason: nonEmptyString.describe("A human-readable explanation of the result"),
201
- result: z.any().describe(
202
- "Your best guess at the output according to the instructions provided, rooted in the context of the input and the reason above"
203
- )
204
- });
205
- var Example = z.object({
206
- input: Input,
207
- output: Output
208
- });
209
- var Options = z.object({
210
- systemMessage: z.string(),
211
- examples: z.array(Example).default([]),
212
- input: Input,
213
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
214
- outputSchema: z.custom((value) => typeof value === "object" && value !== null && "_def" in value),
215
- model: z.string()
216
- });
217
- var isValidExample = /* @__PURE__ */ __name((outputSchema) => (example) => Input.safeParse(example.input).success && Output.safeParse(example.output).success && outputSchema.safeParse(example.output.result).success, "isValidExample");
218
- async function predictJson(_options) {
219
- var _a, _b;
220
- const options = Options.parse(_options);
221
- const [integration, model] = options.model.split("__");
222
- if (!(model == null ? void 0 : model.length)) {
223
- throw new Error("Invalid model");
224
- }
225
- const exampleMessages = options.examples.filter(isValidExample(options.outputSchema)).flatMap(({ input, output: output2 }) => [
226
- { role: "user", content: JSON.stringify(input, null, 2) },
227
- { role: "assistant", content: JSON.stringify(output2, null, 2) }
228
- ]);
229
- const outputSchema = Output.extend({
230
- result: options.outputSchema.describe(Output.shape.result.description)
231
- });
232
- const result = await Context.client.callAction({
233
- type: `${integration}:generateContent`,
234
- input: {
235
- systemPrompt: `
236
- ${options.systemMessage}
237
-
238
- ---
239
- Please generate a JSON response with the following format:
240
- \`\`\`typescript
241
- ${await outputSchema.toTypescriptAsync()}
242
- \`\`\`
243
- `.trim(),
244
- messages: [
245
- ...exampleMessages,
246
- {
247
- role: "user",
248
- content: JSON.stringify(options.input, null, 2)
249
- }
250
- ],
251
- temperature: 0,
252
- responseFormat: "json_object",
253
- model: { id: model }
254
- }
255
- });
256
- const output = result.output;
257
- if (!output.choices.length || typeof ((_b = (_a = output.choices) == null ? void 0 : _a[0]) == null ? void 0 : _b.content) !== "string") {
258
- throw new Error("Invalid response from the model");
259
- }
260
- const json = output.choices[0].content.trim();
261
- if (!json.length) {
262
- throw new Error("No response from the model");
263
- }
264
- return outputSchema.parse(JSON5.parse(json));
265
- }
266
- __name(predictJson, "predictJson");
267
-
268
- // src/assertions/extension.ts
269
- import json5 from "json5";
270
- import { expect as expect2 } from "vitest";
271
- import { getCurrentTest as getCurrentTest3 } from "vitest/suite";
272
- var toAssertion = /* @__PURE__ */ __name((promise) => {
273
- return {
274
- then: promise.then.bind(promise),
275
- value: promise.then((value) => value.result)
276
- };
277
- }, "toAssertion");
278
- var makeToMatchInlineSnapshot = /* @__PURE__ */ __name((promise) => async (expected) => {
279
- const stack = new Error().stack.split("\n")[2];
280
- const newStack = `
281
- at __INLINE_SNAPSHOT__ (node:internal/process/task_queues:1:1)
282
- at randomLine (node:internal/process/task_queues:1:1)
283
- ${stack}
284
- `.trim();
285
- const obj = json5.parse(expected != null ? expected : '""');
286
- const expectation = asyncExpect(promise, (expect3) => expect3.toMatchObject(obj)).catch(() => {
287
- });
288
- try {
289
- expect2((await promise).result).toMatchObject(obj);
290
- } catch (err) {
291
- const newError = new Error();
292
- newError.stack = newStack;
293
- expect2.getState().snapshotState.match({
294
- isInline: true,
295
- received: (await promise).result,
296
- testName: getCurrentTest3().name,
297
- error: newError,
298
- inlineSnapshot: expected
299
- });
300
- }
301
- return expectation;
302
- }, "makeToMatchInlineSnapshot");
303
-
304
- // src/assertions/check.ts
305
- function check(value, condition, options) {
306
- var _a;
307
- const promise = predictJson({
308
- systemMessage: `Check that the value meets the condition: ${condition}`,
309
- examples: (_a = options == null ? void 0 : options.examples) == null ? void 0 : _a.map(({ value: value2, reason, expected }) => ({
310
- input: value2,
311
- output: { reason, result: expected }
312
- })),
313
- outputSchema: z.boolean(),
314
- model: Context.evaluatorModel,
315
- input: value
316
- });
317
- return __spreadProps(__spreadValues({}, toAssertion(promise)), {
318
- toBe: /* @__PURE__ */ __name((expected) => asyncExpect(promise, (expect3) => expect3.toEqual(expected)), "toBe"),
319
- toMatchInlineSnapshot: makeToMatchInlineSnapshot(promise)
320
- });
321
- }
322
- __name(check, "check");
323
-
324
- // src/assertions/extract.ts
325
- function extract(value, shape, options) {
326
- var _a;
327
- const additionalMessage = (options == null ? void 0 : options.description) ? `
328
- In order to extract the right information, follow these instructions:
329
- ${options.description}
330
- ` : "";
331
- const promise = predictJson({
332
- systemMessage: "From the given input, extract the required information into the requested format." + additionalMessage.trim(),
333
- examples: (_a = options == null ? void 0 : options.examples) == null ? void 0 : _a.map(({ value: value2, reason, extracted }) => ({
334
- input: value2,
335
- output: { reason, result: extracted }
336
- })),
337
- outputSchema: shape,
338
- model: Context.evaluatorModel,
339
- input: value
340
- });
341
- return __spreadProps(__spreadValues({}, toAssertion(promise)), {
342
- toBe: /* @__PURE__ */ __name((expected) => asyncExpect(promise, (expect3) => expect3.toEqual(expected)), "toBe"),
343
- toMatchObject: /* @__PURE__ */ __name((expected) => asyncExpect(promise, (expect3) => expect3.toMatchObject(expected)), "toMatchObject"),
344
- toMatchInlineSnapshot: makeToMatchInlineSnapshot(promise)
345
- });
346
- }
347
- __name(extract, "extract");
348
-
349
- // src/assertions/filter.ts
350
- function filter(values, condition, options) {
351
- var _a, _b;
352
- const mappedValues = values.map(
353
- (_, idx) => z.object({
354
- index: z.literal(idx),
355
- reason: z.string(),
356
- keep: z.boolean()
357
- })
358
- );
359
- const input = values.map((value, idx) => ({
360
- index: idx,
361
- value
362
- }));
363
- const schema = z.tuple(mappedValues).describe(
364
- "An array of the objects with the index and a boolean value indicating if the object should be kept or not"
365
- );
366
- const promise = predictJson({
367
- systemMessage: `
368
- Based on the following qualification criteria, you need to filter the given list of objects.
369
- Citeria: ${condition}
370
-
371
- ---
372
- You need to return an array of objects with the index and a boolean value indicating if the object should be kept or not.
373
- `.trim(),
374
- examples: (options == null ? void 0 : options.examples) ? [
375
- {
376
- input: (_a = options == null ? void 0 : options.examples) == null ? void 0 : _a.map((v, index) => ({
377
- index,
378
- value: v.value
379
- })),
380
- output: {
381
- reason: "Here are some examples",
382
- result: (_b = options == null ? void 0 : options.examples) == null ? void 0 : _b.map((v, idx) => ({
383
- index: idx,
384
- reason: v.reason,
385
- keep: v.keep
386
- }))
387
- }
388
- }
389
- ] : void 0,
390
- outputSchema: schema,
391
- model: Context.evaluatorModel,
392
- input
393
- }).then((x) => {
394
- const results = schema.parse(x.result);
395
- return {
396
- result: values.filter((_, idx) => {
397
- var _a2;
398
- return (_a2 = results.find((r) => r.index === idx)) == null ? void 0 : _a2.keep;
399
- }),
400
- reason: x.reason
401
- };
402
- });
403
- return __spreadProps(__spreadValues({}, toAssertion(promise)), {
404
- toBe: /* @__PURE__ */ __name((expected) => asyncExpect(promise, (expect3) => expect3.toEqual(expected)), "toBe"),
405
- toMatchInlineSnapshot: makeToMatchInlineSnapshot(promise),
406
- toHaveNoneFiltered: /* @__PURE__ */ __name(() => asyncExpect(promise, (expect3) => expect3.toEqual(values)), "toHaveNoneFiltered"),
407
- toHaveSomeFiltered: /* @__PURE__ */ __name(() => asyncExpect(promise, (expect3) => expect3.not.toEqual(values)), "toHaveSomeFiltered"),
408
- toBeEmpty: /* @__PURE__ */ __name(() => asyncExpect(promise, (expect3) => expect3.toHaveLength(0)), "toBeEmpty"),
409
- length: {
410
- toBe: /* @__PURE__ */ __name((expected) => asyncExpect(promise, (expect3) => expect3.toHaveLength(expected)), "toBe"),
411
- toBeGreaterThanOrEqual: /* @__PURE__ */ __name((expected) => asyncExpect(promise, (expect3) => expect3.length.greaterThanOrEqual(expected)), "toBeGreaterThanOrEqual"),
412
- toBeLessThanOrEqual: /* @__PURE__ */ __name((expected) => asyncExpect(promise, (expect3) => expect3.length.lessThanOrEqual(expected)), "toBeLessThanOrEqual"),
413
- toBeBetween: /* @__PURE__ */ __name((min, max) => asyncExpect(promise, (expect3) => expect3.length.within(min, max)), "toBeBetween")
414
- }
415
- });
416
- }
417
- __name(filter, "filter");
418
-
419
- // src/assertions/rate.ts
420
- function rate(value, condition, options) {
421
- var _a;
422
- const schema = z.number().min(1).max(5).describe("Rating score, higher is better (1 is the worst, 5 is the best)");
423
- const promise = predictJson({
424
- systemMessage: `Based on the following qualification criteria, you need to rate the given situation from a score of 1 to 5.
425
- Scoring: 1 is the worst score, 5 is the best score possible.
426
- Criteria: ${condition}`,
427
- examples: (_a = options == null ? void 0 : options.examples) == null ? void 0 : _a.map(({ value: value2, reason, rating }) => ({
428
- input: value2,
429
- output: { reason, result: rating }
430
- })),
431
- outputSchema: schema,
432
- model: Context.evaluatorModel,
433
- input: value
434
- }).then((x) => {
435
- return {
436
- result: typeof x.result === "number" ? x.result : parseInt(x.result, 10),
437
- reason: x.reason
438
- };
439
- });
440
- return __spreadProps(__spreadValues({}, toAssertion(promise)), {
441
- toBe: /* @__PURE__ */ __name((expected) => asyncExpect(promise, (expect3) => expect3.toEqual(expected)), "toBe"),
442
- toMatchInlineSnapshot: makeToMatchInlineSnapshot(promise),
443
- toBeGreaterThanOrEqual: /* @__PURE__ */ __name((expected) => asyncExpect(promise, (expect3) => expect3.toBeGreaterThanOrEqual(expected)), "toBeGreaterThanOrEqual"),
444
- toBeLessThanOrEqual: /* @__PURE__ */ __name((expected) => asyncExpect(promise, (expect3) => expect3.toBeLessThanOrEqual(expected)), "toBeLessThanOrEqual")
445
- });
446
- }
447
- __name(rate, "rate");
448
-
449
- // src/hooks/setEvaluator.ts
450
- import { getCurrentTest as getCurrentTest4 } from "vitest/suite";
451
- var setEvaluator = /* @__PURE__ */ __name((model) => {
452
- const test = getCurrentTest4();
453
- if (!test) {
454
- throw new Error("setEvaluator is a Vitest hook and must be called within a test");
455
- }
456
- const meta = test.meta;
457
- meta.evaluatorModel = model;
458
- }, "setEvaluator");
459
-
460
- // src/hooks/setupClient.ts
461
- var setupClient = /* @__PURE__ */ __name((client) => {
462
- Context.setClient(client);
463
- }, "setupClient");
464
- export {
465
- check,
466
- compare,
467
- extract,
468
- filter,
469
- rate,
470
- setEvaluator,
471
- setupClient
472
- };
473
- //# sourceMappingURL=index.js.map
1
+ "use strict";
2
+ export { compare } from "./task/compare";
3
+ export { check } from "./assertions/check";
4
+ export { extract } from "./assertions/extract";
5
+ export { filter } from "./assertions/filter";
6
+ export { rate } from "./assertions/rate";
7
+ export { setEvaluator } from "./hooks/setEvaluator";
8
+ export { setupClient } from "./hooks/setupClient";