langchain提示词模版

PromptTemplate

使用场景

单对话文本输出场景下使用,例如文本翻译 提取标签等

使用方式

prompt = PromptTemplate.from_template(
  "基于如下要求写一幅对联:{text}"
)
chain = prompt|model
print(chain.invoke({"text": "喜庆,给新人用的"}))

ChatPromptTemplate

使用场景

对话模式下使用,里面会包含对话的内容

使用方式

from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate(
     [
        ("system", "You are a helpful AI bot. Your name is {name}."),
        ("human", "Hello, how are you doing?"),
        ("ai", "I'm doing well, thanks!"),
        ("human", "{user_input}"),
    ]
)
print(prompt.format(name="小张", user_input="hello"))
  • 输出
System: You are a helpful AI bot. Your name is 小张.
Human: Hello, how are you doing?
AI: I'm doing well, thanks!
Human: hello

SystemMessagePromptTemplate、AIMessagePromptTemplate、HumanMessagePromptTemplate

使用场景

结合ChatPromptTemplate使用,将里面原来的用("<role>","<message>")替换为对象形式

使用方式

from langchain_core.prompts import ChatPromptTemplate,SystemMessagePromptTemplate,AIMessagePromptTemplate,HumanMessagePromptTemplate

prompt = ChatPromptTemplate(
     [
        SystemMessagePromptTemplate.from_template( "You are a helpful AI bot. Your name is {name}."),
        HumanMessagePromptTemplate.from_template("Hello, how are you doing?"),
        AIMessagePromptTemplate.from_template( "I'm doing well, thanks!"),
      HumanMessagePromptTemplate.from_template("{user_input}"),
    ]
)
print(prompt.format(name="小张", user_input="hello"))

FewShotPromptTemplate

使用场景

给大模型一些示例,需要结合PromptTemplate使用

使用示例

from langchain_core.prompts import FewShotPromptTemplate, PromptTemplate
example_data = [
  {"user_id":"123242","message":"格式不正确"},
{"user_id":"W23242","message":"处理成功"},
{"user_id":"A23242","message":"格式不正确"},
{"user_id":"W23242","message":"处理成功"},
] # 数据集

# 数据集的拼接模版
example_template = PromptTemplate.from_template(
    "用户ID:{user_id}\n处理结果:{message}"
)
few_prompt = FewShotPromptTemplate(
     examples=example_data ,                # 传入准备好的示例数据列表
    example_prompt=example_template ,  # 传入刚才定义的单个示例排版模板
    prefix="请模仿以下示例,处理数据:", # 放在所有示例前面的引导语
    suffix="用户ID:{user_id}\n处理结果:", # 放在所有示例后面的真实提问
)
print(few_prompt .format(user_id="2343242"))
  • 输出
用户ID:W23242
处理结果:处理成功

用户ID:A23242
处理结果:格式不正确

用户ID:W23242
处理结果:处理成功

用户ID:2343242
处理结果:

FewShotChatMessagePromptTemplate

使用场景

在对话模式下使用,主要是仿照出对话时的回答示例

使用方式

from langchain_core.prompts import FewChatMessagePromptTemplate, ChatPromptTemplate

# 1. 准备示例数据
examples = [
    {"input": "2+2", "output": "4"},
    {"input": "2+3", "output": "5"},
]
# 2. 定义单个示例的格式化模板(这里必须用 ChatPromptTemplate 的格式)
# 这里的 from_template 会自动把 input 和 output 转化为 HumanMessage 和 AIMessage
example_prompt = ChatPromptTemplate.from_messages(
    [
        ("human", "{input}"),
        ("ai", "{output}"),
    ]
)
# 3. 创建 FewShotChatMessagePromptTemplate
few_shot_prompt = FewShotChatMessagePromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
)

# 4. 将它嵌入到最终的 ChatPromptTemplate 中
final_prompt = ChatPromptTemplate.from_messages(
    [
        ("system", "你是一个计算小能手,请模仿示例回答用户的问题。"),
        few_shot_prompt,  # 直接把少样本提示作为消息的一部分插进来
        ("human", "{input}"),
    ]
)

# 5. 查看最终生成的消息结构
messages = final_prompt.invoke({"input": "10-3"})
for msg in messages:
    print(f"{msg.type}: {msg.content}")

# 输出结果会是一个完美的对话消息流:
# system: 你是一个计算小能手,请模仿示例回答用户的问题。
# human: 2+2
# ai: 4
# human: 2+3
# ai: 5
# human: 10-3

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容