File size: 6,313 Bytes
e706de2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**

 * Solution 18: ChatPromptTemplate

 */

import {ChatPromptTemplate} from '../../../../src/index.js';

// Test cases
async function exercise() {
    console.log('=== Exercise 18: ChatPromptTemplate ===\n');

    // Test 1: Simple chat with system and human messages
    console.log('--- Test 1: Basic Chat ---');

    const chatPrompt1 = ChatPromptTemplate.fromMessages([
        ["system", "You are a {role} assistant"],
        ["human", "{question}"]
    ]);

    const messages1 = await chatPrompt1.format({
        role: "helpful",
        question: " What's the weather?"
    })

    console.log('Messages:');
    messages1.forEach(msg => console.log(`  ${msg}`))
    console.log();

    // Test 2: Multi-turn conversation
    console.log('--- Test 2: Multi-Turn Conversation ---');

    const chatPrompt2 = ChatPromptTemplate.fromMessages([
        ["system", "You are a {personality} chatbot"],
        ["human", "Hi, I'm {name}"],
        ["ai", "Nice to meet you, {name}!"],
        ["human", "Can you help me with {topic}?"]
    ]);

    console.log('Detected variables:', chatPrompt2.inputVariables);

    const messages2 = await chatPrompt2.format({
        personality: "friendly",
        name: "Alice",
        topic: "JavaScript"
    });

    console.log('\nConversation:');
    messages2.forEach(msg => console.log(`  ${msg}`))
    console.log();

    // Test 3: Translation bot template
    console.log('--- Test 3: Translation Bot ---');

    const translateChat = ChatPromptTemplate.fromMessages([
        ["system", "You are a translator. Translate from {source_lang} to {target_lang}."],
        ["human", "Translate: {text}"]
    ]);

    const messages3 = await translateChat.format({
        source_lang: "English",
        target_lang: "Spanish",
        text: "Hello, world!"
    });

    console.log('Translation request:');
    messages3.forEach(msg => console.log(`  ${msg}`))
    console.log();

    // Test 4: Customer service template
    console.log('--- Test 4: Customer Service ---');

    const serviceChat = ChatPromptTemplate.fromMessages([
        ["system", "You are a {company} customer service agent. Be {tone}."],
        ["human", "Order #{order_id}: {issue}"]
    ]);

    const messages4 = await serviceChat.format({
        company: "TechCorp",
        tone: "professional and empathetic",
        order_id: "12345",
        issue: "My item hasn't arrived"
    });

    console.log('Service interaction:');
    messages4.forEach(msg => console.log(`  ${msg}`))
    console.log();

    // Test 5: Use as Runnable
    console.log('--- Test 5: Use as Runnable ---');

    const runnableChat = ChatPromptTemplate.fromMessages([
        ["system", "You are a {role}"],
        ["human", "{query}"]
    ]);

    const messages5 = await runnableChat.invoke({
        role: "math tutor",
        query: "Explain calculus"
    });

    console.log('Invoked messages:');
    messages5.forEach(msg => console.log(`  ${msg}`))
    console.log();

    // Test 6: Validation
    console.log('--- Test 6: Validation ---');

    const strictChat = ChatPromptTemplate.fromMessages([
        ["system", "You need {var1} and {var2}"],
        ["human", "Using {var3}"]
    ]);

    console.log('Required variables:', strictChat.inputVariables);

    try {
        await strictChat.format({ var1: "one" })
        console.log('ERROR: Should have thrown!');
    } catch (error) {
        console.log('✓ Validation error:', error.message);
    }
    console.log();

    // Test 7: Code review chat
    console.log('--- Test 7: Code Review Chat ---');

    const reviewChat = ChatPromptTemplate.fromMessages([
        ["system", "You are a {language} code reviewer. Focus on {focus}."],
        ["human", "Review this code:\n{code}"],
        ["ai", "I'll review your {language} code for {focus}."]
    ]);

    const messages7 = await reviewChat.format({
        language: "Python",
        focus: "performance",
        code: "def slow_sum(n): return sum([i for i in range(n)])"
    });

    console.log('Code review chat:');
    messages7.forEach(msg => console.log(`  ${msg}`))
    console.log();

    console.log('✓ Exercise 2 complete!');
}

// Run the exercise
exercise().catch(console.error);

/**

 * Expected Output:

 *

 * --- Test 1: Basic Chat ---

 * Messages:

 *   [1:27:22 PM] system: You are a helpful assistant

 *   [1:27:22 PM] human:  What's the weather?

 *

 * --- Test 2: Multi-Turn Conversation ---

 * Detected variables: [ 'personality', 'name', 'topic' ]

 *

 * Conversation:

 *   [1:31:36 PM] system: You are a friendly chatbot

 *   [1:31:36 PM] human: Hi, I'm Alice

 *   [1:31:36 PM] ai: Nice to meet you, Alice!

 *   [1:31:36 PM] human: Can you help me with JavaScript?

 *

 * --- Test 3: Translation Bot ---

 * Translation request:

 *   [1:31:36 PM] system: You are a translator. Translate from English to Spanish.

 *   [1:31:36 PM] human: Translate: Hello, world!

 *

 * --- Test 4: Customer Service ---

 * Service interaction:

 *   [1:31:36 PM] system: You are a TechCorp customer service agent. Be professional and empathetic.

 *   [1:31:36 PM] human: Order #12345: My item hasn't arrived

 *

 * --- Test 5: Use as Runnable ---

 * Invoked messages:

 *   [1:31:36 PM] system: You are a math tutor

 *   [1:31:36 PM] human: Explain calculus

 *

 * --- Test 6: Validation ---

 * Required variables: ['var1', 'var2', 'var3']

 * ✓ Validation error: Missing required input variables: var2, var3

 *

 * --- Test 7: Code Review Chat ---

 * Code review chat:

 *   [1:31:36 PM] system: You are a Python code reviewer. Focus on performance.

 *   [1:31:36 PM] human: Review this code:

 * def slow_sum(n): return sum([i for i in range(n)])

 *   [1:31:36 PM] ai: I'll review your Python code for performance.

 *

 * Learning Points:

 * 1. ChatPromptTemplate creates structured conversations

 * 2. Each message has a role: system, human, ai

 * 3. Variables can span multiple messages

 * 4. Auto-extraction finds all variables across messages

 * 5. Message classes provide type safety

 * 6. Perfect for building chat interfaces

 * 7. Reusable patterns for different domains

 */