提示词(Prompts)
语言模型的提示是用户提供的一组指令或输入,用于指导模型的响应,帮助它理解上下文并生成相关且连贯的基于语言的输出,例如回答问题、完成句子或参与对话。
提示词模板(PromptTemplate)
使用PromptTemplate为字符串提示符创建模板。
默认情况下,PromptTemplate使用Python的str. format语法进行模板化
from langchain.prompts import PromptTemplate
prompt_template = PromptTemplate.from_template(
"Tell me a {adjective} joke about {content}."
)
prompt_template.format(adjective="funny", content="chickens")
该模板支持任意数量的变量,包括没有变量:
from langchain.prompts import PromptTemplate
prompt_template = PromptTemplate.from_template("Tell me a joke")
prompt_template.format()
聊天提示词模板(ChatPromptTemplate)
聊天模型/的提示是聊天消息列表。
每个聊天消息都与内容和一个称为角色的附加参数相关联。例如,在OpenAI Chat Completions API中,聊天消息可以与AI助手、人类或系统角色相关联。
创建一个聊天提示模板,如下所示
from langchain_core.prompts import ChatPromptTemplate
chat_template = ChatPromptTemplate.from_messages(
[
("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}"),
]
)
messages = chat_template.format_messages(name="Bob", user_input="What is your name?")
print(messages)
[SystemMessage(content='You are a helpf


2956

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



