Skip to content

Commit a7e3ddd

Browse files
committed
feat: mipro v2
fix: issues with exaamples
1 parent 79a0c7c commit a7e3ddd

17 files changed

+2537
-1632
lines changed

.cspell/project-words.txt

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ APIURL
77
asig
88
astro
99
astrojs
10+
bootstrapper
1011
Buena
1112
chrono
1213
chunker
@@ -24,6 +25,7 @@ dedup
2425
deepseek
2526
distractor
2627
dosco
28+
fewshot
2729
fres
2830
frontmatter
2931
fstate
@@ -43,8 +45,11 @@ logprobs
4345
Logprobs
4446
Macbook
4547
MCPHTTP
48+
minibatch
4649
minilm
50+
mipro
4751
Mixtral
52+
modelcontextprotocol
4853
modelinfo
4954
multiservice
5055
nanos
@@ -82,17 +87,20 @@ Sukiyabashi
8287
superintelligence
8388
textembedding
8489
tika
90+
trainset
8591
tsimp
8692
tsup
8793
upsert
8894
upserted
8995
usecase
9096
uuidv
97+
valset
9198
vectordb
9299
vectorize
93100
Vikram
94101
weaviate
95102
Weaviate
96103
xlarge
97104
xstate
98-
modelcontextprotocol
105+
minibatching
106+
minibatches

.vscode/settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
"typescript.enablePromptUseWorkspaceTsdk": true,
77
"typescript.format.enable": true,
88
"typescript.tsserver.watchOptions": {},
9-
"js/ts.implicitProjectConfig.target": "ESNext"
9+
"js/ts.implicitProjectConfig.target": "ESNext",
10+
"biome.enabled": false
1011
}

README.md

+135-4
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ const res = await gen.forward({ text });
558558
}
559559
```
560560

561-
## Tuning the prompts (programs)
561+
## Tuning the prompts (Basic)
562562

563563
You can tune your prompts using a larger model to help them run more efficiently and give you better results. This is done by using an optimizer like `AxBootstrapFewShot` with and examples from the popular `HotPotQA` dataset. The optimizer generates demonstrations `demos` which when used with the prompt help improve its efficiency.
564564

@@ -628,6 +628,137 @@ const res = await program.forward({
628628
console.log(res);
629629
```
630630

631+
## Tuning the prompts (Advanced, Mipro v2)
632+
633+
MiPRO v2 is an advanced prompt optimization framework that uses Bayesian optimization to automatically find the best instructions, demonstrations, and examples for your LLM programs. By systematically exploring different prompt configurations, MiPRO v2 helps maximize model performance without manual tuning.
634+
635+
### Key Features
636+
637+
- **Instruction optimization**: Automatically generates and tests multiple instruction candidates
638+
- **Few-shot example selection**: Finds optimal demonstrations from your dataset
639+
- **Smart Bayesian optimization**: Uses UCB (Upper Confidence Bound) strategy to efficiently explore configurations
640+
- **Early stopping**: Stops optimization when improvements plateau to save compute
641+
- **Program and data-aware**: Considers program structure and dataset characteristics
642+
643+
### Basic Usage
644+
645+
```typescript
646+
import { AxAI, AxChainOfThought, AxMiPRO } from '@ax-llm/ax'
647+
648+
// 1. Setup your AI service
649+
const ai = new AxAI({
650+
name: 'google-gemini',
651+
apiKey: process.env.GOOGLE_APIKEY
652+
})
653+
654+
// 2. Create your program
655+
const program = new AxChainOfThought(`input -> output`)
656+
657+
// 3. Configure the optimizer
658+
const optimizer = new AxMiPRO({
659+
ai,
660+
program,
661+
examples: trainingData, // Your training examples
662+
options: {
663+
numTrials: 20, // Number of configurations to try
664+
auto: 'medium' // Optimization level
665+
}
666+
})
667+
668+
// 4. Define your evaluation metric
669+
const metricFn = ({ prediction, example }) => {
670+
return prediction.output === example.output
671+
}
672+
673+
// 5. Run the optimization
674+
const optimizedProgram = await optimizer.compile(metricFn, {
675+
valset: validationData // Optional validation set
676+
})
677+
678+
// 6. Use the optimized program
679+
const result = await optimizedProgram.forward(ai, { input: "test input" })
680+
```
681+
682+
### Configuration Options
683+
684+
MiPRO v2 provides extensive configuration options:
685+
686+
| Option | Description | Default |
687+
|--------|-------------|---------|
688+
| `numCandidates` | Number of instruction candidates to generate | 5 |
689+
| `numTrials` | Number of optimization trials | 30 |
690+
| `maxBootstrappedDemos` | Maximum number of bootstrapped demonstrations | 3 |
691+
| `maxLabeledDemos` | Maximum number of labeled examples | 4 |
692+
| `minibatch` | Use minibatching for faster evaluation | true |
693+
| `minibatchSize` | Size of evaluation minibatches | 25 |
694+
| `earlyStoppingTrials` | Stop if no improvement after N trials | 5 |
695+
| `minImprovementThreshold` | Minimum score improvement threshold | 0.01 |
696+
| `programAwareProposer` | Use program structure for better proposals | true |
697+
| `dataAwareProposer` | Consider dataset characteristics | true |
698+
| `verbose` | Show detailed optimization progress | false |
699+
700+
### Optimization Levels
701+
702+
You can quickly configure optimization intensity with the `auto` parameter:
703+
704+
```typescript
705+
// Light optimization (faster, less thorough)
706+
const optimizedProgram = await optimizer.compile(metricFn, { auto: 'light' })
707+
708+
// Medium optimization (balanced)
709+
const optimizedProgram = await optimizer.compile(metricFn, { auto: 'medium' })
710+
711+
// Heavy optimization (slower, more thorough)
712+
const optimizedProgram = await optimizer.compile(metricFn, { auto: 'heavy' })
713+
```
714+
715+
### Advanced Example: Sentiment Analysis
716+
717+
```typescript
718+
// Create sentiment analysis program
719+
const classifyProgram = new AxChainOfThought<
720+
{ productReview: string },
721+
{ label: string }
722+
>(`productReview -> label:string "positive" or "negative"`)
723+
724+
// Configure optimizer with advanced settings
725+
const optimizer = new AxMiPRO({
726+
ai,
727+
program: classifyProgram,
728+
examples: trainingData,
729+
options: {
730+
numCandidates: 3,
731+
numTrials: 10,
732+
maxBootstrappedDemos: 2,
733+
maxLabeledDemos: 3,
734+
earlyStoppingTrials: 3,
735+
programAwareProposer: true,
736+
dataAwareProposer: true,
737+
verbose: true
738+
}
739+
})
740+
741+
// Run optimization and save the result
742+
const optimizedProgram = await optimizer.compile(metricFn, {
743+
valset: validationData
744+
})
745+
746+
// Save configuration for future use
747+
const programConfig = JSON.stringify(optimizedProgram, null, 2)
748+
await fs.promises.writeFile('./optimized-config.json', programConfig)
749+
```
750+
751+
### How It Works
752+
753+
MiPRO v2 works through these steps:
754+
1. Generates various instruction candidates
755+
2. Bootstraps few-shot examples from your data
756+
3. Selects labeled examples directly from your dataset
757+
4. Uses Bayesian optimization to find the optimal combination
758+
5. Applies the best configuration to your program
759+
760+
By exploring the space of possible prompt configurations and systematically measuring performance, MiPRO v2 delivers optimized prompts that maximize your model's effectiveness.
761+
631762
## Built-in Functions
632763

633764
| Function | Name | Description |
@@ -658,8 +789,6 @@ OPENAI_APIKEY=openai_key npm run tsx ./src/examples/marketing.ts
658789
| rag-docs.ts | Convert PDF to text and embed for rag search |
659790
| react.ts | Use function calling and reasoning to answer questions |
660791
| agent.ts | Agent framework, agents can use other agents, tools etc |
661-
| qna-tune.ts | Use an optimizer to improve prompt efficiency |
662-
| qna-use-tuned.ts | Use the optimized tuned prompts |
663792
| streaming1.ts | Output fields validation while streaming |
664793
| streaming2.ts | Per output field validation while streaming |
665794
| streaming3.ts | End-to-end streaming example `streamingForward()` |
@@ -671,7 +800,9 @@ OPENAI_APIKEY=openai_key npm run tsx ./src/examples/marketing.ts
671800
| simple-classify.ts | Use a simple classifier to classify stuff |
672801
| mcp-client-memory.ts | Example of using an MCP server for memory with Ax |
673802
| mcp-client-blender.ts | Example of using an MCP server for Blender with Ax |
674-
803+
| tune-bootstrap.ts | Use bootstrap optimizer to improve prompt efficiency |
804+
| tune-mipro.ts | Use mipro v2 optimizer to improve prompt efficiency |
805+
| tune-usage.ts | Use the optimized tuned prompts |
675806

676807
## Our Goal
677808

0 commit comments

Comments
 (0)