https://github.com/ax-llm/ax
The "official" unofficial DSPy framework. Build LLM powered agents and other workflows, based on the Stanford DSP paper.
https://github.com/ax-llm/ax
ai anthropic claude cohere gemini google google-gemini gpt-4 javascript large-language-models llm nodejs ollama openai opensource rag together-compute typescript vectordb
Last synced: 15 days ago
JSON representation
The "official" unofficial DSPy framework. Build LLM powered agents and other workflows, based on the Stanford DSP paper.
- Host: GitHub
- URL: https://github.com/ax-llm/ax
- Owner: ax-llm
- License: apache-2.0
- Created: 2023-02-23T19:21:40.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2025-04-03T16:27:00.000Z (10 months ago)
- Last Synced: 2025-04-04T22:02:28.833Z (10 months ago)
- Topics: ai, anthropic, claude, cohere, gemini, google, google-gemini, gpt-4, javascript, large-language-models, llm, nodejs, ollama, openai, opensource, rag, together-compute, typescript, vectordb
- Language: TypeScript
- Homepage: http://axllm.dev
- Size: 20.6 MB
- Stars: 1,370
- Watchers: 17
- Forks: 101
- Open Issues: 19
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
- awesome-ChatGPT-repositories - ax - The unofficial DSPy framework. Build LLM powered Agents and "Agentic workflows" based on the Stanford DSP paper. (NLP)
- StarryDivineSky - ax-llm/ax - > "outputField:type"`的提示签名,自动生成类型安全的提示,并支持多种数据类型输出。 (A01_文本生成_文本对话 / 大语言对话模型及数据)
README
# Ax: Build Reliable AI Apps in TypeScript
Stop wrestling with prompts. Start shipping AI features.
Ax brings DSPy's approach to TypeScript – describe what you want, and let the framework handle the rest. Production-ready, type-safe, works with all major LLMs.
[](https://www.npmjs.com/package/@ax-llm/ax)
[](https://twitter.com/dosco)
[](https://discord.gg/DSHg3dU7dW)
## The Problem
Building with LLMs is painful. You write prompts, test them, they break. You switch providers, everything needs rewriting. You add validation, error handling, retries – suddenly you're maintaining infrastructure instead of shipping features.
## The Solution
Define what goes in and what comes out. Ax handles the rest.
```typescript
import { ai, ax } from "@ax-llm/ax";
const llm = ai({ name: "openai", apiKey: process.env.OPENAI_APIKEY });
const classifier = ax(
'review:string -> sentiment:class "positive, negative, neutral"',
);
const result = await classifier.forward(llm, {
review: "This product is amazing!",
});
console.log(result.sentiment); // "positive"
```
No prompt engineering. No trial and error. Works with GPT-4, Claude, Gemini, or any LLM.
## Why Ax
**Write once, run anywhere.** Switch between OpenAI, Anthropic, Google, or 15+ providers with one line. No rewrites.
**Ship faster.** Stop tweaking prompts. Define inputs and outputs. The framework generates optimal prompts automatically.
**Production-ready.** Built-in streaming, validation, error handling, observability. Used in production handling millions of requests.
**Gets smarter.** Train your programs with examples. Watch accuracy improve automatically. No ML expertise needed.
## Examples
### Extract structured data
```typescript
const extractor = ax(`
customerEmail:string, currentDate:datetime ->
priority:class "high, normal, low",
sentiment:class "positive, negative, neutral",
ticketNumber?:number,
nextSteps:string[],
estimatedResponseTime:string
`);
const result = await extractor.forward(llm, {
customerEmail: "Order #12345 hasn't arrived. Need this resolved immediately!",
currentDate: new Date(),
});
```
### Complex nested objects
```typescript
import { f, ax } from "@ax-llm/ax";
const productExtractor = f()
.input("productPage", f.string())
.output("product", f.object({
name: f.string(),
price: f.number(),
specs: f.object({
dimensions: f.object({
width: f.number(),
height: f.number()
}),
materials: f.array(f.string())
}),
reviews: f.array(f.object({
rating: f.number(),
comment: f.string()
}))
}))
.build();
const generator = ax(productExtractor);
const result = await generator.forward(llm, { productPage: "..." });
// Full TypeScript inference
console.log(result.product.specs.dimensions.width);
console.log(result.product.reviews[0].comment);
```
### Validation and constraints
```typescript
const userRegistration = f()
.input("userData", f.string())
.output("user", f.object({
username: f.string().min(3).max(20),
email: f.string().email(),
age: f.number().min(18).max(120),
password: f.string().min(8).regex("^(?=.*[A-Za-z])(?=.*\\d)", "Must contain letter and digit"),
bio: f.string().max(500).optional(),
website: f.string().url().optional(),
}))
.build();
```
Available constraints: `.min(n)`, `.max(n)`, `.email()`, `.url()`, `.date()`, `.datetime()`, `.regex(pattern, description)`, `.optional()`
Validation runs on both input and output. Automatic retry with corrections on validation errors.
### Agents with tools (ReAct pattern)
```typescript
const assistant = ax(
"question:string -> answer:string",
{
functions: [
{ name: "getCurrentWeather", func: weatherAPI },
{ name: "searchNews", func: newsAPI },
],
},
);
const result = await assistant.forward(llm, {
question: "What's the weather in Tokyo and any news about it?",
});
```
### Multi-modal (images, audio)
```typescript
const analyzer = ax(`
image:image, question:string ->
description:string,
mainColors:string[],
category:class "electronics, clothing, food, other",
estimatedPrice:string
`);
```
## Install
```bash
npm install @ax-llm/ax
```
Additional packages:
```bash
# AWS Bedrock provider
npm install @ax-llm/ax-ai-aws-bedrock
# Vercel AI SDK v5 integration
npm install @ax-llm/ax-ai-sdk-provider
# Tools: MCP stdio transport, JS interpreter
npm install @ax-llm/ax-tools
```
## Features
- **15+ LLM Providers** – OpenAI, Anthropic, Google, Mistral, Ollama, and more
- **Type-safe** – Full TypeScript support with auto-completion
- **Streaming** – Real-time responses with validation
- **Multi-modal** – Images, audio, text in the same signature
- **Optimization** – Automatic prompt tuning with MiPRO, ACE, GEPA
- **Observability** – OpenTelemetry tracing built-in
- **Workflows** – Compose complex pipelines with AxFlow
- **RAG** – Multi-hop retrieval with quality loops
- **Agents** – Tools and multi-agent collaboration
- **Zero dependencies** – Lightweight, fast, reliable
## Documentation
**Get Started**
- [Quick Start Guide](https://github.com/ax-llm/ax/blob/main/docs/QUICKSTART.md) – Set up in 5 minutes
- [Examples Guide](https://github.com/ax-llm/ax/blob/main/docs/EXAMPLES.md) – Comprehensive examples
- [DSPy Concepts](https://github.com/ax-llm/ax/blob/main/docs/DSPY.md) – Understanding the approach
- [Signatures Guide](https://github.com/ax-llm/ax/blob/main/docs/SIGNATURES.md) – Type-safe signature design
**Deep Dives**
- [AI Providers](https://github.com/ax-llm/ax/blob/main/docs/AI.md) – All providers, AWS Bedrock, Vercel AI SDK
- [AxFlow Workflows](https://github.com/ax-llm/ax/blob/main/docs/AXFLOW.md) – Build complex AI systems
- [Optimization (MiPRO, ACE, GEPA)](https://github.com/ax-llm/ax/blob/main/docs/OPTIMIZE.md) – Make programs smarter
- [Advanced RAG](https://github.com/ax-llm/ax/blob/main/docs/AXRAG.md) – Production search and retrieval
## Run Examples
```bash
OPENAI_APIKEY=your-key npm run tsx ./src/examples/[example-name].ts
```
Core examples: `extract.ts`, `react.ts`, `agent.ts`, `streaming1.ts`, `multi-modal.ts`
Production patterns: `customer-support.ts`, `food-search.ts`, `ace-train-inference.ts`, `ax-flow-enhanced-demo.ts`
[View all 70+ examples](src/examples/)
## Community
- [Twitter](https://twitter.com/dosco) – Updates
- [Discord](https://discord.gg/DSHg3dU7dW) – Help and discussion
- [GitHub](https://github.com/ax-llm/ax) – Star the project
- [DeepWiki](https://deepwiki.com/ax-llm/ax) – AI-powered docs
## Production Ready
- Battle-tested in production
- Stable minor versions
- Comprehensive test coverage
- OpenTelemetry built-in
- TypeScript first
## Contributors
- Author: [@dosco](https://github.com/dosco)
- GEPA and ACE optimizers: [@monotykamary](https://github.com/monotykamary)
## License
Apache 2.0
---
```bash
npm install @ax-llm/ax
```