一起学习大模型 - langchain里的 PromptTemplate详细介绍

系列文章目录

一起学习大模型 - 大模型的交互工具prompt简介与运用

一起学习大模型 - langchain里的PromptTemplate详细介绍

一起学习大模型 - langchain里PromptTemplate错误排查总结


文章目录

  • 系列文章目录
  • 前言
  • [一、 安装 LangChain](#一、 安装 LangChain)
  • [二、 基本用法](#二、 基本用法)
    • [1. 导入库并定义模板](#1. 导入库并定义模板)
    • [2. 填充模板](#2. 填充模板)
  • [三、 进阶用法](#三、 进阶用法)
    • [1. 使用多个变量](#1. 使用多个变量)
    • [2. 嵌套模板](#2. 嵌套模板)
    • [3. 动态变量](#3. 动态变量)
  • [四、 应用模板与大模型交互](#四、 应用模板与大模型交互)
  • 五、疑问解答
    • [1. 举例说明](#1. 举例说明)
    • [2. 更详细的例子](#2. 更详细的例子)
  • 总结

前言

上一篇文章我们讲了Prompt的概念和作用(大模型的交互工具 prompt简介与运用),以及如何在langchain里实现一个PromptTemplate。这篇文章我们详细介绍下 LangChain Prompt Template 的用法。

一、 安装 LangChain

首先,确保你已经安装了 LangChain 库:

bash 复制代码
pip install langchain

二、 基本用法

1. 导入库并定义模板

python 复制代码
from langchain.prompts import PromptTemplate

# 定义一个简单的模板
template = "请用简明的语言介绍一下{topic}。"

# 创建 PromptTemplate 对象
prompt_template = PromptTemplate(
    input_variables=["topic"],  # 模板中使用的变量
    template=template  # 模板字符串
)

2. 填充模板

python 复制代码
# 定义输入变量
input_variables = {"topic": "人工智能"}

# 使用输入变量填充模板
prompt = prompt_template.format(**input_variables)
print(prompt)  # 输出: 请用简明的语言介绍一下人工智能。

三、 进阶用法

LangChain 的 PromptTemplate 还支持更多复杂的用法,如嵌套模板和动态变量。

1. 使用多个变量

python 复制代码
template = "请用简明的语言介绍一下{topic},并解释它的{aspect}。"

prompt_template = PromptTemplate(
    input_variables=["topic", "aspect"],
    template=template
)

input_variables = {"topic": "机器学习", "aspect": "应用"}
prompt = prompt_template.format(**input_variables)
print(prompt)  # 输出: 请用简明的语言介绍一下机器学习,并解释它的应用。

2. 嵌套模板

可以创建嵌套的模板,以便在复杂的情况下重用模板。

python 复制代码
base_template = "请用简明的语言介绍一下{topic}。"
aspect_template = base_template + " 并解释它的{aspect}。"

prompt_template = PromptTemplate(
    input_variables=["topic", "aspect"],
    template=aspect_template
)

input_variables = {"topic": "深度学习", "aspect": "基本原理"}
prompt = prompt_template.format(**input_variables)
print(prompt)  # 输出: 请用简明的语言介绍一下深度学习。并解释它的基本原理。

3. 动态变量

LangChain 支持动态生成输入变量。

python 复制代码
def generate_topic():
    return "自然语言处理"

template = "请用简明的语言介绍一下{topic}。"

prompt_template = PromptTemplate(
    input_variables=["topic"],
    template=template
)

# 使用动态生成的变量填充模板
input_variables = {"topic": generate_topic()}
prompt = prompt_template.format(**input_variables)
print(prompt)  # 输出: 请用简明的语言介绍一下自然语言处理。

四、 应用模板与大模型交互

以下示例展示如何将创建的 prompt 应用于与 OpenAI 的大模型进行交互:

python 复制代码
import openai
from langchain.prompts import PromptTemplate

# 定义和创建 PromptTemplate 对象
template = "请用简明的语言介绍一下{topic}。"
prompt_template = PromptTemplate(
    input_variables=["topic"],
    template=template
)

# 填充模板
input_variables = {"topic": "人工智能"}
prompt = prompt_template.format(**input_variables)
print("生成的 prompt:", prompt)

# 设定 OpenAI API 密钥
openai.api_key = 'your-api-key'

# 调用 OpenAI API 生成内容
response = openai.Completion.create(
    engine="davinci-codex",  # 或者使用 "gpt-4" 模型
    prompt=prompt,
    max_tokens=150  # 设置生成内容的长度
)

# 打印模型的响应
print("模型的响应:", response.choices[0].text.strip())

五、疑问解答

有的同学应该会有下面的疑问

想问一下 prompt = prompt_template.format(**input_variables) 这个代码里的 **

的作用是什么

在 Python 中,** 运算符用于将字典解包(unpack)成关键字参数(keyword arguments)。它将字典中的键值对传递给函数或方法,使每个键值对变成单独的关键字参数。

具体到你的代码 prompt = prompt_template.format(**input_variables),这里的 **input_variables 作用是将 input_variables 字典中的键值对解包成 format 方法的关键字参数。

1. 举例说明

假设 input_variables 字典如下:

python 复制代码
input_variables = {"topic": "人工智能"}

使用 ** 解包后,相当于调用:

python 复制代码
prompt = prompt_template.format(topic="人工智能")

这意味着字典中的键 topic 被作为关键字参数名,而对应的值 人工智能 被作为关键字参数值传递给 format 方法。

2. 更详细的例子

考虑一个更复杂的示例,包含多个键值对:

python 复制代码
input_variables = {"topic": "机器学习", "aspect": "应用"}

使用 ** 解包后,相当于调用:

python 复制代码
prompt = prompt_template.format(topic="机器学习", aspect="应用")

这样,format 方法就能识别并替换模板字符串中的 {topic}{aspect} 占位符。

通过这种方式,** 运算符使代码更简洁和灵活,能够方便地将字典中的值传递给函数或方法。

总结

通过以上示例,我们可以看到 LangChain 的 PromptTemplate 是如何灵活地创建和管理提示模板的。这些模板可以极大地简化生成复杂提示的过程,并且可以方便地与大型语言模型进行交互。无论是简单的单变量模板,还是复杂的多变量和嵌套模板,LangChain 都能提供强大的支持。

相关推荐
Dontla2 小时前
黑马大模型RAG与Agent智能体实战教程LangChain提示词——6、提示词工程(提示词优化、few-shot、金融文本信息抽取案例、金融文本匹配案例)
redis·金融·langchain
JaydenAI2 小时前
[LangChain之链]LangChain的Chain——由Runnable构建的管道
python·langchain
草帽lufei2 小时前
LangChain 框架核心向量存储
langchain
猫头虎2 小时前
如何使用Docker部署OpenClaw汉化中文版?
运维·人工智能·docker·容器·langchain·开源·aigc
qq_5470261793 小时前
LangChain 1.0 核心概念
运维·服务器·langchain
uXrvbWJGleS4 小时前
三相两电平整流器Simulink仿真探究
langchain
猫头虎4 小时前
手动部署开源OpenClaw汉化中文版过程中常见问题排查手册
人工智能·langchain·开源·github·aigc·agi·openclaw
程序员ken4 小时前
深入理解大语言模型(8) 使用 LangChain 开发应用程序之上下文记忆
人工智能·python·语言模型·langchain
一切尽在,你来15 小时前
第二章 预告内容
人工智能·langchain·ai编程
一切尽在,你来20 小时前
1.1 AI大模型应用开发和Langchain的关系
人工智能·langchain