@botpress/zai 2.0.7 → 2.0.10
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/adapters/botpress-table.js +4 -3
- package/dist/index.d.ts +73 -280
- package/dist/operations/check.js +19 -8
- package/dist/operations/filter.js +4 -4
- package/dist/operations/label.js +5 -5
- package/dist/operations/rewrite.js +2 -2
- package/dist/utils.js +0 -13
- package/dist/zai.js +8 -5
- package/e2e/client.ts +151 -0
- package/e2e/data/cache.jsonl +113 -118
- package/e2e/utils.ts +2 -54
- package/package.json +4 -3
- package/src/adapters/botpress-table.ts +25 -4
- package/src/operations/check.ts +32 -10
- package/src/operations/extract.ts +7 -1
- package/src/operations/filter.ts +17 -6
- package/src/operations/label.ts +18 -8
- package/src/operations/rewrite.ts +17 -6
- package/src/operations/summarize.ts +19 -2
- package/src/operations/text.ts +5 -1
- package/src/utils.ts +12 -19
- package/src/zai.ts +24 -8
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { z } from "@bpinternal/zui";
|
|
2
|
-
import { GenerationMetadata } from "../utils";
|
|
3
2
|
import { Adapter } from "./adapter";
|
|
4
3
|
const CRITICAL_TAGS = {
|
|
5
4
|
system: "true",
|
|
@@ -29,7 +28,7 @@ const TableSchema = z.object({
|
|
|
29
28
|
input: z.object({}).passthrough().describe("The input to the task"),
|
|
30
29
|
output: z.object({}).passthrough().describe("The expected output"),
|
|
31
30
|
explanation: z.string().nullable(),
|
|
32
|
-
metadata:
|
|
31
|
+
metadata: z.object({}).passthrough(),
|
|
33
32
|
status: z.enum(["pending", "rejected", "approved"]),
|
|
34
33
|
feedback: z.object({
|
|
35
34
|
rating: z.enum(["very-bad", "bad", "good", "very-good"]),
|
|
@@ -105,7 +104,9 @@ export class TableAdapter extends Adapter {
|
|
|
105
104
|
output: { value: output },
|
|
106
105
|
explanation: explanation ?? null,
|
|
107
106
|
status,
|
|
108
|
-
metadata
|
|
107
|
+
metadata,
|
|
108
|
+
feedback: null
|
|
109
|
+
// Feedback is not provided at this point
|
|
109
110
|
}
|
|
110
111
|
]
|
|
111
112
|
}).catch(() => {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,55 +1,18 @@
|
|
|
1
|
-
import * as _bpinternal_zui from '@bpinternal/zui';
|
|
2
|
-
import { z, ZodObject } from '@bpinternal/zui';
|
|
3
1
|
import { Cognitive, Model, BotpressClientLike } from '@botpress/cognitive';
|
|
4
2
|
import { TextTokenizer } from '@bpinternal/thicktoken';
|
|
5
3
|
|
|
6
|
-
type GenerationMetadata =
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
output: _bpinternal_zui.ZodNumber;
|
|
12
|
-
}, "strip", _bpinternal_zui.ZodTypeAny, {
|
|
13
|
-
input?: number;
|
|
14
|
-
output?: number;
|
|
15
|
-
}, {
|
|
16
|
-
input?: number;
|
|
17
|
-
output?: number;
|
|
18
|
-
}>;
|
|
19
|
-
latency: _bpinternal_zui.ZodNumber;
|
|
20
|
-
tokens: _bpinternal_zui.ZodObject<{
|
|
21
|
-
input: _bpinternal_zui.ZodNumber;
|
|
22
|
-
output: _bpinternal_zui.ZodNumber;
|
|
23
|
-
}, "strip", _bpinternal_zui.ZodTypeAny, {
|
|
24
|
-
input?: number;
|
|
25
|
-
output?: number;
|
|
26
|
-
}, {
|
|
27
|
-
input?: number;
|
|
28
|
-
output?: number;
|
|
29
|
-
}>;
|
|
30
|
-
}, "strip", _bpinternal_zui.ZodTypeAny, {
|
|
31
|
-
model?: string;
|
|
32
|
-
cost?: {
|
|
33
|
-
input?: number;
|
|
34
|
-
output?: number;
|
|
4
|
+
type GenerationMetadata = {
|
|
5
|
+
model: string;
|
|
6
|
+
cost: {
|
|
7
|
+
input: number;
|
|
8
|
+
output: number;
|
|
35
9
|
};
|
|
36
|
-
latency
|
|
37
|
-
tokens
|
|
38
|
-
input
|
|
39
|
-
output
|
|
10
|
+
latency: number;
|
|
11
|
+
tokens: {
|
|
12
|
+
input: number;
|
|
13
|
+
output: number;
|
|
40
14
|
};
|
|
41
|
-
}
|
|
42
|
-
model?: string;
|
|
43
|
-
cost?: {
|
|
44
|
-
input?: number;
|
|
45
|
-
output?: number;
|
|
46
|
-
};
|
|
47
|
-
latency?: number;
|
|
48
|
-
tokens?: {
|
|
49
|
-
input?: number;
|
|
50
|
-
output?: number;
|
|
51
|
-
};
|
|
52
|
-
}>;
|
|
15
|
+
};
|
|
53
16
|
|
|
54
17
|
type SaveExampleProps<TInput, TOutput> = {
|
|
55
18
|
key: string;
|
|
@@ -79,60 +42,18 @@ declare abstract class Adapter {
|
|
|
79
42
|
}
|
|
80
43
|
|
|
81
44
|
type ModelId = Required<Parameters<Cognitive['generateContent']>[0]['model']>;
|
|
82
|
-
type ActiveLearning =
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
taskId?: string;
|
|
90
|
-
enable?: boolean;
|
|
91
|
-
}, {
|
|
92
|
-
tableName?: string;
|
|
93
|
-
taskId?: string;
|
|
94
|
-
enable?: boolean;
|
|
95
|
-
}>;
|
|
96
|
-
type ZaiConfig = (typeof ZaiConfig)['_input'];
|
|
97
|
-
declare const ZaiConfig: _bpinternal_zui.ZodObject<{
|
|
98
|
-
client: z.Schema<Cognitive | BotpressClientLike, _bpinternal_zui.ZodTypeDef, Cognitive | BotpressClientLike>;
|
|
99
|
-
userId: _bpinternal_zui.ZodOptional<_bpinternal_zui.ZodString>;
|
|
100
|
-
modelId: _bpinternal_zui.ZodDefault<z.Schema<string, _bpinternal_zui.ZodTypeDef, string>>;
|
|
101
|
-
activeLearning: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodObject<{
|
|
102
|
-
enable: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodBoolean>;
|
|
103
|
-
tableName: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodString>;
|
|
104
|
-
taskId: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodString>;
|
|
105
|
-
}, "strip", _bpinternal_zui.ZodTypeAny, {
|
|
106
|
-
tableName?: string;
|
|
107
|
-
taskId?: string;
|
|
108
|
-
enable?: boolean;
|
|
109
|
-
}, {
|
|
110
|
-
tableName?: string;
|
|
111
|
-
taskId?: string;
|
|
112
|
-
enable?: boolean;
|
|
113
|
-
}>>;
|
|
114
|
-
namespace: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodString>;
|
|
115
|
-
}, "strip", _bpinternal_zui.ZodTypeAny, {
|
|
116
|
-
client?: Cognitive | BotpressClientLike;
|
|
117
|
-
userId?: string;
|
|
118
|
-
modelId?: string;
|
|
119
|
-
activeLearning?: {
|
|
120
|
-
tableName?: string;
|
|
121
|
-
taskId?: string;
|
|
122
|
-
enable?: boolean;
|
|
123
|
-
};
|
|
124
|
-
namespace?: string;
|
|
125
|
-
}, {
|
|
126
|
-
client?: Cognitive | BotpressClientLike;
|
|
45
|
+
type ActiveLearning = {
|
|
46
|
+
enable: boolean;
|
|
47
|
+
tableName: string;
|
|
48
|
+
taskId: string;
|
|
49
|
+
};
|
|
50
|
+
type ZaiConfig = {
|
|
51
|
+
client: BotpressClientLike | Cognitive;
|
|
127
52
|
userId?: string;
|
|
128
|
-
modelId?: string;
|
|
129
|
-
activeLearning?:
|
|
130
|
-
tableName?: string;
|
|
131
|
-
taskId?: string;
|
|
132
|
-
enable?: boolean;
|
|
133
|
-
};
|
|
53
|
+
modelId?: ModelId | string;
|
|
54
|
+
activeLearning?: ActiveLearning;
|
|
134
55
|
namespace?: string;
|
|
135
|
-
}
|
|
56
|
+
};
|
|
136
57
|
declare class Zai {
|
|
137
58
|
protected static tokenizer: TextTokenizer;
|
|
138
59
|
protected client: Cognitive;
|
|
@@ -153,14 +74,10 @@ declare class Zai {
|
|
|
153
74
|
learn(taskId: string): Zai;
|
|
154
75
|
}
|
|
155
76
|
|
|
156
|
-
type Options$6 =
|
|
157
|
-
|
|
158
|
-
length: _bpinternal_zui.ZodOptional<_bpinternal_zui.ZodNumber>;
|
|
159
|
-
}, "strip", _bpinternal_zui.ZodTypeAny, {
|
|
77
|
+
type Options$6 = {
|
|
78
|
+
/** The maximum number of tokens to generate */
|
|
160
79
|
length?: number;
|
|
161
|
-
}
|
|
162
|
-
length?: number;
|
|
163
|
-
}>;
|
|
80
|
+
};
|
|
164
81
|
declare module '@botpress/zai' {
|
|
165
82
|
interface Zai {
|
|
166
83
|
/** Generates a text of the desired length according to the prompt */
|
|
@@ -168,32 +85,17 @@ declare module '@botpress/zai' {
|
|
|
168
85
|
}
|
|
169
86
|
}
|
|
170
87
|
|
|
171
|
-
type
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
input?: string;
|
|
181
|
-
output?: string;
|
|
182
|
-
}>, "many">>;
|
|
183
|
-
length: _bpinternal_zui.ZodOptional<_bpinternal_zui.ZodNumber>;
|
|
184
|
-
}, "strip", _bpinternal_zui.ZodTypeAny, {
|
|
185
|
-
length?: number;
|
|
186
|
-
examples?: {
|
|
187
|
-
input?: string;
|
|
188
|
-
output?: string;
|
|
189
|
-
}[];
|
|
190
|
-
}, {
|
|
88
|
+
type Example$3 = {
|
|
89
|
+
input: string;
|
|
90
|
+
output: string;
|
|
91
|
+
instructions?: string;
|
|
92
|
+
};
|
|
93
|
+
type Options$5 = {
|
|
94
|
+
/** Examples to guide the rewriting */
|
|
95
|
+
examples?: Array<Example$3>;
|
|
96
|
+
/** The maximum number of tokens to generate */
|
|
191
97
|
length?: number;
|
|
192
|
-
|
|
193
|
-
input?: string;
|
|
194
|
-
output?: string;
|
|
195
|
-
}[];
|
|
196
|
-
}>;
|
|
98
|
+
};
|
|
197
99
|
declare module '@botpress/zai' {
|
|
198
100
|
interface Zai {
|
|
199
101
|
/** Rewrites a string according to match the prompt */
|
|
@@ -201,44 +103,23 @@ declare module '@botpress/zai' {
|
|
|
201
103
|
}
|
|
202
104
|
}
|
|
203
105
|
|
|
204
|
-
type Options$4 =
|
|
205
|
-
|
|
206
|
-
prompt: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodString>;
|
|
207
|
-
format: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodString>;
|
|
208
|
-
length: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodNumber>;
|
|
209
|
-
intermediateFactor: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodNumber>;
|
|
210
|
-
maxIterations: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodNumber>;
|
|
211
|
-
sliding: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodObject<{
|
|
212
|
-
window: _bpinternal_zui.ZodNumber;
|
|
213
|
-
overlap: _bpinternal_zui.ZodNumber;
|
|
214
|
-
}, "strip", _bpinternal_zui.ZodTypeAny, {
|
|
215
|
-
window?: number;
|
|
216
|
-
overlap?: number;
|
|
217
|
-
}, {
|
|
218
|
-
window?: number;
|
|
219
|
-
overlap?: number;
|
|
220
|
-
}>>;
|
|
221
|
-
}, "strip", _bpinternal_zui.ZodTypeAny, {
|
|
222
|
-
length?: number;
|
|
106
|
+
type Options$4 = {
|
|
107
|
+
/** What should the text be summarized to? */
|
|
223
108
|
prompt?: string;
|
|
109
|
+
/** How to format the example text */
|
|
224
110
|
format?: string;
|
|
225
|
-
|
|
226
|
-
maxIterations?: number;
|
|
227
|
-
sliding?: {
|
|
228
|
-
window?: number;
|
|
229
|
-
overlap?: number;
|
|
230
|
-
};
|
|
231
|
-
}, {
|
|
111
|
+
/** The length of the summary in tokens */
|
|
232
112
|
length?: number;
|
|
233
|
-
|
|
234
|
-
format?: string;
|
|
113
|
+
/** How many times longer (than final length) are the intermediate summaries generated */
|
|
235
114
|
intermediateFactor?: number;
|
|
115
|
+
/** The maximum number of iterations to perform */
|
|
236
116
|
maxIterations?: number;
|
|
117
|
+
/** Sliding window options */
|
|
237
118
|
sliding?: {
|
|
238
|
-
window
|
|
239
|
-
overlap
|
|
119
|
+
window: number;
|
|
120
|
+
overlap: number;
|
|
240
121
|
};
|
|
241
|
-
}
|
|
122
|
+
};
|
|
242
123
|
declare module '@botpress/zai' {
|
|
243
124
|
interface Zai {
|
|
244
125
|
/** Summarizes a text of any length to a summary of the desired length */
|
|
@@ -246,34 +127,16 @@ declare module '@botpress/zai' {
|
|
|
246
127
|
}
|
|
247
128
|
}
|
|
248
129
|
|
|
249
|
-
type
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
}, {
|
|
260
|
-
input?: any;
|
|
261
|
-
check?: boolean;
|
|
262
|
-
reason?: string;
|
|
263
|
-
}>, "many">>;
|
|
264
|
-
}, "strip", _bpinternal_zui.ZodTypeAny, {
|
|
265
|
-
examples?: {
|
|
266
|
-
input?: any;
|
|
267
|
-
check?: boolean;
|
|
268
|
-
reason?: string;
|
|
269
|
-
}[];
|
|
270
|
-
}, {
|
|
271
|
-
examples?: {
|
|
272
|
-
input?: any;
|
|
273
|
-
check?: boolean;
|
|
274
|
-
reason?: string;
|
|
275
|
-
}[];
|
|
276
|
-
}>;
|
|
130
|
+
type Example$2 = {
|
|
131
|
+
input: unknown;
|
|
132
|
+
check: boolean;
|
|
133
|
+
reason?: string;
|
|
134
|
+
condition?: string;
|
|
135
|
+
};
|
|
136
|
+
type Options$3 = {
|
|
137
|
+
/** Examples to check the condition against */
|
|
138
|
+
examples?: Array<Example$2>;
|
|
139
|
+
};
|
|
277
140
|
declare module '@botpress/zai' {
|
|
278
141
|
interface Zai {
|
|
279
142
|
/** Checks wether a condition is true or not */
|
|
@@ -286,37 +149,17 @@ declare module '@botpress/zai' {
|
|
|
286
149
|
}
|
|
287
150
|
}
|
|
288
151
|
|
|
289
|
-
type
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
}, "strip", _bpinternal_zui.ZodTypeAny, {
|
|
297
|
-
input?: any;
|
|
298
|
-
filter?: boolean;
|
|
299
|
-
reason?: string;
|
|
300
|
-
}, {
|
|
301
|
-
input?: any;
|
|
302
|
-
filter?: boolean;
|
|
303
|
-
reason?: string;
|
|
304
|
-
}>, "many">>;
|
|
305
|
-
}, "strip", _bpinternal_zui.ZodTypeAny, {
|
|
306
|
-
examples?: {
|
|
307
|
-
input?: any;
|
|
308
|
-
filter?: boolean;
|
|
309
|
-
reason?: string;
|
|
310
|
-
}[];
|
|
311
|
-
tokensPerItem?: number;
|
|
312
|
-
}, {
|
|
313
|
-
examples?: {
|
|
314
|
-
input?: any;
|
|
315
|
-
filter?: boolean;
|
|
316
|
-
reason?: string;
|
|
317
|
-
}[];
|
|
152
|
+
type Example$1 = {
|
|
153
|
+
input: unknown;
|
|
154
|
+
filter: boolean;
|
|
155
|
+
reason?: string;
|
|
156
|
+
};
|
|
157
|
+
type Options$2 = {
|
|
158
|
+
/** The maximum number of tokens per item */
|
|
318
159
|
tokensPerItem?: number;
|
|
319
|
-
|
|
160
|
+
/** Examples to filter the condition against */
|
|
161
|
+
examples?: Array<Example$1>;
|
|
162
|
+
};
|
|
320
163
|
declare module '@botpress/zai' {
|
|
321
164
|
interface Zai {
|
|
322
165
|
/** Filters elements of an array against a condition */
|
|
@@ -324,17 +167,12 @@ declare module '@botpress/zai' {
|
|
|
324
167
|
}
|
|
325
168
|
}
|
|
326
169
|
|
|
327
|
-
type Options$1 =
|
|
328
|
-
|
|
329
|
-
instructions: _bpinternal_zui.ZodOptional<_bpinternal_zui.ZodString>;
|
|
330
|
-
chunkLength: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodOptional<_bpinternal_zui.ZodNumber>>;
|
|
331
|
-
}, "strip", _bpinternal_zui.ZodTypeAny, {
|
|
170
|
+
type Options$1 = {
|
|
171
|
+
/** Instructions to guide the user on how to extract the data */
|
|
332
172
|
instructions?: string;
|
|
173
|
+
/** The maximum number of tokens per chunk */
|
|
333
174
|
chunkLength?: number;
|
|
334
|
-
}
|
|
335
|
-
instructions?: string;
|
|
336
|
-
chunkLength?: number;
|
|
337
|
-
}>;
|
|
175
|
+
};
|
|
338
176
|
type __Z<T extends any = any> = {
|
|
339
177
|
_output: T;
|
|
340
178
|
};
|
|
@@ -362,60 +200,15 @@ type Example<T extends string> = {
|
|
|
362
200
|
explanation?: string;
|
|
363
201
|
}>>;
|
|
364
202
|
};
|
|
365
|
-
type Options<T extends string> =
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
examples: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodArray<_bpinternal_zui.ZodObject<{
|
|
370
|
-
input: _bpinternal_zui.ZodAny;
|
|
371
|
-
labels: _bpinternal_zui.ZodRecord<_bpinternal_zui.ZodString, _bpinternal_zui.ZodObject<{
|
|
372
|
-
label: _bpinternal_zui.ZodEnum<never>;
|
|
373
|
-
explanation: _bpinternal_zui.ZodOptional<_bpinternal_zui.ZodString>;
|
|
374
|
-
}, "strip", _bpinternal_zui.ZodTypeAny, {
|
|
375
|
-
label: never;
|
|
376
|
-
explanation?: string;
|
|
377
|
-
}, {
|
|
378
|
-
label: never;
|
|
379
|
-
explanation?: string;
|
|
380
|
-
}>>;
|
|
381
|
-
}, "strip", _bpinternal_zui.ZodTypeAny, {
|
|
382
|
-
input?: any;
|
|
383
|
-
labels?: Record<string, {
|
|
384
|
-
label: never;
|
|
385
|
-
explanation?: string;
|
|
386
|
-
}>;
|
|
387
|
-
}, {
|
|
388
|
-
input?: any;
|
|
389
|
-
labels?: Record<string, {
|
|
390
|
-
label: never;
|
|
391
|
-
explanation?: string;
|
|
392
|
-
}>;
|
|
393
|
-
}>, "many">>;
|
|
394
|
-
instructions: _bpinternal_zui.ZodOptional<_bpinternal_zui.ZodString>;
|
|
395
|
-
chunkLength: _bpinternal_zui.ZodDefault<_bpinternal_zui.ZodOptional<_bpinternal_zui.ZodNumber>>;
|
|
396
|
-
}, "strip", _bpinternal_zui.ZodTypeAny, {
|
|
203
|
+
type Options<T extends string> = {
|
|
204
|
+
/** Examples to help the user make a decision */
|
|
205
|
+
examples?: Array<Example<T>>;
|
|
206
|
+
/** Instructions to guide the user on how to extract the data */
|
|
397
207
|
instructions?: string;
|
|
398
|
-
|
|
399
|
-
input?: any;
|
|
400
|
-
labels?: Record<string, {
|
|
401
|
-
label: never;
|
|
402
|
-
explanation?: string;
|
|
403
|
-
}>;
|
|
404
|
-
}[];
|
|
208
|
+
/** The maximum number of tokens per chunk */
|
|
405
209
|
chunkLength?: number;
|
|
406
|
-
}
|
|
407
|
-
instructions?: string;
|
|
408
|
-
examples?: {
|
|
409
|
-
input?: any;
|
|
410
|
-
labels?: Record<string, {
|
|
411
|
-
label: never;
|
|
412
|
-
explanation?: string;
|
|
413
|
-
}>;
|
|
414
|
-
}[];
|
|
415
|
-
chunkLength?: number;
|
|
416
|
-
}>;
|
|
210
|
+
};
|
|
417
211
|
type Labels<T extends string> = Record<T, string>;
|
|
418
|
-
declare const Labels: z.ZodTransformer<_bpinternal_zui.ZodRecord<_bpinternal_zui.ZodString, _bpinternal_zui.ZodString>, Record<string, string>, Record<string, string>>;
|
|
419
212
|
declare module '@botpress/zai' {
|
|
420
213
|
interface Zai {
|
|
421
214
|
/** Tags the provided input with a list of predefined labels */
|
package/dist/operations/check.js
CHANGED
|
@@ -2,19 +2,20 @@ import { z } from "@bpinternal/zui";
|
|
|
2
2
|
import { fastHash, stringify, takeUntilTokens } from "../utils";
|
|
3
3
|
import { Zai } from "../zai";
|
|
4
4
|
import { PROMPT_INPUT_BUFFER } from "./constants";
|
|
5
|
-
const
|
|
5
|
+
const _Example = z.object({
|
|
6
6
|
input: z.any(),
|
|
7
7
|
check: z.boolean(),
|
|
8
|
-
reason: z.string().optional()
|
|
8
|
+
reason: z.string().optional(),
|
|
9
|
+
condition: z.string().optional()
|
|
9
10
|
});
|
|
10
|
-
const
|
|
11
|
-
examples: z.array(
|
|
11
|
+
const _Options = z.object({
|
|
12
|
+
examples: z.array(_Example).describe("Examples to check the condition against").default([])
|
|
12
13
|
});
|
|
13
14
|
const TRUE = "\u25A0TRUE\u25A0";
|
|
14
15
|
const FALSE = "\u25A0FALSE\u25A0";
|
|
15
16
|
const END = "\u25A0END\u25A0";
|
|
16
17
|
Zai.prototype.check = async function(input, condition, _options) {
|
|
17
|
-
const options =
|
|
18
|
+
const options = _Options.parse(_options ?? {});
|
|
18
19
|
const tokenizer = await this.getTokenizer();
|
|
19
20
|
await this.fetchModelDetails();
|
|
20
21
|
const PROMPT_COMPONENT = Math.max(this.ModelDetails.input.maxTokens - PROMPT_INPUT_BUFFER, 100);
|
|
@@ -45,11 +46,17 @@ Zai.prototype.check = async function(input, condition, _options) {
|
|
|
45
46
|
return { explanation: exactMatch.explanation ?? "", value: exactMatch.output };
|
|
46
47
|
}
|
|
47
48
|
const defaultExamples = [
|
|
48
|
-
{
|
|
49
|
+
{
|
|
50
|
+
input: "50 Cent",
|
|
51
|
+
check: true,
|
|
52
|
+
reason: "50 Cent is widely recognized as a public personality.",
|
|
53
|
+
condition: "Is the input a public personality?"
|
|
54
|
+
},
|
|
49
55
|
{
|
|
50
56
|
input: ["apple", "banana", "carrot", "house"],
|
|
51
57
|
check: false,
|
|
52
|
-
reason: "The list contains a house, which is not a fruit. Also, the list contains a carrot, which is a vegetable."
|
|
58
|
+
reason: "The list contains a house, which is not a fruit. Also, the list contains a carrot, which is a vegetable.",
|
|
59
|
+
condition: "Is the input exclusively a list of fruits?"
|
|
53
60
|
}
|
|
54
61
|
];
|
|
55
62
|
const userExamples = [
|
|
@@ -74,7 +81,11 @@ ${END}
|
|
|
74
81
|
`.trim();
|
|
75
82
|
};
|
|
76
83
|
const formatExample = (example) => [
|
|
77
|
-
{
|
|
84
|
+
{
|
|
85
|
+
type: "text",
|
|
86
|
+
content: formatInput(stringify(example.input ?? null), example.condition ?? condition),
|
|
87
|
+
role: "user"
|
|
88
|
+
},
|
|
78
89
|
{
|
|
79
90
|
type: "text",
|
|
80
91
|
content: formatOutput(example.check, example.reason ?? ""),
|
|
@@ -3,18 +3,18 @@ import { clamp } from "lodash-es";
|
|
|
3
3
|
import { fastHash, stringify, takeUntilTokens } from "../utils";
|
|
4
4
|
import { Zai } from "../zai";
|
|
5
5
|
import { PROMPT_INPUT_BUFFER, PROMPT_OUTPUT_BUFFER } from "./constants";
|
|
6
|
-
const
|
|
6
|
+
const _Example = z.object({
|
|
7
7
|
input: z.any(),
|
|
8
8
|
filter: z.boolean(),
|
|
9
9
|
reason: z.string().optional()
|
|
10
10
|
});
|
|
11
|
-
const
|
|
11
|
+
const _Options = z.object({
|
|
12
12
|
tokensPerItem: z.number().min(1).max(1e5).optional().describe("The maximum number of tokens per item").default(250),
|
|
13
|
-
examples: z.array(
|
|
13
|
+
examples: z.array(_Example).describe("Examples to filter the condition against").default([])
|
|
14
14
|
});
|
|
15
15
|
const END = "\u25A0END\u25A0";
|
|
16
16
|
Zai.prototype.filter = async function(input, condition, _options) {
|
|
17
|
-
const options =
|
|
17
|
+
const options = _Options.parse(_options ?? {});
|
|
18
18
|
const tokenizer = await this.getTokenizer();
|
|
19
19
|
await this.fetchModelDetails();
|
|
20
20
|
const taskId = this.taskId;
|
package/dist/operations/label.js
CHANGED
|
@@ -11,7 +11,7 @@ const LABELS = {
|
|
|
11
11
|
ABSOLUTELY_YES: "ABSOLUTELY_YES"
|
|
12
12
|
};
|
|
13
13
|
const ALL_LABELS = Object.values(LABELS).join(" | ");
|
|
14
|
-
const
|
|
14
|
+
const _Options = z.object({
|
|
15
15
|
examples: z.array(
|
|
16
16
|
z.object({
|
|
17
17
|
input: z.any(),
|
|
@@ -21,7 +21,7 @@ const Options = z.object({
|
|
|
21
21
|
instructions: z.string().optional().describe("Instructions to guide the user on how to extract the data"),
|
|
22
22
|
chunkLength: z.number().min(100).max(1e5).optional().describe("The maximum number of tokens per chunk").default(16e3)
|
|
23
23
|
});
|
|
24
|
-
const
|
|
24
|
+
const _Labels = z.record(z.string().min(1).max(250), z.string()).superRefine((labels, ctx) => {
|
|
25
25
|
const keys = Object.keys(labels);
|
|
26
26
|
for (const key of keys) {
|
|
27
27
|
if (key.length < 1 || key.length > 250) {
|
|
@@ -68,8 +68,8 @@ const getConfidence = (label) => {
|
|
|
68
68
|
}
|
|
69
69
|
};
|
|
70
70
|
Zai.prototype.label = async function(input, _labels, _options) {
|
|
71
|
-
const options =
|
|
72
|
-
const labels =
|
|
71
|
+
const options = _Options.parse(_options ?? {});
|
|
72
|
+
const labels = _Labels.parse(_labels);
|
|
73
73
|
const tokenizer = await this.getTokenizer();
|
|
74
74
|
await this.fetchModelDetails();
|
|
75
75
|
const taskId = this.taskId;
|
|
@@ -126,7 +126,7 @@ Zai.prototype.label = async function(input, _labels, _options) {
|
|
|
126
126
|
options.examples.forEach((example) => {
|
|
127
127
|
examples.push({
|
|
128
128
|
key: fastHash(JSON.stringify(example)),
|
|
129
|
-
input: example.input,
|
|
129
|
+
input: stringify(example.input),
|
|
130
130
|
similarity: 1,
|
|
131
131
|
explanation: "",
|
|
132
132
|
output: example.labels
|
|
@@ -2,12 +2,12 @@ import { z } from "@bpinternal/zui";
|
|
|
2
2
|
import { fastHash, stringify, takeUntilTokens } from "../utils";
|
|
3
3
|
import { Zai } from "../zai";
|
|
4
4
|
import { PROMPT_INPUT_BUFFER } from "./constants";
|
|
5
|
-
const
|
|
5
|
+
const _Example = z.object({
|
|
6
6
|
input: z.string(),
|
|
7
7
|
output: z.string()
|
|
8
8
|
});
|
|
9
9
|
const Options = z.object({
|
|
10
|
-
examples: z.array(
|
|
10
|
+
examples: z.array(_Example).default([]),
|
|
11
11
|
length: z.number().min(10).max(16e3).optional().describe("The maximum number of tokens to generate")
|
|
12
12
|
});
|
|
13
13
|
const START = "\u25A0START\u25A0";
|
package/dist/utils.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { z } from "@bpinternal/zui";
|
|
2
1
|
export const stringify = (input, beautify = true) => {
|
|
3
2
|
return typeof input === "string" && !!input.length ? input : input ? JSON.stringify(input, beautify ? null : void 0, beautify ? 2 : void 0) : "<input is null, false, undefined or empty>";
|
|
4
3
|
};
|
|
@@ -23,15 +22,3 @@ export const takeUntilTokens = (arr, tokens, count) => {
|
|
|
23
22
|
}
|
|
24
23
|
return result;
|
|
25
24
|
};
|
|
26
|
-
export const GenerationMetadata = z.object({
|
|
27
|
-
model: z.string(),
|
|
28
|
-
cost: z.object({
|
|
29
|
-
input: z.number(),
|
|
30
|
-
output: z.number()
|
|
31
|
-
}).describe("Cost in $USD"),
|
|
32
|
-
latency: z.number().describe("Latency in milliseconds"),
|
|
33
|
-
tokens: z.object({
|
|
34
|
-
input: z.number(),
|
|
35
|
-
output: z.number()
|
|
36
|
-
}).describe("Number of tokens used")
|
|
37
|
-
});
|
package/dist/zai.js
CHANGED
|
@@ -3,7 +3,7 @@ import { getWasmTokenizer } from "@bpinternal/thicktoken";
|
|
|
3
3
|
import { z } from "@bpinternal/zui";
|
|
4
4
|
import { TableAdapter } from "./adapters/botpress-table";
|
|
5
5
|
import { MemoryAdapter } from "./adapters/memory";
|
|
6
|
-
const
|
|
6
|
+
const _ActiveLearning = z.object({
|
|
7
7
|
enable: z.boolean().describe("Whether to enable active learning").default(false),
|
|
8
8
|
tableName: z.string().regex(
|
|
9
9
|
/^[A-Za-z0-9_/-]{1,100}Table$/,
|
|
@@ -14,7 +14,7 @@ const ActiveLearning = z.object({
|
|
|
14
14
|
"Namespace must be alphanumeric and contain only letters, numbers, underscores, hyphens and slashes"
|
|
15
15
|
).describe("The ID of the task").default("default")
|
|
16
16
|
});
|
|
17
|
-
const
|
|
17
|
+
const _ZaiConfig = z.object({
|
|
18
18
|
client: z.custom(),
|
|
19
19
|
userId: z.string().describe("The ID of the user consuming the API").optional(),
|
|
20
20
|
modelId: z.custom(
|
|
@@ -31,7 +31,7 @@ const ZaiConfig = z.object({
|
|
|
31
31
|
message: "Invalid model ID"
|
|
32
32
|
}
|
|
33
33
|
).describe("The ID of the model you want to use").default("best"),
|
|
34
|
-
activeLearning:
|
|
34
|
+
activeLearning: _ActiveLearning.default({ enable: false }),
|
|
35
35
|
namespace: z.string().regex(
|
|
36
36
|
/^[A-Za-z0-9_/-]{1,100}$/,
|
|
37
37
|
"Namespace must be alphanumeric and contain only letters, numbers, underscores, hyphens and slashes"
|
|
@@ -49,13 +49,16 @@ export class Zai {
|
|
|
49
49
|
activeLearning;
|
|
50
50
|
constructor(config) {
|
|
51
51
|
this._originalConfig = config;
|
|
52
|
-
const parsed =
|
|
52
|
+
const parsed = _ZaiConfig.parse(config);
|
|
53
53
|
this.client = Cognitive.isCognitiveClient(parsed.client) ? parsed.client : new Cognitive({ client: parsed.client });
|
|
54
54
|
this.namespace = parsed.namespace;
|
|
55
55
|
this._userId = parsed.userId;
|
|
56
56
|
this.Model = parsed.modelId;
|
|
57
57
|
this.activeLearning = parsed.activeLearning;
|
|
58
|
-
this.adapter = parsed.activeLearning?.enable ? new TableAdapter({
|
|
58
|
+
this.adapter = parsed.activeLearning?.enable ? new TableAdapter({
|
|
59
|
+
client: this.client.client,
|
|
60
|
+
tableName: parsed.activeLearning.tableName
|
|
61
|
+
}) : new MemoryAdapter([]);
|
|
59
62
|
}
|
|
60
63
|
/** @internal */
|
|
61
64
|
async callModel(props) {
|