@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.
- package/dist/index.cjs +1898 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +916 -0
- package/dist/index.d.ts +916 -0
- package/dist/index.js +1868 -0
- package/dist/index.js.map +1 -0
- package/ensure-env.cjs +9 -0
- package/package.json +25 -17
- package/tsconfig.json +23 -18
- package/tsup.config.ts +16 -0
- package/vitest.config.ts +2 -16
- package/vitest.setup.ts +11 -15
- package/dist/src/adapters/adapter.d.ts +0 -27
- package/dist/src/adapters/adapter.js +0 -3
- package/dist/src/adapters/adapter.js.map +0 -1
- package/dist/src/adapters/botpress-table.d.ts +0 -155
- package/dist/src/adapters/botpress-table.js +0 -170
- package/dist/src/adapters/botpress-table.js.map +0 -1
- package/dist/src/adapters/memory.d.ts +0 -7
- package/dist/src/adapters/memory.js +0 -13
- package/dist/src/adapters/memory.js.map +0 -1
- package/dist/src/env.d.ts +0 -1
- package/dist/src/env.js +0 -27
- package/dist/src/env.js.map +0 -1
- package/dist/src/index.d.ts +0 -9
- package/dist/src/index.js +0 -10
- package/dist/src/index.js.map +0 -1
- package/dist/src/models.d.ts +0 -253
- package/dist/src/models.js +0 -348
- package/dist/src/models.js.map +0 -1
- package/dist/src/operations/__tests/index.d.ts +0 -19
- package/dist/src/operations/__tests/index.js +0 -24
- package/dist/src/operations/__tests/index.js.map +0 -1
- package/dist/src/operations/check.d.ts +0 -36
- package/dist/src/operations/check.js +0 -146
- package/dist/src/operations/check.js.map +0 -1
- package/dist/src/operations/check.test.d.ts +0 -1
- package/dist/src/operations/check.test.js +0 -129
- package/dist/src/operations/check.test.js.map +0 -1
- package/dist/src/operations/constants.d.ts +0 -2
- package/dist/src/operations/constants.js +0 -3
- package/dist/src/operations/constants.js.map +0 -1
- package/dist/src/operations/errors.d.ts +0 -5
- package/dist/src/operations/errors.js +0 -11
- package/dist/src/operations/errors.js.map +0 -1
- package/dist/src/operations/extract.d.ts +0 -20
- package/dist/src/operations/extract.js +0 -235
- package/dist/src/operations/extract.js.map +0 -1
- package/dist/src/operations/extract.test.d.ts +0 -1
- package/dist/src/operations/extract.test.js +0 -163
- package/dist/src/operations/extract.test.js.map +0 -1
- package/dist/src/operations/filter.d.ts +0 -39
- package/dist/src/operations/filter.js +0 -184
- package/dist/src/operations/filter.js.map +0 -1
- package/dist/src/operations/filter.test.d.ts +0 -1
- package/dist/src/operations/filter.test.js +0 -151
- package/dist/src/operations/filter.test.js.map +0 -1
- package/dist/src/operations/label.d.ts +0 -79
- package/dist/src/operations/label.js +0 -250
- package/dist/src/operations/label.js.map +0 -1
- package/dist/src/operations/label.test.d.ts +0 -1
- package/dist/src/operations/label.test.js +0 -213
- package/dist/src/operations/label.test.js.map +0 -1
- package/dist/src/operations/rewrite.d.ts +0 -34
- package/dist/src/operations/rewrite.js +0 -106
- package/dist/src/operations/rewrite.js.map +0 -1
- package/dist/src/operations/rewrite.test.d.ts +0 -1
- package/dist/src/operations/rewrite.test.js +0 -96
- package/dist/src/operations/rewrite.test.js.map +0 -1
- package/dist/src/operations/summarize.d.ts +0 -46
- package/dist/src/operations/summarize.js +0 -139
- package/dist/src/operations/summarize.js.map +0 -1
- package/dist/src/operations/summarize.test.d.ts +0 -1
- package/dist/src/operations/summarize.test.js +0 -22
- package/dist/src/operations/summarize.test.js.map +0 -1
- package/dist/src/operations/text.d.ts +0 -16
- package/dist/src/operations/text.js +0 -47
- package/dist/src/operations/text.js.map +0 -1
- package/dist/src/operations/text.test.d.ts +0 -1
- package/dist/src/operations/text.test.js +0 -51
- package/dist/src/operations/text.test.js.map +0 -1
- package/dist/src/operations/zai-learn.test.d.ts +0 -1
- package/dist/src/operations/zai-learn.test.js +0 -71
- package/dist/src/operations/zai-learn.test.js.map +0 -1
- package/dist/src/operations/zai-retry.test.d.ts +0 -1
- package/dist/src/operations/zai-retry.test.js +0 -50
- package/dist/src/operations/zai-retry.test.js.map +0 -1
- package/dist/src/utils.d.ts +0 -53
- package/dist/src/utils.js +0 -49
- package/dist/src/utils.js.map +0 -1
- package/dist/src/zai.d.ts +0 -91
- package/dist/src/zai.js +0 -139
- package/dist/src/zai.js.map +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/dist/typings.d.ts +0 -901
- package/dist/typings.tsbuildinfo +0 -1
- package/dist/vitest.config.d.ts +0 -2
- package/dist/vitest.config.js +0 -21
- package/dist/vitest.config.js.map +0 -1
- package/dist/vitest.setup.d.ts +0 -1
- package/dist/vitest.setup.js +0 -22
- package/dist/vitest.setup.js.map +0 -1
- package/src/adapters/adapter.ts +0 -35
- package/src/adapters/botpress-table.ts +0 -213
- package/src/adapters/memory.ts +0 -13
- package/src/env.ts +0 -54
- package/src/index.ts +0 -11
- package/src/models.ts +0 -347
- package/src/operations/__tests/botpress_docs.txt +0 -26040
- package/src/operations/__tests/index.ts +0 -30
- package/src/operations/check.test.ts +0 -155
- package/src/operations/check.ts +0 -187
- package/src/operations/constants.ts +0 -2
- package/src/operations/errors.ts +0 -9
- package/src/operations/extract.test.ts +0 -209
- package/src/operations/extract.ts +0 -291
- package/src/operations/filter.test.ts +0 -182
- package/src/operations/filter.ts +0 -231
- package/src/operations/label.test.ts +0 -239
- package/src/operations/label.ts +0 -332
- package/src/operations/rewrite.test.ts +0 -114
- package/src/operations/rewrite.ts +0 -148
- package/src/operations/summarize.test.ts +0 -25
- package/src/operations/summarize.ts +0 -193
- package/src/operations/text.test.ts +0 -60
- package/src/operations/text.ts +0 -63
- package/src/operations/zai-learn.test.ts +0 -85
- package/src/operations/zai-retry.test.ts +0 -64
- package/src/scripts/update-models.ts +0 -74
- package/src/utils.ts +0 -61
- package/src/zai.ts +0 -185
- package/tsconfig.types.json +0 -22
- package/turbo.json +0 -14
|
@@ -1,148 +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
|
-
type Example = z.input<typeof Example> & { instructions?: string }
|
|
8
|
-
const Example = z.object({
|
|
9
|
-
input: z.string(),
|
|
10
|
-
output: z.string()
|
|
11
|
-
})
|
|
12
|
-
|
|
13
|
-
export type Options = z.input<typeof Options>
|
|
14
|
-
const Options = z.object({
|
|
15
|
-
examples: z.array(Example).default([]),
|
|
16
|
-
length: z.number().min(10).max(16_000).optional().describe('The maximum number of tokens to generate')
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
declare module '../zai' {
|
|
20
|
-
interface Zai {
|
|
21
|
-
/** Rewrites a string according to match the prompt */
|
|
22
|
-
rewrite(original: string, prompt: string, options?: Options): Promise<string>
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const START = '■START■'
|
|
27
|
-
const END = '■END■'
|
|
28
|
-
|
|
29
|
-
Zai.prototype.rewrite = async function (this: Zai, original, prompt, _options) {
|
|
30
|
-
const options = Options.parse(_options ?? {})
|
|
31
|
-
const tokenizer = await this.getTokenizer()
|
|
32
|
-
|
|
33
|
-
const taskId = this.taskId
|
|
34
|
-
const taskType = 'zai.rewrite'
|
|
35
|
-
|
|
36
|
-
const INPUT_COMPONENT_SIZE = Math.max(100, (this.Model.input.maxTokens - PROMPT_INPUT_BUFFER) / 2)
|
|
37
|
-
prompt = tokenizer.truncate(prompt, INPUT_COMPONENT_SIZE)
|
|
38
|
-
|
|
39
|
-
const inputSize = tokenizer.count(original) + tokenizer.count(prompt)
|
|
40
|
-
const maxInputSize = this.Model.input.maxTokens - tokenizer.count(prompt) - PROMPT_INPUT_BUFFER
|
|
41
|
-
if (inputSize > maxInputSize) {
|
|
42
|
-
throw new Error(
|
|
43
|
-
`The input size is ${inputSize} tokens long, which is more than the maximum of ${maxInputSize} tokens for this model (${this.Model.name} = ${this.Model.input.maxTokens} tokens)`
|
|
44
|
-
)
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const instructions: string[] = []
|
|
48
|
-
|
|
49
|
-
const originalSize = tokenizer.count(original)
|
|
50
|
-
if (options.length && originalSize > options.length) {
|
|
51
|
-
instructions.push(`The original text is ${originalSize} tokens long – it should be less than ${options.length}`)
|
|
52
|
-
instructions.push(
|
|
53
|
-
`The text must be standalone and complete in less than ${options.length} tokens, so it has to be shortened to fit the length as well`
|
|
54
|
-
)
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const format = (before: string, prompt: string) => {
|
|
58
|
-
return `
|
|
59
|
-
Prompt: ${prompt}
|
|
60
|
-
|
|
61
|
-
${START}
|
|
62
|
-
${before}
|
|
63
|
-
${END}
|
|
64
|
-
`.trim()
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const Key = fastHash(
|
|
68
|
-
stringify({
|
|
69
|
-
taskId,
|
|
70
|
-
taskType,
|
|
71
|
-
input: original,
|
|
72
|
-
prompt
|
|
73
|
-
})
|
|
74
|
-
)
|
|
75
|
-
|
|
76
|
-
const formatExample = ({ input, output, instructions }: Example) => {
|
|
77
|
-
return [
|
|
78
|
-
{ type: 'text' as const, role: 'user' as const, content: format(input, instructions || prompt) },
|
|
79
|
-
{ type: 'text' as const, role: 'assistant' as const, content: `${START}${output}${END}` }
|
|
80
|
-
]
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const defaultExamples: Example[] = [
|
|
84
|
-
{ input: 'Hello, how are you?', output: 'Bonjour, comment ça va?', instructions: 'translate to French' },
|
|
85
|
-
{ input: '1\n2\n3', output: '3\n2\n1', instructions: 'reverse the order' }
|
|
86
|
-
]
|
|
87
|
-
|
|
88
|
-
const tableExamples = taskId
|
|
89
|
-
? await this.adapter.getExamples<string, string>({
|
|
90
|
-
input: original,
|
|
91
|
-
taskId,
|
|
92
|
-
taskType
|
|
93
|
-
})
|
|
94
|
-
: []
|
|
95
|
-
|
|
96
|
-
const exactMatch = tableExamples.find((x) => x.key === Key)
|
|
97
|
-
if (exactMatch) {
|
|
98
|
-
return exactMatch.output
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const savedExamples: Example[] = [
|
|
102
|
-
...tableExamples.map((x) => ({ input: x.input as string, output: x.output as string })),
|
|
103
|
-
...options.examples
|
|
104
|
-
]
|
|
105
|
-
|
|
106
|
-
const REMAINING_TOKENS = this.Model.input.maxTokens - tokenizer.count(prompt) - PROMPT_INPUT_BUFFER
|
|
107
|
-
const examples = takeUntilTokens(
|
|
108
|
-
savedExamples.length ? savedExamples : defaultExamples,
|
|
109
|
-
REMAINING_TOKENS,
|
|
110
|
-
(el) => tokenizer.count(stringify(el.input)) + tokenizer.count(stringify(el.output))
|
|
111
|
-
)
|
|
112
|
-
.map(formatExample)
|
|
113
|
-
.flat()
|
|
114
|
-
|
|
115
|
-
const output = await this.callModel({
|
|
116
|
-
systemPrompt: `
|
|
117
|
-
Rewrite the text between the ${START} and ${END} tags to match the user prompt.
|
|
118
|
-
${instructions.map((x) => `• ${x}`).join('\n')}
|
|
119
|
-
`.trim(),
|
|
120
|
-
messages: [...examples, { type: 'text', content: format(original, prompt), role: 'user' }],
|
|
121
|
-
maxTokens: options.length,
|
|
122
|
-
stopSequences: [END]
|
|
123
|
-
})
|
|
124
|
-
|
|
125
|
-
let result = output.choices[0].content as string
|
|
126
|
-
|
|
127
|
-
if (result.includes(START)) {
|
|
128
|
-
result = result.slice(result.indexOf(START) + START.length)
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
if (result.includes(END)) {
|
|
132
|
-
result = result.slice(0, result.indexOf(END))
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
if (taskId) {
|
|
136
|
-
await this.adapter.saveExample({
|
|
137
|
-
key: Key,
|
|
138
|
-
metadata: output.metadata,
|
|
139
|
-
instructions: prompt,
|
|
140
|
-
input: original,
|
|
141
|
-
output: result,
|
|
142
|
-
taskType,
|
|
143
|
-
taskId
|
|
144
|
-
})
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
return result
|
|
148
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { check } from '@botpress/vai'
|
|
2
|
-
import { describe, it } from 'vitest'
|
|
3
|
-
|
|
4
|
-
import { BotpressDocumentation, getZai } from './__tests'
|
|
5
|
-
|
|
6
|
-
describe('zai.summarize', () => {
|
|
7
|
-
const zai = getZai()
|
|
8
|
-
|
|
9
|
-
it.skip('summarize long document to a concise 2000 token summary', async () => {
|
|
10
|
-
const result = await zai.summarize(BotpressDocumentation, {
|
|
11
|
-
length: 2000,
|
|
12
|
-
prompt: `Extract the Table of Contents for the Botpress Documentation. Pay special attention to all the different features. Focus on horizontal coverage of features rather than going in depth into one feature. The goal is to have a complete overview of what the documentation covers.`
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
check(result, 'The text is a summary of the Botpress documentation').toBe(true)
|
|
16
|
-
check(result, 'The text explains shortly what botpress is').toBe(true)
|
|
17
|
-
check(result, 'The text uses markdown format').toBe(true)
|
|
18
|
-
check(result, 'The text has some information about integrations').toBe(true)
|
|
19
|
-
check(result, 'The text has a section about Flows (or Workflows)').toBe(true)
|
|
20
|
-
check(result, 'The text has a section about the Botpress API').toBe(true)
|
|
21
|
-
check(result, 'The text mentions the notion of workspaces').toBe(true)
|
|
22
|
-
check(result, 'The text has some information about the Webchat').toBe(true)
|
|
23
|
-
check(result, 'The text has some information about HITL (human in the loop)').toBe(true)
|
|
24
|
-
})
|
|
25
|
-
})
|
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
import { z } from '@bpinternal/zui'
|
|
2
|
-
|
|
3
|
-
import _ from 'lodash'
|
|
4
|
-
import { Zai } from '../zai'
|
|
5
|
-
import { PROMPT_INPUT_BUFFER, PROMPT_OUTPUT_BUFFER } from './constants'
|
|
6
|
-
|
|
7
|
-
export type Options = z.input<typeof Options>
|
|
8
|
-
const Options = z.object({
|
|
9
|
-
prompt: z
|
|
10
|
-
.string()
|
|
11
|
-
.describe('What should the text be summarized to?')
|
|
12
|
-
.default('New information, concepts and ideas that are deemed important'),
|
|
13
|
-
format: z
|
|
14
|
-
.string()
|
|
15
|
-
.describe('How to format the example text')
|
|
16
|
-
.default(
|
|
17
|
-
'A normal text with multiple sentences and paragraphs. Use markdown to format the text into sections. Use headings, lists, and other markdown features to make the text more readable. Do not include links, images, or other non-text elements.'
|
|
18
|
-
),
|
|
19
|
-
length: z.number().min(10).max(100_000).describe('The length of the summary in tokens').default(250),
|
|
20
|
-
intermediateFactor: z
|
|
21
|
-
.number()
|
|
22
|
-
.min(1)
|
|
23
|
-
.max(10)
|
|
24
|
-
.describe('How many times longer (than final length) are the intermediate summaries generated')
|
|
25
|
-
.default(4),
|
|
26
|
-
maxIterations: z.number().min(1).default(100),
|
|
27
|
-
sliding: z
|
|
28
|
-
.object({
|
|
29
|
-
window: z.number().min(10).max(100_000),
|
|
30
|
-
overlap: z.number().min(0).max(100_000)
|
|
31
|
-
})
|
|
32
|
-
.describe('Sliding window options')
|
|
33
|
-
.default({ window: 50_000, overlap: 250 })
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
declare module '../zai' {
|
|
37
|
-
interface Zai {
|
|
38
|
-
/** Summarizes a text of any length to a summary of the desired length */
|
|
39
|
-
summarize(original: string, options?: Options): Promise<string>
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const START = '■START■'
|
|
44
|
-
const END = '■END■'
|
|
45
|
-
|
|
46
|
-
Zai.prototype.summarize = async function (this: Zai, original, _options) {
|
|
47
|
-
const options = Options.parse(_options ?? {})
|
|
48
|
-
const tokenizer = await this.getTokenizer()
|
|
49
|
-
|
|
50
|
-
const INPUT_COMPONENT_SIZE = Math.max(100, (this.Model.input.maxTokens - PROMPT_INPUT_BUFFER) / 4)
|
|
51
|
-
options.prompt = tokenizer.truncate(options.prompt, INPUT_COMPONENT_SIZE)
|
|
52
|
-
options.format = tokenizer.truncate(options.format, INPUT_COMPONENT_SIZE)
|
|
53
|
-
|
|
54
|
-
const maxOutputSize = this.Model.output.maxTokens - PROMPT_OUTPUT_BUFFER
|
|
55
|
-
if (options.length > maxOutputSize) {
|
|
56
|
-
throw new Error(
|
|
57
|
-
`The desired output length is ${maxOutputSize} tokens long, which is more than the maximum of ${this.Model.output.maxTokens} tokens for this model (${this.Model.name})`
|
|
58
|
-
)
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// Ensure the sliding window is not bigger than the model input size
|
|
62
|
-
options.sliding.window = Math.min(options.sliding.window, this.Model.input.maxTokens - PROMPT_INPUT_BUFFER)
|
|
63
|
-
|
|
64
|
-
// Ensure the overlap is not bigger than the window
|
|
65
|
-
// Most extreme case possible (all 3 same size)
|
|
66
|
-
// |ooooooooooooooooooo|wwwwwwwwwwwwwww|ooooooooooooooooooo|
|
|
67
|
-
// |<---- overlap ---->|<-- window -->|<---- overlap ---->|
|
|
68
|
-
options.sliding.overlap = Math.min(options.sliding.overlap, options.sliding.window - 3 * options.sliding.overlap)
|
|
69
|
-
|
|
70
|
-
const format = (summary: string, newText: string) => {
|
|
71
|
-
return `
|
|
72
|
-
${START}
|
|
73
|
-
${summary.length ? summary : '<summary still empty>'}
|
|
74
|
-
${END}
|
|
75
|
-
|
|
76
|
-
Please amend the summary between the ${START} and ${END} tags to accurately reflect the prompt and the additional text below.
|
|
77
|
-
|
|
78
|
-
<|start_new_information|>
|
|
79
|
-
${newText}
|
|
80
|
-
<|new_information|>`.trim()
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const tokens = tokenizer.split(original)
|
|
84
|
-
const parts = Math.ceil(tokens.length / (options.sliding.window - options.sliding.overlap))
|
|
85
|
-
let iteration = 0
|
|
86
|
-
|
|
87
|
-
// We split it recursively into smaller parts until we're at less than 4 window slides per part
|
|
88
|
-
// Then we use a merge strategy to combine the sub-chunks summaries
|
|
89
|
-
// This is basically a merge sort algorithm (but summary instead of sorting)
|
|
90
|
-
const N = 2 // This is the merge sort exponent
|
|
91
|
-
const useMergeSort = parts >= Math.pow(2, N)
|
|
92
|
-
const chunkSize = Math.ceil(tokens.length / (parts * N))
|
|
93
|
-
|
|
94
|
-
if (useMergeSort) {
|
|
95
|
-
const chunks = _.chunk(tokens, chunkSize).map((x) => x.join(''))
|
|
96
|
-
const allSummaries = await Promise.all(chunks.map((chunk) => this.summarize(chunk, options)))
|
|
97
|
-
return this.summarize(allSummaries.join('\n\n============\n\n'), options)
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const summaries: string[] = []
|
|
101
|
-
let currentSummary = ''
|
|
102
|
-
|
|
103
|
-
for (let i = 0; i < tokens.length; i += options.sliding.window) {
|
|
104
|
-
const from = Math.max(0, i - options.sliding.overlap)
|
|
105
|
-
const to = Math.min(tokens.length, i + options.sliding.window + options.sliding.overlap)
|
|
106
|
-
const isFirst = i === 0
|
|
107
|
-
const isLast = to >= tokens.length
|
|
108
|
-
|
|
109
|
-
const slice = tokens.slice(from, to).join('')
|
|
110
|
-
|
|
111
|
-
if (iteration++ >= options.maxIterations) {
|
|
112
|
-
break
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const instructions: string[] = [
|
|
116
|
-
`At each step, you will receive a part of the text to summarize. Make sure to reply with the new summary in the tags ${START} and ${END}.`,
|
|
117
|
-
'Summarize the text and make sure that the main points are included.',
|
|
118
|
-
'Ignore any unnecessary details and focus on the main points.',
|
|
119
|
-
'Use short and concise sentences to increase readability and information density.',
|
|
120
|
-
'When looking at the new information, focus on: ' + options.prompt
|
|
121
|
-
]
|
|
122
|
-
|
|
123
|
-
if (isFirst) {
|
|
124
|
-
instructions.push(
|
|
125
|
-
'The current summary is empty. You need to generate a summary that covers the main points of the text.'
|
|
126
|
-
)
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
let generationLength = options.length
|
|
130
|
-
|
|
131
|
-
if (!isLast) {
|
|
132
|
-
generationLength = Math.min(
|
|
133
|
-
tokenizer.count(currentSummary) + options.length * options.intermediateFactor,
|
|
134
|
-
maxOutputSize
|
|
135
|
-
)
|
|
136
|
-
|
|
137
|
-
instructions.push(
|
|
138
|
-
'You need to amend the summary to include the new information. Make sure the summary is complete and covers all the main points.'
|
|
139
|
-
)
|
|
140
|
-
|
|
141
|
-
instructions.push(`The current summary is ${currentSummary.length} tokens long.`)
|
|
142
|
-
instructions.push(`You can amend the summary to be up to ${generationLength} tokens long.`)
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
if (isLast) {
|
|
146
|
-
instructions.push(
|
|
147
|
-
'This is the last part you will have to summarize. Make sure the summary is complete and covers all the main points.'
|
|
148
|
-
)
|
|
149
|
-
instructions.push(
|
|
150
|
-
`The current summary is ${currentSummary.length} tokens long. You need to make sure it is ${options.length} tokens or less.`
|
|
151
|
-
)
|
|
152
|
-
|
|
153
|
-
if (currentSummary.length > options.length) {
|
|
154
|
-
instructions.push(
|
|
155
|
-
`The current summary is already too long, so you need to shorten it to ${options.length} tokens while also including the new information.`
|
|
156
|
-
)
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
const output = await this.callModel({
|
|
161
|
-
systemPrompt: `
|
|
162
|
-
You are summarizing a text. The text is split into ${parts} parts, and you are currently working on part ${iteration}.
|
|
163
|
-
At every step, you will receive the current summary and a new part of the text. You need to amend the summary to include the new information (if needed).
|
|
164
|
-
The summary needs to cover the main points of the text and must be concise.
|
|
165
|
-
|
|
166
|
-
IMPORTANT INSTRUCTIONS:
|
|
167
|
-
${instructions.map((x) => `- ${x.trim()}`).join('\n')}
|
|
168
|
-
|
|
169
|
-
FORMAT OF THE SUMMARY:
|
|
170
|
-
${options.format}
|
|
171
|
-
`.trim(),
|
|
172
|
-
messages: [{ type: 'text', content: format(currentSummary, slice), role: 'user' }],
|
|
173
|
-
maxTokens: generationLength,
|
|
174
|
-
stopSequences: [END]
|
|
175
|
-
})
|
|
176
|
-
|
|
177
|
-
let result = output.choices[0].content as string
|
|
178
|
-
|
|
179
|
-
if (result.includes(START)) {
|
|
180
|
-
result = result.slice(result.indexOf(START) + START.length)
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
if (result.includes('■')) {
|
|
184
|
-
// can happen if the model truncates the text before the entire END tag is written
|
|
185
|
-
result = result.slice(0, result.indexOf('■'))
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
summaries.push(result)
|
|
189
|
-
currentSummary = result
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
return currentSummary.trim()
|
|
193
|
-
}
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest'
|
|
2
|
-
import { check } from '@botpress/vai'
|
|
3
|
-
|
|
4
|
-
import { getZai, tokenizer } from './__tests'
|
|
5
|
-
|
|
6
|
-
describe('zai.text', { timeout: 60_000 }, () => {
|
|
7
|
-
const zai = getZai()
|
|
8
|
-
|
|
9
|
-
it('generate a horror novel with no params', async () => {
|
|
10
|
-
const story = await zai.text('write a short horror novel')
|
|
11
|
-
check(story, 'is a short horror story').toBe(true)
|
|
12
|
-
})
|
|
13
|
-
|
|
14
|
-
it('No fluffy text at the beginning', async () => {
|
|
15
|
-
const story = await zai.text('write a short horror novel')
|
|
16
|
-
|
|
17
|
-
check(story, 'There is no LLM fluff at the beginning', {
|
|
18
|
-
examples: [
|
|
19
|
-
{
|
|
20
|
-
value: 'Title: A horror story\nChapter 1: The woods\nOnce upen a time, ...',
|
|
21
|
-
expected: true,
|
|
22
|
-
reason: 'It begins straight with a story, no fluff at the beginning'
|
|
23
|
-
},
|
|
24
|
-
{ value: 'Once upon a time, a ...', expected: true, reason: 'The story starts directly' },
|
|
25
|
-
{
|
|
26
|
-
value: 'Sure, I will generate a story.\nOnce upen a time, a...',
|
|
27
|
-
expected: false,
|
|
28
|
-
reason: 'There is some fluff at the beginning'
|
|
29
|
-
}
|
|
30
|
-
]
|
|
31
|
-
}).toBe(true)
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
it('No fluffy text at the end', async () => {
|
|
35
|
-
const story = await zai.text('write a short horror novel')
|
|
36
|
-
|
|
37
|
-
check(story, 'There is no LLM fluff at the end', {
|
|
38
|
-
examples: [
|
|
39
|
-
{
|
|
40
|
-
value: 'Title: A horror story\nChapter 1: The woods\nOnce upen a time, ... The End.',
|
|
41
|
-
expected: true,
|
|
42
|
-
reason: 'The end is clear and direct, no fluff at the end'
|
|
43
|
-
},
|
|
44
|
-
{
|
|
45
|
-
value:
|
|
46
|
-
'Sure, I will generate a story.\nOnce upen a time, a... The End.\nLet me know if you want more or if you are happy with this.',
|
|
47
|
-
expected: false,
|
|
48
|
-
reason: 'There is some fluff from the assistant at the end.'
|
|
49
|
-
}
|
|
50
|
-
]
|
|
51
|
-
}).toBe(true)
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
it('length/max tokens param', async () => {
|
|
55
|
-
const story = await zai.text('write a short but complete horror story (with conclusion)', { length: 100 })
|
|
56
|
-
expect(tokenizer.count(story)).toBeLessThanOrEqual(110)
|
|
57
|
-
|
|
58
|
-
check(story, 'could be the beginning of a horror story').toBe(true)
|
|
59
|
-
})
|
|
60
|
-
})
|
package/src/operations/text.ts
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { z } from '@bpinternal/zui'
|
|
2
|
-
|
|
3
|
-
import _ from 'lodash'
|
|
4
|
-
import { Zai } from '../zai'
|
|
5
|
-
import { PROMPT_INPUT_BUFFER, PROMPT_OUTPUT_BUFFER } from './constants'
|
|
6
|
-
|
|
7
|
-
export type Options = z.input<typeof Options>
|
|
8
|
-
const Options = z.object({
|
|
9
|
-
length: z.number().min(1).max(100_000).optional().describe('The maximum number of tokens to generate')
|
|
10
|
-
})
|
|
11
|
-
|
|
12
|
-
declare module '../zai' {
|
|
13
|
-
interface Zai {
|
|
14
|
-
/** Generates a text of the desired length according to the prompt */
|
|
15
|
-
text(prompt: string, options?: Options): Promise<string>
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
Zai.prototype.text = async function (this: Zai, prompt, _options) {
|
|
20
|
-
const options = Options.parse(_options ?? {})
|
|
21
|
-
const tokenizer = await this.getTokenizer()
|
|
22
|
-
|
|
23
|
-
prompt = tokenizer.truncate(prompt, Math.max(this.Model.input.maxTokens - PROMPT_INPUT_BUFFER, 100))
|
|
24
|
-
|
|
25
|
-
if (options.length) {
|
|
26
|
-
options.length = Math.min(this.Model.output.maxTokens - PROMPT_OUTPUT_BUFFER, options.length)
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const instructions: string[] = []
|
|
30
|
-
let chart = ''
|
|
31
|
-
|
|
32
|
-
if (options.length) {
|
|
33
|
-
const length = _.clamp(options.length * 0.75, 5, options.length)
|
|
34
|
-
instructions.push(`IMPORTANT: Length constraint: ${length} tokens/words`)
|
|
35
|
-
instructions.push(`The text must be standalone and complete in less than ${length} tokens/words`)
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
if (options.length && options.length <= 500) {
|
|
39
|
-
chart = `
|
|
40
|
-
| Tokens | Text Length (approximate) |
|
|
41
|
-
|-------------|--------------------------------------|
|
|
42
|
-
| < 5 tokens | 1-3 words |
|
|
43
|
-
| 5-10 tokens | 3-6 words |
|
|
44
|
-
| 10-20 tokens| 6-15 words |
|
|
45
|
-
| 20-50 tokens| A short sentence (15-30 words) |
|
|
46
|
-
| 50-100 tokens| A medium sentence (30-70 words) |
|
|
47
|
-
| 100-200 tokens| A short paragraph (70-150 words) |
|
|
48
|
-
| 200-300 tokens| A medium paragraph (150-200 words) |
|
|
49
|
-
| 300-500 tokens| A long paragraph (200-300 words) |`.trim()
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const output = await this.callModel({
|
|
53
|
-
systemPrompt: `
|
|
54
|
-
Generate a text that fulfills the user prompt below. Answer directly to the prompt, without any acknowledgements or fluff. Also, make sure the text is standalone and complete.
|
|
55
|
-
${instructions.map((x) => `- ${x}`).join('\n')}
|
|
56
|
-
${chart}
|
|
57
|
-
`.trim(),
|
|
58
|
-
temperature: 0.7,
|
|
59
|
-
messages: [{ type: 'text', content: prompt, role: 'user' }],
|
|
60
|
-
maxTokens: options.length
|
|
61
|
-
})
|
|
62
|
-
return output.choices[0].content as string
|
|
63
|
-
}
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, afterAll, beforeEach, afterEach, vi } from 'vitest'
|
|
2
|
-
|
|
3
|
-
import { getClient, getZai } from './__tests'
|
|
4
|
-
|
|
5
|
-
import { check } from '@botpress/vai'
|
|
6
|
-
import { Client } from '@botpress/client'
|
|
7
|
-
|
|
8
|
-
describe('zai.learn / generic', { timeout: 60_000 }, () => {
|
|
9
|
-
const client = getClient()
|
|
10
|
-
let tableName = 'ZaiTestInternalTable'
|
|
11
|
-
let taskId = 'test'
|
|
12
|
-
let zai = getZai()
|
|
13
|
-
|
|
14
|
-
beforeEach(async () => {
|
|
15
|
-
zai = getZai().with({
|
|
16
|
-
activeLearning: {
|
|
17
|
-
enable: true,
|
|
18
|
-
taskId,
|
|
19
|
-
tableName
|
|
20
|
-
}
|
|
21
|
-
})
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
afterEach(async () => {
|
|
25
|
-
try {
|
|
26
|
-
await client.deleteTableRows({ table: tableName, deleteAllRows: true })
|
|
27
|
-
} catch (err) {}
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
afterAll(async () => {
|
|
31
|
-
try {
|
|
32
|
-
await client.deleteTable({ table: tableName })
|
|
33
|
-
} catch (err) {}
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
it('saves examples to tables', async () => {
|
|
37
|
-
const value = await zai
|
|
38
|
-
.learn(taskId)
|
|
39
|
-
.check('This text is very clearly written in English.', 'is an english sentence')
|
|
40
|
-
|
|
41
|
-
const { rows } = await client.findTableRows({ table: tableName })
|
|
42
|
-
|
|
43
|
-
expect(value).toBe(true)
|
|
44
|
-
expect(rows.length).toBe(1)
|
|
45
|
-
|
|
46
|
-
check(rows[0].explanation, 'is an explanation sentence')
|
|
47
|
-
expect(rows[0].explanation).not.toContain('Final Answer:')
|
|
48
|
-
expect(rows[0].output).toMatchObject({ value: true })
|
|
49
|
-
expect(rows[0].input).toMatchInlineSnapshot(`
|
|
50
|
-
{
|
|
51
|
-
"value": "This text is very clearly written in English.",
|
|
52
|
-
}
|
|
53
|
-
`)
|
|
54
|
-
expect(rows[0].taskId).toEqual('zai/test')
|
|
55
|
-
expect(rows[0].taskType).toBe('zai.check')
|
|
56
|
-
})
|
|
57
|
-
|
|
58
|
-
it('works even if tables are down', async () => {
|
|
59
|
-
const upsertTableRows = vi.fn(async () => {
|
|
60
|
-
throw new Error('Table is down')
|
|
61
|
-
})
|
|
62
|
-
|
|
63
|
-
const findTableRows = vi.fn(async () => {
|
|
64
|
-
throw new Error('Table is down')
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
const client = {
|
|
68
|
-
...getClient(),
|
|
69
|
-
findTableRows,
|
|
70
|
-
upsertTableRows
|
|
71
|
-
} as unknown as Client
|
|
72
|
-
|
|
73
|
-
const value = await zai
|
|
74
|
-
.with({ client })
|
|
75
|
-
.learn(taskId)
|
|
76
|
-
.check('This text is very clearly written in English.', 'is an english sentence')
|
|
77
|
-
|
|
78
|
-
const { rows } = await getClient().findTableRows({ table: tableName })
|
|
79
|
-
|
|
80
|
-
expect(value).toBe(true)
|
|
81
|
-
expect(rows.length).toBe(0)
|
|
82
|
-
expect(upsertTableRows).toHaveBeenCalledTimes(1)
|
|
83
|
-
expect(findTableRows).toHaveBeenCalledTimes(1)
|
|
84
|
-
})
|
|
85
|
-
})
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi } from 'vitest'
|
|
2
|
-
|
|
3
|
-
import { getClient, getZai } from './__tests'
|
|
4
|
-
|
|
5
|
-
import { Client } from '@botpress/client'
|
|
6
|
-
|
|
7
|
-
describe('zai retry', { timeout: 60_000 }, () => {
|
|
8
|
-
const client = getClient()
|
|
9
|
-
let zai = getZai().with({ retry: { maxRetries: 3 } })
|
|
10
|
-
|
|
11
|
-
it('retries 3 times and succeeds', async () => {
|
|
12
|
-
let retryCount = 0
|
|
13
|
-
const throwingClient = {
|
|
14
|
-
...client,
|
|
15
|
-
callAction: vi.fn((input) => {
|
|
16
|
-
if (++retryCount < 3) {
|
|
17
|
-
throw new Error('Failed to call model')
|
|
18
|
-
}
|
|
19
|
-
return client.callAction(input)
|
|
20
|
-
})
|
|
21
|
-
} as unknown as Client
|
|
22
|
-
|
|
23
|
-
const value = await zai
|
|
24
|
-
.with({ client: throwingClient })
|
|
25
|
-
.check('This text is very clearly written in English.', 'is an english sentence')
|
|
26
|
-
|
|
27
|
-
expect(value).toBe(true)
|
|
28
|
-
expect(retryCount).toBe(3)
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
it('retries 0 times when success', async () => {
|
|
32
|
-
const fn = vi.fn((input) => client.callAction(input))
|
|
33
|
-
|
|
34
|
-
const throwingClient = {
|
|
35
|
-
...client,
|
|
36
|
-
callAction: fn
|
|
37
|
-
} as unknown as Client
|
|
38
|
-
|
|
39
|
-
const value = await zai
|
|
40
|
-
.with({ client: throwingClient })
|
|
41
|
-
.check('This text is very clearly written in English.', 'is an english sentence')
|
|
42
|
-
|
|
43
|
-
expect(value).toBe(true)
|
|
44
|
-
expect(fn).toHaveBeenCalledOnce()
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
it('fails when exceeded max', async () => {
|
|
48
|
-
const fn = vi.fn(() => {
|
|
49
|
-
throw new Error('Failed to call model')
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
const throwingClient = {
|
|
53
|
-
...client,
|
|
54
|
-
callAction: fn
|
|
55
|
-
} as unknown as Client
|
|
56
|
-
|
|
57
|
-
const value = zai
|
|
58
|
-
.with({ client: throwingClient, retry: { maxRetries: 1 } })
|
|
59
|
-
.check('This text is very clearly written in English.', 'is an english sentence')
|
|
60
|
-
|
|
61
|
-
await expect(value).rejects.toThrowError(/retries/)
|
|
62
|
-
expect(fn).toHaveBeenCalledTimes(2)
|
|
63
|
-
})
|
|
64
|
-
})
|