模块 2: 提示词模板 (Prompts) - 练习练习目标熟练使用PromptTemplate创建和格式化包含变量的提示词。熟练使用ChatPromptTemplate创建和格式化包含不同角色消息的聊天提示词。理解并实践FewShotPromptTemplate通过示例引导模型行为。准备工作确保已完成模块 1 的环境搭建。准备好您的 DeepSeek API 密钥。练习步骤步骤 1: 使用PromptTemplate生成产品描述创建一个名为product_description.ts的文件使用PromptTemplate为不同的产品生成描述。模板应包含product_name和features两个变量。// product_description.tsimportdotenv/config;import{PromptTemplate}fromlangchain/core/prompts;import{ChatOpenAI}fromlangchain/openai;import{StringOutputParser}fromlangchain/core/output_parsers;asyncfunctionmain(){constmodelnewChatOpenAI({model:deepseek-v4-flash,temperature:0.7,apiKey:process.env.DEEPSEEK_API_KEY,configuration:{baseURL:https://api.deepseek.com,},});constparsernewStringOutputParser();constpromptTemplatePromptTemplate.fromTemplate(请为以下产品生成一个吸引人的描述。产品名称{product_name}主要特点{features}。);constchainpromptTemplate.pipe(model).pipe(parser);console.log(正在为智能手表生成描述...);constresult1awaitchain.invoke({product_name:智能手表,features:心率监测、GPS、NFC支付,});console.log(\n--- 智能手表描述 ---\n);console.log(result1);console.log(\n正在为智能音箱生成描述...);constresult2awaitchain.invoke({product_name:智能音箱,features:语音助手、高保真音质、智能家居控制,});console.log(\n--- 智能音箱描述 ---\n);console.log(result2);}main().catch(console.error);运行您的代码npx ts-node product_description.ts步骤 2: 使用ChatPromptTemplate构建多角色对话创建一个名为chat_roles.ts的文件使用ChatPromptTemplate构建一个包含系统、人类和 AI 消息的对话。系统消息应定义 AI 的角色人类消息应包含用户的问题并预留一个 AI 消息的占位符。// chat_roles.tsimportdotenv/config;import{ChatOpenAI}fromlangchain/openai;import{ChatPromptTemplate,SystemMessagePromptTemplate,HumanMessagePromptTemplate,AIMessagePromptTemplate}fromlangchain/core/prompts;import{HumanMessage,AIMessage}fromlangchain/core/messages;asyncfunctionmain(){constchatModelnewChatOpenAI({model:deepseek-v4-flash,temperature:0.7,apiKey:process.env.DEEPSEEK_API_KEY,configuration:{baseURL:https://api.deepseek.com,},});constchatPromptChatPromptTemplate.fromMessages([SystemMessagePromptTemplate.fromTemplate(你是一个专业的心理咨询师以耐心和同理心回答问题。),HumanMessagePromptTemplate.fromTemplate(我最近感到压力很大工作和生活都一团糟我该怎么办),AIMessagePromptTemplate.fromTemplate(我理解你的感受压力确实会让人感到困扰。我们可以一起探讨一些应对策略。\n{ai_response_placeholder}),// 预留 AI 回复的占位符]);// 格式化提示词并填充 AI 回复的占位符constformattedMessagesawaitchatPrompt.formatMessages({ai_response_placeholder:你愿意分享更多细节吗比如是什么让你感到压力最大,});console.log(正在进行心理咨询对话...);constresponseawaitchatModel.invoke(formattedMessages);console.log(\n--- 对话结果 ---\n);console.log(response.content);}main().catch(console.error);运行您的代码npx ts-node chat_roles.ts步骤 3: 使用FewShotPromptTemplate进行情感分类创建一个名为few_shot_sentiment.ts的文件使用FewShotPromptTemplate通过少量示例来指导模型进行情感分类正面/负面。// few_shot_sentiment.tsimportdotenv/config;import{ChatOpenAI}fromlangchain/openai;import{FewShotPromptTemplate,PromptTemplate}fromlangchain/core/prompts;import{StringOutputParser}fromlangchain/core/output_parsers;asyncfunctionmain(){constmodelnewChatOpenAI({model:deepseek-v4-flash,temperature:0,apiKey:process.env.DEEPSEEK_API_KEY,configuration:{baseURL:https://api.deepseek.com,},});constparsernewStringOutputParser();// 将模型输出解析为字符串constexamples[{input:这部电影太棒了我非常喜欢,output:正面,},{input:今天的会议很无聊浪费时间。,output:负面,},{input:我收到了一个惊喜礼物太开心了,output:正面,},];constexamplePromptnewPromptTemplate({inputVariables:[input,output],template:文本: {input}\n情感: {output},});constfewShotPromptnewFewShotPromptTemplate({examples:examples,examplePrompt:examplePrompt,prefix:请根据以下示例判断文本的情感正面/负面,suffix:文本: {input}\n情感:,inputVariables:[input],exampleSeparator:\n\n,});constchainfewShotPrompt.pipe(model).pipe(parser);console.log(正在判断文本情感...);constresultawaitchain.invoke({input:我对这次的客户服务非常不满意。,});console.log(\n--- 情感判断结果 ---\n);console.log(result);}main().catch(console.error);运行您的代码npx ts-node few_shot_sentiment.ts思考题在什么情况下您会选择使用PromptTemplate而不是ChatPromptTemplate反之亦然。FewShotPromptTemplate中的prefix、suffix和exampleSeparator参数有什么作用尝试修改它们并观察对输出的影响。除了情感分类FewShotPromptTemplate还可以应用于哪些场景请举例说明。纯文本任务用 PromptTemplate对话角色用 ChatPromptTemplateprefix 是总指令suffix 引导输出exampleSeparator 分隔示例FewShot 适用于分类、抽取、格式转换、规则生成等几乎所有任务
LangChain学习之提示词模板 (Prompts) - 练习(2/8)
模块 2: 提示词模板 (Prompts) - 练习练习目标熟练使用PromptTemplate创建和格式化包含变量的提示词。熟练使用ChatPromptTemplate创建和格式化包含不同角色消息的聊天提示词。理解并实践FewShotPromptTemplate通过示例引导模型行为。准备工作确保已完成模块 1 的环境搭建。准备好您的 DeepSeek API 密钥。练习步骤步骤 1: 使用PromptTemplate生成产品描述创建一个名为product_description.ts的文件使用PromptTemplate为不同的产品生成描述。模板应包含product_name和features两个变量。// product_description.tsimportdotenv/config;import{PromptTemplate}fromlangchain/core/prompts;import{ChatOpenAI}fromlangchain/openai;import{StringOutputParser}fromlangchain/core/output_parsers;asyncfunctionmain(){constmodelnewChatOpenAI({model:deepseek-v4-flash,temperature:0.7,apiKey:process.env.DEEPSEEK_API_KEY,configuration:{baseURL:https://api.deepseek.com,},});constparsernewStringOutputParser();constpromptTemplatePromptTemplate.fromTemplate(请为以下产品生成一个吸引人的描述。产品名称{product_name}主要特点{features}。);constchainpromptTemplate.pipe(model).pipe(parser);console.log(正在为智能手表生成描述...);constresult1awaitchain.invoke({product_name:智能手表,features:心率监测、GPS、NFC支付,});console.log(\n--- 智能手表描述 ---\n);console.log(result1);console.log(\n正在为智能音箱生成描述...);constresult2awaitchain.invoke({product_name:智能音箱,features:语音助手、高保真音质、智能家居控制,});console.log(\n--- 智能音箱描述 ---\n);console.log(result2);}main().catch(console.error);运行您的代码npx ts-node product_description.ts步骤 2: 使用ChatPromptTemplate构建多角色对话创建一个名为chat_roles.ts的文件使用ChatPromptTemplate构建一个包含系统、人类和 AI 消息的对话。系统消息应定义 AI 的角色人类消息应包含用户的问题并预留一个 AI 消息的占位符。// chat_roles.tsimportdotenv/config;import{ChatOpenAI}fromlangchain/openai;import{ChatPromptTemplate,SystemMessagePromptTemplate,HumanMessagePromptTemplate,AIMessagePromptTemplate}fromlangchain/core/prompts;import{HumanMessage,AIMessage}fromlangchain/core/messages;asyncfunctionmain(){constchatModelnewChatOpenAI({model:deepseek-v4-flash,temperature:0.7,apiKey:process.env.DEEPSEEK_API_KEY,configuration:{baseURL:https://api.deepseek.com,},});constchatPromptChatPromptTemplate.fromMessages([SystemMessagePromptTemplate.fromTemplate(你是一个专业的心理咨询师以耐心和同理心回答问题。),HumanMessagePromptTemplate.fromTemplate(我最近感到压力很大工作和生活都一团糟我该怎么办),AIMessagePromptTemplate.fromTemplate(我理解你的感受压力确实会让人感到困扰。我们可以一起探讨一些应对策略。\n{ai_response_placeholder}),// 预留 AI 回复的占位符]);// 格式化提示词并填充 AI 回复的占位符constformattedMessagesawaitchatPrompt.formatMessages({ai_response_placeholder:你愿意分享更多细节吗比如是什么让你感到压力最大,});console.log(正在进行心理咨询对话...);constresponseawaitchatModel.invoke(formattedMessages);console.log(\n--- 对话结果 ---\n);console.log(response.content);}main().catch(console.error);运行您的代码npx ts-node chat_roles.ts步骤 3: 使用FewShotPromptTemplate进行情感分类创建一个名为few_shot_sentiment.ts的文件使用FewShotPromptTemplate通过少量示例来指导模型进行情感分类正面/负面。// few_shot_sentiment.tsimportdotenv/config;import{ChatOpenAI}fromlangchain/openai;import{FewShotPromptTemplate,PromptTemplate}fromlangchain/core/prompts;import{StringOutputParser}fromlangchain/core/output_parsers;asyncfunctionmain(){constmodelnewChatOpenAI({model:deepseek-v4-flash,temperature:0,apiKey:process.env.DEEPSEEK_API_KEY,configuration:{baseURL:https://api.deepseek.com,},});constparsernewStringOutputParser();// 将模型输出解析为字符串constexamples[{input:这部电影太棒了我非常喜欢,output:正面,},{input:今天的会议很无聊浪费时间。,output:负面,},{input:我收到了一个惊喜礼物太开心了,output:正面,},];constexamplePromptnewPromptTemplate({inputVariables:[input,output],template:文本: {input}\n情感: {output},});constfewShotPromptnewFewShotPromptTemplate({examples:examples,examplePrompt:examplePrompt,prefix:请根据以下示例判断文本的情感正面/负面,suffix:文本: {input}\n情感:,inputVariables:[input],exampleSeparator:\n\n,});constchainfewShotPrompt.pipe(model).pipe(parser);console.log(正在判断文本情感...);constresultawaitchain.invoke({input:我对这次的客户服务非常不满意。,});console.log(\n--- 情感判断结果 ---\n);console.log(result);}main().catch(console.error);运行您的代码npx ts-node few_shot_sentiment.ts思考题在什么情况下您会选择使用PromptTemplate而不是ChatPromptTemplate反之亦然。FewShotPromptTemplate中的prefix、suffix和exampleSeparator参数有什么作用尝试修改它们并观察对输出的影响。除了情感分类FewShotPromptTemplate还可以应用于哪些场景请举例说明。纯文本任务用 PromptTemplate对话角色用 ChatPromptTemplateprefix 是总指令suffix 引导输出exampleSeparator 分隔示例FewShot 适用于分类、抽取、格式转换、规则生成等几乎所有任务