-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path08-dynamic-content.ts
More file actions
132 lines (125 loc) · 3.88 KB
/
08-dynamic-content.ts
File metadata and controls
132 lines (125 loc) · 3.88 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* Example 8: Dynamic Content with Functions
*
* This example demonstrates using functions for dynamic content
* generation based on parameters.
*/
import { PromptBuilder } from '../dist';
const prompt = new PromptBuilder()
.role((params) => {
if (params.userLevel === 'beginner') {
return 'You are a patient teacher who explains concepts simply';
} else if (params.userLevel === 'intermediate') {
return 'You are a knowledgeable guide who provides detailed explanations';
} else {
return 'You are an expert consultant who provides advanced insights';
}
})
.goal((params) => {
return `Help ${params.userName} learn ${params.topic} at ${params.userLevel} level`;
})
.context((params) => {
const experience = params.experienceYears || 0;
const background = params.background || 'general';
return `User Profile:
- Name: ${params.userName}
- Experience: ${experience} years
- Background: ${background}
- Learning Style: ${params.learningStyle || 'visual'}`
})
.input((params) => {
if (params.userLevel === 'beginner') {
return `Question: ${params.question}\n\nPlease explain in simple terms.`;
} else {
return `Question: ${params.question}\n\nProvide detailed technical explanation.`;
}
})
.tasks((params) => {
if (params.userLevel === 'beginner') {
return [
'Explain the concept in simple terms',
'Provide a basic example',
'Relate it to something familiar'
];
} else {
return [
'Provide detailed technical explanation',
'Show advanced use cases',
'Discuss edge cases and best practices'
];
}
})
.steps((params) => {
if (params.userLevel === 'beginner') {
return [
'Start with the basics',
'Build up complexity gradually',
'Reinforce with examples'
];
}
return [
'Dive into technical details',
'Show real-world applications',
'Discuss trade-offs and alternatives'
];
})
.fewShots((params) => {
if (params.userLevel === 'beginner') {
return [
`Question: What is ${params.topic}?\nAnswer: ${params.topic} is a simple way to...`,
`Question: Why use ${params.topic}?\nAnswer: It helps you...`
];
}
return [
`Question: Advanced ${params.topic} patterns?\nAnswer: Here are some advanced patterns...`,
`Question: ${params.topic} best practices?\nAnswer: Key best practices include...`
];
})
.constraints((params) => {
const constraints = [
`Provide explanation appropriate for ${params.userLevel} level`
];
if (params.userLevel === 'beginner') {
constraints.push('Use simple language');
constraints.push('Avoid jargon');
constraints.push('Include visual examples');
} else {
constraints.push('Include technical details');
constraints.push('Discuss advanced concepts');
}
return constraints;
})
.output((params) => {
const length = params.userLevel === 'beginner' ? '3-5' : '5-10';
return `Provide a ${length} sentence explanation with examples.`;
})
.build({
userName: 'Alice',
userLevel: 'beginner',
topic: 'TypeScript',
question: 'What are generics?',
experienceYears: 1,
background: 'JavaScript',
learningStyle: 'visual'
});
console.log('=== Dynamic Content Example (Beginner) ===');
console.log(prompt);
console.log('\n');
// Example with advanced user
const prompt2 = new PromptBuilder()
.role((params) => {
if (params.userLevel === 'beginner') {
return 'You are a patient teacher';
} else {
return 'You are an expert consultant';
}
})
.goal((params) => `Help ${params.userName} learn ${params.topic}`)
.build({
userName: 'Bob',
userLevel: 'advanced',
topic: 'Advanced TypeScript Patterns'
});
console.log('=== Dynamic Content Example (Advanced) ===');
console.log(prompt2);
console.log('\n');