-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path03-conditional-logic.ts
More file actions
84 lines (78 loc) · 2.51 KB
/
03-conditional-logic.ts
File metadata and controls
84 lines (78 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* Example 3: Conditional Logic
*
* This example demonstrates how to use conditional logic to include/exclude
* components based on runtime parameters.
*/
import { PromptBuilder, createCondition, PersonaComponent, ToneComponent } from '../dist';
const prompt = new PromptBuilder()
.role('You are a mental health support assistant')
.goal('Provide emotional support and guidance')
.persona('The Steady Anchor', {
condition: createCondition(
(params) => params.emotion === 'sad' || params.emotion === 'anxious',
new PersonaComponent('The Steady Anchor - provides calm, grounding support')
)
})
.persona('The Warm Horizon', {
condition: createCondition(
(params) => params.emotion === 'hopeful' || params.emotion === 'motivated',
new PersonaComponent('The Warm Horizon - provides encouraging, uplifting support')
)
})
.tone('Calm and supportive', {
condition: createCondition(
(params) => params.age < 18,
new ToneComponent('Use simple, age-appropriate language')
)
})
.tone('Professional and empathetic', {
condition: createCondition(
(params) => params.age >= 18,
new ToneComponent('Use professional but warm language')
)
})
.input('User feeling: ${emotion}\nUser age: ${age}')
.guardrails([
'Always prioritize user safety',
'Do not provide medical diagnosis',
'Encourage professional help when needed',
'Be empathetic and non-judgmental'
])
.constraints((params) => {
const constraints = [
`Provide ${params.responseLength} sentences of support`,
'Use age-appropriate language'
];
if (params.age < 18) {
constraints.push('Use simpler vocabulary');
constraints.push('Include reassurance');
}
return constraints;
})
.output('Provide ${responseLength} sentences of support')
.build({
emotion: 'sad',
age: 25,
responseLength: 3
});
console.log('=== Conditional Logic Example ===');
console.log(prompt);
console.log('\n');
// Example with different parameters
const prompt2 = new PromptBuilder()
.role('You are a mental health support assistant')
.goal('Provide emotional support and guidance')
.persona('The Warm Horizon', {
condition: createCondition(
(params) => params.emotion === 'hopeful',
new PersonaComponent('The Warm Horizon - provides encouraging support')
)
})
.build({
emotion: 'hopeful',
age: 30
});
console.log('=== Conditional Logic Example (Different Parameters) ===');
console.log(prompt2);
console.log('\n');