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

ai

TypeScript
function ai<T>(options: T): AxAI<InferTModelKey<T>>;

Defined in: https://github.com/ax-llm/ax/blob/5b28f9093bb70863b59459bb6df5062d005bce41/src/ax/ai/wrap.ts#L172

Factory function for creating AI service instances with full type safety.

This is the recommended way to create AI instances. It automatically selects the appropriate provider implementation based on the name field and provides type-safe access to provider-specific models.

Supported Providers:

  • 'openai' - OpenAI (GPT-4, GPT-4o, o1, o3, etc.)
  • 'openai-responses' - OpenAI Responses API (for web search, file search)
  • 'anthropic' - Anthropic (Claude 3.5 Sonnet, Claude 3 Opus, etc.)
  • 'google-gemini' - Google (Gemini 1.5 Pro, Gemini 2.0 Flash, etc.)
  • 'azure-openai' - Azure OpenAI Service
  • 'cohere' - Cohere (Command R+, embeddings)
  • 'mistral' - Mistral AI (Mistral Large, Codestral)
  • 'deepseek' - DeepSeek (DeepSeek-V4-Flash, DeepSeek-V4-Pro)
  • 'reka' - Reka AI
  • 'grok' - xAI Grok

See

Type Parameters

Type Parameter
T extends AxAIArgs<any>

Parameters

ParameterTypeDescription
optionsTProvider-specific configuration. Must include name to identify the provider.

Returns

AxAI<InferTModelKey<T>>

A configured AI service instance ready for chat completions and embeddings

Examples

TypeScript
const ai = ai({
  name: 'openai',
  apiKey: process.env.OPENAI_API_KEY
});
TypeScript
const ai = ai({
  name: 'anthropic',
  apiKey: process.env.ANTHROPIC_API_KEY,
  config: {
    model: 'claude-sonnet-4-20250514',
    maxTokens: 4096,
    temperature: 0.7
  }
});
TypeScript
const ai = ai({
  name: 'google-gemini',
  apiKey: process.env.GOOGLE_API_KEY,
  models: [
    { key: 'fast', model: 'gemini-2.0-flash' },
    { key: 'smart', model: 'gemini-1.5-pro' }
  ]
});
// Now use ai with model: 'fast' or model: 'smart'
TypeScript
const ai = ai({
  name: 'openai',
  apiKey: process.env.PROVIDER_API_KEY,
  apiURL: 'https://example.com/v1',
  config: { model: 'provider/model-name' }
});
Docs