@jaex/dstsx 0.1.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/README.md +2089 -0
- package/dist/index.cjs +2883 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1597 -0
- package/dist/index.d.ts +1597 -0
- package/dist/index.js +2781 -0
- package/dist/index.js.map +1 -0
- package/package.json +113 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,1597 @@
|
|
|
1
|
+
/** Metadata describing a single field in a Signature. */
|
|
2
|
+
interface FieldMeta {
|
|
3
|
+
/** Human-readable description used in the prompt. */
|
|
4
|
+
description?: string;
|
|
5
|
+
/** Optional prefix that precedes the field value in the prompt. */
|
|
6
|
+
prefix?: string;
|
|
7
|
+
/** Optional format hint (e.g. "json", "markdown", "list"). */
|
|
8
|
+
format?: string;
|
|
9
|
+
/** When true the field may be absent from a completion. */
|
|
10
|
+
optional?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* The JSON-schema-compatible type annotation for this field.
|
|
13
|
+
* Defaults to "string".
|
|
14
|
+
*/
|
|
15
|
+
type?: "string" | "number" | "boolean" | "string[]" | "object";
|
|
16
|
+
}
|
|
17
|
+
/** Internal representation of a parsed Signature. */
|
|
18
|
+
interface SignatureMeta {
|
|
19
|
+
/** Ordered list of input field names with their metadata. */
|
|
20
|
+
inputs: Map<string, FieldMeta>;
|
|
21
|
+
/** Ordered list of output field names with their metadata. */
|
|
22
|
+
outputs: Map<string, FieldMeta>;
|
|
23
|
+
/** Optional system-level instruction string. */
|
|
24
|
+
instructions?: string | undefined;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A Signature defines the typed interface (inputs and outputs) for a single
|
|
29
|
+
* language-model call. It mirrors `dspy.Signature` in the Python library.
|
|
30
|
+
*
|
|
31
|
+
* Signatures can be created from a shorthand string:
|
|
32
|
+
* ```ts
|
|
33
|
+
* const sig = Signature.from("question -> answer");
|
|
34
|
+
* const sig2 = Signature.from("context, question -> answer, confidence");
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* Or constructed explicitly:
|
|
38
|
+
* ```ts
|
|
39
|
+
* const sig = new Signature({
|
|
40
|
+
* inputs: new Map([["question", InputField({ description: "The question" })]]),
|
|
41
|
+
* outputs: new Map([["answer", OutputField({ description: "The answer" })]]),
|
|
42
|
+
* instructions: "Answer concisely.",
|
|
43
|
+
* });
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
declare class Signature {
|
|
47
|
+
readonly inputs: ReadonlyMap<string, FieldMeta>;
|
|
48
|
+
readonly outputs: ReadonlyMap<string, FieldMeta>;
|
|
49
|
+
readonly instructions: string | undefined;
|
|
50
|
+
constructor(meta: SignatureMeta);
|
|
51
|
+
/**
|
|
52
|
+
* Parse a shorthand signature string of the form:
|
|
53
|
+
* `"field1, field2? -> out1, out2"`
|
|
54
|
+
*
|
|
55
|
+
* A trailing `?` marks the field as optional.
|
|
56
|
+
*/
|
|
57
|
+
static from(shorthand: string, instructions?: string): Signature;
|
|
58
|
+
/**
|
|
59
|
+
* Return a new Signature with additional or overridden fields / instructions.
|
|
60
|
+
*/
|
|
61
|
+
with(overrides: Partial<SignatureMeta>): Signature;
|
|
62
|
+
/**
|
|
63
|
+
* Append an extra input field and return a new Signature.
|
|
64
|
+
*/
|
|
65
|
+
withInput(name: string, meta?: FieldMeta): Signature;
|
|
66
|
+
/**
|
|
67
|
+
* Append an extra output field and return a new Signature.
|
|
68
|
+
*/
|
|
69
|
+
withOutput(name: string, meta?: FieldMeta): Signature;
|
|
70
|
+
toJSON(): object;
|
|
71
|
+
static fromJSON(json: Record<string, unknown>): Signature;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Creates an input field descriptor for a {@link Signature}.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* const sig = new Signature({
|
|
80
|
+
* inputs: new Map([["question", InputField({ description: "The question to answer" })]]),
|
|
81
|
+
* outputs: new Map([["answer", OutputField({ description: "A concise answer" })]]),
|
|
82
|
+
* });
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
declare function InputField(meta?: FieldMeta): FieldMeta;
|
|
86
|
+
/**
|
|
87
|
+
* Creates an output field descriptor for a {@link Signature}.
|
|
88
|
+
*/
|
|
89
|
+
declare function OutputField(meta?: FieldMeta): FieldMeta;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* An immutable record of named values used as a training example or a module
|
|
93
|
+
* input. Mirrors `dspy.Example` in Python.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```ts
|
|
97
|
+
* const ex = new Example({ question: "What is 2+2?", answer: "4" });
|
|
98
|
+
* const withLabel = ex.with({ answer: "four" });
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
declare class Example {
|
|
102
|
+
#private;
|
|
103
|
+
constructor(data: Record<string, unknown>);
|
|
104
|
+
/** Return the value for `key`, or `undefined` if absent. */
|
|
105
|
+
get(key: string): unknown;
|
|
106
|
+
/** Return a shallow-frozen copy of the underlying data record. */
|
|
107
|
+
toDict(): Readonly<Record<string, unknown>>;
|
|
108
|
+
/**
|
|
109
|
+
* Return a new Example with the provided key-value pairs merged in.
|
|
110
|
+
*/
|
|
111
|
+
with(overrides: Record<string, unknown>): Example;
|
|
112
|
+
/**
|
|
113
|
+
* Return a new Example containing only the keys listed in `keys`.
|
|
114
|
+
*/
|
|
115
|
+
inputs(keys: string[]): Example;
|
|
116
|
+
/**
|
|
117
|
+
* Return a new Example containing only the keys NOT listed in `inputKeys`
|
|
118
|
+
* (i.e. the label / output keys).
|
|
119
|
+
*/
|
|
120
|
+
labels(inputKeys: string[]): Example;
|
|
121
|
+
toJSON(): Record<string, unknown>;
|
|
122
|
+
static fromDict(data: Record<string, unknown>): Example;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The output of a {@link Predict} (or any module) call.
|
|
127
|
+
* Extends {@link Example} by adding `completions` for multi-output calls.
|
|
128
|
+
*
|
|
129
|
+
* Mirrors `dspy.Prediction` in Python.
|
|
130
|
+
*/
|
|
131
|
+
declare class Prediction extends Example {
|
|
132
|
+
/** All candidate completions when `n > 1` was requested. */
|
|
133
|
+
readonly completions: ReadonlyArray<Record<string, unknown>>;
|
|
134
|
+
constructor(data: Record<string, unknown>, completions?: Record<string, unknown>[]);
|
|
135
|
+
/** Typed accessor — casts the value to `T` (caller is responsible for type safety). */
|
|
136
|
+
getTyped<T>(key: string): T;
|
|
137
|
+
toJSON(): Record<string, unknown>;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Token-usage statistics returned by an LM call. */
|
|
141
|
+
interface TokenUsage {
|
|
142
|
+
promptTokens: number;
|
|
143
|
+
completionTokens: number;
|
|
144
|
+
totalTokens: number;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* A single LM call record captured during program execution.
|
|
148
|
+
* Mirrors the trace entries in `dspy.settings.trace`.
|
|
149
|
+
*/
|
|
150
|
+
interface Trace {
|
|
151
|
+
/** The signature used for this call. */
|
|
152
|
+
signature: Signature;
|
|
153
|
+
/** The resolved input values passed to the LM. */
|
|
154
|
+
inputs: Record<string, unknown>;
|
|
155
|
+
/** The parsed output values returned by the LM. */
|
|
156
|
+
outputs: Record<string, unknown>;
|
|
157
|
+
/** Token usage for this call (if reported by the provider). */
|
|
158
|
+
usage: TokenUsage | null;
|
|
159
|
+
/** Wall-clock latency in milliseconds. */
|
|
160
|
+
latencyMs: number;
|
|
161
|
+
/** ISO-8601 timestamp of the call. */
|
|
162
|
+
timestamp: string;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Returns a reducer function that picks the Prediction whose `field` value
|
|
167
|
+
* appears most frequently. Ties go to the first occurrence.
|
|
168
|
+
*/
|
|
169
|
+
declare function majority(field?: string): (predictions: Prediction[]) => Prediction;
|
|
170
|
+
|
|
171
|
+
/** Supported image MIME types. */
|
|
172
|
+
type ImageMimeType = "image/jpeg" | "image/png" | "image/gif" | "image/webp";
|
|
173
|
+
/**
|
|
174
|
+
* A multi-modal image value that can be passed as a field in Predict/TypedPredictor.
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```ts
|
|
178
|
+
* const captioner = new Predict("image, question -> caption");
|
|
179
|
+
* const result = await captioner.forward({
|
|
180
|
+
* image: Image.fromURL("https://example.com/photo.jpg"),
|
|
181
|
+
* question: "What is in this image?",
|
|
182
|
+
* });
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
185
|
+
declare class Image {
|
|
186
|
+
readonly url: string | undefined;
|
|
187
|
+
readonly base64: string | undefined;
|
|
188
|
+
readonly mimeType: ImageMimeType | undefined;
|
|
189
|
+
private constructor();
|
|
190
|
+
/** Create an Image from a URL. */
|
|
191
|
+
static fromURL(url: string): Image;
|
|
192
|
+
/** Create an Image from base64-encoded data. */
|
|
193
|
+
static fromBase64(data: string, mimeType?: ImageMimeType): Image;
|
|
194
|
+
/** Create an Image by reading a local file synchronously. */
|
|
195
|
+
static fromFile(path: string, mimeType?: ImageMimeType): Image;
|
|
196
|
+
/** Serialize to an OpenAI-compatible image_url content part. */
|
|
197
|
+
toOpenAIContentPart(): {
|
|
198
|
+
type: "image_url";
|
|
199
|
+
image_url: {
|
|
200
|
+
url: string;
|
|
201
|
+
};
|
|
202
|
+
};
|
|
203
|
+
/** Serialize to an Anthropic-compatible image content block. */
|
|
204
|
+
toAnthropicContentBlock(): {
|
|
205
|
+
type: "image";
|
|
206
|
+
source: {
|
|
207
|
+
type: "base64" | "url";
|
|
208
|
+
media_type?: string;
|
|
209
|
+
data?: string;
|
|
210
|
+
url?: string;
|
|
211
|
+
};
|
|
212
|
+
};
|
|
213
|
+
/** Returns a string representation (used when Image is serialized in prompts). */
|
|
214
|
+
toString(): string;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** A single chat message (role + content). */
|
|
218
|
+
interface Message {
|
|
219
|
+
role: "system" | "user" | "assistant";
|
|
220
|
+
content: string;
|
|
221
|
+
}
|
|
222
|
+
/** Configuration passed to every LM call. */
|
|
223
|
+
interface LMCallConfig {
|
|
224
|
+
/** Model identifier (e.g. "gpt-4o", "claude-3-opus-20240229"). */
|
|
225
|
+
model?: string;
|
|
226
|
+
/** Sampling temperature (0–2). */
|
|
227
|
+
temperature?: number;
|
|
228
|
+
/** Maximum tokens to generate. */
|
|
229
|
+
maxTokens?: number;
|
|
230
|
+
/** Stop sequences. */
|
|
231
|
+
stop?: string[];
|
|
232
|
+
/** Number of completions to generate (default 1). */
|
|
233
|
+
n?: number;
|
|
234
|
+
/**
|
|
235
|
+
* Optional cache key override. When provided the cache uses this key
|
|
236
|
+
* instead of hashing the prompt.
|
|
237
|
+
*/
|
|
238
|
+
cacheKey?: string;
|
|
239
|
+
/** Pass-through extra options to the underlying provider SDK. */
|
|
240
|
+
extra?: Record<string, unknown>;
|
|
241
|
+
}
|
|
242
|
+
/** The response returned by an LM adapter. */
|
|
243
|
+
interface LMResponse {
|
|
244
|
+
/** The primary (first) completion text. */
|
|
245
|
+
text: string;
|
|
246
|
+
/** All completion texts when `n > 1`. */
|
|
247
|
+
texts: string[];
|
|
248
|
+
/** Token usage (null when the provider does not report it). */
|
|
249
|
+
usage: {
|
|
250
|
+
promptTokens: number;
|
|
251
|
+
completionTokens: number;
|
|
252
|
+
totalTokens: number;
|
|
253
|
+
} | null;
|
|
254
|
+
/** Raw provider response (opaque). */
|
|
255
|
+
raw: unknown;
|
|
256
|
+
}
|
|
257
|
+
/** A single chunk emitted during token streaming. */
|
|
258
|
+
interface StreamChunk {
|
|
259
|
+
/** The incremental text delta for this chunk. */
|
|
260
|
+
delta: string;
|
|
261
|
+
/** True on the final chunk. */
|
|
262
|
+
done: boolean;
|
|
263
|
+
/** Raw provider chunk (opaque). */
|
|
264
|
+
raw: unknown;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Abstract base class for all language model adapters.
|
|
269
|
+
*
|
|
270
|
+
* Subclasses must implement {@link LM._call} which performs the actual
|
|
271
|
+
* provider-specific API call.
|
|
272
|
+
*
|
|
273
|
+
* The base class handles:
|
|
274
|
+
* - LRU response caching
|
|
275
|
+
* - Request counting
|
|
276
|
+
* - Token usage aggregation
|
|
277
|
+
*/
|
|
278
|
+
declare abstract class LM {
|
|
279
|
+
#private;
|
|
280
|
+
/** Human-readable name / identifier for this model instance. */
|
|
281
|
+
readonly model: string;
|
|
282
|
+
constructor(model: string, cacheOptions?: {
|
|
283
|
+
maxSize?: number;
|
|
284
|
+
ttlMs?: number;
|
|
285
|
+
cacheDir?: string;
|
|
286
|
+
});
|
|
287
|
+
/**
|
|
288
|
+
* Call the language model with either a plain string prompt or a list of
|
|
289
|
+
* chat messages.
|
|
290
|
+
*/
|
|
291
|
+
call(prompt: string | Message[], config?: LMCallConfig): Promise<LMResponse>;
|
|
292
|
+
/** Total number of (non-cached) API calls made. */
|
|
293
|
+
get requestCount(): number;
|
|
294
|
+
/** Accumulated token usage across all (non-cached) calls. */
|
|
295
|
+
get tokenUsage(): Readonly<{
|
|
296
|
+
promptTokens: number;
|
|
297
|
+
completionTokens: number;
|
|
298
|
+
totalTokens: number;
|
|
299
|
+
}>;
|
|
300
|
+
/** Clear the in-memory response cache. */
|
|
301
|
+
clearCache(): void;
|
|
302
|
+
/**
|
|
303
|
+
* Stream the language model response token by token.
|
|
304
|
+
*
|
|
305
|
+
* Returns an `AsyncIterable<StreamChunk>`. The last chunk has `done: true`.
|
|
306
|
+
* Subclasses override this to provide real streaming; the base implementation
|
|
307
|
+
* falls back to calling {@link LM.call} and yielding the full response as a
|
|
308
|
+
* single chunk.
|
|
309
|
+
*/
|
|
310
|
+
stream(prompt: string | Message[], config?: LMCallConfig): AsyncGenerator<StreamChunk>;
|
|
311
|
+
/**
|
|
312
|
+
* Perform the actual provider API call. Subclasses must implement this.
|
|
313
|
+
*/
|
|
314
|
+
protected abstract _call(prompt: string | Message[], config: LMCallConfig): Promise<LMResponse>;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* A minimal LRU (Least Recently Used) cache used to memoize LM responses.
|
|
319
|
+
*
|
|
320
|
+
* Entries are evicted when `maxSize` is reached (oldest-first) or when their
|
|
321
|
+
* TTL has expired.
|
|
322
|
+
*/
|
|
323
|
+
declare class LRUCache<K, V> {
|
|
324
|
+
#private;
|
|
325
|
+
constructor(maxSize?: number, ttlMs?: number);
|
|
326
|
+
get(key: K): V | undefined;
|
|
327
|
+
set(key: K, value: V): void;
|
|
328
|
+
has(key: K): boolean;
|
|
329
|
+
delete(key: K): void;
|
|
330
|
+
clear(): void;
|
|
331
|
+
get size(): number;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Disk-persistent JSON cache for LM responses.
|
|
336
|
+
*
|
|
337
|
+
* Cache entries are stored as individual JSON files named by a truncated
|
|
338
|
+
* SHA-256 hash of the key. Supports optional TTL and LRU eviction.
|
|
339
|
+
*/
|
|
340
|
+
declare class DiskCache {
|
|
341
|
+
#private;
|
|
342
|
+
constructor(cacheDir: string, maxSize?: number, ttlMs?: number);
|
|
343
|
+
get(key: string): LMResponse | undefined;
|
|
344
|
+
set(key: string, value: LMResponse): void;
|
|
345
|
+
clear(): void;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** Options for the OpenAI adapter. */
|
|
349
|
+
interface OpenAIOptions {
|
|
350
|
+
apiKey?: string;
|
|
351
|
+
baseURL?: string;
|
|
352
|
+
/** Default model, can be overridden per-call. */
|
|
353
|
+
model?: string;
|
|
354
|
+
maxRetries?: number;
|
|
355
|
+
stream?: boolean;
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* LM adapter for OpenAI chat-completion and text-completion models.
|
|
359
|
+
*
|
|
360
|
+
* Requires the `openai` package as a peer dependency:
|
|
361
|
+
* ```
|
|
362
|
+
* npm install openai
|
|
363
|
+
* ```
|
|
364
|
+
*/
|
|
365
|
+
declare class OpenAI extends LM {
|
|
366
|
+
#private;
|
|
367
|
+
constructor(options?: OpenAIOptions);
|
|
368
|
+
protected _call(prompt: string | Message[], config: LMCallConfig): Promise<LMResponse>;
|
|
369
|
+
stream(prompt: string | Message[], config?: LMCallConfig): AsyncGenerator<StreamChunk>;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/** Options for the Anthropic adapter. */
|
|
373
|
+
interface AnthropicOptions {
|
|
374
|
+
apiKey?: string;
|
|
375
|
+
model?: string;
|
|
376
|
+
maxRetries?: number;
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* LM adapter for Anthropic Claude models.
|
|
380
|
+
*
|
|
381
|
+
* Requires the `@anthropic-ai/sdk` package as a peer dependency:
|
|
382
|
+
* ```
|
|
383
|
+
* npm install @anthropic-ai/sdk
|
|
384
|
+
* ```
|
|
385
|
+
*/
|
|
386
|
+
declare class Anthropic extends LM {
|
|
387
|
+
#private;
|
|
388
|
+
constructor(options?: AnthropicOptions);
|
|
389
|
+
protected _call(prompt: string | Message[], config: LMCallConfig): Promise<LMResponse>;
|
|
390
|
+
stream(prompt: string | Message[], config?: LMCallConfig): AsyncGenerator<StreamChunk>;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/** Options for the Cohere adapter. */
|
|
394
|
+
interface CohereOptions {
|
|
395
|
+
apiKey?: string;
|
|
396
|
+
model?: string;
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* LM adapter for Cohere Command models.
|
|
400
|
+
*
|
|
401
|
+
* Requires the `cohere-ai` package as a peer dependency:
|
|
402
|
+
* ```
|
|
403
|
+
* npm install cohere-ai
|
|
404
|
+
* ```
|
|
405
|
+
*/
|
|
406
|
+
declare class Cohere extends LM {
|
|
407
|
+
#private;
|
|
408
|
+
constructor(options?: CohereOptions);
|
|
409
|
+
protected _call(prompt: string | Message[], config: LMCallConfig): Promise<LMResponse>;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/** Options for the Google Generative AI adapter. */
|
|
413
|
+
interface GoogleAIOptions {
|
|
414
|
+
apiKey?: string;
|
|
415
|
+
model?: string;
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* LM adapter for Google Gemini models via `@google/generative-ai`.
|
|
419
|
+
*
|
|
420
|
+
* Requires the `@google/generative-ai` package as a peer dependency:
|
|
421
|
+
* ```
|
|
422
|
+
* npm install @google/generative-ai
|
|
423
|
+
* ```
|
|
424
|
+
*/
|
|
425
|
+
declare class GoogleAI extends LM {
|
|
426
|
+
#private;
|
|
427
|
+
constructor(options?: GoogleAIOptions);
|
|
428
|
+
protected _call(prompt: string | Message[], config: LMCallConfig): Promise<LMResponse>;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/** Options for the Ollama adapter. */
|
|
432
|
+
interface OllamaOptions {
|
|
433
|
+
/** Base URL of the Ollama server (default: http://localhost:11434). */
|
|
434
|
+
baseURL?: string;
|
|
435
|
+
model?: string;
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* LM adapter for locally running Ollama models.
|
|
439
|
+
* Communicates via the Ollama REST API (OpenAI-compatible `/v1` endpoint).
|
|
440
|
+
*/
|
|
441
|
+
declare class Ollama extends LM {
|
|
442
|
+
#private;
|
|
443
|
+
constructor(options?: OllamaOptions);
|
|
444
|
+
protected _call(prompt: string | Message[], config: LMCallConfig): Promise<LMResponse>;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/** Options for the LM Studio adapter. */
|
|
448
|
+
interface LMStudioOptions {
|
|
449
|
+
/** Base URL of the LM Studio server (default: http://localhost:1234/v1). */
|
|
450
|
+
baseURL?: string;
|
|
451
|
+
model?: string;
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* LM adapter for LM Studio's OpenAI-compatible REST endpoint.
|
|
455
|
+
*/
|
|
456
|
+
declare class LMStudio extends LM {
|
|
457
|
+
#private;
|
|
458
|
+
constructor(options?: LMStudioOptions);
|
|
459
|
+
protected _call(prompt: string | Message[], config: LMCallConfig): Promise<LMResponse>;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/** Options for the HuggingFace Inference API adapter. */
|
|
463
|
+
interface HuggingFaceOptions {
|
|
464
|
+
apiKey?: string;
|
|
465
|
+
model?: string;
|
|
466
|
+
/** Override the inference endpoint URL (for dedicated endpoints). */
|
|
467
|
+
endpointURL?: string;
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* LM adapter for HuggingFace Inference API text-generation models.
|
|
471
|
+
*
|
|
472
|
+
* Uses the HuggingFace HTTP inference API directly (no SDK required).
|
|
473
|
+
*/
|
|
474
|
+
declare class HuggingFace extends LM {
|
|
475
|
+
#private;
|
|
476
|
+
constructor(options?: HuggingFaceOptions);
|
|
477
|
+
protected _call(prompt: string | Message[], config: LMCallConfig): Promise<LMResponse>;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* A deterministic mock LM adapter for use in unit tests.
|
|
482
|
+
*
|
|
483
|
+
* Responses are resolved from a lookup map keyed on the exact prompt string
|
|
484
|
+
* (or the serialized messages array). If no match is found the adapter falls
|
|
485
|
+
* back to `defaultResponse` (or throws if none is configured).
|
|
486
|
+
*
|
|
487
|
+
* @example
|
|
488
|
+
* ```ts
|
|
489
|
+
* const lm = new MockLM({
|
|
490
|
+
* "What is 2+2?": "4",
|
|
491
|
+
* });
|
|
492
|
+
* settings.configure({ lm });
|
|
493
|
+
* ```
|
|
494
|
+
*/
|
|
495
|
+
declare class MockLM extends LM {
|
|
496
|
+
#private;
|
|
497
|
+
constructor(responses?: Record<string, string>, defaultResponse?: string);
|
|
498
|
+
protected _call(prompt: string | Message[], config: LMCallConfig): Promise<LMResponse>;
|
|
499
|
+
/** Register (or overwrite) a prompt → response mapping at runtime. */
|
|
500
|
+
addResponse(prompt: string, response: string): void;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
/**
|
|
504
|
+
* Abstract base class for all DSTsx modules.
|
|
505
|
+
*
|
|
506
|
+
* A Module is a composable, serializable unit that encapsulates one or more
|
|
507
|
+
* language model calls. Subclasses implement {@link Module.forward} to define
|
|
508
|
+
* their behaviour.
|
|
509
|
+
*
|
|
510
|
+
* Mirrors `dspy.Module` in Python.
|
|
511
|
+
*
|
|
512
|
+
* @example
|
|
513
|
+
* ```ts
|
|
514
|
+
* class MyRAG extends Module {
|
|
515
|
+
* retrieve = new Retrieve(3);
|
|
516
|
+
* generate = new ChainOfThought("context, question -> answer");
|
|
517
|
+
*
|
|
518
|
+
* async forward(question: string): Promise<Prediction> {
|
|
519
|
+
* const { passages } = await this.retrieve.forward(question);
|
|
520
|
+
* return this.generate.forward({ context: passages.join("\n"), question });
|
|
521
|
+
* }
|
|
522
|
+
* }
|
|
523
|
+
* ```
|
|
524
|
+
*/
|
|
525
|
+
declare abstract class Module {
|
|
526
|
+
/**
|
|
527
|
+
* Execute the module.
|
|
528
|
+
*
|
|
529
|
+
* Subclasses define their own parameter signatures; the base type uses
|
|
530
|
+
* `unknown` so that TypeScript accepts any subclass override.
|
|
531
|
+
*/
|
|
532
|
+
abstract forward(...args: unknown[]): Promise<Prediction>;
|
|
533
|
+
/**
|
|
534
|
+
* Recursively discover all {@link Predict} sub-modules by walking the own
|
|
535
|
+
* enumerable properties of this instance.
|
|
536
|
+
*/
|
|
537
|
+
namedPredictors(): Array<[string, Module]>;
|
|
538
|
+
/**
|
|
539
|
+
* Serialize the module's learnable parameters (e.g. `Predict.demos`) to a
|
|
540
|
+
* plain JSON-compatible object.
|
|
541
|
+
*/
|
|
542
|
+
dump(): Record<string, unknown>;
|
|
543
|
+
/**
|
|
544
|
+
* Restore learnable parameters from a plain object previously produced by
|
|
545
|
+
* {@link Module.dump}.
|
|
546
|
+
*/
|
|
547
|
+
load(state: Record<string, unknown>): void;
|
|
548
|
+
/**
|
|
549
|
+
* Create a deep clone of this module.
|
|
550
|
+
*
|
|
551
|
+
* Returns a new module with the same prototype. All sub-{@link Module}
|
|
552
|
+
* properties are recursively cloned so that mutating the clone's learnable
|
|
553
|
+
* parameters (e.g. `Predict.demos`) does **not** affect the original.
|
|
554
|
+
* Array properties are shallow-copied (their elements are not cloned).
|
|
555
|
+
* All other properties are copied by reference.
|
|
556
|
+
*/
|
|
557
|
+
clone(): this;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
/**
|
|
561
|
+
* The fundamental DSTsx module — formats a prompt for the configured LM and
|
|
562
|
+
* parses the completion back into a typed {@link Prediction}.
|
|
563
|
+
*
|
|
564
|
+
* Mirrors `dspy.Predict` in Python.
|
|
565
|
+
*
|
|
566
|
+
* @example
|
|
567
|
+
* ```ts
|
|
568
|
+
* const qa = new Predict("question -> answer");
|
|
569
|
+
* const result = await qa.forward({ question: "What is 2 + 2?" });
|
|
570
|
+
* console.log(result.get("answer")); // "4"
|
|
571
|
+
* ```
|
|
572
|
+
*/
|
|
573
|
+
declare class Predict extends Module {
|
|
574
|
+
#private;
|
|
575
|
+
readonly signature: Signature;
|
|
576
|
+
/** Few-shot demonstration examples (learnable parameter). */
|
|
577
|
+
demos: Example[];
|
|
578
|
+
/** System instruction override (learnable parameter). */
|
|
579
|
+
instructions: string | undefined;
|
|
580
|
+
constructor(signature: string | Signature);
|
|
581
|
+
forward(inputs: Record<string, unknown>): Promise<Prediction>;
|
|
582
|
+
/**
|
|
583
|
+
* Stream the LM response token by token.
|
|
584
|
+
* Returns an `AsyncGenerator<StreamChunk>`.
|
|
585
|
+
*/
|
|
586
|
+
stream(inputs: Record<string, unknown>): AsyncGenerator<StreamChunk>;
|
|
587
|
+
dump(): Record<string, unknown>;
|
|
588
|
+
load(state: Record<string, unknown>): void;
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
/**
|
|
592
|
+
* Chain-of-Thought module — extends {@link Predict} by prepending a hidden
|
|
593
|
+
* `rationale` output field so the LM reasons before producing the answer.
|
|
594
|
+
*
|
|
595
|
+
* Mirrors `dspy.ChainOfThought` in Python.
|
|
596
|
+
*
|
|
597
|
+
* @example
|
|
598
|
+
* ```ts
|
|
599
|
+
* const cot = new ChainOfThought("question -> answer");
|
|
600
|
+
* const result = await cot.forward({ question: "What is 9 * 8?" });
|
|
601
|
+
* console.log(result.get("answer")); // "72"
|
|
602
|
+
* ```
|
|
603
|
+
*/
|
|
604
|
+
declare class ChainOfThought extends Predict {
|
|
605
|
+
constructor(signature: string | Signature, options?: {
|
|
606
|
+
rationaleDescription?: string;
|
|
607
|
+
});
|
|
608
|
+
/** Returns the answer without the internal rationale. */
|
|
609
|
+
forward(inputs: Record<string, unknown>): Promise<Prediction>;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* Chain-of-Thought with an optional user-supplied hint injected into the
|
|
614
|
+
* prompt at inference time.
|
|
615
|
+
*
|
|
616
|
+
* Mirrors `dspy.ChainOfThoughtWithHint` in Python.
|
|
617
|
+
*
|
|
618
|
+
* @example
|
|
619
|
+
* ```ts
|
|
620
|
+
* const cot = new ChainOfThoughtWithHint("question -> answer");
|
|
621
|
+
* const result = await cot.forward({
|
|
622
|
+
* question: "What is the capital of France?",
|
|
623
|
+
* hint: "Think about European countries.",
|
|
624
|
+
* });
|
|
625
|
+
* ```
|
|
626
|
+
*/
|
|
627
|
+
declare class ChainOfThoughtWithHint extends ChainOfThought {
|
|
628
|
+
constructor(signature: string | Signature, options?: {
|
|
629
|
+
rationaleDescription?: string;
|
|
630
|
+
});
|
|
631
|
+
forward(inputs: Record<string, unknown>): Promise<Prediction>;
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
/**
|
|
635
|
+
* Runs a signature `M` times and selects the best completion via a final
|
|
636
|
+
* aggregation call.
|
|
637
|
+
*
|
|
638
|
+
* Mirrors `dspy.MultiChainComparison` in Python.
|
|
639
|
+
*/
|
|
640
|
+
declare class MultiChainComparison extends Module {
|
|
641
|
+
#private;
|
|
642
|
+
readonly M: number;
|
|
643
|
+
constructor(signature: string | Signature, M?: number);
|
|
644
|
+
forward(inputs: Record<string, unknown>): Promise<Prediction>;
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
/** A callable tool that ReAct can invoke. */
|
|
648
|
+
interface Tool {
|
|
649
|
+
/** Short unique name used in `Action: toolName[args]` syntax. */
|
|
650
|
+
name: string;
|
|
651
|
+
/** One-sentence description shown in the system prompt. */
|
|
652
|
+
description: string;
|
|
653
|
+
/** Async function that the tool executes. */
|
|
654
|
+
fn: (args: string) => Promise<string>;
|
|
655
|
+
}
|
|
656
|
+
/**
|
|
657
|
+
* ReAct (Reasoning + Acting) module that interleaves Thought, Action, and
|
|
658
|
+
* Observation steps in a loop until a `Finish` action is emitted.
|
|
659
|
+
*
|
|
660
|
+
* Mirrors `dspy.ReAct` in Python.
|
|
661
|
+
*
|
|
662
|
+
* @example
|
|
663
|
+
* ```ts
|
|
664
|
+
* const react = new ReAct("question -> answer", [searchTool]);
|
|
665
|
+
* const result = await react.forward({ question: "Who won the 2024 Olympics?" });
|
|
666
|
+
* ```
|
|
667
|
+
*/
|
|
668
|
+
declare class ReAct extends Module {
|
|
669
|
+
#private;
|
|
670
|
+
readonly tools: ReadonlyMap<string, Tool>;
|
|
671
|
+
readonly maxIter: number;
|
|
672
|
+
constructor(signature: string | Signature, tools: Tool[], maxIter?: number);
|
|
673
|
+
forward(inputs: Record<string, unknown>): Promise<Prediction>;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
/**
|
|
677
|
+
* Generates JavaScript code to answer a question, executes it in a sandboxed
|
|
678
|
+
* manner, and returns the result.
|
|
679
|
+
*
|
|
680
|
+
* Mirrors `dspy.ProgramOfThought` in Python.
|
|
681
|
+
*
|
|
682
|
+
* ⚠️ Security: code runs inside the same process via `new Function()` with a
|
|
683
|
+
* configurable wall-clock timeout (default 5 s). The timeout prevents
|
|
684
|
+
* indefinite hangs but does **not** prevent access to Node.js APIs or the
|
|
685
|
+
* global scope. Do NOT use with untrusted inputs in production without
|
|
686
|
+
* an additional sandboxing layer (e.g. a Worker thread with a restricted
|
|
687
|
+
* `MessageChannel`).
|
|
688
|
+
*/
|
|
689
|
+
declare class ProgramOfThought extends Module {
|
|
690
|
+
#private;
|
|
691
|
+
readonly maxAttempts: number;
|
|
692
|
+
/** Wall-clock timeout (ms) for each code execution attempt. */
|
|
693
|
+
readonly timeoutMs: number;
|
|
694
|
+
readonly sandbox: "worker" | "function" | "none";
|
|
695
|
+
constructor(signature: string | Signature, maxAttempts?: number, timeoutMs?: number, sandbox?: "worker" | "function" | "none");
|
|
696
|
+
forward(inputs: Record<string, unknown>): Promise<Prediction>;
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
/**
|
|
700
|
+
* Retrieve module that calls the globally configured retriever.
|
|
701
|
+
*
|
|
702
|
+
* Mirrors `dspy.Retrieve` in Python.
|
|
703
|
+
*
|
|
704
|
+
* @example
|
|
705
|
+
* ```ts
|
|
706
|
+
* settings.configure({ rm: new ColBERTv2("http://colbert.host") });
|
|
707
|
+
* const retrieve = new Retrieve(3);
|
|
708
|
+
* const result = await retrieve.forward("What is DSPy?");
|
|
709
|
+
* console.log(result.get("passages")); // string[]
|
|
710
|
+
* ```
|
|
711
|
+
*/
|
|
712
|
+
declare class Retrieve extends Module {
|
|
713
|
+
readonly k: number;
|
|
714
|
+
constructor(k?: number);
|
|
715
|
+
forward(query: string): Promise<Prediction>;
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
/**
|
|
719
|
+
* Wraps any module and retries on {@link AssertionError} up to `maxAttempts`,
|
|
720
|
+
* feeding the failure message back into the next attempt.
|
|
721
|
+
*
|
|
722
|
+
* Mirrors `dspy.Retry` in Python.
|
|
723
|
+
*/
|
|
724
|
+
declare class Retry extends Module {
|
|
725
|
+
#private;
|
|
726
|
+
readonly maxAttempts: number;
|
|
727
|
+
constructor(inner: Module, maxAttempts?: number);
|
|
728
|
+
forward(...args: unknown[]): Promise<Prediction>;
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
/**
|
|
732
|
+
* Runs `N` copies of a module in parallel and selects the best output via a
|
|
733
|
+
* provided `reduceFunc` (defaults to returning the first result).
|
|
734
|
+
*
|
|
735
|
+
* Mirrors `dspy.BestOfN` in Python.
|
|
736
|
+
*/
|
|
737
|
+
declare class BestOfN extends Module {
|
|
738
|
+
#private;
|
|
739
|
+
readonly N: number;
|
|
740
|
+
constructor(inner: Module, N?: number, reduceFunc?: (predictions: Prediction[]) => Prediction);
|
|
741
|
+
forward(...args: unknown[]): Promise<Prediction>;
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
/**
|
|
745
|
+
* Combines multiple modules into one via a voting or custom reduce function.
|
|
746
|
+
*
|
|
747
|
+
* Mirrors `dspy.Ensemble` (optimizer version) when used as a module.
|
|
748
|
+
*/
|
|
749
|
+
declare class Ensemble extends Module {
|
|
750
|
+
#private;
|
|
751
|
+
constructor(modules: Module[], reduceFunc?: (predictions: Prediction[]) => Prediction);
|
|
752
|
+
forward(...args: unknown[]): Promise<Prediction>;
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
/**
|
|
756
|
+
* A Prediction that additionally carries a typed `.typed` field.
|
|
757
|
+
*/
|
|
758
|
+
declare class TypedPrediction<T = unknown> extends Prediction {
|
|
759
|
+
readonly typed: T;
|
|
760
|
+
constructor(data: Record<string, unknown>, typed: T, completions?: Record<string, unknown>[]);
|
|
761
|
+
}
|
|
762
|
+
/**
|
|
763
|
+
* TypedPredictor — like Predict but appends JSON formatting instructions and
|
|
764
|
+
* parses the completion as JSON. If an optional schema is provided,
|
|
765
|
+
* validates and returns `.typed`.
|
|
766
|
+
*/
|
|
767
|
+
declare class TypedPredictor<T = unknown> extends Predict {
|
|
768
|
+
#private;
|
|
769
|
+
constructor(signature: string | Signature, schema?: {
|
|
770
|
+
parse: (v: unknown) => T;
|
|
771
|
+
}, options?: {
|
|
772
|
+
maxRetries?: number;
|
|
773
|
+
});
|
|
774
|
+
forward(inputs: Record<string, unknown>): Promise<TypedPrediction<T>>;
|
|
775
|
+
}
|
|
776
|
+
/**
|
|
777
|
+
* TypedChainOfThought — like TypedPredictor but adds a hidden rationale field
|
|
778
|
+
* so the LM reasons before producing the answer.
|
|
779
|
+
*/
|
|
780
|
+
declare class TypedChainOfThought<T = unknown> extends TypedPredictor<T> {
|
|
781
|
+
constructor(signature: string | Signature, schema?: {
|
|
782
|
+
parse: (v: unknown) => T;
|
|
783
|
+
}, options?: {
|
|
784
|
+
maxRetries?: number;
|
|
785
|
+
});
|
|
786
|
+
forward(inputs: Record<string, unknown>): Promise<TypedPrediction<T>>;
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
/**
|
|
790
|
+
* Runs multiple modules in parallel and returns all their results.
|
|
791
|
+
*
|
|
792
|
+
* Note: `forward()` returns the first prediction for Module interface
|
|
793
|
+
* compatibility. Use `run()` to get all predictions.
|
|
794
|
+
*/
|
|
795
|
+
declare class Parallel extends Module {
|
|
796
|
+
#private;
|
|
797
|
+
constructor(modules: Module[], options?: {
|
|
798
|
+
timeoutMs?: number;
|
|
799
|
+
});
|
|
800
|
+
/** Run all modules in parallel and return all predictions. */
|
|
801
|
+
run(...args: unknown[]): Promise<Prediction[]>;
|
|
802
|
+
/** For Module interface compatibility — returns first prediction. */
|
|
803
|
+
forward(...args: unknown[]): Promise<Prediction>;
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
interface RefineOptions {
|
|
807
|
+
/** Maximum refinement iterations (default: 2). */
|
|
808
|
+
maxRefinements?: number;
|
|
809
|
+
/** Field name for feedback in the inner module re-run (default: "feedback"). */
|
|
810
|
+
feedbackField?: string;
|
|
811
|
+
/** If returns true, stop refining early. */
|
|
812
|
+
stopCondition?: (prediction: Prediction) => boolean;
|
|
813
|
+
}
|
|
814
|
+
/**
|
|
815
|
+
* Self-critique / iterative refinement loop.
|
|
816
|
+
*
|
|
817
|
+
* Runs the inner module, then uses a Predict critic to score the output.
|
|
818
|
+
* If the output is not satisfactory, feeds critique back and re-runs.
|
|
819
|
+
*/
|
|
820
|
+
declare class Refine extends Module {
|
|
821
|
+
#private;
|
|
822
|
+
constructor(inner: Module, options?: RefineOptions);
|
|
823
|
+
forward(...args: unknown[]): Promise<Prediction>;
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
/**
|
|
827
|
+
* ReAct variant that uses provider-native tool/function calling instead of
|
|
828
|
+
* text-based action parsing.
|
|
829
|
+
*
|
|
830
|
+
* For OpenAI models, this uses function calling (tools API). For Anthropic, it
|
|
831
|
+
* uses tool_use. Other adapters fall back to the text-based ReAct format.
|
|
832
|
+
*
|
|
833
|
+
* @example
|
|
834
|
+
* ```ts
|
|
835
|
+
* const tools: Tool[] = [{ name: "search", description: "Search", fn: search }];
|
|
836
|
+
* const agent = new NativeReAct("question -> answer", tools);
|
|
837
|
+
* const result = await agent.forward({ question: "What is the capital of France?" });
|
|
838
|
+
* ```
|
|
839
|
+
*/
|
|
840
|
+
declare class NativeReAct extends Module {
|
|
841
|
+
#private;
|
|
842
|
+
readonly tools: ReadonlyMap<string, Tool>;
|
|
843
|
+
readonly maxIter: number;
|
|
844
|
+
constructor(signatureStr: string, tools: Tool[], maxIter?: number);
|
|
845
|
+
forward(inputs: Record<string, unknown>): Promise<Prediction>;
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
/**
|
|
849
|
+
* Abstract base class for all retrieval backends.
|
|
850
|
+
*
|
|
851
|
+
* Mirrors `dspy.Retrieve`'s underlying retriever protocol in Python.
|
|
852
|
+
*/
|
|
853
|
+
declare abstract class Retriever {
|
|
854
|
+
/**
|
|
855
|
+
* Retrieve the top-`k` passages relevant to `query`.
|
|
856
|
+
*
|
|
857
|
+
* @param query - The search query string.
|
|
858
|
+
* @param k - Number of passages to return.
|
|
859
|
+
* @returns An ordered array of passage strings (most relevant first).
|
|
860
|
+
*/
|
|
861
|
+
abstract retrieve(query: string, k: number): Promise<string[]>;
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
/** Options for the ColBERTv2 retriever. */
|
|
865
|
+
interface ColBERTv2Options {
|
|
866
|
+
/** Base URL of the ColBERT server. */
|
|
867
|
+
url: string;
|
|
868
|
+
/** Number of passages to request from the server (defaults to k). */
|
|
869
|
+
passages?: number;
|
|
870
|
+
}
|
|
871
|
+
/**
|
|
872
|
+
* Retriever that queries a ColBERTv2 REST endpoint.
|
|
873
|
+
*
|
|
874
|
+
* Mirrors `dspy.ColBERTv2` in Python.
|
|
875
|
+
*/
|
|
876
|
+
declare class ColBERTv2 extends Retriever {
|
|
877
|
+
#private;
|
|
878
|
+
constructor(options: ColBERTv2Options | string);
|
|
879
|
+
retrieve(query: string, k: number): Promise<string[]>;
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
/** Options for the Pinecone retriever. */
|
|
883
|
+
interface PineconeRMOptions {
|
|
884
|
+
apiKey?: string;
|
|
885
|
+
indexName: string;
|
|
886
|
+
namespace?: string;
|
|
887
|
+
embeddingFn: (text: string) => Promise<number[]>;
|
|
888
|
+
}
|
|
889
|
+
/**
|
|
890
|
+
* Retriever backed by Pinecone vector database.
|
|
891
|
+
*
|
|
892
|
+
* Requires the `@pinecone-database/pinecone` package:
|
|
893
|
+
* ```
|
|
894
|
+
* npm install @pinecone-database/pinecone
|
|
895
|
+
* ```
|
|
896
|
+
*/
|
|
897
|
+
declare class PineconeRM extends Retriever {
|
|
898
|
+
#private;
|
|
899
|
+
constructor(options: PineconeRMOptions);
|
|
900
|
+
retrieve(query: string, k: number): Promise<string[]>;
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
/** Options for the Weaviate retriever. */
|
|
904
|
+
interface WeaviateRMOptions {
|
|
905
|
+
url: string;
|
|
906
|
+
apiKey?: string;
|
|
907
|
+
/** The Weaviate class/collection to query. */
|
|
908
|
+
className: string;
|
|
909
|
+
/** Property name that holds the passage text. */
|
|
910
|
+
textProperty?: string;
|
|
911
|
+
embeddingFn: (text: string) => Promise<number[]>;
|
|
912
|
+
}
|
|
913
|
+
/**
|
|
914
|
+
* Retriever backed by Weaviate vector database.
|
|
915
|
+
*
|
|
916
|
+
* Requires the `weaviate-client` package:
|
|
917
|
+
* ```
|
|
918
|
+
* npm install weaviate-client
|
|
919
|
+
* ```
|
|
920
|
+
*/
|
|
921
|
+
declare class WeaviateRM extends Retriever {
|
|
922
|
+
#private;
|
|
923
|
+
constructor(options: WeaviateRMOptions);
|
|
924
|
+
retrieve(query: string, k: number): Promise<string[]>;
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
/** Options for the ChromaDB retriever. */
|
|
928
|
+
interface ChromadbRMOptions {
|
|
929
|
+
/** ChromaDB server URL (default: http://localhost:8000). */
|
|
930
|
+
url?: string;
|
|
931
|
+
collectionName: string;
|
|
932
|
+
embeddingFn?: (texts: string[]) => Promise<number[][]>;
|
|
933
|
+
}
|
|
934
|
+
/**
|
|
935
|
+
* Retriever backed by ChromaDB.
|
|
936
|
+
*
|
|
937
|
+
* Requires the `chromadb` package:
|
|
938
|
+
* ```
|
|
939
|
+
* npm install chromadb
|
|
940
|
+
* ```
|
|
941
|
+
*/
|
|
942
|
+
declare class ChromadbRM extends Retriever {
|
|
943
|
+
#private;
|
|
944
|
+
constructor(options: ChromadbRMOptions);
|
|
945
|
+
retrieve(query: string, k: number): Promise<string[]>;
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
/** Options for the Qdrant retriever. */
|
|
949
|
+
interface QdrantRMOptions {
|
|
950
|
+
url?: string;
|
|
951
|
+
apiKey?: string;
|
|
952
|
+
collectionName: string;
|
|
953
|
+
/** Property name that holds the passage text in the payload. */
|
|
954
|
+
textField?: string;
|
|
955
|
+
embeddingFn: (text: string) => Promise<number[]>;
|
|
956
|
+
}
|
|
957
|
+
/**
|
|
958
|
+
* Retriever backed by Qdrant vector database.
|
|
959
|
+
*
|
|
960
|
+
* Requires the `@qdrant/js-client-rest` package:
|
|
961
|
+
* ```
|
|
962
|
+
* npm install @qdrant/js-client-rest
|
|
963
|
+
* ```
|
|
964
|
+
*/
|
|
965
|
+
declare class QdrantRM extends Retriever {
|
|
966
|
+
#private;
|
|
967
|
+
constructor(options: QdrantRMOptions);
|
|
968
|
+
retrieve(query: string, k: number): Promise<string[]>;
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
/** Options for the FAISS retriever. */
|
|
972
|
+
interface FaissRMOptions {
|
|
973
|
+
/** Array of passage strings (the corpus). */
|
|
974
|
+
passages: string[];
|
|
975
|
+
/**
|
|
976
|
+
* Pre-computed embeddings (one per passage, in the same order as `passages`).
|
|
977
|
+
* When omitted, embeddings are computed lazily on the first `retrieve()` call
|
|
978
|
+
* using `embeddingFn` and cached for subsequent calls.
|
|
979
|
+
*/
|
|
980
|
+
embeddings?: number[][];
|
|
981
|
+
/**
|
|
982
|
+
* Function that maps a text string to a dense embedding vector.
|
|
983
|
+
* Required for computing the query embedding (and passage embeddings when
|
|
984
|
+
* `embeddings` is not pre-supplied).
|
|
985
|
+
*/
|
|
986
|
+
embeddingFn: (text: string) => Promise<number[]>;
|
|
987
|
+
}
|
|
988
|
+
/**
|
|
989
|
+
* In-process FAISS-style retriever using cosine similarity over dense
|
|
990
|
+
* embeddings. No external server required.
|
|
991
|
+
*
|
|
992
|
+
* For large corpora, consider using the `faiss-node` package instead.
|
|
993
|
+
*/
|
|
994
|
+
declare class FaissRM extends Retriever {
|
|
995
|
+
#private;
|
|
996
|
+
constructor(options: FaissRMOptions);
|
|
997
|
+
retrieve(query: string, k: number): Promise<string[]>;
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
/** Options for the You.com retriever. */
|
|
1001
|
+
interface YouRMOptions {
|
|
1002
|
+
apiKey?: string;
|
|
1003
|
+
/** Number of snippets per web result to include (default: 1). */
|
|
1004
|
+
numWeb?: number;
|
|
1005
|
+
}
|
|
1006
|
+
/**
|
|
1007
|
+
* Retriever backed by the You.com search API.
|
|
1008
|
+
*/
|
|
1009
|
+
declare class YouRM extends Retriever {
|
|
1010
|
+
#private;
|
|
1011
|
+
constructor(options?: YouRMOptions);
|
|
1012
|
+
retrieve(query: string, k: number): Promise<string[]>;
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
/**
|
|
1016
|
+
* In-memory retriever for unit testing.
|
|
1017
|
+
*
|
|
1018
|
+
* Returns passages that contain the query string (case-insensitive), or the
|
|
1019
|
+
* first `k` passages if no matches are found.
|
|
1020
|
+
*/
|
|
1021
|
+
declare class MockRetriever extends Retriever {
|
|
1022
|
+
#private;
|
|
1023
|
+
constructor(passages: string[]);
|
|
1024
|
+
retrieve(query: string, k: number): Promise<string[]>;
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
/**
|
|
1028
|
+
* A metric function receives an example, a prediction, and optionally the
|
|
1029
|
+
* execution trace, and returns a score (number) or a pass/fail (boolean).
|
|
1030
|
+
*
|
|
1031
|
+
* Mirrors `dspy.Metric` in Python.
|
|
1032
|
+
*/
|
|
1033
|
+
type Metric = (example: Example, prediction: Prediction, trace?: Trace[]) => number | boolean;
|
|
1034
|
+
/** Per-example evaluation result. */
|
|
1035
|
+
interface ExampleResult {
|
|
1036
|
+
example: Example;
|
|
1037
|
+
prediction: Prediction;
|
|
1038
|
+
score: number;
|
|
1039
|
+
passed: boolean;
|
|
1040
|
+
}
|
|
1041
|
+
/** Aggregated result returned by {@link evaluate}. */
|
|
1042
|
+
interface EvaluationResult {
|
|
1043
|
+
/** Average metric score across all examples. */
|
|
1044
|
+
score: number;
|
|
1045
|
+
/** Number of examples that passed (score > 0 / true). */
|
|
1046
|
+
numPassed: number;
|
|
1047
|
+
/** Total number of examples. */
|
|
1048
|
+
total: number;
|
|
1049
|
+
/** Per-example breakdown. */
|
|
1050
|
+
results: ExampleResult[];
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
/** Options for the {@link evaluate} function. */
|
|
1054
|
+
interface EvaluateOptions {
|
|
1055
|
+
/**
|
|
1056
|
+
* Number of concurrent evaluations (default: 1 — sequential).
|
|
1057
|
+
* Set > 1 to parallelise calls.
|
|
1058
|
+
*/
|
|
1059
|
+
numThreads?: number;
|
|
1060
|
+
/** When true, logs per-example results to the console. */
|
|
1061
|
+
displayProgress?: boolean;
|
|
1062
|
+
}
|
|
1063
|
+
/**
|
|
1064
|
+
* Evaluate a `program` (any {@link Module}) on a set of `examples` using a
|
|
1065
|
+
* `metric` function.
|
|
1066
|
+
*
|
|
1067
|
+
* Mirrors `dspy.Evaluate` in Python.
|
|
1068
|
+
*
|
|
1069
|
+
* @example
|
|
1070
|
+
* ```ts
|
|
1071
|
+
* const result = await evaluate(rag, devset, exactMatch("answer"));
|
|
1072
|
+
* console.log(`Score: ${result.score.toFixed(2)}`);
|
|
1073
|
+
* ```
|
|
1074
|
+
*/
|
|
1075
|
+
declare function evaluate(program: Module, examples: Example[], metric: Metric, options?: EvaluateOptions): Promise<EvaluationResult>;
|
|
1076
|
+
|
|
1077
|
+
/**
|
|
1078
|
+
* Returns 1 if the prediction's `field` exactly matches the example's `field`
|
|
1079
|
+
* (case-insensitive by default), 0 otherwise.
|
|
1080
|
+
*/
|
|
1081
|
+
declare function exactMatch(field?: string, caseSensitive?: boolean): Metric;
|
|
1082
|
+
/**
|
|
1083
|
+
* Token-level F1 metric (word overlap), commonly used for SQuAD-style QA.
|
|
1084
|
+
*/
|
|
1085
|
+
declare function f1(field?: string): Metric;
|
|
1086
|
+
/**
|
|
1087
|
+
* Returns 1 if at least one of the top-`k` completions passes the `innerMetric`.
|
|
1088
|
+
*/
|
|
1089
|
+
declare function passAtK(innerMetric: Metric, k: number): Metric;
|
|
1090
|
+
/**
|
|
1091
|
+
* Simplified BLEU score (1-gram + 2-gram precision) for a single field.
|
|
1092
|
+
*/
|
|
1093
|
+
declare function bleu(field?: string): Metric;
|
|
1094
|
+
/**
|
|
1095
|
+
* ROUGE-L metric (LCS-based recall/precision/F1) for a single field.
|
|
1096
|
+
*/
|
|
1097
|
+
declare function rouge(field?: string): Metric;
|
|
1098
|
+
|
|
1099
|
+
/**
|
|
1100
|
+
* Abstract base class for all DSTsx optimizers (teleprompters).
|
|
1101
|
+
*
|
|
1102
|
+
* An optimizer takes a student program and a training set, then returns a new
|
|
1103
|
+
* (optimized) program with better prompts / few-shot examples.
|
|
1104
|
+
*
|
|
1105
|
+
* Mirrors `dspy.Teleprompter` in Python.
|
|
1106
|
+
*/
|
|
1107
|
+
declare abstract class Optimizer {
|
|
1108
|
+
/**
|
|
1109
|
+
* Compile (optimize) a `student` program.
|
|
1110
|
+
*
|
|
1111
|
+
* @param student - The module to optimize (must not be mutated).
|
|
1112
|
+
* @param trainset - Examples used to generate / score candidates.
|
|
1113
|
+
* @param metric - Function that scores a prediction.
|
|
1114
|
+
* @returns - A new, optimized copy of `student`.
|
|
1115
|
+
*/
|
|
1116
|
+
abstract compile(student: Module, trainset: Example[], metric: Metric): Promise<Module>;
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1119
|
+
/**
|
|
1120
|
+
* The simplest optimizer — directly assigns labeled examples as `demos` on
|
|
1121
|
+
* every `Predict` sub-module without running the LM at all.
|
|
1122
|
+
*
|
|
1123
|
+
* Mirrors `dspy.LabeledFewShot` in Python.
|
|
1124
|
+
*/
|
|
1125
|
+
declare class LabeledFewShot extends Optimizer {
|
|
1126
|
+
#private;
|
|
1127
|
+
/**
|
|
1128
|
+
* @param k - Maximum number of demos to assign per predictor (default: 16).
|
|
1129
|
+
*/
|
|
1130
|
+
constructor(k?: number);
|
|
1131
|
+
compile(student: Module, trainset: Example[], _metric: Metric): Promise<Module>;
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
/** Options for BootstrapFewShot. */
|
|
1135
|
+
interface BootstrapFewShotOptions {
|
|
1136
|
+
/** Maximum number of bootstrapped demos per predictor (default: 4). */
|
|
1137
|
+
maxBootstrappedDemos?: number;
|
|
1138
|
+
/** Maximum number of labeled demos per predictor (default: 16). */
|
|
1139
|
+
maxLabeledDemos?: number;
|
|
1140
|
+
/** Optional teacher module; defaults to the student. */
|
|
1141
|
+
teacher?: Module;
|
|
1142
|
+
}
|
|
1143
|
+
/**
|
|
1144
|
+
* Collects successful execution traces by running the student (or a teacher)
|
|
1145
|
+
* on the training set and uses them as few-shot demonstrations.
|
|
1146
|
+
*
|
|
1147
|
+
* Mirrors `dspy.BootstrapFewShot` in Python.
|
|
1148
|
+
*/
|
|
1149
|
+
declare class BootstrapFewShot extends Optimizer {
|
|
1150
|
+
#private;
|
|
1151
|
+
constructor(options?: BootstrapFewShotOptions);
|
|
1152
|
+
compile(student: Module, trainset: Example[], metric: Metric): Promise<Module>;
|
|
1153
|
+
}
|
|
1154
|
+
|
|
1155
|
+
/** Options for BootstrapFewShotWithRandomSearch. */
|
|
1156
|
+
interface BootstrapFewShotWithRandomSearchOptions extends BootstrapFewShotOptions {
|
|
1157
|
+
/** Number of candidate demo subsets to evaluate (default: 8). */
|
|
1158
|
+
numCandidatePrograms?: number;
|
|
1159
|
+
/** Held-out validation set. If omitted, the trainset is used for scoring. */
|
|
1160
|
+
valset?: Example[];
|
|
1161
|
+
}
|
|
1162
|
+
/**
|
|
1163
|
+
* Extends {@link BootstrapFewShot} by trying multiple random demo subsets and
|
|
1164
|
+
* selecting the combination with the highest validation score.
|
|
1165
|
+
*
|
|
1166
|
+
* Mirrors `dspy.BootstrapFewShotWithRandomSearch` in Python.
|
|
1167
|
+
*/
|
|
1168
|
+
declare class BootstrapFewShotWithRandomSearch extends BootstrapFewShot {
|
|
1169
|
+
#private;
|
|
1170
|
+
constructor(options?: BootstrapFewShotWithRandomSearchOptions);
|
|
1171
|
+
compile(student: Module, trainset: Example[], metric: Metric): Promise<Module>;
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
interface BootstrapFewShotWithOptunaOptions {
|
|
1175
|
+
maxBootstrappedDemos?: number;
|
|
1176
|
+
maxLabeledDemos?: number;
|
|
1177
|
+
/** Number of TPE trials (default: 20). */
|
|
1178
|
+
numTrials?: number;
|
|
1179
|
+
valset?: Example[];
|
|
1180
|
+
}
|
|
1181
|
+
/**
|
|
1182
|
+
* Bayesian optimizer using a simplified TPE (Tree-structured Parzen Estimator).
|
|
1183
|
+
*
|
|
1184
|
+
* Extends BootstrapFewShot: first collects candidate demos via the parent,
|
|
1185
|
+
* then runs `numTrials` iterations sampling demo subsets using TPE to find
|
|
1186
|
+
* the best-scoring configuration.
|
|
1187
|
+
*/
|
|
1188
|
+
declare class BootstrapFewShotWithOptuna extends BootstrapFewShot {
|
|
1189
|
+
#private;
|
|
1190
|
+
constructor(options?: BootstrapFewShotWithOptunaOptions);
|
|
1191
|
+
compile(student: Module, trainset: Example[], metric: Metric): Promise<Module>;
|
|
1192
|
+
}
|
|
1193
|
+
|
|
1194
|
+
/** Options for COPRO. */
|
|
1195
|
+
interface COPROOptions {
|
|
1196
|
+
/** Number of instruction candidates to generate per predictor (default: 5). */
|
|
1197
|
+
breadth?: number;
|
|
1198
|
+
/** Number of refinement rounds (default: 3). */
|
|
1199
|
+
depth?: number;
|
|
1200
|
+
}
|
|
1201
|
+
/**
|
|
1202
|
+
* Collaborative Prompt Optimizer (COPRO) — uses the LM to propose improved
|
|
1203
|
+
* instructions for each `Predict` sub-module and selects the best via
|
|
1204
|
+
* metric evaluation.
|
|
1205
|
+
*
|
|
1206
|
+
* Mirrors `dspy.COPRO` in Python.
|
|
1207
|
+
*/
|
|
1208
|
+
declare class COPRO extends Optimizer {
|
|
1209
|
+
#private;
|
|
1210
|
+
constructor(options?: COPROOptions);
|
|
1211
|
+
compile(student: Module, trainset: Example[], metric: Metric): Promise<Module>;
|
|
1212
|
+
}
|
|
1213
|
+
|
|
1214
|
+
/** Options for MIPRO. */
|
|
1215
|
+
interface MIPROOptions {
|
|
1216
|
+
/** Number of instruction candidates per predictor (default: 5). */
|
|
1217
|
+
numCandidates?: number;
|
|
1218
|
+
/** Temperature for instruction generation (default: 0.9). */
|
|
1219
|
+
initTemperature?: number;
|
|
1220
|
+
/** Number of random bootstrap candidate programs to evaluate (default: 8). */
|
|
1221
|
+
numCandidatePrograms?: number;
|
|
1222
|
+
/** Emit verbose progress logs (default: false). */
|
|
1223
|
+
verbose?: boolean;
|
|
1224
|
+
}
|
|
1225
|
+
/**
|
|
1226
|
+
* Multi-stage Instruction Prompt Optimizer (MIPRO) — combines COPRO-style
|
|
1227
|
+
* instruction proposal with BootstrapFewShotWithRandomSearch to jointly
|
|
1228
|
+
* optimize instructions and demonstrations.
|
|
1229
|
+
*
|
|
1230
|
+
* Mirrors `dspy.MIPRO` in Python.
|
|
1231
|
+
*/
|
|
1232
|
+
declare class MIPRO extends Optimizer {
|
|
1233
|
+
#private;
|
|
1234
|
+
constructor(options?: MIPROOptions);
|
|
1235
|
+
compile(student: Module, trainset: Example[], metric: Metric): Promise<Module>;
|
|
1236
|
+
}
|
|
1237
|
+
|
|
1238
|
+
/** Options for KNNFewShot. */
|
|
1239
|
+
interface KNNFewShotOptions {
|
|
1240
|
+
/** Number of nearest neighbors to use as demos (default: 3). */
|
|
1241
|
+
k?: number;
|
|
1242
|
+
/**
|
|
1243
|
+
* Embedding function that maps a text string to a dense vector.
|
|
1244
|
+
* Required for computing similarity between examples.
|
|
1245
|
+
*/
|
|
1246
|
+
embeddingFn: (text: string) => Promise<number[]>;
|
|
1247
|
+
/**
|
|
1248
|
+
* Which field of each example to embed for similarity comparison
|
|
1249
|
+
* (default: first input field of the signature).
|
|
1250
|
+
*/
|
|
1251
|
+
keyField?: string;
|
|
1252
|
+
}
|
|
1253
|
+
/**
|
|
1254
|
+
* Dynamic few-shot optimizer that selects demonstrations at inference time
|
|
1255
|
+
* using k-nearest-neighbour search over the training set.
|
|
1256
|
+
*
|
|
1257
|
+
* Unlike static few-shot optimizers this one injects demos into a
|
|
1258
|
+
* `Predict.forward` call by wrapping each predictor at compile time.
|
|
1259
|
+
*
|
|
1260
|
+
* Mirrors `dspy.KNNFewShot` in Python.
|
|
1261
|
+
*/
|
|
1262
|
+
declare class KNNFewShot extends Optimizer {
|
|
1263
|
+
#private;
|
|
1264
|
+
constructor(options: KNNFewShotOptions);
|
|
1265
|
+
compile(student: Module, trainset: Example[], _metric: Metric): Promise<Module>;
|
|
1266
|
+
}
|
|
1267
|
+
|
|
1268
|
+
/** Options for the Ensemble optimizer. */
|
|
1269
|
+
interface EnsembleOptimizerOptions {
|
|
1270
|
+
/**
|
|
1271
|
+
* Custom function to combine multiple predictions into one.
|
|
1272
|
+
* Defaults to majority vote on the first output field.
|
|
1273
|
+
*/
|
|
1274
|
+
reduceFunc?: (predictions: Prediction[]) => Prediction;
|
|
1275
|
+
}
|
|
1276
|
+
/**
|
|
1277
|
+
* Combines multiple optimized programs into a single ensemble module.
|
|
1278
|
+
*
|
|
1279
|
+
* Mirrors `dspy.Ensemble` (optimizer version) in Python.
|
|
1280
|
+
*/
|
|
1281
|
+
declare class EnsembleOptimizer extends Optimizer {
|
|
1282
|
+
#private;
|
|
1283
|
+
constructor(options?: EnsembleOptimizerOptions);
|
|
1284
|
+
compile(student: Module, _trainset: Example[], _metric: Metric): Promise<Module>;
|
|
1285
|
+
}
|
|
1286
|
+
|
|
1287
|
+
/** Output format for the exported fine-tuning data. */
|
|
1288
|
+
type FinetuneFormat = "openai" | "generic";
|
|
1289
|
+
/** Options for BootstrapFinetune. */
|
|
1290
|
+
interface BootstrapFinetuneOptions {
|
|
1291
|
+
/** Path to write the JSONL fine-tuning file (default: "./finetune_data.jsonl"). */
|
|
1292
|
+
exportPath?: string | undefined;
|
|
1293
|
+
/** Format of the exported data (default: "openai"). */
|
|
1294
|
+
format?: FinetuneFormat | undefined;
|
|
1295
|
+
/** Bootstrap options passed to BootstrapFewShot internally. */
|
|
1296
|
+
maxBootstrappedDemos?: number | undefined;
|
|
1297
|
+
}
|
|
1298
|
+
/**
|
|
1299
|
+
* Collects LM traces via BootstrapFewShot and exports them as a JSONL file
|
|
1300
|
+
* suitable for fine-tuning.
|
|
1301
|
+
*
|
|
1302
|
+
* - `"openai"` format: `{ messages: [{role, content}, ...] }` per line
|
|
1303
|
+
* - `"generic"` format: `{ prompt: string, completion: string }` per line
|
|
1304
|
+
*
|
|
1305
|
+
* @example
|
|
1306
|
+
* ```ts
|
|
1307
|
+
* const optimizer = new BootstrapFinetune({
|
|
1308
|
+
* exportPath: "./finetune_data.jsonl",
|
|
1309
|
+
* format: "openai",
|
|
1310
|
+
* });
|
|
1311
|
+
* const recipe = await optimizer.compile(program, trainset, metric);
|
|
1312
|
+
* ```
|
|
1313
|
+
*/
|
|
1314
|
+
declare class BootstrapFinetune extends Optimizer {
|
|
1315
|
+
#private;
|
|
1316
|
+
constructor(options?: BootstrapFinetuneOptions);
|
|
1317
|
+
compile(student: Module, trainset: Example[], metric: Metric): Promise<Module>;
|
|
1318
|
+
}
|
|
1319
|
+
|
|
1320
|
+
/** Options for GRPO. */
|
|
1321
|
+
interface GRPOOptions {
|
|
1322
|
+
/** Number of optimization steps (default: 20). */
|
|
1323
|
+
numSteps?: number | undefined;
|
|
1324
|
+
/** Number of candidates per group per step (default: 8). */
|
|
1325
|
+
groupSize?: number | undefined;
|
|
1326
|
+
/** Sampling temperature for candidate generation (default: 1.0). */
|
|
1327
|
+
temperature?: number | undefined;
|
|
1328
|
+
/** Max labeled demos (default: 16). */
|
|
1329
|
+
maxLabeledDemos?: number | undefined;
|
|
1330
|
+
}
|
|
1331
|
+
/**
|
|
1332
|
+
* Group Relative Policy Optimization optimizer.
|
|
1333
|
+
*
|
|
1334
|
+
* Mirrors `dspy.GRPO` in Python. Runs `numSteps` iterations where each step:
|
|
1335
|
+
* 1. Samples `groupSize` candidate instruction variants via the LM.
|
|
1336
|
+
* 2. Evaluates each against the training set.
|
|
1337
|
+
* 3. Updates the best instruction using group-relative scoring.
|
|
1338
|
+
*
|
|
1339
|
+
* Pure TypeScript — no external dependencies beyond the configured LM.
|
|
1340
|
+
*/
|
|
1341
|
+
declare class GRPO extends Optimizer {
|
|
1342
|
+
#private;
|
|
1343
|
+
constructor(options?: GRPOOptions);
|
|
1344
|
+
compile(student: Module, trainset: Example[], metric: Metric): Promise<Module>;
|
|
1345
|
+
}
|
|
1346
|
+
|
|
1347
|
+
/** Options for SIMBA. */
|
|
1348
|
+
interface SIMBAOptions {
|
|
1349
|
+
/** Number of optimization iterations (default: 10). */
|
|
1350
|
+
numIter?: number | undefined;
|
|
1351
|
+
/** Mini-batch size for each evaluation (default: 8). */
|
|
1352
|
+
batchSize?: number | undefined;
|
|
1353
|
+
/** Max bootstrapped demos (default: 4). */
|
|
1354
|
+
maxBootstrappedDemos?: number | undefined;
|
|
1355
|
+
}
|
|
1356
|
+
/**
|
|
1357
|
+
* SIMBA (Stochastic Introspective Mini-Batch Ascent) optimizer.
|
|
1358
|
+
*
|
|
1359
|
+
* A lightweight stochastic optimizer that:
|
|
1360
|
+
* 1. Selects a random mini-batch from the training set each iteration.
|
|
1361
|
+
* 2. Proposes a candidate (via demo subset sampling).
|
|
1362
|
+
* 3. Accepts the candidate if it improves on the current best.
|
|
1363
|
+
* 4. Returns the overall best module found.
|
|
1364
|
+
*/
|
|
1365
|
+
declare class SIMBA extends Optimizer {
|
|
1366
|
+
#private;
|
|
1367
|
+
constructor(options?: SIMBAOptions);
|
|
1368
|
+
compile(student: Module, trainset: Example[], metric: Metric): Promise<Module>;
|
|
1369
|
+
}
|
|
1370
|
+
|
|
1371
|
+
/** Options for AvatarOptimizer. */
|
|
1372
|
+
interface AvatarOptimizerOptions {
|
|
1373
|
+
/** Number of avatar candidates to try per predictor (default: 4). */
|
|
1374
|
+
numAvatars?: number | undefined;
|
|
1375
|
+
/** Max labeled demos (default: 8). */
|
|
1376
|
+
maxLabeledDemos?: number | undefined;
|
|
1377
|
+
}
|
|
1378
|
+
/**
|
|
1379
|
+
* AvatarOptimizer iteratively proposes and evaluates "avatar" role descriptions
|
|
1380
|
+
* (persona prefixes) for each Predict module.
|
|
1381
|
+
*
|
|
1382
|
+
* Mirrors `dspy.AvatarOptimizer` in Python.
|
|
1383
|
+
*
|
|
1384
|
+
* For each predictor, proposes `numAvatars` different role/persona descriptions
|
|
1385
|
+
* and selects the one that scores highest on the training set.
|
|
1386
|
+
*/
|
|
1387
|
+
declare class AvatarOptimizer extends Optimizer {
|
|
1388
|
+
#private;
|
|
1389
|
+
constructor(options?: AvatarOptimizerOptions);
|
|
1390
|
+
compile(student: Module, trainset: Example[], metric: Metric): Promise<Module>;
|
|
1391
|
+
}
|
|
1392
|
+
|
|
1393
|
+
/**
|
|
1394
|
+
* A typed error thrown by {@link Assert} when a condition is not met.
|
|
1395
|
+
*
|
|
1396
|
+
* {@link Retry} catches this error and feeds the message back into the next
|
|
1397
|
+
* attempt as feedback.
|
|
1398
|
+
*/
|
|
1399
|
+
declare class AssertionError extends Error {
|
|
1400
|
+
constructor(message?: string);
|
|
1401
|
+
}
|
|
1402
|
+
/**
|
|
1403
|
+
* Hard assertion — throws an {@link AssertionError} if `condition` is falsy.
|
|
1404
|
+
* Caught and retried by the {@link Retry} module.
|
|
1405
|
+
*
|
|
1406
|
+
* Mirrors `dspy.Assert` in Python.
|
|
1407
|
+
*
|
|
1408
|
+
* @example
|
|
1409
|
+
* ```ts
|
|
1410
|
+
* Assert(result.get("answer") !== "", "Answer must not be empty");
|
|
1411
|
+
* ```
|
|
1412
|
+
*/
|
|
1413
|
+
declare function Assert(condition: unknown, message?: string): asserts condition;
|
|
1414
|
+
|
|
1415
|
+
/**
|
|
1416
|
+
* Soft suggestion — logs a warning if `condition` is falsy but does NOT throw.
|
|
1417
|
+
*
|
|
1418
|
+
* Mirrors `dspy.Suggest` in Python.
|
|
1419
|
+
*
|
|
1420
|
+
* @example
|
|
1421
|
+
* ```ts
|
|
1422
|
+
* Suggest(result.get("confidence") === "high", "Low confidence in answer");
|
|
1423
|
+
* ```
|
|
1424
|
+
*/
|
|
1425
|
+
declare function Suggest(condition: unknown, message?: string): void;
|
|
1426
|
+
|
|
1427
|
+
/** Configuration options for the global DSTsx settings. */
|
|
1428
|
+
interface SettingsOptions {
|
|
1429
|
+
/** Default language model used by all Predict modules. */
|
|
1430
|
+
lm?: LM;
|
|
1431
|
+
/** Default retrieval model used by the Retrieve module. */
|
|
1432
|
+
rm?: Retriever;
|
|
1433
|
+
/** Default LM call configuration (temperature, maxTokens, etc.). */
|
|
1434
|
+
lmConfig?: LMCallConfig;
|
|
1435
|
+
/** Log level for internal messages. */
|
|
1436
|
+
logLevel?: "silent" | "error" | "warn" | "info" | "debug";
|
|
1437
|
+
/** Directory for caching compiled programs (JSON). */
|
|
1438
|
+
cacheDir?: string;
|
|
1439
|
+
}
|
|
1440
|
+
/**
|
|
1441
|
+
* Global settings singleton for DSTsx.
|
|
1442
|
+
*
|
|
1443
|
+
* Configure the default LM and retriever before using any modules:
|
|
1444
|
+
* ```ts
|
|
1445
|
+
* import { settings } from "dstsx";
|
|
1446
|
+
* import { OpenAI } from "dstsx";
|
|
1447
|
+
*
|
|
1448
|
+
* settings.configure({ lm: new OpenAI({ model: "gpt-4o" }) });
|
|
1449
|
+
* ```
|
|
1450
|
+
*
|
|
1451
|
+
* Use {@link Settings.context} for per-request overrides in server environments.
|
|
1452
|
+
* Each invocation gets an isolated async context via `AsyncLocalStorage`, so
|
|
1453
|
+
* concurrent overlapping requests never interfere with each other:
|
|
1454
|
+
* ```ts
|
|
1455
|
+
* await settings.context({ lm: perRequestLM }, async () => {
|
|
1456
|
+
* return program.forward({ question });
|
|
1457
|
+
* });
|
|
1458
|
+
* ```
|
|
1459
|
+
*/
|
|
1460
|
+
declare class Settings {
|
|
1461
|
+
#private;
|
|
1462
|
+
get lm(): LM | undefined;
|
|
1463
|
+
get rm(): Retriever | undefined;
|
|
1464
|
+
get lmConfig(): LMCallConfig | undefined;
|
|
1465
|
+
get logLevel(): SettingsOptions["logLevel"];
|
|
1466
|
+
get cacheDir(): string | undefined;
|
|
1467
|
+
/**
|
|
1468
|
+
* Merge `options` into the global settings. Existing keys are overwritten;
|
|
1469
|
+
* omitted keys are unchanged. This does NOT affect currently running
|
|
1470
|
+
* {@link Settings.context} scopes.
|
|
1471
|
+
*/
|
|
1472
|
+
configure(options: SettingsOptions): void;
|
|
1473
|
+
/**
|
|
1474
|
+
* Reset all global settings to their defaults.
|
|
1475
|
+
*/
|
|
1476
|
+
reset(): void;
|
|
1477
|
+
/**
|
|
1478
|
+
* Return a deep-frozen snapshot of the currently effective settings
|
|
1479
|
+
* (respects any active async-context overrides).
|
|
1480
|
+
*/
|
|
1481
|
+
inspect(): Readonly<SettingsOptions>;
|
|
1482
|
+
/**
|
|
1483
|
+
* Run `fn` inside an async-context-local settings scope.
|
|
1484
|
+
*
|
|
1485
|
+
* The `overrides` are merged on top of the current global settings and
|
|
1486
|
+
* stored in an `AsyncLocalStorage` context. Concurrent calls each get
|
|
1487
|
+
* their own isolated snapshot — they never overwrite each other's settings.
|
|
1488
|
+
*
|
|
1489
|
+
* @example
|
|
1490
|
+
* ```ts
|
|
1491
|
+
* // In an Express/Fastify handler:
|
|
1492
|
+
* await settings.context({ lm: perRequestLM }, () => program.forward(inputs));
|
|
1493
|
+
* ```
|
|
1494
|
+
*/
|
|
1495
|
+
context<T>(overrides: SettingsOptions, fn: () => Promise<T>): Promise<T>;
|
|
1496
|
+
}
|
|
1497
|
+
/** The global DSTsx settings singleton. */
|
|
1498
|
+
declare const settings: Settings;
|
|
1499
|
+
|
|
1500
|
+
interface MCPAdapterOptions {
|
|
1501
|
+
serverUrl?: string;
|
|
1502
|
+
tools?: Array<{
|
|
1503
|
+
name: string;
|
|
1504
|
+
description: string;
|
|
1505
|
+
inputSchema: Record<string, unknown>;
|
|
1506
|
+
}>;
|
|
1507
|
+
callHandler?: (name: string, args: Record<string, unknown>) => Promise<unknown>;
|
|
1508
|
+
}
|
|
1509
|
+
/**
|
|
1510
|
+
* Wraps MCP server tools as DSTsx Tool objects.
|
|
1511
|
+
*
|
|
1512
|
+
* When `tools` + `callHandler` are provided, no network connection is needed.
|
|
1513
|
+
* A live MCP connection (via `serverUrl`) requires the
|
|
1514
|
+
* `@modelcontextprotocol/sdk` package to be installed.
|
|
1515
|
+
*/
|
|
1516
|
+
declare class MCPToolAdapter {
|
|
1517
|
+
#private;
|
|
1518
|
+
constructor(options?: MCPAdapterOptions);
|
|
1519
|
+
getTools(): Promise<Tool[]>;
|
|
1520
|
+
}
|
|
1521
|
+
|
|
1522
|
+
interface MCPTool {
|
|
1523
|
+
name: string;
|
|
1524
|
+
description: string;
|
|
1525
|
+
inputSchema: {
|
|
1526
|
+
type: "object";
|
|
1527
|
+
properties: Record<string, {
|
|
1528
|
+
type: string;
|
|
1529
|
+
description?: string;
|
|
1530
|
+
}>;
|
|
1531
|
+
required?: string[];
|
|
1532
|
+
};
|
|
1533
|
+
handler: (inputs: Record<string, unknown>) => Promise<unknown>;
|
|
1534
|
+
}
|
|
1535
|
+
/**
|
|
1536
|
+
* Exposes DSTsx modules as MCP-compatible tool definitions.
|
|
1537
|
+
*
|
|
1538
|
+
* Registered modules can be called via `callTool()`. To create a live stdio
|
|
1539
|
+
* server the `@modelcontextprotocol/sdk` package must be installed.
|
|
1540
|
+
*/
|
|
1541
|
+
declare class DSTsxMCPServer {
|
|
1542
|
+
#private;
|
|
1543
|
+
registerModule(name: string, description: string, module: Module, inputFields: string[]): this;
|
|
1544
|
+
getToolDefinitions(): MCPTool[];
|
|
1545
|
+
callTool(name: string, inputs: Record<string, unknown>): Promise<unknown>;
|
|
1546
|
+
createStdioServer(): Promise<unknown>;
|
|
1547
|
+
}
|
|
1548
|
+
|
|
1549
|
+
/** Event types emitted during optimization. */
|
|
1550
|
+
interface TrackerEvent {
|
|
1551
|
+
/** Type of event. */
|
|
1552
|
+
type: "step" | "trial" | "best" | "done";
|
|
1553
|
+
/** Current step number. */
|
|
1554
|
+
step?: number | undefined;
|
|
1555
|
+
/** Score at this event. */
|
|
1556
|
+
score?: number | undefined;
|
|
1557
|
+
/** Additional metadata. */
|
|
1558
|
+
metadata?: Record<string, unknown> | undefined;
|
|
1559
|
+
}
|
|
1560
|
+
/**
|
|
1561
|
+
* Abstract base class for experiment trackers.
|
|
1562
|
+
*
|
|
1563
|
+
* Implement this to log optimization events to console, files, or external
|
|
1564
|
+
* experiment tracking services.
|
|
1565
|
+
*/
|
|
1566
|
+
declare abstract class Tracker {
|
|
1567
|
+
/** Log a single event. */
|
|
1568
|
+
abstract log(event: TrackerEvent): void;
|
|
1569
|
+
/** Flush any buffered events (e.g. write to disk). */
|
|
1570
|
+
abstract flush(): Promise<void>;
|
|
1571
|
+
}
|
|
1572
|
+
|
|
1573
|
+
/**
|
|
1574
|
+
* A simple tracker that logs events to the console.
|
|
1575
|
+
*/
|
|
1576
|
+
declare class ConsoleTracker extends Tracker {
|
|
1577
|
+
log(event: TrackerEvent): void;
|
|
1578
|
+
flush(): Promise<void>;
|
|
1579
|
+
}
|
|
1580
|
+
|
|
1581
|
+
/**
|
|
1582
|
+
* A tracker that appends JSON-encoded events as lines to a file.
|
|
1583
|
+
*
|
|
1584
|
+
* @example
|
|
1585
|
+
* ```ts
|
|
1586
|
+
* const tracker = new JsonFileTracker("./runs/experiment1.jsonl");
|
|
1587
|
+
* const optimizer = new GRPO({ numSteps: 10 });
|
|
1588
|
+
* ```
|
|
1589
|
+
*/
|
|
1590
|
+
declare class JsonFileTracker extends Tracker {
|
|
1591
|
+
#private;
|
|
1592
|
+
constructor(path: string);
|
|
1593
|
+
log(event: TrackerEvent): void;
|
|
1594
|
+
flush(): Promise<void>;
|
|
1595
|
+
}
|
|
1596
|
+
|
|
1597
|
+
export { Anthropic, type AnthropicOptions, Assert, AssertionError, AvatarOptimizer, type AvatarOptimizerOptions, BestOfN, BootstrapFewShot, type BootstrapFewShotOptions, BootstrapFewShotWithOptuna, type BootstrapFewShotWithOptunaOptions, BootstrapFewShotWithRandomSearch, type BootstrapFewShotWithRandomSearchOptions, BootstrapFinetune, type BootstrapFinetuneOptions, COPRO, type COPROOptions, ChainOfThought, ChainOfThoughtWithHint, ChromadbRM, type ChromadbRMOptions, Cohere, type CohereOptions, ColBERTv2, type ColBERTv2Options, ConsoleTracker, DSTsxMCPServer, DiskCache, Ensemble, EnsembleOptimizer, type EnsembleOptimizerOptions, type EvaluateOptions, type EvaluationResult, Example, type ExampleResult, FaissRM, type FaissRMOptions, type FieldMeta, type FinetuneFormat, GRPO, type GRPOOptions, GoogleAI, type GoogleAIOptions, HuggingFace, type HuggingFaceOptions, Image, type ImageMimeType, InputField, JsonFileTracker, KNNFewShot, type KNNFewShotOptions, LM, type LMCallConfig, type LMResponse, LMStudio, type LMStudioOptions, LRUCache, LabeledFewShot, type MCPAdapterOptions, type MCPTool, MCPToolAdapter, MIPRO, type MIPROOptions, type Message, type Metric, MockLM, MockRetriever, Module, MultiChainComparison, NativeReAct, Ollama, type OllamaOptions, OpenAI, type OpenAIOptions, Optimizer, OutputField, Parallel, PineconeRM, type PineconeRMOptions, Predict, Prediction, ProgramOfThought, QdrantRM, type QdrantRMOptions, ReAct, Refine, type RefineOptions, Retrieve, Retriever, Retry, SIMBA, type SIMBAOptions, Settings, type SettingsOptions, Signature, type SignatureMeta, type StreamChunk, Suggest, type TokenUsage, type Tool, type Trace, Tracker, type TrackerEvent, TypedChainOfThought, TypedPrediction, TypedPredictor, WeaviateRM, type WeaviateRMOptions, YouRM, type YouRMOptions, bleu, evaluate, exactMatch, f1, majority, passAtK, rouge, settings };
|