LangChain学习之提示词模板 Prompts(2/8)

模块 2: 提示词模板 (Prompts)

2.1 提示词 (Prompts) 概述

在与大型语言模型(LLM)交互时,提示词 (Prompt) 是向模型发出的指令或问题。一个好的提示词能够引导模型生成高质量、符合预期的输出。LangChain 提供了强大的提示词管理功能,允许开发者创建可复用、动态的提示词模板 [1]。

2.2 PromptTemplate

PromptTemplate 是 LangChain 中最基础的提示词模板。它允许您定义一个包含一个或多个变量的字符串模板。在实际调用模型时,这些变量会被具体的值填充。这使得提示词可以根据不同的输入动态生成。

2.2.1 基本用法

typescript 复制代码
import { PromptTemplate } from "@langchain/core/prompts";

// 定义一个包含变量 `product` 的提示词模板
const promptTemplate = PromptTemplate.fromTemplate(
  "请为以下产品名称生成一个吸引人的营销口号:{product}"
);

// 格式化提示词,填充变量
const formattedPrompt = await promptTemplate.format({
  product: "智能咖啡机",
});

console.log(formattedPrompt);
// 输出: 请为以下产品名称生成一个吸引人的营销口号:智能咖啡机

2.2.2 多个输入变量

PromptTemplate 可以处理多个输入变量,只需在模板字符串中定义它们即可。

typescript 复制代码
import { PromptTemplate } from "@langchain/core/prompts";

const promptTemplate = PromptTemplate.fromTemplate(
  "请根据以下主题和受众,生成一篇短文。主题:{topic},受众:{audience}"
);

const formattedPrompt = await promptTemplate.format({
  topic: "未来科技",
  audience: "青少年",
});

console.log(formattedPrompt);
// 输出: 请根据以下主题和受众,生成一篇短文。主题:未来科技,受众:青少年

2.3 ChatPromptTemplate

ChatPromptTemplate 专门用于聊天模型 (Chat Models)。与 PromptTemplate 处理纯文本不同,ChatPromptTemplate 允许您定义一系列消息,这些消息可以包含不同的角色(如系统、人类、AI)和变量。这对于构建复杂的对话系统至关重要 [2]。

2.3.1 基本用法

ChatPromptTemplate 通过 fromMessages 方法接收一个消息数组,每个消息可以是 SystemMessagePromptTemplateHumanMessagePromptTemplateAIMessagePromptTemplate

typescript 复制代码
import { ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate } from "@langchain/core/prompts";

const chatPrompt = ChatPromptTemplate.fromMessages([
  SystemMessagePromptTemplate.fromTemplate("你是一个友好的助手,你的名字是 {name}。"),
  HumanMessagePromptTemplate.fromTemplate("你好,我的名字是 {user_name}。请问你有什么可以帮助我的吗?"),
]);

const formattedChatPrompt = await chatPrompt.formatMessages({
  name: "小智",
  user_name: "张三",
});

console.log(formattedChatPrompt);
/*
输出:
[
  SystemMessage { content: '你是一个友好的助手,你的名字是 小智。', name: undefined },
  HumanMessage { content: '你好,我的名字是 张三。请问你有什么可以帮助我的吗?', name: undefined }
]
*/

2.4 少样本提示 (Few-shot Prompts)

少样本提示是一种通过在提示词中提供少量示例来引导模型行为的技术。这对于模型理解任务的意图和期望的输出格式非常有效。LangChain 提供了 FewShotPromptTemplate 来方便地实现少样本提示 [3]。

2.4.1 示例定义

首先,需要定义一些示例。这些示例通常是输入-输出对。

typescript 复制代码
const examples = [
  {
    input: "开心",
    output: "😄",
  },
  {
    input: "悲伤",
    output: "😢",
  },
];

2.4.2 创建 FewShotPromptTemplate

FewShotPromptTemplate 结合了 PromptTemplate 和示例。它会根据提供的示例和输入变量动态生成提示词。

typescript 复制代码
import { FewShotPromptTemplate, PromptTemplate } from "@langchain/core/prompts";

const examplePrompt = new PromptTemplate({
  inputVariables: ["input", "output"],
  template: "输入: {input}\n输出: {output}",
});

const fewShotPrompt = new FewShotPromptTemplate({
  examples: examples,
  examplePrompt: examplePrompt,
  prefix: "请将以下文本转换为表情符号:",
  suffix: "输入: {input}\n输出:",
  inputVariables: ["input"],
  exampleSeparator: "\n\n",
});

const formattedPrompt = await fewShotPrompt.format({
  input: "生气",
});

console.log(formattedPrompt);
/*
输出:
请将以下文本转换为表情符号:
输入: 开心
输出: 😄

输入: 悲伤
输出: 😢

输入: 生气
输出:
*/

参考文献

1\] LangChain.js Prompts. (n.d.). Docs by LangChain. Retrieved from \[2\] LangChain.js Chat Prompts. (n.d.). Docs by LangChain. Retrieved from \[3\] LangChain.js Few-shot Prompts. (n.d.). Docs by LangChain. Retrieved from

相关推荐
weixin_428005301 小时前
C#调用 AI学习从0开始-第1阶段(基础与工具)-第6天流式输出
开发语言·学习·c#·流式输出stream
清平乐的技术专栏1 小时前
【Flink学习】(七)Flink 四大窗口机制,实时时间段统计
大数据·学习·flink
清平乐的技术专栏1 小时前
【Flink学习】(九)Flink 容错机制 Checkpoint 与 Savepoint
大数据·学习·flink
库奇噜啦呼1 小时前
【iOS】源码学习-dyld加载
学习·ios·cocoa
Artech1 小时前
[对比学习LangChain和MAF-02]基本编程模式的差异(下篇)
ai·langchain·agent·maf
210Brian1 小时前
蓝桥杯单片机学习笔记(十二):V2026 大模板构建(上)
单片机·学习·蓝桥杯
尘埃落定wf2 小时前
五大主流 Agent 架构模式详解
ai·langchain
敲上瘾2 小时前
LangChain 消息机制与提示词模板指南
大数据·python·langchain
Bechamz2 小时前
大数据开发学习Day37
大数据·学习