1. 基本提示词模板:PromptTemplate
模板依赖包及导入方式
langchain0.1版本,组件langchain.prompts;
from langchain.prompts import PromptTemplate
langchain0.2及以上版本,调整架构,组件langchain_core.prompts
from langchain_core.prompts import PromptTemplate
PromptTemplate 构造方法的参数及其使用
| 参数名 | 类型 | 是否必传 | 核心作用 |
|---|
| template | str | 必传 | 提示词模板字符串,包含{变量名}占位符 |
| input_variables | list[str] | 可选 | 模板中所有占位符的名称列表,用于校验参数完整性 |
| template_format | f-string/jinja2 | 可选 | 模板解析格式,默认f-string |
| validate_template | f-string/jinja2 | 可选 | 是否校验模板中占位符与input_variables匹配,默认True;具体项目中也无需修改为false,可忽略此参数 |
| partial_variables | dict | 可选 | 部分固定参数 |
template + input_variables + template_format="f-string"(参数默认,可缺省)的使用
一个占位符
from langchain_core.prompts import PromptTemplate
tmp = """ 我是一位{varliable}初学者 """
prompt_template = PromptTemplate(
template = tmp,
input_variables = ["varliable"]
)
""" 给占位符赋值的一种方式,langchain1.0前的用法
final_prompt = prompt_template.format(varliable="AI应用")
print(final_prompt)
"""
result = prompt_template.invoke( {"varliable":"AI应用"} )
print(result.text)
多个占位符
from langchain_core.prompts import PromptTemplate
tmp = """ 我是一位{varliable}初学者,练习时长{learnLenth} """
prompt_template = PromptTemplate(
template = tmp,
input_variables = ["varliable","learnLenth"]
)
result = prompt_template.invoke( {"varliable":"AI应用","learnLenth":"两年半"} )
print(result.text)
template_format 'jinja2'的使用
| f-string | jinja2 | 功能 |
|---|
| {varliable} | {{varliable}} | 占位符 |
| {% if 条件 %} {% endif %} | 条件判断 |
| {% for 元素 in 列表 %} {% endfor %} | 循环语句 |
jinja2
from langchain_core.prompts import PromptTemplate
tmp = """ 我是一位{{varliable}}初学者 """
prompt_template = PromptTemplate(
template = tmp,
input_variables=["varliable"],
template_format="jinja2"
)
result = prompt_template.invoke({"varliable":"AI应用"})
print(result.text)
{% if 条件 %} {% endif %}
from langchain_core.prompts import PromptTemplate
tmp = """
你是一名{{role}},为{{user_type}}撰写文案
{% if user_type == "学生" %}
要求:语言活泼,突出性价比,不超过80字
{% elif user_type == "职场人" %}
要求:语言专业,突出效率,不超过100字。
{% else %}
要求:通用风格,简洁明了,不超过90字。
{% endif %}
产品:{{product}},卖点:{{selling_point}}
"""
prompt_template = PromptTemplate(
template = tmp,
input_variables=["role","user_type","product","selling_point"],
template_format="jinja2"
)
result = prompt_template.invoke({"role":"专家","user_type":"学生","product":"蓝牙耳机","selling_point":"省电"})
print(result.text)
result = prompt_template.invoke({"role":"专家","user_type":"职场人","product":"蓝牙耳机","selling_point":"省电"})
print(result.text)
result = prompt_template.invoke({"role":"专家","user_type":"学者","product":"蓝牙耳机","selling_point":"省电"})
{% for 元素 in 列表 %}
from langchain_core.prompts import PromptTemplate
template = """
{% for trunk in trunks %}
- {{trunk}}
{% endfor %}
"""
prompt_template = PromptTemplate(
template=template,
input_variables=["trunks"],
template_format="jinja2"
)
result = prompt_template.invoke({"trunks":["北京今天多云", "气温10-20℃", "微风"]})
print(result.text)
""" 输出
- 北京今天多云
- 气温10-20℃
- 微风
"""
partial_variables 的使用
from langchain_core.prompts import PromptTemplate
tmp = """ 我是一位{varliable}初学者,练习时长{learnLenth} """
prompt_template = PromptTemplate(
template = tmp,
input_variables = ["varliable","learnLenth"],
partial_variables={"learnLenth":"两年半"}
)
result = prompt_template.invoke( {"varliable":"AI应用"} )
print(result.text)