LangChain使用提示词模版

from langchain_core.prompts import PromptTemplate, ChatPromptTemplate

# 1.  字符串提示词
prompt_template = PromptTemplate.from_template("hello, my name is {name}, nice to meet you")
print(prompt_template)

# 实例化
prompt_1=prompt_template.invoke({"name":"Alice"})
print(prompt_1) # text='hello, my name is Alice, nice to meet you'

# 提示词处理
# 转字符串
print(prompt_1.to_string()) # hello, my name is Alice, nice to meet you

# 转messages
print(prompt_1.to_messages()) # [HumanMessage(content='hello, my name is Alice, nice to meet you', additional_kwargs={}, response_metadata={})]


# 2 聊天提示词模版
chat_prompt_template = ChatPromptTemplate.from_messages(
    [
        ("system","回复内容不超过{max_len}个字"),
        ("user","hello, my name is {name}, nice to meet you"),
        ("user","hello")
    ]
)
prompt_2 = chat_prompt_template.invoke({"max_len":100,"name":"Alice"})
print(prompt_2.to_messages()) #[SystemMessage(content='回复内容不超过100个字', additional_kwargs={}, response_metadata={}), HumanMessage(content='hello, my name is Alice, nice to meet you', additional_kwargs={}, response_metadata={}), HumanMessage(content='hello', additional_kwargs={}, response_metadata={})]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值