@botpress/zai 1.0.0-beta.2 → 1.0.0-beta.4

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.
Files changed (133) hide show
  1. package/dist/index.cjs +1898 -0
  2. package/dist/index.cjs.map +1 -0
  3. package/dist/index.d.cts +916 -0
  4. package/dist/index.d.ts +916 -0
  5. package/dist/index.js +1868 -0
  6. package/dist/index.js.map +1 -0
  7. package/ensure-env.cjs +9 -0
  8. package/package.json +25 -17
  9. package/tsconfig.json +23 -18
  10. package/tsup.config.ts +16 -0
  11. package/vitest.config.ts +2 -16
  12. package/vitest.setup.ts +11 -15
  13. package/dist/src/adapters/adapter.d.ts +0 -27
  14. package/dist/src/adapters/adapter.js +0 -3
  15. package/dist/src/adapters/adapter.js.map +0 -1
  16. package/dist/src/adapters/botpress-table.d.ts +0 -155
  17. package/dist/src/adapters/botpress-table.js +0 -170
  18. package/dist/src/adapters/botpress-table.js.map +0 -1
  19. package/dist/src/adapters/memory.d.ts +0 -7
  20. package/dist/src/adapters/memory.js +0 -13
  21. package/dist/src/adapters/memory.js.map +0 -1
  22. package/dist/src/env.d.ts +0 -1
  23. package/dist/src/env.js +0 -27
  24. package/dist/src/env.js.map +0 -1
  25. package/dist/src/index.d.ts +0 -9
  26. package/dist/src/index.js +0 -10
  27. package/dist/src/index.js.map +0 -1
  28. package/dist/src/models.d.ts +0 -253
  29. package/dist/src/models.js +0 -348
  30. package/dist/src/models.js.map +0 -1
  31. package/dist/src/operations/__tests/index.d.ts +0 -19
  32. package/dist/src/operations/__tests/index.js +0 -24
  33. package/dist/src/operations/__tests/index.js.map +0 -1
  34. package/dist/src/operations/check.d.ts +0 -36
  35. package/dist/src/operations/check.js +0 -146
  36. package/dist/src/operations/check.js.map +0 -1
  37. package/dist/src/operations/check.test.d.ts +0 -1
  38. package/dist/src/operations/check.test.js +0 -129
  39. package/dist/src/operations/check.test.js.map +0 -1
  40. package/dist/src/operations/constants.d.ts +0 -2
  41. package/dist/src/operations/constants.js +0 -3
  42. package/dist/src/operations/constants.js.map +0 -1
  43. package/dist/src/operations/errors.d.ts +0 -5
  44. package/dist/src/operations/errors.js +0 -11
  45. package/dist/src/operations/errors.js.map +0 -1
  46. package/dist/src/operations/extract.d.ts +0 -20
  47. package/dist/src/operations/extract.js +0 -235
  48. package/dist/src/operations/extract.js.map +0 -1
  49. package/dist/src/operations/extract.test.d.ts +0 -1
  50. package/dist/src/operations/extract.test.js +0 -163
  51. package/dist/src/operations/extract.test.js.map +0 -1
  52. package/dist/src/operations/filter.d.ts +0 -39
  53. package/dist/src/operations/filter.js +0 -184
  54. package/dist/src/operations/filter.js.map +0 -1
  55. package/dist/src/operations/filter.test.d.ts +0 -1
  56. package/dist/src/operations/filter.test.js +0 -151
  57. package/dist/src/operations/filter.test.js.map +0 -1
  58. package/dist/src/operations/label.d.ts +0 -79
  59. package/dist/src/operations/label.js +0 -250
  60. package/dist/src/operations/label.js.map +0 -1
  61. package/dist/src/operations/label.test.d.ts +0 -1
  62. package/dist/src/operations/label.test.js +0 -213
  63. package/dist/src/operations/label.test.js.map +0 -1
  64. package/dist/src/operations/rewrite.d.ts +0 -34
  65. package/dist/src/operations/rewrite.js +0 -106
  66. package/dist/src/operations/rewrite.js.map +0 -1
  67. package/dist/src/operations/rewrite.test.d.ts +0 -1
  68. package/dist/src/operations/rewrite.test.js +0 -96
  69. package/dist/src/operations/rewrite.test.js.map +0 -1
  70. package/dist/src/operations/summarize.d.ts +0 -46
  71. package/dist/src/operations/summarize.js +0 -139
  72. package/dist/src/operations/summarize.js.map +0 -1
  73. package/dist/src/operations/summarize.test.d.ts +0 -1
  74. package/dist/src/operations/summarize.test.js +0 -22
  75. package/dist/src/operations/summarize.test.js.map +0 -1
  76. package/dist/src/operations/text.d.ts +0 -16
  77. package/dist/src/operations/text.js +0 -47
  78. package/dist/src/operations/text.js.map +0 -1
  79. package/dist/src/operations/text.test.d.ts +0 -1
  80. package/dist/src/operations/text.test.js +0 -51
  81. package/dist/src/operations/text.test.js.map +0 -1
  82. package/dist/src/operations/zai-learn.test.d.ts +0 -1
  83. package/dist/src/operations/zai-learn.test.js +0 -71
  84. package/dist/src/operations/zai-learn.test.js.map +0 -1
  85. package/dist/src/operations/zai-retry.test.d.ts +0 -1
  86. package/dist/src/operations/zai-retry.test.js +0 -50
  87. package/dist/src/operations/zai-retry.test.js.map +0 -1
  88. package/dist/src/utils.d.ts +0 -53
  89. package/dist/src/utils.js +0 -49
  90. package/dist/src/utils.js.map +0 -1
  91. package/dist/src/zai.d.ts +0 -91
  92. package/dist/src/zai.js +0 -139
  93. package/dist/src/zai.js.map +0 -1
  94. package/dist/tsconfig.tsbuildinfo +0 -1
  95. package/dist/typings.d.ts +0 -901
  96. package/dist/typings.tsbuildinfo +0 -1
  97. package/dist/vitest.config.d.ts +0 -2
  98. package/dist/vitest.config.js +0 -21
  99. package/dist/vitest.config.js.map +0 -1
  100. package/dist/vitest.setup.d.ts +0 -1
  101. package/dist/vitest.setup.js +0 -22
  102. package/dist/vitest.setup.js.map +0 -1
  103. package/src/adapters/adapter.ts +0 -35
  104. package/src/adapters/botpress-table.ts +0 -213
  105. package/src/adapters/memory.ts +0 -13
  106. package/src/env.ts +0 -54
  107. package/src/index.ts +0 -11
  108. package/src/models.ts +0 -347
  109. package/src/operations/__tests/botpress_docs.txt +0 -26040
  110. package/src/operations/__tests/index.ts +0 -30
  111. package/src/operations/check.test.ts +0 -155
  112. package/src/operations/check.ts +0 -187
  113. package/src/operations/constants.ts +0 -2
  114. package/src/operations/errors.ts +0 -9
  115. package/src/operations/extract.test.ts +0 -209
  116. package/src/operations/extract.ts +0 -291
  117. package/src/operations/filter.test.ts +0 -182
  118. package/src/operations/filter.ts +0 -231
  119. package/src/operations/label.test.ts +0 -239
  120. package/src/operations/label.ts +0 -332
  121. package/src/operations/rewrite.test.ts +0 -114
  122. package/src/operations/rewrite.ts +0 -148
  123. package/src/operations/summarize.test.ts +0 -25
  124. package/src/operations/summarize.ts +0 -193
  125. package/src/operations/text.test.ts +0 -60
  126. package/src/operations/text.ts +0 -63
  127. package/src/operations/zai-learn.test.ts +0 -85
  128. package/src/operations/zai-retry.test.ts +0 -64
  129. package/src/scripts/update-models.ts +0 -74
  130. package/src/utils.ts +0 -61
  131. package/src/zai.ts +0 -185
  132. package/tsconfig.types.json +0 -22
  133. package/turbo.json +0 -14
@@ -1,30 +0,0 @@
1
- import { Client } from '@botpress/client'
2
- import { type TextTokenizer, getWasmTokenizer } from '@botpress/wasm'
3
- import fs from 'node:fs'
4
- import path from 'node:path'
5
- import { beforeAll } from 'vitest'
6
-
7
- import { Zai } from '../..'
8
-
9
- export const getClient = () => {
10
- return new Client({
11
- apiUrl: process.env.VITE_CLOUD_API_ENDPOINT,
12
- botId: process.env.VITEST_BOT_ID ?? process.env.VITE_BOT_ID,
13
- token: process.env.VITEST_CLOUD_PAT ?? process.env.VITE_CLOUD_PAT
14
- })
15
- }
16
-
17
- export const getZai = () => {
18
- const client = getClient()
19
- return new Zai({ client, retry: { maxRetries: 0 } })
20
- }
21
-
22
- export let tokenizer: TextTokenizer = null!
23
-
24
- beforeAll(async () => {
25
- tokenizer = await getWasmTokenizer()
26
- })
27
-
28
- export const BotpressDocumentation = fs.readFileSync(path.join(__dirname, './botpress_docs.txt'), 'utf-8').trim()
29
-
30
- export const metadata = { cost: { input: 1, output: 1 }, latency: 0, model: '', tokens: { input: 1, output: 1 } }
@@ -1,155 +0,0 @@
1
- import { describe, it, expect, afterAll, beforeEach, afterEach } from 'vitest'
2
-
3
- import { BotpressDocumentation, getClient, getZai, metadata } from './__tests'
4
-
5
- import { TableAdapter } from '../adapters/botpress-table'
6
-
7
- describe('zai.check', { timeout: 60_000 }, () => {
8
- const zai = getZai()
9
-
10
- it('basic check on a string', async () => {
11
- const value = await zai.check('This text is very clearly written in English.', 'is an english sentence')
12
- expect(value).toBe(true)
13
- })
14
-
15
- it('text that is too long gets truncated', async () => {
16
- const isBotpressDocumentation = await zai.check(
17
- BotpressDocumentation,
18
- 'is about botpress and looks like documentation'
19
- )
20
-
21
- const isAboutBirds = await zai.check(BotpressDocumentation, 'is a book about birds and their species')
22
-
23
- expect(isBotpressDocumentation).toBe(true)
24
- expect(isAboutBirds).toBe(false)
25
- })
26
-
27
- it('works with any input type', async () => {
28
- const sly = { name: 'Sylvain Perron', age: 30, job: 'CEO', company: 'Botpress', location: 'Quebec' }
29
- const american = await zai.check(sly, 'person lives in north america')
30
- const european = await zai.check(sly, 'person lives in europe')
31
-
32
- expect(american).toBe(true)
33
- expect(european).toBe(false)
34
- })
35
-
36
- it('check with examples', async () => {
37
- const examples = [
38
- {
39
- input: 'Rasa (framework)',
40
- check: true,
41
- reason: 'Rasa is a chatbot framework, so it competes with us (Botpress).'
42
- },
43
- {
44
- input: 'Rasa (coffee company)',
45
- check: false,
46
- reason:
47
- 'Rasa (coffee company) is not in the chatbot or AI agent industry, therefore it does not compete with us (Botpress).'
48
- },
49
- {
50
- input: 'Dialogflow',
51
- check: true,
52
- reason: 'Dialogflow is a chatbot development product, so it competes with us (Botpress).'
53
- }
54
- ]
55
-
56
- const moveworks = await zai.check('Moveworks', 'competes with us', { examples })
57
- const ada = await zai.check('Ada.cx', 'competes with us', { examples })
58
- const voiceflow = await zai.check('Voiceflow', 'competes with us', { examples })
59
- const nike = await zai.check('Nike', 'competes with us', { examples })
60
- const adidas = await zai.check('Adidas', 'competes with us', { examples })
61
-
62
- expect(moveworks).toBe(true)
63
- expect(ada).toBe(true)
64
- expect(voiceflow).toBe(true)
65
-
66
- expect(nike).toBe(false)
67
- expect(adidas).toBe(false)
68
- })
69
- })
70
-
71
- describe('zai.learn.check', { timeout: 60_000 }, () => {
72
- const client = getClient()
73
- let tableName = 'ZaiTestCheckInternalTable'
74
- let taskId = 'check'
75
- let zai = getZai()
76
-
77
- beforeEach(async () => {
78
- zai = getZai().with({
79
- activeLearning: {
80
- enable: true,
81
- taskId,
82
- tableName
83
- }
84
- })
85
- })
86
-
87
- afterEach(async () => {
88
- try {
89
- await client.deleteTableRows({ table: tableName, deleteAllRows: true })
90
- } catch (err) {}
91
- })
92
-
93
- afterAll(async () => {
94
- try {
95
- await client.deleteTable({ table: tableName })
96
- } catch (err) {}
97
- })
98
-
99
- it('learns a contradiction from examples', async () => {
100
- const adapter = new TableAdapter({
101
- client,
102
- tableName
103
- })
104
-
105
- const value = await zai.learn(taskId).check(`What's up`, 'is a greeting')
106
- expect(value).toBe(true)
107
-
108
- let rows = await client.findTableRows({ table: tableName })
109
- expect(rows.rows.length).toBe(1)
110
- expect(rows.rows[0].output.value).toEqual(value)
111
-
112
- await adapter.saveExample({
113
- key: 't1',
114
- taskId: `zai/${taskId}`,
115
- taskType: 'zai.check',
116
- instructions: 'is a greeting',
117
- input: 'what is up',
118
- output: false,
119
- explanation: `"What's up" is our business scenario is NOT considered an official greeting.`,
120
- metadata,
121
- status: 'approved'
122
- })
123
-
124
- await adapter.saveExample({
125
- key: 't2',
126
- taskId: `zai/${taskId}`,
127
- taskType: 'zai.check',
128
- instructions: 'is a greeting',
129
- input: 'hello! how are you?',
130
- output: true,
131
- explanation: `"hello!" is a common greeting in English.`,
132
- metadata,
133
- status: 'approved'
134
- })
135
-
136
- await adapter.saveExample({
137
- key: 't3',
138
- taskId: `zai/${taskId}`,
139
- taskType: 'zai.check',
140
- instructions: 'is a greeting',
141
- input: 'wassup',
142
- output: false,
143
- explanation: `"wassup" is a slang term and not considered a formal greeting. It is therefore NOT considered a greeting.`,
144
- metadata,
145
- status: 'approved'
146
- })
147
-
148
- const second = await zai.learn(taskId).check(`What's up`, 'is a greeting')
149
- expect(second).toBe(false)
150
-
151
- rows = await client.findTableRows({ table: tableName })
152
- expect(rows.rows.length).toBe(4)
153
- expect(rows.rows[0].output.value).toEqual(second)
154
- })
155
- })
@@ -1,187 +0,0 @@
1
- import { z } from '@bpinternal/zui'
2
-
3
- import { fastHash, stringify, takeUntilTokens } from '../utils'
4
- import { Zai } from '../zai'
5
- import { PROMPT_INPUT_BUFFER } from './constants'
6
-
7
- const Example = z.object({
8
- input: z.any(),
9
- check: z.boolean(),
10
- reason: z.string().optional()
11
- })
12
-
13
- export type Options = z.input<typeof Options>
14
- const Options = z.object({
15
- examples: z.array(Example).describe('Examples to check the condition against').default([])
16
- })
17
-
18
- declare module '../zai' {
19
- interface Zai {
20
- /** Checks wether a condition is true or not */
21
- check(input: unknown, condition: string, options?: Options): Promise<boolean>
22
- }
23
- }
24
-
25
- const TRUE = '■TRUE■'
26
- const FALSE = '■FALSE■'
27
- const END = '■END■'
28
-
29
- Zai.prototype.check = async function (this: Zai, input, condition, _options) {
30
- const options = Options.parse(_options ?? {})
31
- const tokenizer = await this.getTokenizer()
32
- const PROMPT_COMPONENT = Math.max(this.Model.input.maxTokens - PROMPT_INPUT_BUFFER, 100)
33
-
34
- const taskId = this.taskId
35
- const taskType = 'zai.check'
36
-
37
- const PROMPT_TOKENS = {
38
- INPUT: Math.floor(0.5 * PROMPT_COMPONENT),
39
- CONDITION: Math.floor(0.2 * PROMPT_COMPONENT)
40
- }
41
-
42
- // Truncate the input to fit the model's input size
43
- const inputAsString = tokenizer.truncate(stringify(input), PROMPT_TOKENS.INPUT)
44
- condition = tokenizer.truncate(condition, PROMPT_TOKENS.CONDITION)
45
-
46
- // All tokens remaining after the input and condition are accounted can be used for examples
47
- const EXAMPLES_TOKENS = PROMPT_COMPONENT - tokenizer.count(inputAsString) - tokenizer.count(condition)
48
-
49
- const Key = fastHash(
50
- JSON.stringify({
51
- taskType,
52
- taskId,
53
- input: inputAsString,
54
- condition
55
- })
56
- )
57
-
58
- const examples = taskId
59
- ? await this.adapter.getExamples<string, boolean>({
60
- input: inputAsString,
61
- taskType,
62
- taskId
63
- })
64
- : []
65
-
66
- const exactMatch = examples.find((x) => x.key === Key)
67
- if (exactMatch) {
68
- return exactMatch.output
69
- }
70
-
71
- const defaultExamples = [
72
- { input: '50 Cent', check: true, reason: '50 Cent is widely recognized as a public personality.' },
73
- {
74
- input: ['apple', 'banana', 'carrot', 'house'],
75
- check: false,
76
- reason: 'The list contains a house, which is not a fruit. Also, the list contains a carrot, which is a vegetable.'
77
- }
78
- ]
79
-
80
- const userExamples = [
81
- ...examples.map((e) => ({ input: e.input, check: e.output, reason: e.explanation })),
82
- ...options.examples
83
- ]
84
-
85
- let exampleId = 1
86
-
87
- const formatInput = (input: string, condition: string) => {
88
- const header = userExamples.length ? `Expert Example #${exampleId++}` : `Example of condition: "${condition}"`
89
-
90
- return `
91
- ${header}
92
- <|start_input|>
93
- ${input.trim()}
94
- <|end_input|>
95
- `.trim()
96
- }
97
-
98
- const formatOutput = (answer: boolean, justification: string) => {
99
- return `
100
- Analysis: ${justification}
101
- Final Answer: ${answer ? TRUE : FALSE}
102
- ${END}
103
- `.trim()
104
- }
105
-
106
- const formatExample = (example: { input?: any; check: boolean; reason?: string }) => [
107
- { type: 'text' as const, content: formatInput(stringify(example.input ?? null), condition), role: 'user' as const },
108
- {
109
- type: 'text' as const,
110
- content: formatOutput(example.check, example.reason ?? ''),
111
- role: 'assistant' as const
112
- }
113
- ]
114
-
115
- const allExamples = takeUntilTokens(
116
- userExamples.length ? userExamples : defaultExamples,
117
- EXAMPLES_TOKENS,
118
- (el) => tokenizer.count(stringify(el.input)) + tokenizer.count(el.reason ?? '')
119
- )
120
- .map(formatExample)
121
- .flat()
122
-
123
- const specialInstructions = userExamples.length
124
- ? `
125
- - You have been provided with examples from previous experts. Make sure to read them carefully before making your decision.
126
- - Make sure to refer to the examples provided by the experts to justify your decision (when applicable).
127
- - When in doubt, ground your decision on the examples provided by the experts instead of your own intuition.
128
- - When no example is similar to the input, make sure to provide a clear justification for your decision while inferring the decision-making process from the examples provided by the experts.
129
- `.trim()
130
- : ''
131
-
132
- const output = await this.callModel({
133
- systemPrompt: `
134
- Check if the following condition is true or false for the given input. Before answering, make sure to read the input and the condition carefully.
135
- Justify your answer, then answer with either ${TRUE} or ${FALSE} at the very end, then add ${END} to finish the response.
136
- IMPORTANT: Make sure to answer with either ${TRUE} or ${FALSE} at the end of your response, but NOT both.
137
- ---
138
- Expert Examples (#1 to #${exampleId - 1}):
139
- ${specialInstructions}
140
- `.trim(),
141
- stopSequences: [END],
142
- messages: [
143
- ...allExamples,
144
- {
145
- type: 'text',
146
- content: `
147
- Considering the below input and above examples, is the following condition true or false?
148
- ${formatInput(inputAsString, condition)}
149
- In your "Analysis", please refer to the Expert Examples # to justify your decision.`.trim(),
150
- role: 'user'
151
- }
152
- ]
153
- })
154
-
155
- const answer = output.choices[0].content as string
156
-
157
- const hasTrue = answer.includes(TRUE)
158
- const hasFalse = answer.includes(FALSE)
159
-
160
- if (!hasTrue && !hasFalse) {
161
- throw new Error(`The model did not return a valid answer. The response was: ${answer}`)
162
- }
163
-
164
- let finalAnswer: boolean
165
-
166
- if (hasTrue && hasFalse) {
167
- // If both TRUE and FALSE are present, we need to check which one was answered last
168
- finalAnswer = answer.lastIndexOf(TRUE) > answer.lastIndexOf(FALSE)
169
- } else {
170
- finalAnswer = hasTrue
171
- }
172
-
173
- if (taskId) {
174
- await this.adapter.saveExample({
175
- key: Key,
176
- taskType,
177
- taskId,
178
- input: inputAsString,
179
- instructions: condition,
180
- metadata: output.metadata,
181
- output: finalAnswer,
182
- explanation: answer.replace(TRUE, '').replace(FALSE, '').replace(END, '').replace('Final Answer:', '').trim()
183
- })
184
- }
185
-
186
- return finalAnswer
187
- }
@@ -1,2 +0,0 @@
1
- export const PROMPT_INPUT_BUFFER = 1048
2
- export const PROMPT_OUTPUT_BUFFER = 512
@@ -1,9 +0,0 @@
1
- export class JsonParsingError extends Error {
2
- constructor(
3
- public json: unknown,
4
- public error: Error
5
- ) {
6
- const message = `Error parsing JSON:\n\n---JSON---\n${json}\n\n---Error---\n\n ${error}`
7
- super(message)
8
- }
9
- }
@@ -1,209 +0,0 @@
1
- import { describe, it, expect, beforeEach, afterEach, afterAll } from 'vitest'
2
-
3
- import { BotpressDocumentation, getClient, getZai, metadata } from './__tests'
4
- import { z } from '@bpinternal/zui'
5
- import { check } from '@botpress/vai'
6
- import { TableAdapter } from '../adapters/botpress-table'
7
-
8
- describe('zai.extract', () => {
9
- const zai = getZai()
10
-
11
- it('extract simple object from paragraph', async () => {
12
- const person = await zai.extract(
13
- 'My name is John Doe, I am 30 years old and I live in Quebec',
14
- z.object({
15
- name: z.string(),
16
- age: z.number(),
17
- location: z.string()
18
- })
19
- )
20
-
21
- expect(person).toMatchInlineSnapshot(`
22
- {
23
- "age": 30,
24
- "location": "Quebec",
25
- "name": "John Doe",
26
- }
27
- `)
28
- })
29
-
30
- it('extract an array of objects from paragraph', async () => {
31
- const people = await zai.extract(
32
- `
33
- My name is John Doe, I am 30 years old and I live in Quebec.
34
- My name is Jane Doe, I am 25 years old and I live in Montreal.
35
- His name is Jack Doe, he is 35 years old and he lives in Toronto.
36
- Her name is Jill Doe, she is 40 years old and she lives in Vancouver.`,
37
- z.array(
38
- z.object({
39
- name: z.string(),
40
- age: z.number(),
41
- location: z.string()
42
- })
43
- )
44
- )
45
-
46
- expect(people).toMatchInlineSnapshot(`
47
- [
48
- {
49
- "age": 30,
50
- "location": "Quebec",
51
- "name": "John Doe",
52
- },
53
- {
54
- "age": 25,
55
- "location": "Montreal",
56
- "name": "Jane Doe",
57
- },
58
- {
59
- "age": 35,
60
- "location": "Toronto",
61
- "name": "Jack Doe",
62
- },
63
- {
64
- "age": 40,
65
- "location": "Vancouver",
66
- "name": "Jill Doe",
67
- },
68
- ]
69
- `)
70
- })
71
-
72
- it('extract an object from anything as input', async () => {
73
- const person = await zai.extract(
74
- {
75
- person: { first: 'John', last: 'Doe', age: 30 }
76
- },
77
- z.object({
78
- a: z.string().describe('The full name of the person in the text'),
79
- b: z.number().describe('The age of the person in the text')
80
- })
81
- )
82
-
83
- expect(person).toMatchInlineSnapshot(`
84
- {
85
- "a": "John Doe",
86
- "b": 30,
87
- }
88
- `)
89
- })
90
-
91
- it('extract an array of objects from a super long text', async () => {
92
- const features = await zai.extract(
93
- BotpressDocumentation,
94
- z.array(
95
- z
96
- .object({
97
- feature: z.string().describe('The name of the feature'),
98
- parent: z.string().optional().describe('The parent feature').nullable(),
99
- description: z.string().describe('The description of the feature')
100
- })
101
- .describe('A feature of Botpress')
102
- ),
103
- {
104
- instructions:
105
- 'Extract all things that looks like a Botpress feature in the provided input. You must extract a minimum of one element.'
106
- }
107
- )
108
-
109
- check(features, 'Contains an element about flows / workflows').toBe(true)
110
- check(features, 'Contains an element about tables').toBe(true)
111
- check(features, 'Contains an element about webchat').toBe(true)
112
- check(features, 'Contains an element about integrations').toBe(true)
113
- check(features, 'Contains an element about HITL (human in the loop)').toBe(true)
114
- check(features, 'Contains an element about knowledge bases (KB) or Files API').toBe(true)
115
- })
116
- })
117
-
118
- describe('zai.learn.extract', () => {
119
- const client = getClient()
120
- let tableName = 'ZaiTestExtractInternalTable'
121
- let taskId = 'extract'
122
- let zai = getZai()
123
-
124
- beforeEach(async () => {
125
- zai = getZai().with({
126
- activeLearning: {
127
- enable: true,
128
- taskId,
129
- tableName
130
- }
131
- })
132
- })
133
-
134
- afterEach(async () => {
135
- try {
136
- await client.deleteTableRows({ table: tableName, deleteAllRows: true })
137
- } catch (err) {}
138
- })
139
-
140
- afterAll(async () => {
141
- try {
142
- await client.deleteTable({ table: tableName })
143
- } catch (err) {}
144
- })
145
-
146
- it('learns a extraction format from examples', async () => {
147
- const adapter = new TableAdapter({
148
- client,
149
- tableName
150
- })
151
-
152
- const value = await zai.learn(taskId).extract(
153
- `I really liked Casino Royale`,
154
- z.object({
155
- name: z.string(),
156
- movie: z.string()
157
- }),
158
- { instructions: 'extract the name of the movie and name of the main character' }
159
- )
160
-
161
- check(value, 'extracted james bond and casino royale').toBe(true)
162
- check(value, 'the values are NOT IN ALL CAPS').toBe(true)
163
-
164
- let rows = await client.findTableRows({ table: tableName })
165
- expect(rows.rows.length).toBe(1)
166
-
167
- await adapter.saveExample({
168
- key: 't1',
169
- taskId: `zai/${taskId}`,
170
- taskType: 'zai.extract',
171
- instructions: 'extract name of movie and main character',
172
- input: `I went to see the Titanic yesterday and I fell asleep`,
173
- output: { name: 'JACK DAWSON', movie: 'TITANIC' },
174
- metadata,
175
- status: 'approved'
176
- })
177
-
178
- await adapter.saveExample({
179
- key: 't2',
180
- taskId: `zai/${taskId}`,
181
- taskType: 'zai.extract',
182
- instructions: 'extract name of movie and main character',
183
- input: `Did you know that the gladiator movie has a lot of fighting scenes?`,
184
- output: { name: 'MAXIMUS DECIMUS MERIDIUS', movie: 'GLADIATOR' },
185
- metadata,
186
- status: 'approved'
187
- })
188
-
189
- const second = await zai.learn(taskId).extract(
190
- `I really liked Casino Royale`,
191
- z.object({
192
- name: z.string(),
193
- movie: z.string()
194
- }),
195
- { instructions: 'extract the name of the movie and name of the main character' }
196
- )
197
-
198
- expect(second).toMatchInlineSnapshot(`
199
- {
200
- "movie": "CASINO ROYALE",
201
- "name": "JAMES BOND",
202
- }
203
- `)
204
-
205
- rows = await client.findTableRows({ table: tableName })
206
- expect(rows.rows.length).toBe(3)
207
- expect(rows.rows[0].output.value).toMatchObject(second)
208
- })
209
- })