LangChain 框架入门:模型抽象与 Prompt 模板
系列文章 :RAG 与 LangChain 开发实战(2/7)
阅读时间 :约 20 分钟
前置知识 :LLM 基础与提示词工程:从 API 调用到 Prompt 优化技巧
一、为什么需要 LangChain?
直接使用原始 API 开发 LLM 应用存在诸多痛点:
python
# 原始 API 调用 - 每个模型都要重新写
from openai import OpenAI
client = OpenAI(...)
response = client.chat.completions.create(...)
# 换个模型又要改代码
from dashscope import Generation
response = Generation.call(...)
LangChain 的核心理念:
为各种 LLM 实现通用接口,把 LLM 相关的组件"链接"在一起,简化开发难度,方便快速构建复杂应用。
二、LangChain 的三种模型类型
LangChain 统一抽象了三种模型类型:
┌─────────────────────────────────────────┐
│ LangChain 支持的三种模型类型 │
├─────────────────────────────────────────┤
│ 1. LLMs - 语言大模型 │
│ 2. Chat Models - 聊天模型 │
│ 3. Embeddings Models - 嵌入模型 │
└─────────────────────────────────────────┘
2.1 LLMs - 语言大模型
适用于通用文本生成任务,来自 langchain_community 库。
python
from langchain_community.llms.tongyi import Tongyi
llm = Tongyi(model='qwen-max')
# invoke 方法 - 一次性返回完整结果
res = llm.invoke("帮我讲个笑话吧")
print(res)
# stream 方法 - 流式输出
res = llm.stream("帮我讲个笑话吧")
for chunk in res:
print(chunk, end="", flush=True)
参数说明:
end="":去掉结束换行符flush=True:立即刷新输出缓冲区
2.2 Chat Models - 聊天模型
专为对话场景设计,支持三种消息类型:
python
from langchain_community.chat_models.tongyi import ChatTongyi
from langchain_core.messages import HumanMessage, SystemMessage, AIMessage
chat = ChatTongyi(model="qwen3-max")
messages = [
SystemMessage(content="你是一个边塞诗人"),
HumanMessage(content="给我写一首唐诗"),
AIMessage(content="锄禾日当午,汗滴禾下土"),
HumanMessage(content="按照上一个回复的格式,再写一首")
]
for chunk in chat.stream(input=messages):
print(chunk.content, end="", flush=True)
消息类型:
| 类型 | 说明 |
|---|---|
SystemMessage |
系统指令,设定角色和行为 |
HumanMessage |
用户输入 |
AIMessage |
AI 回复 |
2.3 消息的简化写法
LangChain 支持更简洁的元组写法:
python
from langchain_community.chat_models.tongyi import ChatTongyi
model = ChatTongyi(model="qwen3-max")
# 简化写法 - 动态模板,支持变量占位
messages = [
("system", "你是一个边塞诗人"),
("human", "给我写一首唐诗"),
("ai", "锄禾日当午,汗滴禾下土"),
("human", "再来一首"),
]
res = model.stream(input=messages)
for chunk in res:
print(chunk.content, end="", flush=True)
优势 :支持内部填充 {变量} 占位符
python
messages = [
("system", "今天的天气是{weather}")
]
2.4 Embeddings Models - 嵌入模型
将文本转换为向量,用于相似度计算和检索。
python
from langchain_community.embeddings import DashScopeEmbeddings
embeddings = DashScopeEmbeddings()
# 单个文本转向量
vector = embeddings.embed_query("我喜欢你")
print(vector) # [-3.02, 3.31, 4.41, ...]
# 批量文本转向量
vectors = embeddings.embed_documents(['我喜欢你', '我稀饭你', '晚上吃啥'])
三、Prompt 模板详解
LangChain 提供了三种核心 Prompt 模板类,均继承自 Runnable 接口。
3.1 PromptTemplate - Zero-Shot 模板
最基础的提示词模板,支持变量注入。
python
from langchain_core.prompts import PromptTemplate
from langchain_community.llms.tongyi import Tongyi
# 创建模板
prompt_template = PromptTemplate.from_template(
"我的邻居姓{lastname}, 刚生了{gender}, 帮我起个名字,简单回答。"
)
# 方式 1:直接生成提示词文本
prompt_text = prompt_template.format(lastname="张", gender="女儿")
model = Tongyi(model="qwen-max")
res = model.invoke(input=prompt_text)
# 方式 2:使用 Chain 链式调用(推荐)
chain = prompt_template | model
res = chain.invoke({"lastname": "张", "gender": "女儿"})
print(res)
3.2 FewShotPromptTemplate - Few-Shot模板
支持动态注入任意数量的示例。
python
from langchain_core.prompts import PromptTemplate, FewShotPromptTemplate
from langchain_community.llms.tongyi import Tongyi
# 示例模板
example_template = PromptTemplate.from_template("单词:{word}, 反义词:{antonym}")
# 示例数据 - list 内套字典
examples_data = [
{"word": "大", "antonym": "小"},
{"word": "上", "antonym": "下"},
]
few_shot_template = FewShotPromptTemplate(
example_prompt=example_template, # 示例模板
examples=examples_data, # 示例数据
prefix="告知我单词的反义词,我提供如下的示例:",
suffix="基于前面的示例告知我,{input_word}的反义词是?",
input_variables=['input_word'] # 声明需要的变量
)
# 生成提示词
prompt_text = few_shot_template.invoke({"input_word": "左"}).to_string()
print(prompt_text)
# 调用模型
model = Tongyi(model="qwen-max")
print(model.invoke(input=prompt_text))
生成的 Prompt 示例:
告知我单词的反义词,我提供如下的示例:
单词:大,反义词:小
单词:上,反义词:下
基于前面的示例告知我,左的反义词是?
3.3 ChatPromptTemplate - 聊天模板
专为对话场景设计,支持注入历史会话。
python
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_community.chat_models.tongyi import ChatTongyi
chat_prompt_template = ChatPromptTemplate.from_messages(
[
("system", "你是一个边塞诗人,可以作诗。"),
MessagesPlaceholder("history"), # 动态插入历史消息
("human", "请再来一首唐诗"),
]
)
# 历史数据
history_data = [
("human", "你来写一个唐诗"),
("ai", "床前明月光,疑是地上霜"),
("human", "好诗再来一个"),
("ai", "锄禾日当午,汗滴禾下土"),
]
# 生成提示词
prompt_text = chat_prompt_template.invoke({"history": history_data}).to_string()
MessagesPlaceholder 的作用:
- 动态插入历史消息列表
- 让模型知晓对话上下文
- 是实现多轮对话的关键
四、三种模板对比
| 模板类型 | 适用场景 | 核心特点 |
|---|---|---|
PromptTemplate |
简单单次任务 | 支持变量注入 |
FewShotPromptTemplate |
需要示例的任务 | 支持动态示例 |
ChatPromptTemplate |
多轮对话 | 支持历史消息 |
五、format 与 invoke 方法
所有模板类都支持两种变量注入方式:
python
# format 方法 - 直接传入变量
prompt_text = prompt_template.format(lastname="张", gender="女儿")
# invoke 方法 - 传入字典,返回 PromptValue
prompt_value = few_shot_template.invoke({"input_word": "左"})
prompt_text = prompt_value.to_string()
区别:
format()直接返回字符串invoke()返回PromptValue对象(可调用.to_string()转为字符串)
六、关键知识点总结
6.1 核心概念
| 概念 | 说明 |
|---|---|
| LLMs | 通用语言模型,langchain_community.llms |
| Chat Models | 对话模型,支持 System/Human/AI 消息 |
| Embeddings | 文本转向量,用于检索 |
| PromptTemplate | 基础模板,支持变量 |
| FewShotPromptTemplate | 带示例的模板 |
| ChatPromptTemplate | 聊天模板,支持历史 |
| MessagesPlaceholder | 动态插入历史消息 |
6.2 代码速查
python
# LLM 调用
from langchain_community.llms.tongyi import Tongyi
llm = Tongyi(model='qwen-max')
res = llm.invoke("问题")
# Chat Model 调用
from langchain_community.chat_models.tongyi import ChatTongyi
chat = ChatTongyi(model="qwen3-max")
res = chat.stream(input=messages)
# 嵌入模型
from langchain_community.embeddings import DashScopeEmbeddings
emb = DashScopeEmbeddings()
vector = emb.embed_query("文本")
# Prompt 模板
from langchain_core.prompts import PromptTemplate, ChatPromptTemplate, MessagesPlaceholder
prompt = PromptTemplate.from_template("模板{变量}")
七、下一步
本文介绍了 LangChain 的模型抽象和 Prompt 模板。但你可能注意到:
- 多个组件如何串联?
- 上一个组件的输出如何作为下一个组件的输入?
- 如何实现复杂的数据流转?
下一篇 我们将深入讲解 Chain 链式调用 和 输出解析器,这是 LangChain 最强大的功能之一。
系列文章导航
- LLM 基础与提示词工程:从 API 调用到 Prompt 优化技巧
- LangChain 框架入门:模型抽象与 Prompt 模板(本文)
- LangChain 进阶:Chain 链与输出解析器
- LangChain 记忆与文档处理
- RAG 核心概念与向量存储
- RAG 实战 (上):知识库构建
- RAG 实战 (下):智能客服系统
版权声明:本文基于学习笔记整理,转载请注明出处。