ax Generated TypeScript API reference. typescript api api/reference build/apidocs/Function.ax.md function ax

ax

Call Signature

TypeScript
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/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/template.ts#L113

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:

text
"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 values
  • json - Arbitrary JSON objects
  • date - Date in YYYY-MM-DD format
  • datetime - ISO 8601 datetime
  • dateRange - date range with start/end dates
  • datetimeRange - datetime range with start/end datetimes
  • code - Code blocks (preserves formatting)
  • image - Image input (for multimodal models)
  • audio - Audio input
  • class - 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:

text
"question: string 'The user question' -> answer: string 'A helpful response'"

Type Parameters

Type ParameterDefault type
T extends string-
ThoughtKey extends string"thought"

Parameters

ParameterTypeDescription
signatureTEither 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

TypeScript
const qa = ax('question: string -> answer: string');
const result = await qa.forward(ai, { question: 'What is TypeScript?' });
console.log(result.answer);
TypeScript
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'
TypeScript
const extractor = ax(`
  document: string ->
  summary: string,
  keywords: string[],
  wordCount: number
`);
const result = await extractor.forward(ai, { document: longText });
TypeScript
const solver = ax('problem: string -> solution: string', {
  thoughtFieldName: 'reasoning'
});
// Enable thinking in forward options to get step-by-step reasoning
TypeScript
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

TypeScript
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/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/template.ts#L128

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:

text
"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 values
  • json - Arbitrary JSON objects
  • date - Date in YYYY-MM-DD format
  • datetime - ISO 8601 datetime
  • dateRange - date range with start/end dates
  • datetimeRange - datetime range with start/end datetimes
  • code - Code blocks (preserves formatting)
  • image - Image input (for multimodal models)
  • audio - Audio input
  • class - 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:

text
"question: string 'The user question' -> answer: string 'A helpful response'"

Type Parameters

Type ParameterDefault type
TInput extends Record<string, any>-
TOutput extends Record<string, any>-
ThoughtKey extends string"thought"

Parameters

ParameterTypeDescription
signatureAxSignature<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

TypeScript
const qa = ax('question: string -> answer: string');
const result = await qa.forward(ai, { question: 'What is TypeScript?' });
console.log(result.answer);
TypeScript
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'
TypeScript
const extractor = ax(`
  document: string ->
  summary: string,
  keywords: string[],
  wordCount: number
`);
const result = await extractor.forward(ai, { document: longText });
TypeScript
const solver = ax('problem: string -> solution: string', {
  thoughtFieldName: 'reasoning'
});
// Enable thinking in forward options to get step-by-step reasoning
TypeScript
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

TypeScript
function ax(signature: Readonly<AxSignatureConfig>, options?: Readonly<AxProgramForwardOptions<any>>): AxGen<AxGenIn, AxGenOut>;

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/dsp/template.ts#L144

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:

text
"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 values
  • json - Arbitrary JSON objects
  • date - Date in YYYY-MM-DD format
  • datetime - ISO 8601 datetime
  • dateRange - date range with start/end dates
  • datetimeRange - datetime range with start/end datetimes
  • code - Code blocks (preserves formatting)
  • image - Image input (for multimodal models)
  • audio - Audio input
  • class - 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:

text
"question: string 'The user question' -> answer: string 'A helpful response'"

Parameters

ParameterTypeDescription
signatureReadonly<AxSignatureConfig>Either a signature string or a pre-built AxSignature object
options?Readonly<AxProgramForwardOptions<any>>Optional configuration for the generator

Returns

AxGen<AxGenIn, AxGenOut>

An AxGen instance that can be executed with .forward(ai, inputs)

Examples

TypeScript
const qa = ax('question: string -> answer: string');
const result = await qa.forward(ai, { question: 'What is TypeScript?' });
console.log(result.answer);
TypeScript
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'
TypeScript
const extractor = ax(`
  document: string ->
  summary: string,
  keywords: string[],
  wordCount: number
`);
const result = await extractor.forward(ai, { document: longText });
TypeScript
const solver = ax('problem: string -> solution: string', {
  thoughtFieldName: 'reasoning'
});
// Enable thinking in forward options to get step-by-step reasoning
TypeScript
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)
  }]
});
Docs