一起学习大模型 - 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 都能提供强大的支持。

相关推荐
虫无涯1 天前
LangSmith:大模型应用开发的得力助手
人工智能·langchain·llm
玲小珑1 天前
LangChain.js 完全开发手册(九)LangGraph 状态图与工作流编排
前端·langchain·ai编程
RainbowSea2 天前
12. LangChain4j + 向量数据库操作详细说明
java·langchain·ai编程
RainbowSea2 天前
11. LangChain4j + Tools(Function Calling)的使用详细说明
java·langchain·ai编程
叫我詹躲躲2 天前
n8n 自动化工作流平台完整部署
前端·langchain·领域驱动设计
刘立军3 天前
本地大模型编程实战(33)用SSE实现大模型的流式输出
架构·langchain·全栈
ChinaRainbowSea4 天前
9. LangChain4j + 整合 Spring Boot
java·人工智能·spring boot·后端·spring·langchain·ai编程
玲小珑4 天前
LangChain.js 完全开发手册(八)Agent 智能代理系统开发
前端·langchain·ai编程
RainbowSea5 天前
10. LangChain4j + 持久化实操详细说明
langchain·llm·ai编程
RainbowSea5 天前
9. LangChain4j + 整合 Spring Boot
langchain·llm·ai编程