一、Chains的本質

Chains是什么?Chains 在 LangChain 框架中的核心概念是用于將多個組件及其他 Chains 組合成更復雜功能,通過模塊化與復用提高開發效率、代碼可讀性及可維護性。

Chains兩種類型的鏈:通用鏈和實用鏈。

通用鏈和實用鏈

Chains能干什么?

Chains的靈活性使得我們可以根據具體需求將多個鏈組合在一起,或者將鏈與其他功能組件相結合,從而構建出更復雜、更強大的應用程序。

Legacy Chains 是一系列傳統的工具鏈,用于處理和轉換數據,執行特定的任務,并在早期的技術環境中提供關鍵的功能支持。

二、Chains的原理

Chains的工作流程:通過定義鏈構造函數來初始化鏈,配置所需的組件和可能的其他鏈,以形成具有特定功能的執行流程。

Chains流程包含以下四個核心步驟:

Chains的工作流程

Chains的架構:Chains作為LangChain的核心組件,通過串聯各個邏輯單元,實現了流程控制、數據傳遞和狀態管理,使得復雜的業務邏輯能夠高效、有序地執行。

三、Chains的應用

LangChain Expression Language(LCEL)是一種聲明式的方式,可以輕松地組合鏈。LCEL 從一開始就被設計為支持將原型投入生產,無需任何代碼更改,從最簡單的“提示+LLM”鏈到最復雜的鏈。

Chains的基本案例:prompt + model + output parser

在此鏈中,用戶輸入傳遞到提示模板,然后提示模板輸出傳遞到模型,然后模型輸出傳遞到輸出解析器。

基本案例的流程

1. 提示(Prompt):你創建了一個?ChatPromptTemplate?對象,該對象使用給定的模板字符串,并根據提供的輸入(在這種情況下是主題)生成完整的提示。當你調用?prompt.invoke({“topic”: “ice cream”})?時,它會返回一個?ChatPromptValue?對象,該對象包含要傳遞給模型的完整提示。

prompt value = prompt,invoke({"topic": "ice cream"])
prompt value
ChatPromptValue(messages=[HumanMessage(content='tell me a short joke about ice crean')])
prompt value,to messages )
[HumanMessage(content='tell me a short joke about ice cream')]
prompt value.to string()
Human: tell me a short joke about ice cream'

ChatPromptTemplate

2. 模型(Model):PromptValue作為輸入傳遞給ChatModel,該模型處理后輸出一個BaseMessage格式的回復。

message = model invoke(prompt value)
message
AIMessage(content-"why don't ice creams ever get invited to parties?\n\nBecause they al

ChatModel

如果我們的模型是一個大語言模型(LLM),它將輸出一個字符串作為結果。

from langchain openai. llms import OpenAI
llm = OpenAI(model="gpt-3.5-turbo-instruct")
llm. invoke(prompt value)
\nnRobot: Why did the ice cream truck break down? Because it had a meltdown!'

大語言模型(LLM)

3. 輸出解析器(Output Parser):最后,我們將模型輸出傳遞給output_parser,它是一個BaseOutputParser,可以接受字符串或BaseMessage作為輸入。其中,StrOutputParser專門將任何輸入簡單轉換為字符串。

output parser.invoke(messagel
"why did the ice cream go to therapy? ininBecause it had too many toppings and couldn't

輸出解析器(Output Parser)

RAG Search Example:在這個例子中,我們想要運行一個檢索增強的生成鏈,以便在回答問題時添加一些上下文。

RAG Search Example的流程

該流程通過創建一個包含上下文和問題的RunnableParallel對象,將其傳遞給提示組件以生成PromptValue,然后將該值輸入到OpenAI的大型語言模型(LLM)中以生成ChatMessage,最后通過輸出解析器將其轉換為Python字符串并返回。

1. 創建RunnableParallel對象:首先,創建一個RunnableParallel對象,它包含兩個條目:一個是context(由檢索器獲取的文檔結果),另一個是question(用戶的原始問題)。為了傳遞問題,使用RunnablePassthrough來復制這個條目。

2. 傳遞給提示組件:將上述步驟中創建的字典傳遞給提示組件。提示組件接收用戶輸入的問題和檢索到的文檔(作為上下文),然后構建一個提示并輸出一個PromptValue

3. 模型組件評估:模型組件接收生成的提示,并將其傳遞給OpenAI的大型語言模型(LLM)進行評估。模型生成的輸出是一個ChatMessage對象。

4. 輸出解析:最后,output_parser組件接收一個ChatMessage對象,并將其轉換為Python字符串。這個字符串是從調用方法中返回的最終結果。

# Requires:
# pip install langchain docarray tiktoken
from langchain community.vectorstores import DocArrayInMemorySearchfromlangchain core.output parsers import StrOutputParser
langchain core.prompts import ChatPromptTemplatefrom
fromilangchain core,runnables import RunnableParallel, RunnablePassthrough
fromlangchain openaichat models import ChatOpenAIfrom langchain openai.embeddings import OpenAIEmbeddings
vectorstore = DocArrayInMemorySearch, from texts(
["harrison worked at kensho", "bears like to eat honey"].embedding 0penAIEmbeddings()
retriever = vectorstore.as retriever()
template = mAnswer the question based only on the following context;{context}
Question: {question》
prompt = ChatPromptTemplate.from template(template)model = ChatOpenAi()
output parser = StrOutputParser()
setup and retrieval = RunnableParallel(["context": retriever,"question": RunnablePassthrough()]
chain = setup and retrieval l prompt model output parser
chain.invoke("where did harrison work?")

本文章轉載微信公眾號@架構師帶你玩轉AI

上一篇:

大模型開發 - 一文搞懂 LangChain(四):Agents

下一篇:

大模型開發 - 一文搞懂 Function Calling(函數調用)
#你可能也喜歡這些API文章!

我們有何不同?

API服務商零注冊

多API并行試用

數據驅動選型,提升決策效率

查看全部API→
??

熱門場景實測,選對API

#AI文本生成大模型API

對比大模型API的內容創意新穎性、情感共鳴力、商業轉化潛力

25個渠道
一鍵對比試用API 限時免費

#AI深度推理大模型API

對比大模型API的邏輯推理準確性、分析深度、可視化建議合理性

10個渠道
一鍵對比試用API 限時免費