| name | mistral-migration-deep-dive |
| description | Execute Mistral AI major migrations and re-architecture strategies.
Use when migrating to Mistral AI from another provider, performing major refactoring,
or re-platforming existing AI integrations to Mistral AI.
Trigger with phrases like "migrate to mistral", "mistral migration",
"switch to mistral", "mistral replatform", "openai to mistral".
|
| allowed-tools | Read, Write, Edit, Bash(npm:*), Bash(node:*), Bash(kubectl:*) |
| version | 1.0.0 |
| license | MIT |
| author | Jeremy Longshore <jeremy@intentsolutions.io> |
Mistral AI Migration Deep Dive
Overview
Comprehensive guide for migrating to Mistral AI from other providers or major version upgrades.
Prerequisites
- Current system documentation
- Mistral AI SDK installed
- Feature flag infrastructure
- Rollback strategy tested
Migration Types
| Type | Complexity | Duration | Risk |
|---|
| Fresh install | Low | Days | Low |
| OpenAI to Mistral | Medium | Weeks | Medium |
| Multi-provider | Medium | Weeks | Medium |
| Full replatform | High | Months | High |
Instructions
Step 1: Pre-Migration Assessment
echo "=== Pre-Migration Assessment ==="
find . -name "*.ts" -o -name "*.py" | xargs grep -l "openai\|anthropic\|ai" > ai-files.txt
echo "Files with AI code: $(wc -l < ai-files.txt)"
grep -r "chat.completions\|createChatCompletion" src/ --include="*.ts" | wc -l
npm list openai @anthropic-ai/sdk 2>/dev/null || echo "No existing AI SDKs"
interface MigrationAssessment {
currentProvider: string;
integrationPoints: number;
features: string[];
estimatedEffort: 'low' | 'medium' | 'high';
risks: string[];
}
async function assessMigration(): Promise<MigrationAssessment> {
const files = await glob('src/**/*.{ts,js}');
const features = new Set<string>();
let integrationPoints = 0;
for (const file of files) {
const content = await fs.readFile(file, 'utf-8');
if (/chat\.completions|createChatCompletion/i.test(content)) {
features.add('chat');
integrationPoints++;
}
if (/embeddings\.create|createEmbedding/i.test(content)) {
features.add('embeddings');
integrationPoints++;
}
(.(content)) {
features.();
integrationPoints++;
}
(.(content)) {
features.();
integrationPoints++;
}
}
{
: (files),
integrationPoints,
: .(features),
: integrationPoints > ? : integrationPoints > ? : ,
: (features),
};
}
Step 2: Create Adapter Layer
export interface Message {
role: 'system' | 'user' | 'assistant';
content: string;
}
export interface ChatOptions {
model?: string;
temperature?: number;
maxTokens?: number;
stream?: boolean;
}
export interface ChatResponse {
content: string;
usage?: {
inputTokens: number;
outputTokens: number;
};
}
export interface AIAdapter {
chat(messages: Message[], options?: ChatOptions): Promise<ChatResponse>;
chatStream(messages: Message[], options?: ChatOptions): AsyncGenerator<string>;
embed(text: string | string[]): <[][]>;
}
Step 3: Implement OpenAI Adapter (Current)
import OpenAI from 'openai';
import { AIAdapter, Message, ChatOptions, ChatResponse } from '../adapter';
export class OpenAIAdapter implements AIAdapter {
private client: OpenAI;
constructor() {
this.client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
}
async chat(messages: Message[], options?: ChatOptions): Promise<ChatResponse> {
const response = await this.client.chat.completions.create({
model: options?.model || 'gpt-3.5-turbo',
messages,
temperature: options?.temperature,
max_tokens: options?.maxTokens,
});
{
: response.[]?.?. || ,
: response. ? {
: response..,
: response..,
} : ,
};
}
*(: [], ?: ): <> {
stream = ....({
: options?. || ,
messages,
: ,
});
( chunk stream) {
content = chunk.[]?.?.;
(content) content;
}
}
(: | []): <[][]> {
input = .(text) ? text : [text];
response = ...({
: ,
input,
});
response..( d.);
}
}
Step 4: Implement Mistral Adapter (Target)
import Mistral from '@mistralai/mistralai';
import { AIAdapter, Message, ChatOptions, ChatResponse } from '../adapter';
export class MistralAdapter implements AIAdapter {
private client: Mistral;
constructor() {
this.client = new Mistral({ apiKey: process.env.MISTRAL_API_KEY });
}
async chat(messages: Message[], options?: ChatOptions): Promise<ChatResponse> {
const response = await this.client.chat.complete({
model: options?.model || 'mistral-small-latest',
messages,
temperature: options?.temperature,
maxTokens: options?.maxTokens,
});
return {
: response.?.[]?.?. || ,
: response. ? {
: response.. || ,
: response.. || ,
} : ,
};
}
*(: [], ?: ): <> {
stream = ...({
: options?. || ,
messages,
: options?.,
: options?.,
});
( event stream) {
content = event.?.?.[]?.?.;
(content) content;
}
}
(: | []): <[][]> {
input = .(text) ? text : [text];
response = ...({
: ,
: input,
});
response..( d.);
}
}
Step 5: Feature Flag Controlled Migration
import { AIAdapter } from './adapter';
import { OpenAIAdapter } from './adapters/openai';
import { MistralAdapter } from './adapters/mistral';
type Provider = 'openai' | 'mistral';
function getAIProvider(): Provider {
const mistralPercentage = parseInt(process.env.MISTRAL_ROLLOUT_PERCENT || '0');
const random = Math.random() * 100;
if (random < mistralPercentage) {
return 'mistral';
}
return 'openai';
}
export function createAIAdapter(): AIAdapter {
const provider = getAIProvider();
switch (provider) {
case 'mistral':
console.log('[AI] Using Mistral adapter');
return new ();
:
:
.();
();
}
}
Step 6: Gradual Rollout
export MISTRAL_ROLLOUT_PERCENT=0
export MISTRAL_ROLLOUT_PERCENT=5
export MISTRAL_ROLLOUT_PERCENT=25
export MISTRAL_ROLLOUT_PERCENT=50
export MISTRAL_ROLLOUT_PERCENT=100
Step 7: Model Mapping
interface ModelMapping {
openai: string;
mistral: string;
notes: string;
}
const MODEL_MAPPINGS: ModelMapping[] = [
{
openai: 'gpt-3.5-turbo',
mistral: 'mistral-small-latest',
notes: 'Fast, cost-effective',
},
{
openai: 'gpt-4',
mistral: 'mistral-large-latest',
notes: 'Complex reasoning',
},
{
openai: 'gpt-4-turbo',
mistral: 'mistral-large-latest',
notes: 'Best available',
},
{
openai: 'text-embedding-ada-002',
mistral: 'mistral-embed',
notes: '1024 dimensions',
},
];
export function mapModel(openaiModel: string): string {
const mapping = MODEL_MAPPINGS.find(m => m.openai === openaiModel);
return mapping?.mistral || ;
}
Step 8: Validation & Testing
import { describe, it, expect } from 'vitest';
import { OpenAIAdapter } from '../../src/ai/adapters/openai';
import { MistralAdapter } from '../../src/ai/adapters/mistral';
describe('Migration Validation', () => {
const openai = new OpenAIAdapter();
const mistral = new MistralAdapter();
const testCases = [
{ name: 'Simple greeting', messages: [{ role: 'user', content: 'Hello' }] },
{ name: 'Math question', messages: [{ role: 'user', content: 'What is 2+2?' }] },
{ name: 'Code generation', messages: [{ role: 'user', content: 'Write hello world in Python' }] },
];
for (const testCase of testCases) {
it(`should produce similar output: ${testCase.name}`, async () => {
const [openaiResult, mistralResult] = .([
openai.(testCase., { : }),
mistral.(testCase., { : }),
]);
(openaiResult..).();
(mistralResult..).();
.();
.(, openaiResult.);
.(, mistralResult.);
});
}
});
Step 9: Rollback Plan
#!/bin/bash
echo "=== Rolling back to OpenAI ==="
kubectl set env deployment/ai-service MISTRAL_ROLLOUT_PERCENT=0
kubectl rollout status deployment/ai-service
curl -sf https://api.yourapp.com/health | jq '.services.ai'
echo "Rollback complete. Mistral disabled."
Output
- Migration assessment complete
- Adapter layer implemented
- Gradual rollout in progress
- Rollback procedure ready
Error Handling
| Issue | Cause | Solution |
|---|
| Different output format | API differences | Normalize in adapter |
| Missing feature | Not supported | Implement fallback |
| Performance difference | Model characteristics | Adjust timeouts |
| Cost increase | Token differences | Monitor and optimize |
Examples
Quick A/B Comparison
const [openaiResponse, mistralResponse] = await Promise.all([
openaiAdapter.chat(messages, { temperature: 0 }),
mistralAdapter.chat(messages, { temperature: 0 }),
]);
console.log('OpenAI tokens:', openaiResponse.usage);
console.log('Mistral tokens:', mistralResponse.usage);
Resources
Completion
Congratulations! You've completed the Mistral AI skill pack. For ongoing support, visit docs.mistral.ai.