1、介绍与分类
PromptTemplate就是通过模版管理大模型的输入。接收用户的输入,返回一个传递给LLM的信息(提示词)。它在调用时:
- 以字典作为输入,每个键代表要填充的提示模版中的变量;
- 输出是PromptValue,该输出可以传递给LLM或者ChatModel,并且可以转换类型(转为字符串或者消息列表)。
接下来是几种常见的提示词模版:
- PromptTemplate:LLM提示模版,用于生成字符串提示;
- ChatPromptTemplate:聊天提示模版,用于组合各种角色的消息模版,传入聊天模型;
- FewShotPromptTemplate:样本提示词模版,通过少量示例教给模型如何回答。
本篇主要基于第一种类型------PromptTemplate来展开。
2、PromptTemplate的使用
2.1 如何获取实例
2.1.1 使用构造方法
python
from langchain_core.prompts import PromptTemplate
# 1、创建PromptTemplate实例
# 以下参数必须指明
prompt_template = PromptTemplate(
template = "你是一个{role},你的名字是{name}。",
input_variables = ["role","name"],
)
# print(prompt_template)
# 2、填充实例中变量
prompt = prompt_template.format(role = "人工智能专家",name = "小志")
print(prompt) # 输出:你是一个人工智能专家,你的名字是小志.
2.1.2 使用from_template()方法✅️
python
from langchain_core.prompts import PromptTemplate
# 1、创建PromptTemplate实例
prompt_template = PromptTemplate.from_template(template = "你是一个{role},你的名字是{name}。")
# print(prompt_template)
# 2、填充实例中变量
prompt = prompt_template.format(role = "人工智能专家",name = "小志")
print(prompt)# 输出:你是一个人工智能专家,你的名字是小志。
如果提示词模版中不包含变量,调用format()时不需要传参数。
2.2 两种特殊结构的使用
2.2.1 部分提示词模版使用 ✅️
在生成prompt前就已经提前初始化部分的提示词,实际导入模版时只导入未初始化的变量。
(1)使用partial_variables变量
python
from langchain_core.prompts import PromptTemplate
prompt_template = PromptTemplate.from_template(
template = "请评价{product}的优缺点,包括{aspect1}和{aspect2}。",
partial_variables = {"aspect1":"电池续航"}
)
prompt = prompt_template.format(product = "手机",aspect2 = "价格")
print(prompt) #输出:请评价手机的优缺点,包括电池续航和价格。
(2)调用方法partial()
python
from langchain_core.prompts import PromptTemplate
prompt_template = PromptTemplate.from_template(
template = "请评价{product}的优缺点,包括{aspect1}和{aspect2}。",
# partial_variables = {"aspect1":"电池续航"}
)
# .partial()方法调用后原本的模版没变化,返回值的模版起作用
template = prompt_template.partial(aspect1="电池续航")
prompt = template.format(product = "手机",aspect2 = "价格")
print(prompt)
2.2.2 组合提示词使用
python
from langchain_core.prompts import PromptTemplate
template = (
PromptTemplate.from_template("Tell me a joke about {topic}")
+ ", make it funny"
+ "\n\nand in {language}"
)
prompt = template.format(topic="sports", language="spanish")
print(prompt) # 输出:Tell me a joke about sports, make it funny
and in spanish
3、变量赋值的两种方式
上述代码的变量赋值均是采用的format(),这种方式返回值为字符串类型。
另一种方式就是invoke(),他的参数部分是字典 ,返回值是PromptValue类型。
python
from langchain_core.prompts import PromptTemplate
# 1、创建PromptTemplate实例
prompt_template = PromptTemplate.from_template(template = "你是一个{role},你的名字是{name}。")
# 2、填充实例中变量
prompt = prompt_template.invoke(input = {"role":"人工智能专家","name":"小志"})
print(prompt)
print(type(prompt)) # <class 'langchain_core.prompt_values.StringPromptValue'>
4、结合大模型调用
调用对话大模型,使用gpt-4o-mini
python
from langchain_openai import ChatOpenAI
from langchain_core.prompts import PromptTemplate
import os
import dotenv
dotenv.load_dotenv()
os.environ["OPENAI_BASE_URL"] = os.getenv("OPENAI_BASE_URL")
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
# 获取对话模型
chat_model = ChatOpenAI(
model = "gpt-4o-mini",
temperature = 0.7,
#max_tokens = 20,
)
# 生成提示词模版
template = PromptTemplate.from_template(template = "请论述{product}在{aspect}上的价值。")
# 模版变量赋值
prompt = template.invoke(input = {"product":"AI","aspect":"医疗"})
# 调用大模型
response = chat_model.invoke(prompt)
print(response.content)
