0. 思維導圖
1. 引言 ??
在人工智能快速發展的今天,如何有效地利用大語言模型(LLM)構建強大的應用成為眾多開發者關注的焦點。前面的課程中,我們學習了正則表達式以及向量數據庫的相關知識,了解了如何處理文檔并將其附加給大模型。本章我們將深入探討LangChain中的核心概念——“Chain”(鏈)。
LangChain作為一個強大的框架,讓我們能夠將LLM與其他計算資源或知識源結合起來,創建更加智能的應用。而Chain則是這個框架的重要組成部分,它就像是將各種功能模塊串聯起來的紐帶,使得復雜的AI工作流成為可能。
2. 什么是Chain(鏈)??
2.1 鏈的基本概念
在LangChain中,Chain(鏈)是一個核心概念,它代表一系列組件的連接,這些組件按照特定的順序執行,以完成復雜的任務。簡單來說,Chain就是將多個步驟組合成一個可調用的單元,讓信息能夠從一個組件流向另一個組件。
鏈的基本工作流程是:接收輸入 → 處理數據 → 產生輸出。而這個處理過程可能涉及到與LLM的交互、文檔的檢索、信息的提取等多個步驟。
2.2 鏈的重要性
鏈的設計理念使得我們可以:
- ?? 模塊化地組合不同功能
- ?? 封裝復雜的邏輯流程
- ??? 重用常見的處理模式
- ?? 靈活地擴展應用功能
正是這種靈活性和模塊化的特性,使得LangChain能夠適應各種各樣的AI應用場景,從簡單的問答系統到復雜的智能助手。
3. 四種基本的內置鏈 ??
LangChain提供了多種內置的鏈類型,其中最基礎的四種分別是LLM鏈、順序鏈、路由鏈和檢索鏈。這些內置鏈為我們提供了處理不同任務的基本工具。
3.1 LLM Chain
LLM Chain(LLM鏈)是最基礎也是最常用的鏈類型,它將提示模板(PromptTemplate)和語言模型(LLM)結合在一起,形成一個簡單但強大的處理單元。
工作原理:
- 接收用戶輸入
- 根據提示模板格式化輸入
- 將格式化后的提示發送給LLM
- 返回LLM的響應
代碼示例:
from langchain.chains import LLMChain
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate# 創建提示模板
template = "請告訴我{topic}的三個重要知識點"
prompt = PromptTemplate(input_variables=["topic"], template=template)# 初始化LLM
llm = OpenAI(temperature=0.7)# 創建LLM鏈
chain = LLMChain(llm=llm, prompt=prompt)# 使用鏈
response = chain.run("人工智能")
print(response)
3.2 Sequential Chain(順序鏈)
順序鏈允許我們將多個鏈按照特定順序連接起來,前一個鏈的輸出可以作為后一個鏈的輸入,從而實現更復雜的處理流程。
順序鏈主要有兩種類型:
- SimpleSequentialChain:每個鏈只有一個輸入和一個輸出,鏈之間一對一串聯
- SequentialChain:支持多輸入多輸出,更加靈活
代碼示例(SimpleSequentialChain):
from langchain.chains import SimpleSequentialChain
from langchain.chains import LLMChain
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplatellm = OpenAI(temperature=0.7)# 第一個鏈:生成一個故事概要
first_prompt = PromptTemplate(input_variables=["subject"],template="請為{subject}寫一個簡短的故事概要"
)
chain_one = LLMChain(llm=llm, prompt=first_prompt)# 第二個鏈:基于故事概要寫一個完整故事
second_prompt = PromptTemplate(input_variables=["概要"],template="基于以下概要,寫一個完整的故事:\n\n{概要}"
)
chain_two = LLMChain(llm=llm, prompt=second_prompt)# 創建順序鏈
overall_chain = SimpleSequentialChain(chains=[chain_one, chain_two])# 運行鏈
response = overall_chain.run("一只迷路的貓")
print(response)
代碼示例(SequentialChain):
from langchain.chains import SequentialChain
from langchain.chains import LLMChain
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplatellm = OpenAI(temperature=0.7)# 第一個鏈:為電影生成標題
first_prompt = PromptTemplate(input_variables=["genre"],template="為{genre}類型的電影想一個標題"
)
title_chain = LLMChain(llm=llm, prompt=first_prompt, output_key="title")# 第二個鏈:為電影生成簡介
second_prompt = PromptTemplate(input_variables=["title"],template="為電影《{title}》寫一個簡短的簡介"
)
synopsis_chain = LLMChain(llm=llm, prompt=second_prompt, output_key="synopsis")# 創建順序鏈
overall_chain = SequentialChain(chains=[title_chain, synopsis_chain],input_variables=["genre"],output_variables=["title", "synopsis"]
)# 運行鏈
response = overall_chain.run("科幻")
print(f"標題: {response['title']}\n簡介: {response['synopsis']}")
3.3 Router Chain(路由鏈)
路由鏈是一種能夠根據輸入動態決定調用哪個子鏈的高級鏈。它根據輸入的內容或特征,選擇最適合處理該輸入的鏈,類似于一個智能分發器。
Router Chain由三個主要部分組成:
- 路由器(決定使用哪個鏈)
- 目標鏈(可選擇的鏈集合)
- 默認鏈(當無法確定路由時使用)
代碼示例:
from langchain.chains.router import MultiPromptChain
from langchain.chains.router.llm_router import LLMRouterChain
from langchain.chains import LLMChain
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplatellm = OpenAI(temperature=0.7)# 定義各專業領域的提示模板
physics_template = PromptTemplate(template="你是一位物理學專家。回答以下物理學問題:{question}",input_variables=["question"]