Prompt Optimization

Automatically optimize your AI prompts for better persona generation. The SDK includes tools to test, refine, and improve prompts based on real results, ensuring consistently high-quality persona outputs.

Automatic Optimization

Let the SDK optimize your prompts automatically:

auto-optimization.ts
1import { PromptOptimizer, PersonaBuilder } from '@jamesaphoenix/persona-sdk';
2
3// Initialize optimizer
4const optimizer = new PromptOptimizer({
5 apiKey: process.env.OPENAI_API_KEY,
6 objective: 'diversity', // or 'accuracy', 'consistency'
7 model: 'gpt-4'
8});
9
10// Start with a basic prompt
11const initialPrompt = 'Create a software developer persona';
12
13// Optimize the prompt
14const optimizedPrompt = await optimizer.optimize(initialPrompt, {
15 iterations: 5,
16 samplesPerIteration: 10,
17 metrics: ['diversity', 'realism', 'completeness'],
18 targetAttributes: ['age', 'skills', 'experience', 'personality']
19});
20
21console.log('Original prompt:', initialPrompt);
22console.log('Optimized prompt:', optimizedPrompt.text);
23console.log('Improvement:', optimizedPrompt.improvement);
24
25// Results:
26// Original: "Create a software developer persona"
27// Optimized: "Create a detailed persona of a software developer
28// including their age (20-50), technical skills, years of
29// experience, personality traits, work preferences, and
30// career goals. Make them realistic and diverse."
31// Improvement: 78% better diversity, 65% more complete
32
33// Use the optimized prompt
34const persona = await PersonaBuilder.fromPrompt(
35 optimizedPrompt.text,
36 { apiKey: process.env.OPENAI_API_KEY }
37);

A/B Testing Prompts

Test different prompt variations to find what works best:

ab-testing.ts
1import { PromptTester } from '@jamesaphoenix/persona-sdk';
2
3const tester = new PromptTester({
4 apiKey: process.env.OPENAI_API_KEY
5});
6
7// Define prompt variations
8const prompts = [
9 {
10 id: 'detailed',
11 text: 'Generate a persona with age, occupation, hobbies, personality'
12 },
13 {
14 id: 'narrative',
15 text: 'Tell me about a person including their background and interests'
16 },
17 {
18 id: 'structured',
19 text: 'Create persona: Demographics, Professional info, Personal traits'
20 }
21];
22
23// Run A/B test
24const results = await tester.abTest(prompts, {
25 samplesPerPrompt: 50,
26 metrics: {
27 completeness: (persona) => {
28 const fields = ['age', 'occupation', 'hobbies', 'personality'];
29 return fields.filter(f => persona.attributes[f]).length / fields.length;
30 },
31 diversity: (personas) => {
32 // Calculate diversity score across all generated personas
33 return calculateDiversityScore(personas);
34 },
35 consistency: (personas) => {
36 // Check if similar prompts generate similar personas
37 return calculateConsistencyScore(personas);
38 }
39 }
40});
41
42// Analyze results
43console.log('A/B Test Results:');
44results.forEach(result => {
45 console.log(`Prompt "${result.id}":`);
46 console.log(` Completeness: ${result.metrics.completeness.toFixed(2)}`);
47 console.log(` Diversity: ${result.metrics.diversity.toFixed(2)}`);
48 console.log(` Consistency: ${result.metrics.consistency.toFixed(2)}`);
49 console.log(` Overall Score: ${result.overallScore.toFixed(2)}`);
50});
51
52// Use the winning prompt
53const winner = results.reduce((a, b) =>
54 a.overallScore > b.overallScore ? a : b
55);
56
57console.log(`\nWinning prompt: "${winner.prompt.text}"`);

Bootstrap Learning

Train the optimizer with your own examples:

bootstrap-learning.ts
1import { PromptOptimizer } from '@jamesaphoenix/persona-sdk';
2
3const optimizer = new PromptOptimizer({
4 apiKey: process.env.OPENAI_API_KEY
5});
6
7// Provide example personas you want to generate
8const goodExamples = [
9 {
10 name: 'Sarah Chen',
11 age: 28,
12 occupation: 'UX Designer',
13 personality: 'creative, detail-oriented, empathetic',
14 hobbies: ['digital art', 'user research', 'hiking'],
15 background: 'Studied HCI, worked at 2 startups'
16 },
17 // ... more examples
18];
19
20// Train the optimizer
21await optimizer.bootstrap({
22 examples: goodExamples,
23
24 // What makes these examples good?
25 criteria: [
26 'Complete demographic information',
27 'Realistic personality traits',
28 'Diverse backgrounds',
29 'Professional details included'
30 ],
31
32 // Learn patterns from examples
33 learnPatterns: true
34});
35
36// Generate new prompts based on learned patterns
37const prompt = optimizer.generatePrompt({
38 similarTo: goodExamples,
39 emphasize: ['personality', 'background'],
40 avoid: ['generic descriptions', 'missing details']
41});
42
43console.log('Generated prompt:', prompt);
44// "Create a detailed persona including full name, age (20-40),
45// specific occupation with industry context, 3-5 personality
46// traits that influence their work style, hobbies that reflect
47// their interests, and a brief professional background with
48// education and career progression."

Prompt Templates

Use and customize proven prompt templates:

prompt-templates.ts
1import { PromptTemplates } from '@jamesaphoenix/persona-sdk';
2
3// Built-in templates for common use cases
4const templates = PromptTemplates.getAll();
5
6// Marketing persona template
7const marketingPersona = await PersonaBuilder.fromPrompt(
8 PromptTemplates.marketing({
9 product: 'fitness app',
10 targetAge: [25, 40],
11 includeInterests: true,
12 includePainPoints: true
13 }),
14 { apiKey: process.env.OPENAI_API_KEY }
15);
16
17// User research template
18const researchPersona = await PersonaBuilder.fromPrompt(
19 PromptTemplates.userResearch({
20 domain: 'e-commerce',
21 userType: 'frequent shopper',
22 includeJourney: true,
23 includeFrustrations: true
24 }),
25 { apiKey: process.env.OPENAI_API_KEY }
26);
27
28// Custom template with variables
29const customTemplate = PromptTemplates.create({
30 name: 'startup-employee',
31 template: `Create a persona for a {{role}} at a {{stage}} startup:
32 - Age range: {{ageMin}}-{{ageMax}}
33 - Experience level: {{experience}}
34 - Key motivations for joining a startup
35 - Work style and preferences
36 - Career goals and aspirations
37 - Technical skills if applicable`,
38 defaults: {
39 stage: 'series-A',
40 ageMin: 25,
41 ageMax: 40,
42 experience: 'mid-level'
43 }
44});
45
46// Use custom template
47const startupPersona = await PersonaBuilder.fromPrompt(
48 customTemplate.render({
49 role: 'frontend engineer',
50 stage: 'seed',
51 experience: 'senior'
52 }),
53 { apiKey: process.env.OPENAI_API_KEY }
54);

Evaluation Metrics

Measure and improve prompt quality:

evaluation-metrics.ts
1import { PromptEvaluator } from '@jamesaphoenix/persona-sdk';
2
3const evaluator = new PromptEvaluator();
4
5// Evaluate a single prompt
6const prompt = 'Create a detailed marketing persona for our SaaS product';
7const evaluation = await evaluator.evaluate(prompt, {
8 samples: 20,
9 metrics: [
10 'completeness', // Are all expected fields present?
11 'consistency', // Are results consistent across runs?
12 'diversity', // Is there good variety in outputs?
13 'realism', // Do personas seem realistic?
14 'relevance', // Are personas relevant to the context?
15 'specificity' // Are details specific vs generic?
16 ]
17});
18
19console.log('Prompt Evaluation:');
20console.log(`Completeness: ${evaluation.completeness}/10`);
21console.log(`Consistency: ${evaluation.consistency}/10`);
22console.log(`Diversity: ${evaluation.diversity}/10`);
23console.log(`Overall Quality: ${evaluation.overall}/10`);
24
25// Get specific recommendations
26const recommendations = evaluator.recommend(evaluation);
27console.log('\nRecommendations:');
28recommendations.forEach(rec => {
29 console.log(`- ${rec.issue}: ${rec.suggestion}`);
30});
31
32// Example output:
33// Recommendations:
34// - Low specificity: Add more specific attributes like "age range 25-45"
35// - Missing context: Include industry or product details
36// - Generic output: Request unique personality traits or backgrounds
37
38// Track improvement over time
39const history = await evaluator.trackProgress([
40 { prompt: initialPrompt, timestamp: new Date('2024-01-01') },
41 { prompt: improvedPrompt, timestamp: new Date('2024-01-15') },
42 { prompt: optimizedPrompt, timestamp: new Date('2024-02-01') }
43]);
44
45// Visualize improvement
46console.log('Quality improvement over time:');
47history.forEach(entry => {
48 console.log(`${entry.date}: ${entry.quality}/10 (${entry.change})`);
49});

Chain-of-Thought Prompting

Use advanced prompting techniques for complex personas:

chain-prompting.ts
1import { ChainPromptBuilder } from '@jamesaphoenix/persona-sdk';
2
3// Build complex personas step by step
4const chain = new ChainPromptBuilder();
5
6const complexPersona = await chain
7 // Step 1: Basic demographics
8 .addStep('Generate basic demographics for a tech professional')
9
10 // Step 2: Enrich with context
11 .addStep((previous) =>
12 `Given these demographics: ${JSON.stringify(previous)},
13 add relevant professional background and career trajectory`
14 )
15
16 // Step 3: Add personality based on background
17 .addStep((previous) =>
18 `Based on this person's background: ${JSON.stringify(previous)},
19 infer personality traits, work style, and motivations`
20 )
21
22 // Step 4: Generate realistic challenges
23 .addStep((previous) =>
24 `What challenges might this person face: ${JSON.stringify(previous)}?
25 Include professional, personal, and industry-specific challenges`
26 )
27
28 // Execute the chain
29 .execute({
30 apiKey: process.env.OPENAI_API_KEY,
31 model: 'gpt-4',
32 mergeStrategy: 'deep' // Merge all steps into final persona
33 });
34
35console.log('Complex persona:', complexPersona);
36
37// Conditional chains based on initial output
38const conditionalChain = chain
39 .addStep('Generate a professional persona')
40 .addConditionalStep({
41 condition: (persona) => persona.age < 30,
42 prompt: 'Add early career challenges and learning goals',
43 else: 'Add leadership experience and mentoring interests'
44 })
45 .addConditionalStep({
46 condition: (persona) => persona.occupation.includes('Engineer'),
47 prompt: 'Add technical skills and project experience',
48 else: 'Add domain expertise and industry knowledge'
49 });

🎯 Optimization Strategies

  • • Start with simple prompts and iterate
  • • Use specific examples in prompts
  • • Define clear success metrics
  • • Test with diverse scenarios
  • • Track performance over time
  • • Learn from generated outputs

âš¡ Performance Tips

  • • Cache optimized prompts
  • • Batch similar requests
  • • Use smaller models for testing
  • • Implement retry logic
  • • Monitor API costs
  • • Set reasonable timeouts

🚀 Advanced Techniques

  • • Few-shot learning: Provide examples in prompts for better results
  • • Temperature tuning: Adjust randomness for diversity vs consistency
  • • Prompt chaining: Build complex personas incrementally
  • • Negative prompting: Specify what to avoid in outputs
  • • Meta-prompting: Use AI to generate better prompts