ai-functions 0.2.19 → 0.4.0
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/.turbo/turbo-build.log +5 -0
- package/.turbo/turbo-test.log +105 -0
- package/README.md +232 -37
- package/TODO.md +138 -0
- package/dist/ai-promise.d.ts +219 -0
- package/dist/ai-promise.d.ts.map +1 -0
- package/dist/ai-promise.js +610 -0
- package/dist/ai-promise.js.map +1 -0
- package/dist/ai.d.ts +285 -0
- package/dist/ai.d.ts.map +1 -0
- package/dist/ai.js +842 -0
- package/dist/ai.js.map +1 -0
- package/dist/batch/anthropic.d.ts +23 -0
- package/dist/batch/anthropic.d.ts.map +1 -0
- package/dist/batch/anthropic.js +257 -0
- package/dist/batch/anthropic.js.map +1 -0
- package/dist/batch/bedrock.d.ts +64 -0
- package/dist/batch/bedrock.d.ts.map +1 -0
- package/dist/batch/bedrock.js +586 -0
- package/dist/batch/bedrock.js.map +1 -0
- package/dist/batch/cloudflare.d.ts +37 -0
- package/dist/batch/cloudflare.d.ts.map +1 -0
- package/dist/batch/cloudflare.js +289 -0
- package/dist/batch/cloudflare.js.map +1 -0
- package/dist/batch/google.d.ts +41 -0
- package/dist/batch/google.d.ts.map +1 -0
- package/dist/batch/google.js +360 -0
- package/dist/batch/google.js.map +1 -0
- package/dist/batch/index.d.ts +31 -0
- package/dist/batch/index.d.ts.map +1 -0
- package/dist/batch/index.js +31 -0
- package/dist/batch/index.js.map +1 -0
- package/dist/batch/memory.d.ts +44 -0
- package/dist/batch/memory.d.ts.map +1 -0
- package/dist/batch/memory.js +188 -0
- package/dist/batch/memory.js.map +1 -0
- package/dist/batch/openai.d.ts +37 -0
- package/dist/batch/openai.d.ts.map +1 -0
- package/dist/batch/openai.js +403 -0
- package/dist/batch/openai.js.map +1 -0
- package/dist/batch-map.d.ts +125 -0
- package/dist/batch-map.d.ts.map +1 -0
- package/dist/batch-map.js +406 -0
- package/dist/batch-map.js.map +1 -0
- package/dist/batch-queue.d.ts +273 -0
- package/dist/batch-queue.d.ts.map +1 -0
- package/dist/batch-queue.js +271 -0
- package/dist/batch-queue.js.map +1 -0
- package/dist/context.d.ts +133 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +267 -0
- package/dist/context.js.map +1 -0
- package/dist/embeddings.d.ts +123 -0
- package/dist/embeddings.d.ts.map +1 -0
- package/dist/embeddings.js +170 -0
- package/dist/embeddings.js.map +1 -0
- package/dist/eval/index.d.ts +8 -0
- package/dist/eval/index.d.ts.map +1 -0
- package/dist/eval/index.js +8 -0
- package/dist/eval/index.js.map +1 -0
- package/dist/eval/models.d.ts +66 -0
- package/dist/eval/models.d.ts.map +1 -0
- package/dist/eval/models.js +120 -0
- package/dist/eval/models.js.map +1 -0
- package/dist/eval/runner.d.ts +64 -0
- package/dist/eval/runner.d.ts.map +1 -0
- package/dist/eval/runner.js +148 -0
- package/dist/eval/runner.js.map +1 -0
- package/dist/generate.d.ts +168 -0
- package/dist/generate.d.ts.map +1 -0
- package/dist/generate.js +174 -0
- package/dist/generate.js.map +1 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +54 -0
- package/dist/index.js.map +1 -0
- package/dist/primitives.d.ts +292 -0
- package/dist/primitives.d.ts.map +1 -0
- package/dist/primitives.js +471 -0
- package/dist/primitives.js.map +1 -0
- package/dist/providers/cloudflare.d.ts +9 -0
- package/dist/providers/cloudflare.d.ts.map +1 -0
- package/dist/providers/cloudflare.js +9 -0
- package/dist/providers/cloudflare.js.map +1 -0
- package/dist/providers/index.d.ts +9 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +9 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/schema.d.ts +54 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +109 -0
- package/dist/schema.js.map +1 -0
- package/dist/template.d.ts +73 -0
- package/dist/template.d.ts.map +1 -0
- package/dist/template.js +129 -0
- package/dist/template.js.map +1 -0
- package/dist/types.d.ts +481 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/evalite.config.ts +19 -0
- package/evals/README.md +212 -0
- package/evals/classification.eval.ts +108 -0
- package/evals/marketing.eval.ts +370 -0
- package/evals/math.eval.ts +94 -0
- package/evals/run-evals.ts +166 -0
- package/evals/structured-output.eval.ts +143 -0
- package/evals/writing.eval.ts +117 -0
- package/examples/batch-blog-posts.ts +160 -0
- package/package.json +59 -43
- package/src/ai-promise.ts +784 -0
- package/src/ai.ts +1183 -0
- package/src/batch/anthropic.ts +375 -0
- package/src/batch/bedrock.ts +801 -0
- package/src/batch/cloudflare.ts +421 -0
- package/src/batch/google.ts +491 -0
- package/src/batch/index.ts +31 -0
- package/src/batch/memory.ts +253 -0
- package/src/batch/openai.ts +557 -0
- package/src/batch-map.ts +534 -0
- package/src/batch-queue.ts +493 -0
- package/src/context.ts +332 -0
- package/src/embeddings.ts +244 -0
- package/src/eval/index.ts +8 -0
- package/src/eval/models.ts +158 -0
- package/src/eval/runner.ts +217 -0
- package/src/generate.ts +245 -0
- package/src/index.ts +154 -0
- package/src/primitives.ts +612 -0
- package/src/providers/cloudflare.ts +15 -0
- package/src/providers/index.ts +14 -0
- package/src/schema.ts +147 -0
- package/src/template.ts +209 -0
- package/src/types.ts +540 -0
- package/test/README.md +105 -0
- package/test/ai-proxy.test.ts +192 -0
- package/test/async-iterators.test.ts +327 -0
- package/test/batch-background.test.ts +482 -0
- package/test/batch-blog-posts.test.ts +387 -0
- package/test/blog-generation.test.ts +510 -0
- package/test/browse-read.test.ts +611 -0
- package/test/core-functions.test.ts +694 -0
- package/test/decide.test.ts +393 -0
- package/test/define.test.ts +274 -0
- package/test/e2e-bedrock-manual.ts +163 -0
- package/test/e2e-bedrock.test.ts +191 -0
- package/test/e2e-flex-gateway.ts +157 -0
- package/test/e2e-flex-manual.ts +183 -0
- package/test/e2e-flex.test.ts +209 -0
- package/test/e2e-google-manual.ts +178 -0
- package/test/e2e-google.test.ts +216 -0
- package/test/embeddings.test.ts +284 -0
- package/test/evals/define-function.eval.test.ts +379 -0
- package/test/evals/primitives.eval.test.ts +384 -0
- package/test/function-types.test.ts +492 -0
- package/test/generate-core.test.ts +319 -0
- package/test/generate.test.ts +163 -0
- package/test/implicit-batch.test.ts +422 -0
- package/test/schema.test.ts +109 -0
- package/test/tagged-templates.test.ts +302 -0
- package/tsconfig.json +8 -6
- package/vitest.config.ts +42 -0
- package/LICENSE +0 -21
- package/db/cache.ts +0 -6
- package/db/mongo.ts +0 -75
- package/dist/mjs/db/cache.d.ts +0 -1
- package/dist/mjs/db/cache.js +0 -5
- package/dist/mjs/db/mongo.d.ts +0 -31
- package/dist/mjs/db/mongo.js +0 -48
- package/dist/mjs/examples/data.d.ts +0 -1105
- package/dist/mjs/examples/data.js +0 -1105
- package/dist/mjs/functions/ai.d.ts +0 -20
- package/dist/mjs/functions/ai.js +0 -83
- package/dist/mjs/functions/ai.test.d.ts +0 -1
- package/dist/mjs/functions/ai.test.js +0 -29
- package/dist/mjs/functions/gpt.d.ts +0 -4
- package/dist/mjs/functions/gpt.js +0 -10
- package/dist/mjs/functions/list.d.ts +0 -7
- package/dist/mjs/functions/list.js +0 -72
- package/dist/mjs/index.d.ts +0 -3
- package/dist/mjs/index.js +0 -3
- package/dist/mjs/queue/kafka.d.ts +0 -0
- package/dist/mjs/queue/kafka.js +0 -1
- package/dist/mjs/queue/memory.d.ts +0 -0
- package/dist/mjs/queue/memory.js +0 -1
- package/dist/mjs/queue/mongo.d.ts +0 -30
- package/dist/mjs/queue/mongo.js +0 -42
- package/dist/mjs/streams/kafka.d.ts +0 -0
- package/dist/mjs/streams/kafka.js +0 -1
- package/dist/mjs/streams/memory.d.ts +0 -0
- package/dist/mjs/streams/memory.js +0 -1
- package/dist/mjs/streams/mongo.d.ts +0 -0
- package/dist/mjs/streams/mongo.js +0 -1
- package/dist/mjs/streams/types.d.ts +0 -0
- package/dist/mjs/streams/types.js +0 -1
- package/dist/mjs/types.d.ts +0 -11
- package/dist/mjs/types.js +0 -1
- package/dist/mjs/utils/completion.d.ts +0 -9
- package/dist/mjs/utils/completion.js +0 -20
- package/dist/mjs/utils/schema.d.ts +0 -10
- package/dist/mjs/utils/schema.js +0 -72
- package/dist/mjs/utils/schema.test.d.ts +0 -1
- package/dist/mjs/utils/schema.test.js +0 -60
- package/dist/mjs/utils/state.d.ts +0 -1
- package/dist/mjs/utils/state.js +0 -19
- package/examples/data.ts +0 -1105
- package/fixup +0 -11
- package/functions/ai.test.ts +0 -41
- package/functions/ai.ts +0 -115
- package/functions/gpt.ts +0 -12
- package/functions/list.ts +0 -84
- package/index.ts +0 -3
- package/queue/kafka.ts +0 -0
- package/queue/memory.ts +0 -0
- package/queue/mongo.ts +0 -88
- package/streams/kafka.ts +0 -0
- package/streams/memory.ts +0 -0
- package/streams/mongo.ts +0 -0
- package/streams/types.ts +0 -0
- package/tsconfig-backup.json +0 -105
- package/tsconfig-base.json +0 -26
- package/tsconfig-cjs.json +0 -8
- package/types.ts +0 -12
- package/utils/completion.ts +0 -28
- package/utils/schema.test.ts +0 -69
- package/utils/schema.ts +0 -74
- package/utils/state.ts +0 -23
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-Memory Batch Adapter for Testing
|
|
3
|
+
*
|
|
4
|
+
* Simulates batch processing locally for testing purposes.
|
|
5
|
+
* Executes requests immediately (or with configurable delay).
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
registerBatchAdapter,
|
|
12
|
+
type BatchAdapter,
|
|
13
|
+
type BatchItem,
|
|
14
|
+
type BatchJob,
|
|
15
|
+
type BatchQueueOptions,
|
|
16
|
+
type BatchResult,
|
|
17
|
+
type BatchSubmitResult,
|
|
18
|
+
} from '../batch-queue.js'
|
|
19
|
+
import { generateObject, generateText } from '../generate.js'
|
|
20
|
+
|
|
21
|
+
// ============================================================================
|
|
22
|
+
// Types
|
|
23
|
+
// ============================================================================
|
|
24
|
+
|
|
25
|
+
interface MemoryBatch {
|
|
26
|
+
id: string
|
|
27
|
+
items: BatchItem[]
|
|
28
|
+
options: BatchQueueOptions
|
|
29
|
+
status: 'pending' | 'in_progress' | 'completed' | 'failed'
|
|
30
|
+
results: BatchResult[]
|
|
31
|
+
createdAt: Date
|
|
32
|
+
completedAt?: Date
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// ============================================================================
|
|
36
|
+
// Storage
|
|
37
|
+
// ============================================================================
|
|
38
|
+
|
|
39
|
+
const batches = new Map<string, MemoryBatch>()
|
|
40
|
+
let batchCounter = 0
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Options for the memory adapter
|
|
44
|
+
*/
|
|
45
|
+
export interface MemoryAdapterOptions {
|
|
46
|
+
/** Simulate processing delay in ms */
|
|
47
|
+
delay?: number
|
|
48
|
+
/** Mock handler for custom processing */
|
|
49
|
+
handler?: (item: BatchItem) => Promise<unknown>
|
|
50
|
+
/** Simulate failure rate (0-1) */
|
|
51
|
+
failureRate?: number
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
let memoryOptions: MemoryAdapterOptions = {}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Configure the memory adapter
|
|
58
|
+
*/
|
|
59
|
+
export function configureMemoryAdapter(options: MemoryAdapterOptions): void {
|
|
60
|
+
memoryOptions = options
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Clear all stored batches (for testing)
|
|
65
|
+
*/
|
|
66
|
+
export function clearBatches(): void {
|
|
67
|
+
batches.clear()
|
|
68
|
+
batchCounter = 0
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Get all stored batches (for testing)
|
|
73
|
+
*/
|
|
74
|
+
export function getBatches(): Map<string, MemoryBatch> {
|
|
75
|
+
return batches
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// ============================================================================
|
|
79
|
+
// Memory Batch Adapter
|
|
80
|
+
// ============================================================================
|
|
81
|
+
|
|
82
|
+
const memoryAdapter: BatchAdapter = {
|
|
83
|
+
async submit(items: BatchItem[], options: BatchQueueOptions): Promise<BatchSubmitResult> {
|
|
84
|
+
const batchId = `batch_memory_${++batchCounter}`
|
|
85
|
+
|
|
86
|
+
const batch: MemoryBatch = {
|
|
87
|
+
id: batchId,
|
|
88
|
+
items: [...items],
|
|
89
|
+
options,
|
|
90
|
+
status: 'pending',
|
|
91
|
+
results: [],
|
|
92
|
+
createdAt: new Date(),
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
batches.set(batchId, batch)
|
|
96
|
+
|
|
97
|
+
// Start processing asynchronously
|
|
98
|
+
const completion = processMemoryBatch(batch)
|
|
99
|
+
|
|
100
|
+
const job: BatchJob = {
|
|
101
|
+
id: batchId,
|
|
102
|
+
provider: 'openai', // Simulating OpenAI
|
|
103
|
+
status: 'pending',
|
|
104
|
+
totalItems: items.length,
|
|
105
|
+
completedItems: 0,
|
|
106
|
+
failedItems: 0,
|
|
107
|
+
createdAt: batch.createdAt,
|
|
108
|
+
webhookUrl: options.webhookUrl,
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return { job, completion }
|
|
112
|
+
},
|
|
113
|
+
|
|
114
|
+
async getStatus(batchId: string): Promise<BatchJob> {
|
|
115
|
+
const batch = batches.get(batchId)
|
|
116
|
+
if (!batch) {
|
|
117
|
+
throw new Error(`Batch not found: ${batchId}`)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const completedItems = batch.results.filter((r) => r.status === 'completed').length
|
|
121
|
+
const failedItems = batch.results.filter((r) => r.status === 'failed').length
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
id: batch.id,
|
|
125
|
+
provider: 'openai',
|
|
126
|
+
status: batch.status === 'completed' ? 'completed' : batch.status === 'failed' ? 'failed' : 'in_progress',
|
|
127
|
+
totalItems: batch.items.length,
|
|
128
|
+
completedItems,
|
|
129
|
+
failedItems,
|
|
130
|
+
createdAt: batch.createdAt,
|
|
131
|
+
completedAt: batch.completedAt,
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
|
|
135
|
+
async cancel(batchId: string): Promise<void> {
|
|
136
|
+
const batch = batches.get(batchId)
|
|
137
|
+
if (!batch) {
|
|
138
|
+
throw new Error(`Batch not found: ${batchId}`)
|
|
139
|
+
}
|
|
140
|
+
batch.status = 'failed'
|
|
141
|
+
},
|
|
142
|
+
|
|
143
|
+
async getResults(batchId: string): Promise<BatchResult[]> {
|
|
144
|
+
const batch = batches.get(batchId)
|
|
145
|
+
if (!batch) {
|
|
146
|
+
throw new Error(`Batch not found: ${batchId}`)
|
|
147
|
+
}
|
|
148
|
+
return batch.results
|
|
149
|
+
},
|
|
150
|
+
|
|
151
|
+
async waitForCompletion(batchId: string): Promise<BatchResult[]> {
|
|
152
|
+
const batch = batches.get(batchId)
|
|
153
|
+
if (!batch) {
|
|
154
|
+
throw new Error(`Batch not found: ${batchId}`)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Wait for completion
|
|
158
|
+
while (batch.status !== 'completed' && batch.status !== 'failed') {
|
|
159
|
+
await new Promise((resolve) => setTimeout(resolve, 100))
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return batch.results
|
|
163
|
+
},
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// ============================================================================
|
|
167
|
+
// Processing
|
|
168
|
+
// ============================================================================
|
|
169
|
+
|
|
170
|
+
async function processMemoryBatch(batch: MemoryBatch): Promise<BatchResult[]> {
|
|
171
|
+
batch.status = 'in_progress'
|
|
172
|
+
|
|
173
|
+
// Optional delay
|
|
174
|
+
if (memoryOptions.delay) {
|
|
175
|
+
await new Promise((resolve) => setTimeout(resolve, memoryOptions.delay))
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const results: BatchResult[] = []
|
|
179
|
+
|
|
180
|
+
for (const item of batch.items) {
|
|
181
|
+
try {
|
|
182
|
+
// Simulate failure
|
|
183
|
+
if (memoryOptions.failureRate && Math.random() < memoryOptions.failureRate) {
|
|
184
|
+
throw new Error('Simulated failure')
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
let result: unknown
|
|
188
|
+
|
|
189
|
+
if (memoryOptions.handler) {
|
|
190
|
+
// Use custom handler
|
|
191
|
+
result = await memoryOptions.handler(item)
|
|
192
|
+
} else if (item.schema) {
|
|
193
|
+
// Generate structured output
|
|
194
|
+
const response = await generateObject({
|
|
195
|
+
model: batch.options.model || 'sonnet',
|
|
196
|
+
schema: item.schema,
|
|
197
|
+
prompt: item.prompt,
|
|
198
|
+
system: item.options?.system,
|
|
199
|
+
temperature: item.options?.temperature,
|
|
200
|
+
maxTokens: item.options?.maxTokens,
|
|
201
|
+
})
|
|
202
|
+
result = response.object
|
|
203
|
+
} else {
|
|
204
|
+
// Generate text
|
|
205
|
+
const response = await generateText({
|
|
206
|
+
model: batch.options.model || 'sonnet',
|
|
207
|
+
prompt: item.prompt,
|
|
208
|
+
system: item.options?.system,
|
|
209
|
+
temperature: item.options?.temperature,
|
|
210
|
+
maxTokens: item.options?.maxTokens,
|
|
211
|
+
})
|
|
212
|
+
result = response.text
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
results.push({
|
|
216
|
+
id: item.id,
|
|
217
|
+
customId: item.id,
|
|
218
|
+
status: 'completed',
|
|
219
|
+
result,
|
|
220
|
+
usage: {
|
|
221
|
+
promptTokens: 100,
|
|
222
|
+
completionTokens: 200,
|
|
223
|
+
totalTokens: 300,
|
|
224
|
+
},
|
|
225
|
+
})
|
|
226
|
+
} catch (error) {
|
|
227
|
+
results.push({
|
|
228
|
+
id: item.id,
|
|
229
|
+
customId: item.id,
|
|
230
|
+
status: 'failed',
|
|
231
|
+
error: error instanceof Error ? error.message : 'Unknown error',
|
|
232
|
+
})
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
batch.results = results
|
|
237
|
+
batch.status = results.every((r) => r.status === 'completed') ? 'completed' : 'failed'
|
|
238
|
+
batch.completedAt = new Date()
|
|
239
|
+
|
|
240
|
+
return results
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// ============================================================================
|
|
244
|
+
// Register Adapter
|
|
245
|
+
// ============================================================================
|
|
246
|
+
|
|
247
|
+
registerBatchAdapter('openai', memoryAdapter)
|
|
248
|
+
registerBatchAdapter('anthropic', memoryAdapter)
|
|
249
|
+
registerBatchAdapter('google', memoryAdapter)
|
|
250
|
+
registerBatchAdapter('bedrock', memoryAdapter)
|
|
251
|
+
registerBatchAdapter('cloudflare', memoryAdapter)
|
|
252
|
+
|
|
253
|
+
export { memoryAdapter }
|