LangChain少样本学习:两种方式对比与实践

掌握FewShotPromptTemplate与FewShotChatMessagePromptTemplate的差异与应用场景

在AI应用中,少样本学习(Few-Shot Learning)是一种强大的技术,它允许我们通过提供少量示例,让大语言模型快速学习新任务。LangChain提供了两种实现少样本学习的方式,本文将详细对比这两种方法。

一、环境准备

首先配置好LangChain环境:

import os
from dotenv import load_dotenv
from langchain_core.prompts import FewShotPromptTemplate, PromptTemplate, ChatPromptTemplate, \
    FewShotChatMessagePromptTemplate
from langchain_openai import ChatOpenAI

# 加载环境变量
load_dotenv()

# 初始化聊天模型
chat_model = ChatOpenAI(
    model="deepseek-chat",
    base_url=os.getenv('OPENAI_BASE_URL'),
    api_key=os.getenv('OPENAI_API_KEY'),
)

二、方法一:FewShotPromptTemplate

2.1 基本概念

FewShotPromptTemplate 是一种基于字符串的少样本学习模板,适合传统的文本生成任务。

2.2 代码实现

# 1. 创建示例模板 - 定义单个示例的显示格式
example_prompt = PromptTemplate.from_template(
    template="input:{input}\noutput:{output}",
)

# 2. 提供训练示例
examples = [
    {"input": "北京天气怎么样", "output": "北京市"},
    {"input": "南京下雨吗", "output": "南京市"},
    {"input": "武汉热吗", "output": "武汉市"},
]

# 3. 创建FewShotPromptTemplate实例
few_shot_template = FewShotPromptTemplate(
    example_prompt=example_prompt,  # 单个示例的模板
    examples=examples,              # 示例数据
    suffix="input:{input}\noutput:", # 用户问题的格式
    input_variables=["input"]       # 需要填充的变量
)

# 4. 使用模板
prompt = few_shot_template.invoke({"input": "天津会下雨吗?"})
response = chat_model.invoke(prompt)
print(response.content)  # 输出:天津市

2.3 生成的提示词结构

input:北京天气怎么样
output:北京市

input:南京下雨吗
output:南京市

input:武汉热吗
output:武汉市

input:天津会下雨吗?
output:

2.4 特点总结

  • 简单直接:纯文本格式,易于理解

  • 适合场景:文本提取、格式转换等简单任务

  • 局限性:不支持对话角色,结构化程度低

三、方法二:FewShotChatMessagePromptTemplate

3.1 基本概念

FewShotChatMessagePromptTemplate 是专为对话场景设计的少样本模板,需要与 ChatPromptTemplate 配合使用。

3.2 代码实现

# 1. 创建对话示例模板
chat_example_prompt = ChatPromptTemplate.from_messages([
    ("human", "{input}"),    # 用户消息
    ("ai", "{output}"),      # AI回复
])

# 2. 提供示例数据
chat_examples = [
    {"input": "北京天气怎么样", "output": "北京市"},
    {"input": "南京下雨吗", "output": "南京市"},
    {"input": "武汉热吗", "output": "武汉市"},
]

# 3. 创建少样本消息模板
chat_few_shot_template = FewShotChatMessagePromptTemplate(
    example_prompt=chat_example_prompt,  # 示例模板
    examples=chat_examples,              # 示例数据
)

# 4. 关键步骤:与ChatPromptTemplate组合
final_prompt_template = ChatPromptTemplate.from_messages([
    ("system", "你是一个城市信息提取助手。请参考以下示例"),  # 系统指令
    chat_few_shot_template,  # 插入少样本示例
    ("human", "{input}"),    # 用户的实际问题
])

# 5. 使用模板
prompt2 = final_prompt_template.invoke({"input": "哈尔滨会下雪吗?"})
response2 = chat_model.invoke(prompt2)
print(response2.content)  # 输出:哈尔滨市

3.3 生成的提示词结构

System: 你是一个城市信息提取助手。请参考以下示例
Human: 北京天气怎么样
AI: 北京市
Human: 南京下雨吗
AI: 南京市
Human: 武汉热吗
AI: 武汉市
Human: 哈尔滨会下雪吗?

3.4 特点总结

  • 对话友好:明确区分用户和AI角色
  • 灵活组合:可与其他消息模板自由组合
  • 推荐使用:更适合现代对话应用
  • 注意:不能单独使用,必须与ChatPromptTemplate组合

四、两种方法对比

特性FewShotPromptTemplateFewShotChatMessagePromptTemplate
模板类型字符串模板消息模板
使用方式独立使用必须与ChatPromptTemplate组合
输出格式纯文本结构化消息
角色区分支持human/ai/system角色
适合场景简单文本任务对话式应用
推荐程度⭐⭐⭐⭐⭐⭐⭐⭐
Logo

这里是“一人公司”的成长家园。我们提供从产品曝光、技术变现到法律财税的全栈内容,并连接云服务、办公空间等稀缺资源,助你专注创造,无忧运营。

更多推荐