PersonaBuilder

PersonaBuilder provides a fluent API for constructing personas with method chaining, distributions, and AI-powered generation.

Basic Builder Usage

Create personas using the fluent builder pattern:

basic-builder.ts
1import { PersonaBuilder } from '@jamesaphoenix/persona-sdk';
2
3// Basic fluent API
4const persona = PersonaBuilder.create()
5 .withName('Emma Rodriguez')
6 .withAge(29)
7 .withOccupation('Data Scientist')
8 .withSex('female')
9 .withAttribute('education', 'PhD in Statistics')
10 .withAttribute('programmingLanguages', ['Python', 'R', 'SQL'])
11 .withAttribute('yearsExperience', 5)
12 .build();
13
14console.log(persona.toObject());

Using Distributions

Mix static values with statistical distributions for variety:

builder-distributions.ts
1import { PersonaBuilder, NormalDistribution, CategoricalDistribution } from '@jamesaphoenix/persona-sdk';
2
3// Using distributions for dynamic values
4const persona = PersonaBuilder.create()
5 .withName('Random Employee')
6 .withAge(new NormalDistribution(35, 7)) // Mean 35, StdDev 7
7 .withOccupation(new CategoricalDistribution([
8 { value: 'Engineer', probability: 0.4 },
9 { value: 'Designer', probability: 0.3 },
10 { value: 'Manager', probability: 0.3 }
11 ]))
12 .withSex('other')
13 .withAttribute('salary', new NormalDistribution(85000, 15000))
14 .build();
15
16// Each build() call generates different values
17const persona1 = builder.build(); // age: 32, occupation: 'Engineer'
18const persona2 = builder.build(); // age: 41, occupation: 'Designer'

Building Multiple Personas

Generate multiple personas with variations:

build-many.ts
1// Build multiple personas at once
2const personas = PersonaBuilder.create()
3 .withAge(new NormalDistribution(30, 5))
4 .withOccupation(new CategoricalDistribution([
5 { value: 'Developer', probability: 0.6 },
6 { value: 'QA Engineer', probability: 0.4 }
7 ]))
8 .withSex('other')
9 .buildMany(10, 'Employee'); // Creates Employee 1, Employee 2, etc.
10
11console.log(`Generated ${personas.length} personas`);
12personas.forEach(p => console.log(`${p.name}: ${p.age} year old ${p.occupation}`));

AI-Powered Generation

Generate personas from natural language descriptions:

ai-generation.ts
1// Generate from text prompt
2const persona = await PersonaBuilder.fromPrompt(
3 'Create a 28-year-old freelance graphic designer who loves travel and coffee',
4 { apiKey: process.env.OPENAI_API_KEY }
5);
6
7console.log(persona.toObject());
8// Result might include:
9// {
10// name: 'Maya Patel',
11// age: 28,
12// occupation: 'Freelance Graphic Designer',
13// interests: ['travel', 'coffee', 'design'],
14// personality: 'creative, adventurous, detail-oriented'
15// }
16
17// Generate multiple diverse personas
18const personas = await PersonaBuilder.generateMultiple(
19 'Tech startup employees in their 20s-30s',
20 5,
21 { apiKey: process.env.OPENAI_API_KEY }
22);

Correlated Generation

Create personas with realistic attribute relationships:

correlated-generation.ts
1// Generate with correlations
2const persona = PersonaBuilder.create()
3 .withName('Realistic Professional')
4 .buildWithCorrelations({
5 attributes: {
6 age: new NormalDistribution(35, 8),
7 income: new NormalDistribution(75000, 25000),
8 yearsExperience: new NormalDistribution(10, 5)
9 },
10 correlations: [
11 { attribute1: 'age', attribute2: 'income', correlation: 0.6 },
12 { attribute1: 'age', attribute2: 'yearsExperience', correlation: 0.8 }
13 ],
14 conditionals: [{
15 attribute: 'yearsExperience',
16 dependsOn: 'age',
17 transform: (exp, age) => Math.min(exp, Math.max(0, age - 22))
18 }]
19 });
20
21// Results in realistic combinations:
22// 45-year-old with 20 years experience and $95k income
23// 25-year-old with 3 years experience and $55k income

✨ Builder Methods

  • withName() - Set persona name
  • withAge() - Set age (static or distribution)
  • withOccupation() - Set occupation
  • withSex() - Set sex/gender
  • withAttribute() - Add custom attribute
  • withAttributes() - Add multiple attributes

🔧 Build Methods

  • build() - Create single persona
  • buildMany() - Create multiple personas
  • buildWithCorrelations() - Create with correlations