@ax-llm/ax 16.0.11 → 16.0.12
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/cli/index.mjs +265 -0
- package/index.cjs +42 -42
- package/index.cjs.map +1 -1
- package/index.d.cts +953 -28
- package/index.d.ts +953 -28
- package/index.global.js +54 -54
- package/index.global.js.map +1 -1
- package/index.js +42 -42
- package/index.js.map +1 -1
- package/package.json +7 -1
- package/scripts/postinstall.mjs +209 -0
- package/skills/ax-llm.md +1582 -0
package/index.d.cts
CHANGED
|
@@ -30,6 +30,8 @@ interface AxAPIConfig extends AxAPI, RequestValidation, ResponseValidation {
|
|
|
30
30
|
retry?: Partial<RetryConfig>;
|
|
31
31
|
abortSignal?: AbortSignal;
|
|
32
32
|
corsProxy?: string;
|
|
33
|
+
/** Whether to include request body in error messages. Defaults to true. Set to false when request may contain sensitive data or large base64 content. */
|
|
34
|
+
includeRequestBodyInErrors?: boolean;
|
|
33
35
|
}
|
|
34
36
|
declare class AxAIServiceError extends Error {
|
|
35
37
|
readonly url: string;
|
|
@@ -38,36 +40,37 @@ declare class AxAIServiceError extends Error {
|
|
|
38
40
|
readonly timestamp: string;
|
|
39
41
|
readonly errorId: string;
|
|
40
42
|
readonly context: Record<string, unknown>;
|
|
41
|
-
|
|
43
|
+
readonly includeRequestBodyInErrors: boolean;
|
|
44
|
+
constructor(message: string, url: string, requestBody: unknown, responseBody: unknown, context?: Record<string, unknown>, includeRequestBodyInErrors?: boolean);
|
|
42
45
|
toString(): string;
|
|
43
46
|
}
|
|
44
47
|
declare class AxAIServiceStatusError extends AxAIServiceError {
|
|
45
48
|
readonly status: number;
|
|
46
49
|
readonly statusText: string;
|
|
47
|
-
constructor(status: number, statusText: string, url: string, requestBody: unknown, responseBody: unknown, context?: Record<string, unknown>, retryCount?: number);
|
|
50
|
+
constructor(status: number, statusText: string, url: string, requestBody: unknown, responseBody: unknown, context?: Record<string, unknown>, retryCount?: number, includeRequestBodyInErrors?: boolean);
|
|
48
51
|
}
|
|
49
52
|
declare class AxAIServiceNetworkError extends AxAIServiceError {
|
|
50
53
|
readonly originalError: Error;
|
|
51
|
-
constructor(originalError: Error, url: string, requestBody: unknown, responseBody: unknown, context?: Record<string, unknown
|
|
54
|
+
constructor(originalError: Error, url: string, requestBody: unknown, responseBody: unknown, context?: Record<string, unknown>, includeRequestBodyInErrors?: boolean);
|
|
52
55
|
}
|
|
53
56
|
declare class AxAIServiceResponseError extends AxAIServiceError {
|
|
54
|
-
constructor(message: string, url: string, requestBody?: unknown, context?: Record<string, unknown
|
|
57
|
+
constructor(message: string, url: string, requestBody?: unknown, context?: Record<string, unknown>, includeRequestBodyInErrors?: boolean);
|
|
55
58
|
}
|
|
56
59
|
declare class AxAIServiceStreamTerminatedError extends AxAIServiceError {
|
|
57
60
|
readonly lastChunk?: unknown | undefined;
|
|
58
|
-
constructor(url: string, requestBody?: unknown, lastChunk?: unknown | undefined, context?: Record<string, unknown
|
|
61
|
+
constructor(url: string, requestBody?: unknown, lastChunk?: unknown | undefined, context?: Record<string, unknown>, includeRequestBodyInErrors?: boolean);
|
|
59
62
|
}
|
|
60
63
|
declare class AxAIServiceTimeoutError extends AxAIServiceError {
|
|
61
|
-
constructor(url: string, timeoutMs: number, requestBody?: unknown, context?: Record<string, unknown
|
|
64
|
+
constructor(url: string, timeoutMs: number, requestBody?: unknown, context?: Record<string, unknown>, includeRequestBodyInErrors?: boolean);
|
|
62
65
|
}
|
|
63
66
|
declare class AxTokenLimitError extends AxAIServiceStatusError {
|
|
64
|
-
constructor(status: number, statusText: string, url: string, requestBody: unknown, responseBody: unknown, context?: Record<string, unknown
|
|
67
|
+
constructor(status: number, statusText: string, url: string, requestBody: unknown, responseBody: unknown, context?: Record<string, unknown>, includeRequestBodyInErrors?: boolean);
|
|
65
68
|
}
|
|
66
69
|
declare class AxAIServiceAbortedError extends AxAIServiceError {
|
|
67
|
-
constructor(url: string, reason?: string, requestBody?: unknown, context?: Record<string, unknown
|
|
70
|
+
constructor(url: string, reason?: string, requestBody?: unknown, context?: Record<string, unknown>, includeRequestBodyInErrors?: boolean);
|
|
68
71
|
}
|
|
69
72
|
declare class AxAIServiceAuthenticationError extends AxAIServiceError {
|
|
70
|
-
constructor(url: string, requestBody: unknown, responseBody: unknown, context?: Record<string, unknown
|
|
73
|
+
constructor(url: string, requestBody: unknown, responseBody: unknown, context?: Record<string, unknown>, includeRequestBodyInErrors?: boolean);
|
|
71
74
|
}
|
|
72
75
|
declare class AxAIRefusalError extends Error {
|
|
73
76
|
readonly refusalMessage: string;
|
|
@@ -210,16 +213,126 @@ type AxTokenUsage = {
|
|
|
210
213
|
cacheReadTokens?: number;
|
|
211
214
|
serviceTier?: 'standard' | 'priority' | 'batch';
|
|
212
215
|
};
|
|
216
|
+
/**
|
|
217
|
+
* Configuration options for AI model behavior.
|
|
218
|
+
*
|
|
219
|
+
* These settings control how the model generates responses. They can be set
|
|
220
|
+
* as defaults when creating an AI instance, or overridden per-request.
|
|
221
|
+
*
|
|
222
|
+
* @example
|
|
223
|
+
* ```typescript
|
|
224
|
+
* const config: AxModelConfig = {
|
|
225
|
+
* maxTokens: 2000,
|
|
226
|
+
* temperature: 0.7,
|
|
227
|
+
* topP: 0.9
|
|
228
|
+
* };
|
|
229
|
+
* ```
|
|
230
|
+
*/
|
|
213
231
|
type AxModelConfig = {
|
|
232
|
+
/**
|
|
233
|
+
* Maximum number of tokens to generate in the response.
|
|
234
|
+
*
|
|
235
|
+
* **Token estimation guide:**
|
|
236
|
+
* - ~750 tokens ≈ 1 page of English text
|
|
237
|
+
* - ~100 tokens ≈ 75 words
|
|
238
|
+
* - ~4 characters ≈ 1 token (English)
|
|
239
|
+
*
|
|
240
|
+
* Set higher for long-form content (articles, code), lower for concise
|
|
241
|
+
* responses (classifications, short answers).
|
|
242
|
+
*
|
|
243
|
+
* @example 500 for short responses, 2000 for detailed explanations, 4000+ for long-form content
|
|
244
|
+
*/
|
|
214
245
|
maxTokens?: number;
|
|
246
|
+
/**
|
|
247
|
+
* Controls randomness in generation. Range: 0 to 2.
|
|
248
|
+
*
|
|
249
|
+
* **Use case guide:**
|
|
250
|
+
* - `0` - Deterministic, always picks most likely token. Best for factual Q&A,
|
|
251
|
+
* classification, code generation where consistency matters.
|
|
252
|
+
* - `0.3-0.5` - Low creativity. Good for structured outputs, summaries.
|
|
253
|
+
* - `0.7` - Balanced (default for most models). Good for general conversation.
|
|
254
|
+
* - `1.0` - High creativity. Good for brainstorming, creative writing.
|
|
255
|
+
* - `1.5-2.0` - Very high randomness. Often produces incoherent output.
|
|
256
|
+
*
|
|
257
|
+
* @default Varies by provider, typically 0.7-1.0
|
|
258
|
+
*/
|
|
215
259
|
temperature?: number;
|
|
260
|
+
/**
|
|
261
|
+
* Nucleus sampling: only consider tokens with cumulative probability >= topP.
|
|
262
|
+
* Range: 0 to 1.
|
|
263
|
+
*
|
|
264
|
+
* Lower values make output more focused and deterministic. Alternative to
|
|
265
|
+
* temperature for controlling randomness.
|
|
266
|
+
*
|
|
267
|
+
* **Recommendation:** Adjust either temperature OR topP, not both.
|
|
268
|
+
*
|
|
269
|
+
* @example 0.1 for focused output, 0.9 for diverse output
|
|
270
|
+
*/
|
|
216
271
|
topP?: number;
|
|
272
|
+
/**
|
|
273
|
+
* Only consider the top K most likely tokens at each step.
|
|
274
|
+
*
|
|
275
|
+
* Lower values (e.g., 10-40) make output more focused. Not supported by all
|
|
276
|
+
* providers (OpenAI doesn't support this; Anthropic, Google do).
|
|
277
|
+
*
|
|
278
|
+
* @example 40 for focused output, 100 for more variety
|
|
279
|
+
*/
|
|
217
280
|
topK?: number;
|
|
281
|
+
/**
|
|
282
|
+
* Penalizes tokens that have already appeared in the output.
|
|
283
|
+
* Range: -2.0 to 2.0.
|
|
284
|
+
*
|
|
285
|
+
* Positive values reduce repetition by penalizing tokens that have appeared
|
|
286
|
+
* at all, regardless of frequency. Useful for encouraging diverse vocabulary.
|
|
287
|
+
*
|
|
288
|
+
* - `0` - No penalty (default)
|
|
289
|
+
* - `0.5-1.0` - Mild penalty, reduces obvious repetition
|
|
290
|
+
* - `1.5-2.0` - Strong penalty, may hurt coherence
|
|
291
|
+
*
|
|
292
|
+
* @example 0.6 to reduce repetitive phrasing
|
|
293
|
+
*/
|
|
218
294
|
presencePenalty?: number;
|
|
295
|
+
/**
|
|
296
|
+
* Penalizes tokens based on how frequently they've appeared.
|
|
297
|
+
* Range: -2.0 to 2.0.
|
|
298
|
+
*
|
|
299
|
+
* Unlike presencePenalty, this scales with frequency: tokens that appear many
|
|
300
|
+
* times get penalized more. Useful for preventing the model from repeating
|
|
301
|
+
* the same phrases verbatim.
|
|
302
|
+
*
|
|
303
|
+
* @example 0.5 to discourage word/phrase repetition
|
|
304
|
+
*/
|
|
219
305
|
frequencyPenalty?: number;
|
|
306
|
+
/**
|
|
307
|
+
* Sequences that will stop generation when encountered.
|
|
308
|
+
*
|
|
309
|
+
* The model stops generating as soon as any stop sequence is produced.
|
|
310
|
+
* The stop sequence itself is NOT included in the output.
|
|
311
|
+
*
|
|
312
|
+
* @example ['\\n\\n', 'END', '---'] to stop at double newlines or markers
|
|
313
|
+
*/
|
|
220
314
|
stopSequences?: string[];
|
|
315
|
+
/**
|
|
316
|
+
* Similar to stopSequences, but the sequence IS included in the output.
|
|
317
|
+
*
|
|
318
|
+
* @example ['</answer>'] to include closing tag in output
|
|
319
|
+
*/
|
|
221
320
|
endSequences?: string[];
|
|
321
|
+
/**
|
|
322
|
+
* Enable streaming responses for real-time output.
|
|
323
|
+
*
|
|
324
|
+
* When true, the response is returned as a stream of chunks, allowing
|
|
325
|
+
* you to display partial results as they're generated.
|
|
326
|
+
*/
|
|
222
327
|
stream?: boolean;
|
|
328
|
+
/**
|
|
329
|
+
* Number of completions to generate for each prompt.
|
|
330
|
+
*
|
|
331
|
+
* Generates multiple independent responses. Useful with result pickers
|
|
332
|
+
* to select the best response. Increases cost proportionally.
|
|
333
|
+
*
|
|
334
|
+
* @example 3 to generate three alternatives and pick the best
|
|
335
|
+
*/
|
|
223
336
|
n?: number;
|
|
224
337
|
};
|
|
225
338
|
type AxFunctionHandler = (args?: any, extra?: Readonly<{
|
|
@@ -652,42 +765,168 @@ type AxContextCacheInfo = {
|
|
|
652
765
|
/** Hash of the cached content for validation */
|
|
653
766
|
contentHash?: string;
|
|
654
767
|
};
|
|
768
|
+
/**
|
|
769
|
+
* Runtime options for AI service requests.
|
|
770
|
+
*
|
|
771
|
+
* These options control how requests are made to the AI service, including
|
|
772
|
+
* debugging, rate limiting, streaming, function calling, and extended thinking.
|
|
773
|
+
*
|
|
774
|
+
* @example
|
|
775
|
+
* ```typescript
|
|
776
|
+
* const options: AxAIServiceOptions = {
|
|
777
|
+
* stream: true,
|
|
778
|
+
* thinkingTokenBudget: 'medium',
|
|
779
|
+
* debug: true
|
|
780
|
+
* };
|
|
781
|
+
* await gen.forward(ai, values, options);
|
|
782
|
+
* ```
|
|
783
|
+
*/
|
|
655
784
|
type AxAIServiceOptions = {
|
|
785
|
+
/**
|
|
786
|
+
* Enable debug logging for troubleshooting.
|
|
787
|
+
*
|
|
788
|
+
* When true, logs detailed information about prompts, responses, and
|
|
789
|
+
* the generation pipeline. Useful for understanding AI behavior.
|
|
790
|
+
*/
|
|
656
791
|
debug?: boolean;
|
|
792
|
+
/**
|
|
793
|
+
* Enable low-level HTTP request/response logging.
|
|
794
|
+
*
|
|
795
|
+
* More verbose than `debug`. Shows raw HTTP traffic including headers.
|
|
796
|
+
* Useful for debugging API issues.
|
|
797
|
+
*/
|
|
657
798
|
verbose?: boolean;
|
|
799
|
+
/** Custom rate limiter function to control request throughput. */
|
|
658
800
|
rateLimiter?: AxRateLimiterFunction;
|
|
801
|
+
/** Custom fetch implementation (useful for proxies or custom HTTP handling). */
|
|
659
802
|
fetch?: typeof fetch;
|
|
803
|
+
/** OpenTelemetry tracer for distributed tracing. */
|
|
660
804
|
tracer?: Tracer;
|
|
805
|
+
/** OpenTelemetry meter for metrics collection. */
|
|
661
806
|
meter?: Meter;
|
|
807
|
+
/**
|
|
808
|
+
* Request timeout in milliseconds.
|
|
809
|
+
*
|
|
810
|
+
* @default 300000 (5 minutes)
|
|
811
|
+
*/
|
|
662
812
|
timeout?: number;
|
|
813
|
+
/** Exclude message content from OpenTelemetry traces (for privacy). */
|
|
663
814
|
excludeContentFromTrace?: boolean;
|
|
815
|
+
/** AbortSignal for cancelling in-flight requests. */
|
|
664
816
|
abortSignal?: AbortSignal;
|
|
817
|
+
/** Custom logger function for debug output. */
|
|
665
818
|
logger?: AxLoggerFunction;
|
|
819
|
+
/** Session identifier for conversation tracking and memory isolation. */
|
|
666
820
|
sessionId?: string;
|
|
821
|
+
/** Hide system prompt in debug output (for cleaner logs). */
|
|
667
822
|
debugHideSystemPrompt?: boolean;
|
|
823
|
+
/** OpenTelemetry trace context for distributed tracing. */
|
|
668
824
|
traceContext?: Context;
|
|
825
|
+
/**
|
|
826
|
+
* Enable streaming responses.
|
|
827
|
+
*
|
|
828
|
+
* When true, the AI returns responses as a stream of chunks, enabling
|
|
829
|
+
* real-time display of generated text.
|
|
830
|
+
*/
|
|
669
831
|
stream?: boolean;
|
|
832
|
+
/**
|
|
833
|
+
* How to handle function/tool calling.
|
|
834
|
+
*
|
|
835
|
+
* - `'auto'` - Let the provider decide the best approach (default)
|
|
836
|
+
* - `'native'` - Use the provider's native function calling API. Fails if
|
|
837
|
+
* the model doesn't support it.
|
|
838
|
+
* - `'prompt'` - Simulate function calling via prompt engineering. Works with
|
|
839
|
+
* any model but may be less reliable.
|
|
840
|
+
*
|
|
841
|
+
* @default 'auto'
|
|
842
|
+
*/
|
|
670
843
|
functionCallMode?: 'auto' | 'native' | 'prompt';
|
|
844
|
+
/**
|
|
845
|
+
* Token budget for extended thinking (chain-of-thought reasoning).
|
|
846
|
+
*
|
|
847
|
+
* Extended thinking allows models to "think through" complex problems before
|
|
848
|
+
* responding. Higher budgets allow deeper reasoning but cost more.
|
|
849
|
+
*
|
|
850
|
+
* **Approximate token allocations:**
|
|
851
|
+
* - `'none'` - Disabled (default)
|
|
852
|
+
* - `'minimal'` - ~1,000 tokens (~750 words of thinking)
|
|
853
|
+
* - `'low'` - ~4,000 tokens
|
|
854
|
+
* - `'medium'` - ~10,000 tokens
|
|
855
|
+
* - `'high'` - ~20,000 tokens
|
|
856
|
+
* - `'highest'` - ~32,000+ tokens (provider maximum)
|
|
857
|
+
*
|
|
858
|
+
* **Provider support:**
|
|
859
|
+
* - Anthropic Claude: Full support with `claude-sonnet-4` and above
|
|
860
|
+
* - OpenAI: Supported with o1/o3 models (uses `reasoning_effort`)
|
|
861
|
+
* - Google: Supported with Gemini 2.0 Flash Thinking
|
|
862
|
+
* - DeepSeek: Supported with DeepSeek-R1
|
|
863
|
+
*
|
|
864
|
+
* @example
|
|
865
|
+
* ```typescript
|
|
866
|
+
* // Enable medium thinking for complex reasoning
|
|
867
|
+
* await gen.forward(ai, values, { thinkingTokenBudget: 'medium' });
|
|
868
|
+
* ```
|
|
869
|
+
*/
|
|
671
870
|
thinkingTokenBudget?: 'minimal' | 'low' | 'medium' | 'high' | 'highest' | 'none';
|
|
871
|
+
/**
|
|
872
|
+
* Include the model's thinking/reasoning in the output.
|
|
873
|
+
*
|
|
874
|
+
* When true and `thinkingTokenBudget` is set, the model's internal reasoning
|
|
875
|
+
* is included in the response. Useful for debugging and understanding AI behavior.
|
|
876
|
+
*
|
|
877
|
+
* @default false
|
|
878
|
+
*/
|
|
672
879
|
showThoughts?: boolean;
|
|
880
|
+
/**
|
|
881
|
+
* Hint to use a more capable (and expensive) model for complex tasks.
|
|
882
|
+
*
|
|
883
|
+
* Some providers offer tiered models. Setting this to 'yes' requests the
|
|
884
|
+
* higher-capability tier when available.
|
|
885
|
+
*/
|
|
673
886
|
useExpensiveModel?: 'yes';
|
|
887
|
+
/** Internal: Current step index for multi-step operations. */
|
|
674
888
|
stepIndex?: number;
|
|
889
|
+
/**
|
|
890
|
+
* CORS proxy URL for browser environments.
|
|
891
|
+
*
|
|
892
|
+
* When running in a browser, API calls may be blocked by CORS. Specify a
|
|
893
|
+
* proxy URL to route requests through.
|
|
894
|
+
*
|
|
895
|
+
* @example 'https://cors-anywhere.herokuapp.com/'
|
|
896
|
+
*/
|
|
675
897
|
corsProxy?: string;
|
|
898
|
+
/**
|
|
899
|
+
* Retry configuration for failed requests.
|
|
900
|
+
*
|
|
901
|
+
* Controls automatic retry behavior for transient errors (rate limits,
|
|
902
|
+
* timeouts, server errors).
|
|
903
|
+
*/
|
|
676
904
|
retry?: Partial<RetryConfig>;
|
|
677
905
|
/**
|
|
678
|
-
*
|
|
679
|
-
*
|
|
680
|
-
*
|
|
906
|
+
* Context caching options for large prompt prefixes.
|
|
907
|
+
*
|
|
908
|
+
* When enabled, large prompt prefixes can be cached for cost savings and
|
|
909
|
+
* lower latency on subsequent requests.
|
|
910
|
+
*
|
|
911
|
+
* **Currently supported by:** Google Gemini/Vertex AI
|
|
681
912
|
*/
|
|
682
913
|
contextCache?: AxContextCacheOptions;
|
|
683
914
|
/**
|
|
684
|
-
*
|
|
685
|
-
*
|
|
915
|
+
* Render examples/demos in the system prompt instead of as message pairs.
|
|
916
|
+
*
|
|
917
|
+
* - `false` (default) - Examples rendered as alternating user/assistant messages
|
|
918
|
+
* - `true` - Examples embedded in system prompt (legacy behavior)
|
|
919
|
+
*
|
|
920
|
+
* Message pair rendering generally produces better results.
|
|
686
921
|
*/
|
|
687
922
|
examplesInSystem?: boolean;
|
|
688
923
|
/**
|
|
689
|
-
* Custom labels
|
|
690
|
-
*
|
|
924
|
+
* Custom labels for OpenTelemetry metrics.
|
|
925
|
+
*
|
|
926
|
+
* These labels are merged with `axGlobals.customLabels` (service-level
|
|
927
|
+
* options override global settings).
|
|
928
|
+
*
|
|
929
|
+
* @example { environment: 'production', feature: 'search' }
|
|
691
930
|
*/
|
|
692
931
|
customLabels?: Record<string, string>;
|
|
693
932
|
};
|
|
@@ -932,20 +1171,364 @@ type ValidateNoMediaTypes<TFields> = {
|
|
|
932
1171
|
fields: ValidateNoMediaTypes<TNestedFields>;
|
|
933
1172
|
} : TFields[K] : TFields[K] : TFields[K];
|
|
934
1173
|
};
|
|
1174
|
+
/**
|
|
1175
|
+
* Fluent field builder for creating type-safe signature fields.
|
|
1176
|
+
*
|
|
1177
|
+
* The `f` object provides factory methods for all supported field types, each returning
|
|
1178
|
+
* a chainable builder that allows adding constraints and modifiers.
|
|
1179
|
+
*
|
|
1180
|
+
* **Basic Usage:**
|
|
1181
|
+
* When called as a function, `f()` returns a new `AxSignatureBuilder` for programmatic
|
|
1182
|
+
* signature construction. More commonly, use its type methods directly.
|
|
1183
|
+
*
|
|
1184
|
+
* **Type Methods:**
|
|
1185
|
+
* - `f.string(desc?)` - Text content
|
|
1186
|
+
* - `f.number(desc?)` - Numeric values
|
|
1187
|
+
* - `f.boolean(desc?)` - True/false values
|
|
1188
|
+
* - `f.json(desc?)` - Arbitrary JSON objects
|
|
1189
|
+
* - `f.datetime(desc?)` - ISO 8601 datetime strings
|
|
1190
|
+
* - `f.date(desc?)` - Date in YYYY-MM-DD format
|
|
1191
|
+
* - `f.class(options, desc?)` - Classification with predefined choices
|
|
1192
|
+
* - `f.image(desc?)` - Image input (multimodal)
|
|
1193
|
+
* - `f.audio(desc?)` - Audio input
|
|
1194
|
+
* - `f.file(desc?)` - File input
|
|
1195
|
+
* - `f.url(desc?)` - URL strings
|
|
1196
|
+
* - `f.email(desc?)` - Email addresses
|
|
1197
|
+
* - `f.code(language?, desc?)` - Code blocks
|
|
1198
|
+
* - `f.object(fields, desc?)` - Nested object with typed fields
|
|
1199
|
+
*
|
|
1200
|
+
* **Modifier Methods (chainable):**
|
|
1201
|
+
* - `.optional()` - Mark field as optional
|
|
1202
|
+
* - `.array(desc?)` - Convert to array of this type
|
|
1203
|
+
* - `.internal()` - Hide from final output (for intermediate reasoning)
|
|
1204
|
+
* - `.cache()` - Mark for context caching
|
|
1205
|
+
* - `.min(value)` - Minimum length (strings) or value (numbers)
|
|
1206
|
+
* - `.max(value)` - Maximum length (strings) or value (numbers)
|
|
1207
|
+
* - `.regex(pattern, desc)` - Regex validation for strings
|
|
1208
|
+
* - `.email()` - Email format validation for strings
|
|
1209
|
+
* - `.url()` - URL format validation for strings
|
|
1210
|
+
*
|
|
1211
|
+
* @example Basic field types
|
|
1212
|
+
* ```typescript
|
|
1213
|
+
* const sig = f()
|
|
1214
|
+
* .input('name', f.string('User name'))
|
|
1215
|
+
* .input('age', f.number('Age in years'))
|
|
1216
|
+
* .output('greeting', f.string('Personalized greeting'))
|
|
1217
|
+
* .build();
|
|
1218
|
+
* ```
|
|
1219
|
+
*
|
|
1220
|
+
* @example With constraints
|
|
1221
|
+
* ```typescript
|
|
1222
|
+
* const sig = f()
|
|
1223
|
+
* .input('email', f.string().email())
|
|
1224
|
+
* .input('score', f.number('Score between 0-100').min(0).max(100))
|
|
1225
|
+
* .input('tags', f.string('Tag').array('List of tags'))
|
|
1226
|
+
* .output('isValid', f.boolean())
|
|
1227
|
+
* .build();
|
|
1228
|
+
* ```
|
|
1229
|
+
*
|
|
1230
|
+
* @example Classification
|
|
1231
|
+
* ```typescript
|
|
1232
|
+
* const sig = f()
|
|
1233
|
+
* .input('text', f.string('Text to classify'))
|
|
1234
|
+
* .output('sentiment', f.class(['positive', 'negative', 'neutral'] as const))
|
|
1235
|
+
* .output('confidence', f.number().min(0).max(1))
|
|
1236
|
+
* .build();
|
|
1237
|
+
* ```
|
|
1238
|
+
*
|
|
1239
|
+
* @example Nested objects
|
|
1240
|
+
* ```typescript
|
|
1241
|
+
* const sig = f()
|
|
1242
|
+
* .input('query', f.string())
|
|
1243
|
+
* .output('result', f.object({
|
|
1244
|
+
* title: f.string('Article title'),
|
|
1245
|
+
* score: f.number('Relevance score'),
|
|
1246
|
+
* metadata: f.object({
|
|
1247
|
+
* author: f.string().optional(),
|
|
1248
|
+
* date: f.date()
|
|
1249
|
+
* })
|
|
1250
|
+
* }))
|
|
1251
|
+
* .build();
|
|
1252
|
+
* ```
|
|
1253
|
+
*
|
|
1254
|
+
* @example Optional and internal fields
|
|
1255
|
+
* ```typescript
|
|
1256
|
+
* const sig = f()
|
|
1257
|
+
* .input('context', f.string().optional())
|
|
1258
|
+
* .input('question', f.string())
|
|
1259
|
+
* .output('reasoning', f.string('Step-by-step thinking').internal())
|
|
1260
|
+
* .output('answer', f.string())
|
|
1261
|
+
* .build();
|
|
1262
|
+
* ```
|
|
1263
|
+
*/
|
|
935
1264
|
declare const f: (() => AxSignatureBuilder) & {
|
|
1265
|
+
/**
|
|
1266
|
+
* Creates a string field type.
|
|
1267
|
+
*
|
|
1268
|
+
* Strings are the default and most common field type. Use modifiers to add
|
|
1269
|
+
* validation constraints.
|
|
1270
|
+
*
|
|
1271
|
+
* @param desc - Optional description explaining the field's purpose
|
|
1272
|
+
* @returns A chainable field builder
|
|
1273
|
+
*
|
|
1274
|
+
* @example
|
|
1275
|
+
* ```typescript
|
|
1276
|
+
* f.string('User question')
|
|
1277
|
+
* f.string().min(10).max(1000) // Length constraints
|
|
1278
|
+
* f.string().email() // Email format
|
|
1279
|
+
* f.string().regex('^[A-Z]', 'Must start with uppercase')
|
|
1280
|
+
* ```
|
|
1281
|
+
*/
|
|
936
1282
|
string: (desc?: string) => AxFluentFieldType<"string", false, undefined, false, false, undefined, false>;
|
|
1283
|
+
/**
|
|
1284
|
+
* Creates a number field type.
|
|
1285
|
+
*
|
|
1286
|
+
* Numbers can be integers or floats. Use `.min()` and `.max()` to constrain the range.
|
|
1287
|
+
*
|
|
1288
|
+
* @param desc - Optional description explaining the field's purpose
|
|
1289
|
+
* @returns A chainable field builder
|
|
1290
|
+
*
|
|
1291
|
+
* @example
|
|
1292
|
+
* ```typescript
|
|
1293
|
+
* f.number('Age in years')
|
|
1294
|
+
* f.number().min(0).max(100) // Constrained range
|
|
1295
|
+
* f.number('Rating').min(1).max(5)
|
|
1296
|
+
* ```
|
|
1297
|
+
*/
|
|
937
1298
|
number: (desc?: string) => AxFluentFieldType<"number", false, undefined, false, false, undefined, false>;
|
|
1299
|
+
/**
|
|
1300
|
+
* Creates a boolean field type.
|
|
1301
|
+
*
|
|
1302
|
+
* Booleans represent true/false values. Useful for yes/no questions,
|
|
1303
|
+
* flags, and binary decisions.
|
|
1304
|
+
*
|
|
1305
|
+
* @param desc - Optional description explaining what true/false means
|
|
1306
|
+
* @returns A chainable field builder
|
|
1307
|
+
*
|
|
1308
|
+
* @example
|
|
1309
|
+
* ```typescript
|
|
1310
|
+
* f.boolean('Whether the text contains personally identifiable information')
|
|
1311
|
+
* f.boolean('Is the sentiment positive')
|
|
1312
|
+
* ```
|
|
1313
|
+
*/
|
|
938
1314
|
boolean: (desc?: string) => AxFluentFieldType<"boolean", false, undefined, false, false, undefined, false>;
|
|
1315
|
+
/**
|
|
1316
|
+
* Creates a JSON field type for arbitrary structured data.
|
|
1317
|
+
*
|
|
1318
|
+
* Use this when you need flexible object output without a predefined schema.
|
|
1319
|
+
* For structured data with known fields, prefer `f.object()` for type safety.
|
|
1320
|
+
*
|
|
1321
|
+
* @param desc - Optional description of the expected JSON structure
|
|
1322
|
+
* @returns A chainable field builder
|
|
1323
|
+
*
|
|
1324
|
+
* @example
|
|
1325
|
+
* ```typescript
|
|
1326
|
+
* f.json('Extracted entities as key-value pairs')
|
|
1327
|
+
* f.json('Configuration object')
|
|
1328
|
+
* ```
|
|
1329
|
+
*/
|
|
939
1330
|
json: (desc?: string) => AxFluentFieldType<"json", false, undefined, false, false, undefined, false>;
|
|
1331
|
+
/**
|
|
1332
|
+
* Creates a datetime field type for ISO 8601 timestamps.
|
|
1333
|
+
*
|
|
1334
|
+
* Values are formatted as full ISO 8601 datetime strings (e.g., "2024-01-15T14:30:00Z").
|
|
1335
|
+
* For date-only values, use `f.date()` instead.
|
|
1336
|
+
*
|
|
1337
|
+
* @param desc - Optional description explaining the datetime's purpose
|
|
1338
|
+
* @returns A chainable field builder
|
|
1339
|
+
*
|
|
1340
|
+
* @example
|
|
1341
|
+
* ```typescript
|
|
1342
|
+
* f.datetime('When the event occurred')
|
|
1343
|
+
* f.datetime('Appointment start time')
|
|
1344
|
+
* ```
|
|
1345
|
+
*/
|
|
940
1346
|
datetime: (desc?: string) => AxFluentFieldType<"datetime", false, undefined, false, false, undefined, false>;
|
|
1347
|
+
/**
|
|
1348
|
+
* Creates a date field type for YYYY-MM-DD formatted dates.
|
|
1349
|
+
*
|
|
1350
|
+
* Values are formatted as date strings without time components (e.g., "2024-01-15").
|
|
1351
|
+
* For datetime values with time, use `f.datetime()` instead.
|
|
1352
|
+
*
|
|
1353
|
+
* @param desc - Optional description explaining the date's purpose
|
|
1354
|
+
* @returns A chainable field builder
|
|
1355
|
+
*
|
|
1356
|
+
* @example
|
|
1357
|
+
* ```typescript
|
|
1358
|
+
* f.date('Date of birth')
|
|
1359
|
+
* f.date('Publication date')
|
|
1360
|
+
* ```
|
|
1361
|
+
*/
|
|
941
1362
|
date: (desc?: string) => AxFluentFieldType<"date", false, undefined, false, false, undefined, false>;
|
|
1363
|
+
/**
|
|
1364
|
+
* Creates a classification field type with predefined options.
|
|
1365
|
+
*
|
|
1366
|
+
* The AI will always return exactly one of the provided options.
|
|
1367
|
+
* Use `as const` for the options array to get literal type inference.
|
|
1368
|
+
*
|
|
1369
|
+
* @param options - Array of allowed values (use `as const` for type safety)
|
|
1370
|
+
* @param desc - Optional description of the classification task
|
|
1371
|
+
* @returns A chainable field builder
|
|
1372
|
+
*
|
|
1373
|
+
* @example
|
|
1374
|
+
* ```typescript
|
|
1375
|
+
* f.class(['positive', 'negative', 'neutral'] as const, 'Sentiment')
|
|
1376
|
+
* f.class(['bug', 'feature', 'question', 'docs'] as const, 'Issue type')
|
|
1377
|
+
* f.class(['low', 'medium', 'high', 'critical'] as const).optional()
|
|
1378
|
+
* ```
|
|
1379
|
+
*/
|
|
942
1380
|
class: <const TOptions extends readonly string[]>(options: TOptions, desc?: string) => AxFluentFieldType<"class", false, TOptions, false, false, undefined, false>;
|
|
1381
|
+
/**
|
|
1382
|
+
* Creates an image field type for multimodal inputs.
|
|
1383
|
+
*
|
|
1384
|
+
* Pass images as base64-encoded data URLs or URLs to external images.
|
|
1385
|
+
* Only supported as input fields with multimodal models (GPT-4V, Claude 3, Gemini, etc.).
|
|
1386
|
+
*
|
|
1387
|
+
* **Note:** Cannot be used in nested `f.object()` fields - only as top-level inputs.
|
|
1388
|
+
*
|
|
1389
|
+
* @param desc - Optional description of what the image contains or its purpose
|
|
1390
|
+
* @returns A chainable field builder
|
|
1391
|
+
*
|
|
1392
|
+
* @example
|
|
1393
|
+
* ```typescript
|
|
1394
|
+
* f.image('Product photo to analyze')
|
|
1395
|
+
* f.image('Screenshot of the UI bug')
|
|
1396
|
+
* f.image().array('Multiple images to compare')
|
|
1397
|
+
* ```
|
|
1398
|
+
*/
|
|
943
1399
|
image: (desc?: string) => AxFluentFieldType<"image", false, undefined, false, false, undefined, false>;
|
|
1400
|
+
/**
|
|
1401
|
+
* Creates an audio field type for audio inputs.
|
|
1402
|
+
*
|
|
1403
|
+
* Pass audio as base64-encoded data or URLs. Only supported as input fields
|
|
1404
|
+
* with models that support audio processing.
|
|
1405
|
+
*
|
|
1406
|
+
* **Note:** Cannot be used in nested `f.object()` fields - only as top-level inputs.
|
|
1407
|
+
*
|
|
1408
|
+
* @param desc - Optional description of the audio content
|
|
1409
|
+
* @returns A chainable field builder
|
|
1410
|
+
*
|
|
1411
|
+
* @example
|
|
1412
|
+
* ```typescript
|
|
1413
|
+
* f.audio('Voice recording to transcribe')
|
|
1414
|
+
* f.audio('Audio clip for analysis')
|
|
1415
|
+
* ```
|
|
1416
|
+
*/
|
|
944
1417
|
audio: (desc?: string) => AxFluentFieldType<"audio", false, undefined, false, false, undefined, false>;
|
|
1418
|
+
/**
|
|
1419
|
+
* Creates a file field type for document inputs.
|
|
1420
|
+
*
|
|
1421
|
+
* Pass files as base64-encoded data or file references. Only supported as input
|
|
1422
|
+
* fields with models that support file processing (PDFs, documents, etc.).
|
|
1423
|
+
*
|
|
1424
|
+
* **Note:** Cannot be used in nested `f.object()` fields - only as top-level inputs.
|
|
1425
|
+
*
|
|
1426
|
+
* @param desc - Optional description of the expected file content
|
|
1427
|
+
* @returns A chainable field builder
|
|
1428
|
+
*
|
|
1429
|
+
* @example
|
|
1430
|
+
* ```typescript
|
|
1431
|
+
* f.file('PDF document to summarize')
|
|
1432
|
+
* f.file('Resume to parse')
|
|
1433
|
+
* ```
|
|
1434
|
+
*/
|
|
945
1435
|
file: (desc?: string) => AxFluentFieldType<"file", false, undefined, false, false, undefined, false>;
|
|
1436
|
+
/**
|
|
1437
|
+
* Creates a URL field type with URI format validation.
|
|
1438
|
+
*
|
|
1439
|
+
* The AI will be instructed to return a valid URL. Use for outputs that
|
|
1440
|
+
* should be clickable links or API endpoints.
|
|
1441
|
+
*
|
|
1442
|
+
* @param desc - Optional description of what the URL should point to
|
|
1443
|
+
* @returns A chainable field builder
|
|
1444
|
+
*
|
|
1445
|
+
* @example
|
|
1446
|
+
* ```typescript
|
|
1447
|
+
* f.url('Link to the source')
|
|
1448
|
+
* f.url('API endpoint URL')
|
|
1449
|
+
* f.url().optional()
|
|
1450
|
+
* ```
|
|
1451
|
+
*/
|
|
946
1452
|
url: (desc?: string) => AxFluentFieldType<"url", false, undefined, false, false, undefined, false>;
|
|
1453
|
+
/**
|
|
1454
|
+
* Creates an email field type with email format validation.
|
|
1455
|
+
*
|
|
1456
|
+
* Shorthand for `f.string().email()`. The AI will be instructed to return
|
|
1457
|
+
* a valid email address format.
|
|
1458
|
+
*
|
|
1459
|
+
* @param desc - Optional description of whose email or for what purpose
|
|
1460
|
+
* @returns A chainable field builder
|
|
1461
|
+
*
|
|
1462
|
+
* @example
|
|
1463
|
+
* ```typescript
|
|
1464
|
+
* f.email('Contact email address')
|
|
1465
|
+
* f.email().optional()
|
|
1466
|
+
* ```
|
|
1467
|
+
*/
|
|
947
1468
|
email: (desc?: string) => AxFluentFieldType<"string", false, undefined, false, false, undefined, false>;
|
|
1469
|
+
/**
|
|
1470
|
+
* Creates a code field type for source code blocks.
|
|
1471
|
+
*
|
|
1472
|
+
* Code fields preserve formatting and are rendered in code blocks.
|
|
1473
|
+
* Optionally specify a language for syntax highlighting context.
|
|
1474
|
+
*
|
|
1475
|
+
* @param language - Optional programming language (e.g., 'typescript', 'python')
|
|
1476
|
+
* @param desc - Optional description of what the code should do
|
|
1477
|
+
* @returns A chainable field builder
|
|
1478
|
+
*
|
|
1479
|
+
* @example
|
|
1480
|
+
* ```typescript
|
|
1481
|
+
* f.code('typescript', 'Generated TypeScript function')
|
|
1482
|
+
* f.code('python', 'Python script to solve the problem')
|
|
1483
|
+
* f.code() // Language-agnostic code
|
|
1484
|
+
* ```
|
|
1485
|
+
*/
|
|
948
1486
|
code: (language?: string, desc?: string) => AxFluentFieldType<"code", false, undefined, false, false, undefined, false>;
|
|
1487
|
+
/**
|
|
1488
|
+
* Creates a nested object field type with typed properties.
|
|
1489
|
+
*
|
|
1490
|
+
* Use this when you need structured output with known fields. Each property
|
|
1491
|
+
* in the fields object should be created using `f.string()`, `f.number()`, etc.
|
|
1492
|
+
*
|
|
1493
|
+
* Objects can be nested to create complex hierarchical structures.
|
|
1494
|
+
*
|
|
1495
|
+
* **Restrictions:**
|
|
1496
|
+
* - Media types (`image`, `audio`, `file`) cannot be used in nested objects
|
|
1497
|
+
* - Deep nesting may reduce AI output quality
|
|
1498
|
+
*
|
|
1499
|
+
* @param fields - Object mapping field names to field types created with `f.*` methods
|
|
1500
|
+
* @param desc - Optional description of what the object represents
|
|
1501
|
+
* @returns A chainable field builder
|
|
1502
|
+
*
|
|
1503
|
+
* @example Simple object
|
|
1504
|
+
* ```typescript
|
|
1505
|
+
* f.object({
|
|
1506
|
+
* name: f.string('Person name'),
|
|
1507
|
+
* age: f.number('Age in years'),
|
|
1508
|
+
* isActive: f.boolean()
|
|
1509
|
+
* }, 'User profile')
|
|
1510
|
+
* ```
|
|
1511
|
+
*
|
|
1512
|
+
* @example Nested objects
|
|
1513
|
+
* ```typescript
|
|
1514
|
+
* f.object({
|
|
1515
|
+
* title: f.string(),
|
|
1516
|
+
* author: f.object({
|
|
1517
|
+
* name: f.string(),
|
|
1518
|
+
* email: f.email().optional()
|
|
1519
|
+
* }),
|
|
1520
|
+
* tags: f.string().array()
|
|
1521
|
+
* })
|
|
1522
|
+
* ```
|
|
1523
|
+
*
|
|
1524
|
+
* @example Array of objects
|
|
1525
|
+
* ```typescript
|
|
1526
|
+
* f.object({
|
|
1527
|
+
* item: f.string(),
|
|
1528
|
+
* quantity: f.number().min(1)
|
|
1529
|
+
* }).array('List of order items')
|
|
1530
|
+
* ```
|
|
1531
|
+
*/
|
|
949
1532
|
object: <TFields extends Record<string, AxFluentFieldInfo<any, any, any, any, any, any, any> | AxFluentFieldType<any, any, any, any, any, any, any>>>(fields: TFields & ValidateNoMediaTypes<TFields>, desc?: string) => AxFluentFieldType<"object", false, undefined, false, false, TFields, false>;
|
|
950
1533
|
};
|
|
951
1534
|
interface AxField {
|
|
@@ -1662,14 +2245,104 @@ interface AxParetoResult<OUT = any> extends AxOptimizerResult<OUT> {
|
|
|
1662
2245
|
paretoFrontSize: number;
|
|
1663
2246
|
convergenceMetrics?: Record<string, number>;
|
|
1664
2247
|
}
|
|
2248
|
+
/**
|
|
2249
|
+
* Interface for optimizing AI programs through automated prompt tuning.
|
|
2250
|
+
*
|
|
2251
|
+
* Optimizers improve program performance by finding optimal demonstrations (few-shot examples)
|
|
2252
|
+
* that guide the AI toward better outputs. The optimization process:
|
|
2253
|
+
*
|
|
2254
|
+
* 1. Runs the program on training examples
|
|
2255
|
+
* 2. Evaluates outputs using the metric function
|
|
2256
|
+
* 3. Selects high-scoring input/output pairs as demonstrations
|
|
2257
|
+
* 4. Iterates to find the best demonstration set
|
|
2258
|
+
*
|
|
2259
|
+
* @example Basic optimization
|
|
2260
|
+
* ```typescript
|
|
2261
|
+
* const optimizer = new AxBootstrapFewShot({ maxBootstrappedDemos: 4 });
|
|
2262
|
+
*
|
|
2263
|
+
* const result = await optimizer.compile(
|
|
2264
|
+
* program,
|
|
2265
|
+
* trainingExamples,
|
|
2266
|
+
* ({ prediction, example }) => prediction.answer === example.expectedAnswer ? 1 : 0
|
|
2267
|
+
* );
|
|
2268
|
+
*
|
|
2269
|
+
* // Apply optimized demos to program
|
|
2270
|
+
* program.setDemos(result.demos);
|
|
2271
|
+
* ```
|
|
2272
|
+
*/
|
|
1665
2273
|
interface AxOptimizer {
|
|
1666
2274
|
/**
|
|
1667
|
-
* Optimize a program using the provided metric function
|
|
1668
|
-
*
|
|
1669
|
-
*
|
|
1670
|
-
*
|
|
1671
|
-
*
|
|
1672
|
-
*
|
|
2275
|
+
* Optimize a program using the provided training examples and metric function.
|
|
2276
|
+
*
|
|
2277
|
+
* The optimizer runs the program on examples, scores the outputs, and builds
|
|
2278
|
+
* a set of demonstrations that improve program performance. The process is
|
|
2279
|
+
* automatic - you provide the data and metric, the optimizer does the rest.
|
|
2280
|
+
*
|
|
2281
|
+
* **Metric Function:**
|
|
2282
|
+
* The metric function evaluates how well the program's output matches expectations.
|
|
2283
|
+
* It receives the prediction and original example, and should return a score
|
|
2284
|
+
* between 0 (worst) and 1 (best).
|
|
2285
|
+
*
|
|
2286
|
+
* @param program - The AxGen program to optimize. The optimizer will call
|
|
2287
|
+
* `.forward()` on this program during training.
|
|
2288
|
+
*
|
|
2289
|
+
* @param examples - Training examples with input values. Examples are automatically
|
|
2290
|
+
* split into training and validation sets. Each example should have the input
|
|
2291
|
+
* fields required by the program's signature.
|
|
2292
|
+
*
|
|
2293
|
+
* @param metricFn - Function that scores program outputs. Called with:
|
|
2294
|
+
* - `prediction`: The program's output for this example
|
|
2295
|
+
* - `example`: The original input example (useful if it contains expected outputs)
|
|
2296
|
+
* - Returns: Number between 0 (bad) and 1 (perfect)
|
|
2297
|
+
*
|
|
2298
|
+
* @param options - Optional configuration:
|
|
2299
|
+
* - `ai`: AI service to use (required if not set on program)
|
|
2300
|
+
* - `valSet`: Custom validation set (otherwise auto-split from examples)
|
|
2301
|
+
* - `trainSplit`: Fraction of examples for training (default: 0.8)
|
|
2302
|
+
*
|
|
2303
|
+
* @returns Promise resolving to optimization results including:
|
|
2304
|
+
* - `demos`: Optimized demonstrations to use with the program
|
|
2305
|
+
* - `stats`: Training/validation scores and iteration counts
|
|
2306
|
+
*
|
|
2307
|
+
* @example Exact match metric
|
|
2308
|
+
* ```typescript
|
|
2309
|
+
* const result = await optimizer.compile(
|
|
2310
|
+
* qa,
|
|
2311
|
+
* examples,
|
|
2312
|
+
* ({ prediction, example }) => {
|
|
2313
|
+
* return prediction.answer.toLowerCase() === example.expectedAnswer.toLowerCase() ? 1 : 0;
|
|
2314
|
+
* }
|
|
2315
|
+
* );
|
|
2316
|
+
* ```
|
|
2317
|
+
*
|
|
2318
|
+
* @example Semantic similarity metric
|
|
2319
|
+
* ```typescript
|
|
2320
|
+
* const result = await optimizer.compile(
|
|
2321
|
+
* summarizer,
|
|
2322
|
+
* examples,
|
|
2323
|
+
* async ({ prediction, example }) => {
|
|
2324
|
+
* const similarity = await computeCosineSimilarity(
|
|
2325
|
+
* await embed(prediction.summary),
|
|
2326
|
+
* await embed(example.referenceSummary)
|
|
2327
|
+
* );
|
|
2328
|
+
* return similarity; // 0-1 based on embedding similarity
|
|
2329
|
+
* }
|
|
2330
|
+
* );
|
|
2331
|
+
* ```
|
|
2332
|
+
*
|
|
2333
|
+
* @example Partial credit metric
|
|
2334
|
+
* ```typescript
|
|
2335
|
+
* const result = await optimizer.compile(
|
|
2336
|
+
* extractor,
|
|
2337
|
+
* examples,
|
|
2338
|
+
* ({ prediction, example }) => {
|
|
2339
|
+
* const expectedKeywords = example.keywords;
|
|
2340
|
+
* const foundKeywords = prediction.keywords;
|
|
2341
|
+
* const matches = expectedKeywords.filter(k => foundKeywords.includes(k));
|
|
2342
|
+
* return matches.length / expectedKeywords.length; // Fraction found
|
|
2343
|
+
* }
|
|
2344
|
+
* );
|
|
2345
|
+
* ```
|
|
1673
2346
|
*/
|
|
1674
2347
|
compile<IN, OUT extends AxGenOut>(program: Readonly<AxGen<IN, OUT>>, examples: readonly AxTypedExample<IN>[], metricFn: AxMetricFn, options?: AxCompileOptions): Promise<AxOptimizerResult<OUT>>;
|
|
1675
2348
|
/**
|
|
@@ -2114,6 +2787,97 @@ declare class AxGen<IN = any, OUT extends AxGenOut = any> extends AxProgram<IN,
|
|
|
2114
2787
|
*/
|
|
2115
2788
|
private validateObjectFields;
|
|
2116
2789
|
_forward1(ai: Readonly<AxAIService>, values: IN | AxMessage<IN>[], options: Readonly<AxProgramForwardOptions<any>>): AxGenStreamingOut<OUT>;
|
|
2790
|
+
/**
|
|
2791
|
+
* Executes the generator with the given AI service and input values.
|
|
2792
|
+
*
|
|
2793
|
+
* This is the main entry point for running an AI generation. The execution pipeline:
|
|
2794
|
+
* 1. **Validate** - Check input values match the signature
|
|
2795
|
+
* 2. **Render** - Build the prompt from signature, examples, and inputs
|
|
2796
|
+
* 3. **Call** - Send the request to the AI service
|
|
2797
|
+
* 4. **Parse** - Extract structured outputs from the response
|
|
2798
|
+
* 5. **Assert** - Validate outputs and retry with error correction if needed
|
|
2799
|
+
*
|
|
2800
|
+
* @param ai - The AI service instance to use (created via `ai()` factory)
|
|
2801
|
+
* @param values - Input values matching the signature's input fields, or an array of
|
|
2802
|
+
* `AxMessage` objects for multi-turn conversations
|
|
2803
|
+
* @param options - Optional execution configuration
|
|
2804
|
+
*
|
|
2805
|
+
* @param options.model - Override the default model for this request
|
|
2806
|
+
* @param options.maxTokens - Maximum tokens in the response. Rule of thumb: ~750 tokens ≈ 1 page
|
|
2807
|
+
* of English text. Set higher for long-form content, lower for concise responses.
|
|
2808
|
+
* @param options.temperature - Controls randomness in generation (0-2):
|
|
2809
|
+
* - `0` - Deterministic, always picks most likely token (best for factual tasks)
|
|
2810
|
+
* - `0.3-0.7` - Balanced creativity (good for most tasks)
|
|
2811
|
+
* - `1.0+` - High creativity (good for brainstorming, creative writing)
|
|
2812
|
+
* - `2.0` - Maximum randomness (often incoherent)
|
|
2813
|
+
* @param options.thinkingTokenBudget - Enable extended thinking for complex reasoning:
|
|
2814
|
+
* - `'none'` - Disabled (default)
|
|
2815
|
+
* - `'minimal'` - ~1K tokens of thinking
|
|
2816
|
+
* - `'low'` - ~4K tokens
|
|
2817
|
+
* - `'medium'` - ~10K tokens
|
|
2818
|
+
* - `'high'` - ~20K tokens
|
|
2819
|
+
* - `'highest'` - ~32K+ tokens (provider maximum)
|
|
2820
|
+
* @param options.stream - Enable streaming responses for real-time output
|
|
2821
|
+
* @param options.functions - Array of function tools the AI can call
|
|
2822
|
+
* @param options.functionCallMode - How to handle function calling:
|
|
2823
|
+
* - `'auto'` - Let the provider decide (default)
|
|
2824
|
+
* - `'native'` - Force native function calling (if supported)
|
|
2825
|
+
* - `'prompt'` - Simulate via prompt engineering (for models without native support)
|
|
2826
|
+
* @param options.mem - Memory instance for conversation history
|
|
2827
|
+
* @param options.sessionId - Session identifier for memory isolation
|
|
2828
|
+
* @param options.maxRetries - Maximum error correction attempts (default: 3)
|
|
2829
|
+
* @param options.maxSteps - Maximum function call iterations (default: 10)
|
|
2830
|
+
* @param options.debug - Enable debug logging
|
|
2831
|
+
*
|
|
2832
|
+
* @returns Promise resolving to the output values matching the signature's output fields
|
|
2833
|
+
*
|
|
2834
|
+
* @throws {AxValidationError} When input values don't match the signature
|
|
2835
|
+
* @throws {AxAssertionError} When output parsing/validation fails after all retries
|
|
2836
|
+
* @throws {AxAIServiceError} When the AI service request fails
|
|
2837
|
+
*
|
|
2838
|
+
* @example Basic usage
|
|
2839
|
+
* ```typescript
|
|
2840
|
+
* const gen = ax('question: string -> answer: string');
|
|
2841
|
+
* const result = await gen.forward(ai, { question: 'What is 2+2?' });
|
|
2842
|
+
* console.log(result.answer); // "4"
|
|
2843
|
+
* ```
|
|
2844
|
+
*
|
|
2845
|
+
* @example With configuration
|
|
2846
|
+
* ```typescript
|
|
2847
|
+
* const result = await gen.forward(ai, { question: 'Explain quantum computing' }, {
|
|
2848
|
+
* maxTokens: 2000,
|
|
2849
|
+
* temperature: 0.3,
|
|
2850
|
+
* stream: true
|
|
2851
|
+
* });
|
|
2852
|
+
* ```
|
|
2853
|
+
*
|
|
2854
|
+
* @example Multi-turn conversation
|
|
2855
|
+
* ```typescript
|
|
2856
|
+
* const mem = new AxMemory();
|
|
2857
|
+
* const chat = ax('message: string -> reply: string');
|
|
2858
|
+
*
|
|
2859
|
+
* await chat.forward(ai, { message: 'Hi, my name is Alice' }, { mem });
|
|
2860
|
+
* const result = await chat.forward(ai, { message: 'What is my name?' }, { mem });
|
|
2861
|
+
* // result.reply will reference "Alice" from conversation history
|
|
2862
|
+
* ```
|
|
2863
|
+
*
|
|
2864
|
+
* @example With function calling
|
|
2865
|
+
* ```typescript
|
|
2866
|
+
* const result = await gen.forward(ai, values, {
|
|
2867
|
+
* functions: [{
|
|
2868
|
+
* name: 'getWeather',
|
|
2869
|
+
* description: 'Get current weather for a city',
|
|
2870
|
+
* parameters: {
|
|
2871
|
+
* type: 'object',
|
|
2872
|
+
* properties: { city: { type: 'string', description: 'City name' } },
|
|
2873
|
+
* required: ['city']
|
|
2874
|
+
* },
|
|
2875
|
+
* func: async ({ city }) => fetchWeather(city)
|
|
2876
|
+
* }],
|
|
2877
|
+
* maxSteps: 5
|
|
2878
|
+
* });
|
|
2879
|
+
* ```
|
|
2880
|
+
*/
|
|
2117
2881
|
forward<T extends Readonly<AxAIService>>(ai: T, values: IN | AxMessage<IN>[], options?: Readonly<AxProgramForwardOptionsWithModels<T>>): Promise<OUT>;
|
|
2118
2882
|
streamingForward<T extends Readonly<AxAIService>>(ai: T, values: IN | AxMessage<IN>[], options?: Readonly<AxProgramStreamingForwardOptionsWithModels<T>>): AxGenStreamingOut<OUT>;
|
|
2119
2883
|
setExamples(examples: Readonly<AxProgramExamples<IN, OUT>>, options?: Readonly<AxSetExamplesOptions>): void;
|
|
@@ -5807,17 +6571,80 @@ type InferTModelKey<T> = T extends {
|
|
|
5807
6571
|
models: infer M;
|
|
5808
6572
|
} ? ExtractModelKeysAndValues<M> : string;
|
|
5809
6573
|
/**
|
|
5810
|
-
* Factory function for creating
|
|
5811
|
-
* This is the recommended way to create AxAI instances instead of using the constructor.
|
|
6574
|
+
* Factory function for creating AI service instances with full type safety.
|
|
5812
6575
|
*
|
|
5813
|
-
*
|
|
5814
|
-
*
|
|
6576
|
+
* This is the recommended way to create AI instances. It automatically selects
|
|
6577
|
+
* the appropriate provider implementation based on the `name` field and provides
|
|
6578
|
+
* type-safe access to provider-specific models.
|
|
5815
6579
|
*
|
|
5816
|
-
*
|
|
6580
|
+
* **Supported Providers:**
|
|
6581
|
+
* - `'openai'` - OpenAI (GPT-4, GPT-4o, o1, o3, etc.)
|
|
6582
|
+
* - `'openai-responses'` - OpenAI Responses API (for web search, file search)
|
|
6583
|
+
* - `'anthropic'` - Anthropic (Claude 3.5 Sonnet, Claude 3 Opus, etc.)
|
|
6584
|
+
* - `'google-gemini'` - Google (Gemini 1.5 Pro, Gemini 2.0 Flash, etc.)
|
|
6585
|
+
* - `'azure-openai'` - Azure OpenAI Service
|
|
6586
|
+
* - `'groq'` - Groq (Llama, Mixtral with fast inference)
|
|
6587
|
+
* - `'cohere'` - Cohere (Command R+, embeddings)
|
|
6588
|
+
* - `'mistral'` - Mistral AI (Mistral Large, Codestral)
|
|
6589
|
+
* - `'deepseek'` - DeepSeek (DeepSeek-V3, DeepSeek-R1)
|
|
6590
|
+
* - `'together'` - Together AI (various open models)
|
|
6591
|
+
* - `'openrouter'` - OpenRouter (unified API for many providers)
|
|
6592
|
+
* - `'ollama'` - Ollama (local models)
|
|
6593
|
+
* - `'huggingface'` - Hugging Face Inference API
|
|
6594
|
+
* - `'reka'` - Reka AI
|
|
6595
|
+
* - `'grok'` - xAI Grok
|
|
6596
|
+
* - `'webllm'` - WebLLM (browser-based inference)
|
|
6597
|
+
*
|
|
6598
|
+
* @param options - Provider-specific configuration. Must include `name` to identify the provider.
|
|
6599
|
+
* @param options.name - The provider identifier (see list above)
|
|
6600
|
+
* @param options.apiKey - API key for the provider (not needed for Ollama/WebLLM)
|
|
6601
|
+
* @param options.config - Optional default model configuration (maxTokens, temperature, etc.)
|
|
6602
|
+
* @param options.models - Optional custom model aliases for type-safe model selection
|
|
6603
|
+
*
|
|
6604
|
+
* @returns A configured AI service instance ready for chat completions and embeddings
|
|
6605
|
+
*
|
|
6606
|
+
* @see {@link AxModelConfig} for model configuration options
|
|
6607
|
+
* @see {@link AxAIServiceOptions} for runtime options like streaming and function calling
|
|
6608
|
+
*
|
|
6609
|
+
* @example Basic OpenAI setup
|
|
5817
6610
|
* ```typescript
|
|
5818
6611
|
* const ai = ai({
|
|
5819
6612
|
* name: 'openai',
|
|
5820
|
-
* apiKey: process.env.
|
|
6613
|
+
* apiKey: process.env.OPENAI_API_KEY
|
|
6614
|
+
* });
|
|
6615
|
+
* ```
|
|
6616
|
+
*
|
|
6617
|
+
* @example Anthropic with custom defaults
|
|
6618
|
+
* ```typescript
|
|
6619
|
+
* const ai = ai({
|
|
6620
|
+
* name: 'anthropic',
|
|
6621
|
+
* apiKey: process.env.ANTHROPIC_API_KEY,
|
|
6622
|
+
* config: {
|
|
6623
|
+
* model: 'claude-sonnet-4-20250514',
|
|
6624
|
+
* maxTokens: 4096,
|
|
6625
|
+
* temperature: 0.7
|
|
6626
|
+
* }
|
|
6627
|
+
* });
|
|
6628
|
+
* ```
|
|
6629
|
+
*
|
|
6630
|
+
* @example Google Gemini with model aliases
|
|
6631
|
+
* ```typescript
|
|
6632
|
+
* const ai = ai({
|
|
6633
|
+
* name: 'google-gemini',
|
|
6634
|
+
* apiKey: process.env.GOOGLE_API_KEY,
|
|
6635
|
+
* models: [
|
|
6636
|
+
* { key: 'fast', model: 'gemini-2.0-flash' },
|
|
6637
|
+
* { key: 'smart', model: 'gemini-1.5-pro' }
|
|
6638
|
+
* ]
|
|
6639
|
+
* });
|
|
6640
|
+
* // Now use ai with model: 'fast' or model: 'smart'
|
|
6641
|
+
* ```
|
|
6642
|
+
*
|
|
6643
|
+
* @example Local models with Ollama
|
|
6644
|
+
* ```typescript
|
|
6645
|
+
* const ai = ai({
|
|
6646
|
+
* name: 'ollama',
|
|
6647
|
+
* config: { model: 'llama3.2' }
|
|
5821
6648
|
* });
|
|
5822
6649
|
* ```
|
|
5823
6650
|
*/
|
|
@@ -7246,7 +8073,105 @@ declare const AxStringUtil: {
|
|
|
7246
8073
|
batchArray: <T>(arr: readonly T[], size: number) => T[][];
|
|
7247
8074
|
};
|
|
7248
8075
|
|
|
8076
|
+
/**
|
|
8077
|
+
* Creates a type-safe signature from a string template.
|
|
8078
|
+
*
|
|
8079
|
+
* @param signature - The signature string in the format `"inputFields -> outputFields"`
|
|
8080
|
+
* @returns A typed AxSignature instance
|
|
8081
|
+
*
|
|
8082
|
+
* @example
|
|
8083
|
+
* ```typescript
|
|
8084
|
+
* const sig = s('question: string -> answer: string');
|
|
8085
|
+
* ```
|
|
8086
|
+
*/
|
|
7249
8087
|
declare function s<const T extends string>(signature: T): AxSignature<ParseSignature<T>['inputs'], ParseSignature<T>['outputs']>;
|
|
8088
|
+
/**
|
|
8089
|
+
* Creates a type-safe AI generator from a signature string or AxSignature object.
|
|
8090
|
+
*
|
|
8091
|
+
* This is the primary way to define AI-powered functions in Ax. The signature string
|
|
8092
|
+
* declares input and output fields with their types, which are then used to generate
|
|
8093
|
+
* prompts and parse responses.
|
|
8094
|
+
*
|
|
8095
|
+
* **Signature String Format:**
|
|
8096
|
+
* ```
|
|
8097
|
+
* "inputField1: type, inputField2: type -> outputField1: type, outputField2: type"
|
|
8098
|
+
* ```
|
|
8099
|
+
*
|
|
8100
|
+
* **Supported Field Types:**
|
|
8101
|
+
* - `string` - Text content (default if no type specified)
|
|
8102
|
+
* - `number` - Numeric values (integers or floats)
|
|
8103
|
+
* - `boolean` - True/false values
|
|
8104
|
+
* - `json` - Arbitrary JSON objects
|
|
8105
|
+
* - `date` - Date in YYYY-MM-DD format
|
|
8106
|
+
* - `datetime` - ISO 8601 datetime
|
|
8107
|
+
* - `code` - Code blocks (preserves formatting)
|
|
8108
|
+
* - `image` - Image input (for multimodal models)
|
|
8109
|
+
* - `audio` - Audio input
|
|
8110
|
+
* - `class` - Classification with predefined options: `class(option1, option2, option3)`
|
|
8111
|
+
*
|
|
8112
|
+
* **Type Modifiers:**
|
|
8113
|
+
* - `[]` suffix - Array of values: `tags: string[]`
|
|
8114
|
+
* - `?` suffix - Optional field: `context?: string`
|
|
8115
|
+
* - `!` prefix - Internal field (hidden from output): `!reasoning: string`
|
|
8116
|
+
*
|
|
8117
|
+
* **Field Descriptions:**
|
|
8118
|
+
* Add descriptions after the type using a string literal:
|
|
8119
|
+
* ```
|
|
8120
|
+
* "question: string 'The user question' -> answer: string 'A helpful response'"
|
|
8121
|
+
* ```
|
|
8122
|
+
*
|
|
8123
|
+
* @param signature - Either a signature string or a pre-built AxSignature object
|
|
8124
|
+
* @param options - Optional configuration for the generator
|
|
8125
|
+
* @param options.thoughtFieldName - Custom name for chain-of-thought field (default: 'thought')
|
|
8126
|
+
*
|
|
8127
|
+
* @returns An AxGen instance that can be executed with `.forward(ai, inputs)`
|
|
8128
|
+
*
|
|
8129
|
+
* @example Simple question-answering
|
|
8130
|
+
* ```typescript
|
|
8131
|
+
* const qa = ax('question: string -> answer: string');
|
|
8132
|
+
* const result = await qa.forward(ai, { question: 'What is TypeScript?' });
|
|
8133
|
+
* console.log(result.answer);
|
|
8134
|
+
* ```
|
|
8135
|
+
*
|
|
8136
|
+
* @example Classification with predefined options
|
|
8137
|
+
* ```typescript
|
|
8138
|
+
* const classifier = ax('text: string -> sentiment: class(positive, negative, neutral)');
|
|
8139
|
+
* const result = await classifier.forward(ai, { text: 'I love this!' });
|
|
8140
|
+
* console.log(result.sentiment); // 'positive'
|
|
8141
|
+
* ```
|
|
8142
|
+
*
|
|
8143
|
+
* @example Multiple outputs with arrays
|
|
8144
|
+
* ```typescript
|
|
8145
|
+
* const extractor = ax(`
|
|
8146
|
+
* document: string ->
|
|
8147
|
+
* summary: string,
|
|
8148
|
+
* keywords: string[],
|
|
8149
|
+
* wordCount: number
|
|
8150
|
+
* `);
|
|
8151
|
+
* const result = await extractor.forward(ai, { document: longText });
|
|
8152
|
+
* ```
|
|
8153
|
+
*
|
|
8154
|
+
* @example With chain-of-thought reasoning
|
|
8155
|
+
* ```typescript
|
|
8156
|
+
* const solver = ax('problem: string -> solution: string', {
|
|
8157
|
+
* thoughtFieldName: 'reasoning'
|
|
8158
|
+
* });
|
|
8159
|
+
* // Enable thinking in forward options to get step-by-step reasoning
|
|
8160
|
+
* ```
|
|
8161
|
+
*
|
|
8162
|
+
* @example Using function tools
|
|
8163
|
+
* ```typescript
|
|
8164
|
+
* const agent = ax('query: string -> response: string');
|
|
8165
|
+
* const result = await agent.forward(ai, { query: 'What is 25 * 4?' }, {
|
|
8166
|
+
* functions: [{
|
|
8167
|
+
* name: 'calculate',
|
|
8168
|
+
* description: 'Perform math calculations',
|
|
8169
|
+
* parameters: { type: 'object', properties: { expression: { type: 'string' } } },
|
|
8170
|
+
* func: ({ expression }) => eval(expression)
|
|
8171
|
+
* }]
|
|
8172
|
+
* });
|
|
8173
|
+
* ```
|
|
8174
|
+
*/
|
|
7250
8175
|
declare function ax<const T extends string, ThoughtKey extends string = 'thought'>(signature: T, options?: Readonly<AxProgramForwardOptions<any> & {
|
|
7251
8176
|
thoughtFieldName?: ThoughtKey;
|
|
7252
8177
|
}>): AxGen<ParseSignature<T>['inputs'], ParseSignature<T>['outputs'] & (string extends ThoughtKey ? {
|