langchain 提示词(一) 字符提示词和聊天提示词

1,简介

‌LangChain中的提示词(Prompts)‌是用户与模型交互的方式,即模型的输入,通过提示词可以让模型返回用户期望的内容。提示词在LLM(Large Language Models,大语言模型)领域中扮演着关键角色,它不仅是模型接收信息的入口,也是模型生成输出的起点。
提示词也是用于生成语言模型提示词的预定义脚本。语言模型能够接受的提示词是字符串或聊天消息列表。
提示词中提供了两个提示词模版:
PromptTemplate:字符提示词模版,模版支持任意数量的变量,包括无边量。
ChatPromptTemplate:聊天提示词模版,聊天模版的提示是一个聊天消息列表。每条聊天消息都是有角色和内容组成。

2,代码示例

字符提示词PromptTemplate

from langchain.prompts import PromptTemplate

#有变量
prompt_template = PromptTemplate.from_template(
    "给我讲一个关于{content}的{adjective}笑话。"
)
rep = prompt_template.format(adjective="有意思的", content="猫")
print(rep)
#给我讲一个关于猫的有意思的笑话。

#无变量
prompt_template = PromptTemplate.from_template(
    "给我讲个笑话"
)
rep = prompt_template.for
### LangChain Prompt Templates 使用详解 LangChain种强大的工具,支持多种提示模板(Prompt Template),这些模板可以显著简化开发流程并提高效率。以下是关于 `FewShotPromptTemplate` `FewShotChatMessagePromptTemplate` 的具体用法及其在少样本学习中的应用。 #### FewShotPromptTemplate 此模板适用于通用文本生成任务,能够帮助开发者快速构建基于少量样例的学习模型。它通过提供固定的示例来引导模型生成期望的结果[^2]。 下面是个简单的代码实现: ```python from langchain.prompts import FewShotPromptTemplate, PromptTemplate # 定义单个示例的结构 example_prompt = PromptTemplate( input_variables=["input", "output"], template="Input: {input}\nOutput: {output}", ) # 创建 FewShotPromptTemplate 实例 few_shot_prompt = FewShotPromptTemplate( examples=[ {"input": "What is the capital of France?", "output": "Paris"}, {"input": "Who wrote 'Romeo and Juliet'?", "output": "Shakespeare"} ], example_prompt=example_prompt, prefix="You are a helpful assistant that answers questions.", suffix="Now answer this question:\n{question}", input_variables=["question"] ) # 渲染最终的提示语句 final_prompt = few_shot_prompt.format(question="What is 2+2?") print(final_prompt) ``` 上述代码展示了如何定义个带有固定示例的 `FewShotPromptTemplate` 并渲染成完整的提示字符串[^3]。 --- #### FewShotChatMessagePromptTemplate 该模板专为聊天对话系统设计,特别适合模拟人类与 AI 的交互过程。它的核心功能在于将每条示例转化为多条消息,从而更好地适应对话场景的需求。 以下是段典型的代码片段: ```python from langchain.prompts.chat import ( ChatPromptTemplate, FewShotChatMessagePromptTemplate, HumanMessagePromptTemplate, AIMessagePromptTemplate, ) # 单个示例的消息结构 human_message_prompt = HumanMessagePromptTemplate.from_template("{input}") ai_message_prompt = AIMessagePromptTemplate.from_template("{output}") # 将两者组合为组消息 message_pair = [human_message_prompt, ai_message_prompt] # 构建 FewShotChatMessagePromptTemplate few_shot_chat_message_prompt = FewShotChatMessagePromptTemplate( messages=message_pair, example_separator="\n" ) # 添加前缀后缀以完成整个提示模板 chat_prompt = ChatPromptTemplate( messages=[ few_shot_chat_message_prompt, HumanMessagePromptTemplate.from_template("User query: {query}"), ] ) # 格式化最终的提示内容 formatted_prompt = chat_prompt.format_messages( examples=[{"input": "Hello!", "output": "Hi there!"}], query="How can I help you?" ) for msg in formatted_prompt: print(f"{msg.type}: {msg.content}") ``` 这段代码说明了如何利用 `FewShotChatMessagePromptTemplate` 来创建复杂的对话型提示模板,并将其应用于实际场景中。 --- ### 总结 无论是针对传统文本生成还是对话系统的特殊需求,LangChain 提供的这两种提示模板都能有效提升工作效率。前者更适合般性的自然语言处理任务;后者则专注于增强人机互动体验。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值