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

相关推荐
Python测试之道5 小时前
RAG实战:基于LangChain的《肖申克的救赎》知识问答系统构建指南
langchain·embedding·rag·deepseek
MarkGosling7 小时前
🚀 引言:当 Java 遇上大模型,LangChain 4 j 如何成为开发者的「AI 胶水」?
java·langchain
用户711283928471 天前
什么?大模型删库跑路了?
langchain·llm
金汐脉动1 天前
LangChain × Elasticsearch:手把手教你搭建智能向量数据库
langchain
金汐脉动1 天前
LangChain × PGVector:手把手教你搭建智能向量数据库
langchain
耿玉1 天前
什么是 AI AGENT?与大语言模型的区别?
langchain·ai编程
AI大模型学习教程2 天前
前端学AI之LangChain.js入门教程:实现智能对话机器人
人工智能·langchain
金汐脉动2 天前
解锁聊天模型的隐藏能力:工具调用全指南
langchain
金汐脉动2 天前
增强LangChain交互体验:消息历史(记忆)功能详解
langchain
牛大姐2 天前
DeepSeek+LangChain:搭建一个基础的本地agent
langchain