ax
Call Signature
function ax<T, ThoughtKey>(signature: T, options?: Readonly<AxAIServiceOptions & object & object>): AxGen<ParseSignature<T>["inputs"], ParseSignature<T>["outputs"] & string extends ThoughtKey ? object : { [P in string]?: string }>;
Defined in: https://github.com/ax-llm/ax/blob/05ff5bd88d050f7ba85a3fcc6eb0ed2975ad7d51/src/ax/dsp/template.ts#L110
Creates a type-safe AI generator from a signature string or AxSignature object.
This is the primary way to define AI-powered functions in Ax. The signature string declares input and output fields with their types, which are then used to generate prompts and parse responses.
Signature String Format:
"inputField1: type, inputField2: type -> outputField1: type, outputField2: type"
Supported Field Types:
string- Text content (default if no type specified)number- Numeric values (integers or floats)boolean- True/false valuesjson- Arbitrary JSON objectsdate- Date in YYYY-MM-DD formatdatetime- ISO 8601 datetimecode- Code blocks (preserves formatting)image- Image input (for multimodal models)audio- Audio inputclass- Classification with predefined options:class(option1, option2, option3)
Type Modifiers:
[]suffix - Array of values:tags: string[]?suffix - Optional field:context?: string!prefix - Internal field (hidden from output):!reasoning: string
Field Descriptions: Add descriptions after the type using a string literal:
"question: string 'The user question' -> answer: string 'A helpful response'"
Type Parameters
| Type Parameter | Default type |
|---|---|
T extends string | - |
ThoughtKey extends string | "thought" |
Parameters
| Parameter | Type | Description |
|---|---|---|
signature | T | Either a signature string or a pre-built AxSignature object |
options? | Readonly<AxAIServiceOptions & object & object> | Optional configuration for the generator |
Returns
AxGen<ParseSignature<T>["inputs"], ParseSignature<T>["outputs"] & string extends ThoughtKey ? object : { [P in string]?: string }>
An AxGen instance that can be executed with .forward(ai, inputs)
Examples
const qa = ax('question: string -> answer: string');
const result = await qa.forward(ai, { question: 'What is TypeScript?' });
console.log(result.answer);
const classifier = ax('text: string -> sentiment: class(positive, negative, neutral)');
const result = await classifier.forward(ai, { text: 'I love this!' });
console.log(result.sentiment); // 'positive'
const extractor = ax(`
document: string ->
summary: string,
keywords: string[],
wordCount: number
`);
const result = await extractor.forward(ai, { document: longText });
const solver = ax('problem: string -> solution: string', {
thoughtFieldName: 'reasoning'
});
// Enable thinking in forward options to get step-by-step reasoning
const agent = ax('query: string -> response: string');
const result = await agent.forward(ai, { query: 'What is 25 * 4?' }, {
functions: [{
name: 'calculate',
description: 'Perform math calculations',
parameters: { type: 'object', properties: { expression: { type: 'string' } } },
func: ({ expression }) => eval(expression)
}]
});
Call Signature
function ax<TInput, TOutput, ThoughtKey>(signature: AxSignature<TInput, TOutput>, options?: Readonly<AxAIServiceOptions & object & object>): AxGen<TInput, TOutput & string extends ThoughtKey ? object : { [P in string]?: string }>;
Defined in: https://github.com/ax-llm/ax/blob/05ff5bd88d050f7ba85a3fcc6eb0ed2975ad7d51/src/ax/dsp/template.ts#L125
Creates a type-safe AI generator from a signature string or AxSignature object.
This is the primary way to define AI-powered functions in Ax. The signature string declares input and output fields with their types, which are then used to generate prompts and parse responses.
Signature String Format:
"inputField1: type, inputField2: type -> outputField1: type, outputField2: type"
Supported Field Types:
string- Text content (default if no type specified)number- Numeric values (integers or floats)boolean- True/false valuesjson- Arbitrary JSON objectsdate- Date in YYYY-MM-DD formatdatetime- ISO 8601 datetimecode- Code blocks (preserves formatting)image- Image input (for multimodal models)audio- Audio inputclass- Classification with predefined options:class(option1, option2, option3)
Type Modifiers:
[]suffix - Array of values:tags: string[]?suffix - Optional field:context?: string!prefix - Internal field (hidden from output):!reasoning: string
Field Descriptions: Add descriptions after the type using a string literal:
"question: string 'The user question' -> answer: string 'A helpful response'"
Type Parameters
| Type Parameter | Default type |
|---|---|
TInput extends Record<string, any> | - |
TOutput extends Record<string, any> | - |
ThoughtKey extends string | "thought" |
Parameters
| Parameter | Type | Description |
|---|---|---|
signature | AxSignature<TInput, TOutput> | Either a signature string or a pre-built AxSignature object |
options? | Readonly<AxAIServiceOptions & object & object> | Optional configuration for the generator |
Returns
AxGen<TInput, TOutput & string extends ThoughtKey ? object : { [P in string]?: string }>
An AxGen instance that can be executed with .forward(ai, inputs)
Examples
const qa = ax('question: string -> answer: string');
const result = await qa.forward(ai, { question: 'What is TypeScript?' });
console.log(result.answer);
const classifier = ax('text: string -> sentiment: class(positive, negative, neutral)');
const result = await classifier.forward(ai, { text: 'I love this!' });
console.log(result.sentiment); // 'positive'
const extractor = ax(`
document: string ->
summary: string,
keywords: string[],
wordCount: number
`);
const result = await extractor.forward(ai, { document: longText });
const solver = ax('problem: string -> solution: string', {
thoughtFieldName: 'reasoning'
});
// Enable thinking in forward options to get step-by-step reasoning
const agent = ax('query: string -> response: string');
const result = await agent.forward(ai, { query: 'What is 25 * 4?' }, {
functions: [{
name: 'calculate',
description: 'Perform math calculations',
parameters: { type: 'object', properties: { expression: { type: 'string' } } },
func: ({ expression }) => eval(expression)
}]
});