| | |
| | |
| |
|
| |
|
| | import {ChatPromptTemplate} from '../../../../src/index.js';
|
| |
|
| |
|
| | async function exercise() {
|
| | console.log('=== Exercise 18: ChatPromptTemplate ===\n');
|
| |
|
| |
|
| | 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();
|
| |
|
| |
|
| | 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();
|
| |
|
| |
|
| | 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();
|
| |
|
| |
|
| | 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();
|
| |
|
| |
|
| | 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();
|
| |
|
| |
|
| | 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();
|
| |
|
| |
|
| | 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!');
|
| | }
|
| |
|
| |
|
| | exercise().catch(console.error);
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |