|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 糖逗 于 2026-3-7 18:58 编辑
1. LangChain中的提示词模板介绍
LangChain提供了PromptTemplate类,用来协助优化提示词。PromptTemplate表示提示词模板,可以构建一个自定义的基础出提示词模板,支持变量的注入,最终生成所需的提示词。
PS:PromptTemplat的好处是不仅支持占位符{变量}值的动态变化,而可以基于chain链的写法(后续学习)
2. 代码实战
①通用提示词模板
- #方法1:标准写法
- from langchain_core.prompts import PromptTemplate
- from langchain_community.llms.tongyi import Tongyi
- # zero-shot
- prompt_template = PromptTemplate.from_template(
- "我的邻居姓{lastname}, 刚生了{gender}, 你帮我起个名字,简单回答。"
- )
- model = Tongyi(model="qwen-max")
- #调用.format方法注入信息即可
- prompt_text = prompt_template.format(lastname="张", gender="女儿")
- model = Tongyi(model="qwen-max")
- res = model.invoke(input=prompt_text)
- print(res)
- #方法2:基于chain链的写法
- from langchain_core.prompts import PromptTemplate
- from langchain_community.llms.tongyi import Tongyi
- # zero-shot
- prompt_template = PromptTemplate.from_template(
- "我的邻居姓{lastname}, 刚生了{gender}, 你帮我起个名字,简单回答。"
- )
- model = Tongyi(model="qwen-max")
- chain = prompt_template | model
- res = chain.invoke(input={"lastname": "张", "gender": "女儿"})
- print(res)
复制代码 ②fewshot提示词模板
- 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, # 示例的数据(用来注入动态数据的),list内套字典
- prefix="告知我单词的反义词,我提供如下的示例:", # 示例之前的提示词
- suffix="基于前面的示例告知我,{input_word}的反义词是?", # 示例之后的提示词
- input_variables=['input_word'] # 声明在前缀或后缀中所需要注入的变量名
- )
- prompt_text = few_shot_template.invoke(input={"input_word": "左"}).to_string()
- print(prompt_text)
- model = Tongyi(model="qwen-max")
- print(model.invoke(input=prompt_text))
复制代码 ③会话提示词模板
会话提示词模板支持注入任意数量的历史会话信息
- 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", "锄禾日当午,汗滴禾下锄,谁知盘中餐,粒粒皆辛苦"),
- ]
- # StringPromptValue to_string()
- prompt_text = chat_prompt_template.invoke({"history": history_data}).to_string()
- model = ChatTongyi(model="qwen3-max")
- res = model.invoke(prompt_text)
- print(res.content, type(res))
复制代码
3. format和invoke方法
- 在PromptTemplate(通用提示词模板)和FewShotPromptTenmplate(FewShot提示词模板)的使用中,分别用了format和invoke方法
- from langchain_core.prompts import PromptTemplate
- from langchain_core.prompts import FewShotPromptTemplate
- from langchain_core.prompts import ChatPromptTemplate
- from langchain_community.llms.tongyi import Tongyi
- from langchain_community.chat_models.tongyi import ChatTongyi
- """
- 继承关系
- PromptTemplate -> StringPromptTemplate -> BasePromptTemplate -> RunnableSerializable -> Runnable
- FewShotPromptTemplate -> StringPromptTemplate -> BasePromptTemplate -> RunnableSerializable -> Runnable
- ChatPromptTemplate -> BaseChatPromptTemplate -> BasePromptTemplate -> RunnableSerializable -> Runnable
- Tongyi -> BaseLLM -> BaseLanguageModel -> RunnableSerializable -> Runnable
- ChatTongyi -> BaseChatModel -> BaseLanguageModel -> RunnableSerializable -> Runnable
- """
- template = PromptTemplate.from_template("我的邻居是:{lastname},最喜欢:{hobby}")
- res = template.format(lastname="张大明", hobby="钓鱼")
- print(res, type(res))
- res2 = template.invoke({"lastname": "周杰轮", "hobby": "唱歌"})
- print(res2, type(res2))
复制代码
学习视频:【黑马程序员大模型RAG与Agent智能体项目实战教程,基于主流的LangChain技术从大模型提示词到实战项目】 https://www.bilibili.com/video/BV1yjz5BLEoY/?p=28&share_source=copy_web&vd_source=792a2cb63a1882bff4ed856eadc41a71
|
评分
-
查看全部评分
|