利用LangChain实现高效提示工程与链式应用
1. 提示模板创建与使用
在使用大语言模型(LLMs)时,创建一个合适的提示模板是关键的第一步。下面我们将详细介绍如何使用LangChain库创建一个聊天提示模板,并结合用户输入生成聊天完成结果。
1.1 创建提示模板
我们使用 ChatPromptTemplate 类的 from_messages() 方法来创建一个提示模板实例。这个方法接受一个消息提示模板列表作为参数,其中包含系统消息提示模板和人类消息提示模板。
from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate
prompt = ChatPromptTemplate.from_messages([
SystemMessagePromptTemplate.from_template(
"You are an enthusiastic assistant that rewrites the user's text to sound more exciting."
),
HumanMessagePromptTemplate.from_template("{text}"),
])
在上述代码中,系统消息提示模板定义了助手的角色和行为,即重写用户输入的文本,使其听起来更令人兴奋。人类消息提示模板包含一个
超级会员免费看
订阅专栏 解锁全文
4602

被折叠的 条评论
为什么被折叠?



