系列文章目錄
一起學習大模型 - 大模型的交互工具prompt簡介與運用
一起學習大模型 - langchain里的PromptTemplate詳細介紹
一起學習大模型 - langchain里PromptTemplate錯誤排查總結
文章目錄
- 系列文章目錄
- 前言
- 一、 安裝 LangChain
- 二、 基本用法
- 1. 導入庫并定義模板
- 2. 填充模板
- 三、 進階用法
- 1. 使用多個變量
- 2. 嵌套模板
- 3. 動態變量
- 四、 應用模板與大模型交互
- 五、疑問解答
- 1. 舉例說明
- 2. 更詳細的例子
- 總結
前言
上一篇文章我們講了Prompt的概念和作用(大模型的交互工具 prompt簡介與運用),以及如何在langchain里實現一個PromptTemplate。這篇文章我們詳細介紹下 LangChain Prompt Template 的用法。
一、 安裝 LangChain
首先,確保你已經安裝了 LangChain 庫:
pip install langchain
二、 基本用法
1. 導入庫并定義模板
from langchain.prompts import PromptTemplate# 定義一個簡單的模板
template = "請用簡明的語言介紹一下{topic}。"# 創建 PromptTemplate 對象
prompt_template = PromptTemplate(input_variables=["topic"], # 模板中使用的變量template=template # 模板字符串
)
2. 填充模板
# 定義輸入變量
input_variables = {"topic": "人工智能"}# 使用輸入變量填充模板
prompt = prompt_template.format(**input_variables)
print(prompt) # 輸出: 請用簡明的語言介紹一下人工智能。
三、 進階用法
LangChain 的 PromptTemplate 還支持更多復雜的用法,如嵌套模板和動態變量。
1. 使用多個變量
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. 嵌套模板
可以創建嵌套的模板,以便在復雜的情況下重用模板。
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 支持動態生成輸入變量。
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 的大模型進行交互:
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
字典如下:
input_variables = {"topic": "人工智能"}
使用 **
解包后,相當于調用:
prompt = prompt_template.format(topic="人工智能")
這意味著字典中的鍵 topic
被作為關鍵字參數名,而對應的值 人工智能
被作為關鍵字參數值傳遞給 format
方法。
2. 更詳細的例子
考慮一個更復雜的示例,包含多個鍵值對:
input_variables = {"topic": "機器學習", "aspect": "應用"}
使用 **
解包后,相當于調用:
prompt = prompt_template.format(topic="機器學習", aspect="應用")
這樣,format
方法就能識別并替換模板字符串中的 {topic}
和 {aspect}
占位符。
通過這種方式,**
運算符使代碼更簡潔和靈活,能夠方便地將字典中的值傳遞給函數或方法。
總結
通過以上示例,我們可以看到 LangChain 的 PromptTemplate 是如何靈活地創建和管理提示模板的。這些模板可以極大地簡化生成復雜提示的過程,并且可以方便地與大型語言模型進行交互。無論是簡單的單變量模板,還是復雜的多變量和嵌套模板,LangChain 都能提供強大的支持。