langchain-提示词模板的用法

25 阅读3分钟

1. 基本提示词模板:PromptTemplate

模板依赖包及导入方式

langchain0.1版本,组件langchain.prompts;
    from langchain.prompts import PromptTemplate
langchain0.2及以上版本,调整架构,组件langchain_core.prompts
    from langchain_core.prompts import PromptTemplate
    

PromptTemplate 构造方法的参数及其使用

参数名类型是否必传核心作用
templatestr必传提示词模板字符串,包含{变量名}占位符
input_variableslist[str]可选模板中所有占位符的名称列表,用于校验参数完整性
template_formatf-string/jinja2可选模板解析格式,默认f-string
validate_templatef-string/jinja2可选是否校验模板中占位符与input_variables匹配,默认True;具体项目中也无需修改为false,可忽略此参数
partial_variablesdict可选部分固定参数

template + input_variables + template_format="f-string"(参数默认,可缺省)的使用

一个占位符

from langchain_core.prompts import PromptTemplate

# {varliable}是一个占位符,可通过接口修改,改变输入内容
tmp = """ 我是一位{varliable}初学者 """ 

prompt_template = PromptTemplate(
   template = tmp,
   input_variables = ["varliable"] # 列表类型
)

""" 给占位符赋值的一种方式,langchain1.0前的用法; .format返回值是str类型
final_prompt = prompt_template.format(varliable="AI应用")
print(final_prompt)
"""

# 给占位符赋值的另一种方式,langchain1.0后的用法,给占位符赋值;PromptTemplate的.invoke()返回值是StringPromptValue封装纯文本提示词,可通过.text获取字符串

result = prompt_template.invoke( {"varliable":"AI应用"} ) 
print(result.text)

# 输出  我是一位AI应用初学者

多个占位符

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)

# 输出  我是一位AI应用初学者,练习时长两年半

template_format 'jinja2'的使用

f-stringjinja2功能
{varliable}{{varliable}}占位符
{% if 条件 %} {% endif %}条件判断
{% for 元素 in 列表 %} {% endfor %}循环语句

jinja2

from langchain_core.prompts import PromptTemplate

# {{varliable}}是一个占位符,可通过接口修改,改变输入内容
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)

# 输出:你是一名专家,为学生撰写文案 要求:语言活泼,突出性价比,不超过80字 产品:蓝牙耳机,卖点:省电


result = prompt_template.invoke({"role":"专家","user_type":"职场人","product":"蓝牙耳机","selling_point":"省电"})

print(result.text)

# 输出:你是一名专家,为职场人撰写文案  要求:语言专业,突出效率,不超过100字。 产品:蓝牙耳机,卖点:省电

result = prompt_template.invoke({"role":"专家","user_type":"学者","product":"蓝牙耳机","selling_point":"省电"})

# 输出:你是一名专家,为学者撰写文案  要求:通用风格,简洁明了,不超过90字。 产品:蓝牙耳机,卖点:省电

{% 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":"两年半"}   # 固定 learnLenth 参数值
)

result = prompt_template.invoke( {"varliable":"AI应用"} ) 

print(result.text)

# 输出  我是一位AI应用初学者,练习时长两年半