如何创建模板提示prompt

定义模型

python 复制代码
from langchain_ollama import ChatOllama

llm =  ChatOllama(
    base_url="http://ip:11434",
    model="qwen2",
    temperature=0,
    tool_choice="auto"
)

什么是提示模板?

它的目的是根据不同的输入动态生成特定格式的文本,以便为大语言模型(如GPT)提供更清晰、结构化的指令或上下文。

提示模板的结构:

一个提示模板通常包含以下部分:

静态文本部分:不会随输入变化的文本。这部分通常是用于引导模型的固定指令。

动态占位符部分:用于插入不同输入数据的占位符,随实际输入变化。例如, {input_text} 或 {user_question} 就是占位符,稍后会被替换为实际输入。

python 复制代码
from langchain import PromptTemplate


template = """\
您是新公司的命名顾问。
生产{product}的公司起什么好名字?
"""

prompt = PromptTemplate.from_template(template)
input=prompt.format(product="无添加剂的饼干")
response = llm.invoke(input)
print(response.content)

创建提示模板

您可以使用 PromptTemplate 类创建简单的硬编码提示。提示模板可以采用任意数量的输入变量,并且可以格式化以生成提示。

python 复制代码
from langchain import PromptTemplate

# 没有输入变量的示例提示
no_input_prompt = PromptTemplate(input_variables=[], template="给我讲个笑话。")
no_input_prompt.format()
# -> "给我讲个笑话。"

# 带有一个输入变量的示例提示
one_input_prompt = PromptTemplate(input_variables=["adjective"], template="给我讲一个{adjective}笑话。")
one_input_prompt.format(adjective="有趣")
# -> "给我讲一个有趣的笑话。"

# 具有多个输入变量的示例提示
multiple_input_prompt = PromptTemplate(
    input_variables=["adjective", "content"], 
    template="给我讲一个关于{content}的{adjective}笑话。"
)
input=multiple_input_prompt.format(adjective="funny", content="chickens")
# -> "给我讲一个关于鸡的有趣笑话。"
response = llm.invoke(input)
print(response.content)

如果您不想手动指定 input_variables,您还可以使用 from_template 类方法创建 PromptTemplate。 langchain 将根据传递的模板自动推断 input_variables。

python 复制代码
template = "给我讲一个关于{content}的{adjective}笑话。"

prompt_template = PromptTemplate.from_template(template)
prompt_template.input_variables
# -> ['adjective', 'content']
input=prompt_template.format(adjective="funny", content="chickens")
# -> 给我讲一个关于鸡的有趣笑话。
response = llm.invoke(input)
print(response.content)

聊天提示模板(Chat prompt template)

聊天模型将聊天消息列表作为输入 - 该列表通常称为提示(prompt)。这些聊天消息与原始字符串(您将传递到 LLM 模型中)不同,因为每条消息都与一个角色关联。

例如,在 OpenAI 的Chat Completion API中,聊天消息可以与 AI、人类或系统角色相关联。该模型会更紧密地遵循系统聊天消息的指令。

LangChain 提供了多种提示模板,可以轻松构建和使用提示。官方鼓励在查询聊天模型时使用这些聊天相关的提示模板而不是 PromptTemplate,以充分利用底层聊天模型的潜力。

python 复制代码
from langchain.prompts import (
    ChatPromptTemplate,
    PromptTemplate,
    SystemMessagePromptTemplate,
    AIMessagePromptTemplate,
    HumanMessagePromptTemplate,
)
from langchain.schema import (
    AIMessage,
    HumanMessage,
    SystemMessage
)

要创建与角色关联的消息模板,请使用 MessagePromptTemplate。

为了方便起见,模板上公开了一个 from_template 方法。如果您要使用此模板,它将如下所示:

python 复制代码
template="您是将 {input_language} 翻译成 {output_language} 的得力助手。"
# 创建角色:系统的模板
system_message_prompt = SystemMessagePromptTemplate.from_template(template)
human_template="{text}"
# 创建角色:人类的模板
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)

如果你想更直接地构造MessagePromptTemplate,你也可以在外部创建一个PromptTemplate,然后将其传入,例如:

python 复制代码
# 创建一个常规的模板
prompt=PromptTemplate(
    template="您是将 {input_language} 翻译成 {output_language} 的得力助手。",
    input_variables=["input_language", "output_language"],
)
# 再创建一个角色:系统 的模板
system_message_prompt_2 = SystemMessagePromptTemplate(prompt=prompt)
# 判断和之前创建的是否一样
assert system_message_prompt == system_message_prompt_2
system_message_prompt == system_message_prompt_2

之后,您可以从一个或多个 MessagePromptTemplate 构建 ChatPromptTemplate。

我们可以使用 ChatPromptTemplate 的 format_prompt ------这会返回一个 PromptValue,您可以将其转换为字符串或 Message 对象,具体取决于您是否想要使用格式化值作为 llm 或聊天模型的输入。

python 复制代码
chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])

# 从格式化消息中获取聊天完成信息
prompt=chat_prompt.format_prompt(input_language="English", output_language="Chinese", text="I love programming.").to_messages()
response = llm.invoke(prompt)
print(response.content)

总结

  1. 如何创建模板提示:
    方式一:PromptTemplate(input_variables=[], template="Tell me a joke."),这种要写input_variables。
    方式二:template = "Tell me a {adjective} joke about {content}." prompt_template = PromptTemplate.from_template(template),这种不用写input_variables。
  2. 如何创建messageTemplate,我们常常需要与角色相关联:
    角色有:
  • AI(AIMessagePromptTemplate)、
  • 人类(HumanMessagePromptTemplate)、
  • 系统(SystemMessagePromptTemplate)。
    最后利用ChatPromptTemplate.from_messages([xxx])方法,整合这些角色,就构造出了chat prompt。
相关推荐
背太阳的牧羊人19 小时前
RAG检索中使用一个 长上下文重排序器(Long Context Reorder) 对检索到的文档进行进一步的处理和排序,优化输出顺序
开发语言·人工智能·python·langchain·rag
Java知识技术分享20 小时前
使用LangChain构建第一个ReAct Agent
python·react.js·ai·语言模型·langchain
workflower1 天前
Prompt Engineering的重要性
大数据·人工智能·设计模式·prompt·软件工程·需求分析·ai编程
forestsea2 天前
DeepSeek 提示词:定义、作用、分类与设计原则
人工智能·prompt·deepseek
圆内~搁浅2 天前
langchain本地知识库问答机器人集成本地知识库
数据库·langchain·机器人
Neo很努力3 天前
【deepseek】本地部署+RAG知识库挂载+对话测试
自然语言处理·chatgpt·langchain·aigc·llama
小白顶呱呱3 天前
图解【提示工程 VS 微调 VS RAG、全量微调 VS LoRA微调、TopK VS TopP】截图笔记
笔记·大模型·prompt·rag·大模型微调
merlin-mm3 天前
langchain应用-agent
langchain
朴拙数科4 天前
Langchain vs. LlamaIndex:哪个在集成MongoDB并分析资产负债表时效果更好?
数据库·mongodb·langchain
Allen-Steven4 天前
《Stable Diffusion绘画完全指南:从入门到精通的Prompt设计艺术》-配套代码示例
人工智能·pytorch·深度学习·stable diffusion·prompt·checkpoint