單個本地大模型搭建參考博客
- 單個Chain:面對一個需求,我們需要創建一個llmchain,設置一個prompt模板,這個chain能夠接收一個用戶input,并輸出一個結果;
- 多個Chain:考慮到同時面對多個需求,我們需要設置多個Chain。
Router Chain往往會配合下游的destination chain一起使用,成為“一個網關路由+多個下子鏈”的架構,實現根據用戶輸入,自動路由到最相關的下游chain
下圖中是一個RouterChian使用場景的示意圖,我們可以看到,一個RouterChain連接了多個下游的子鏈,每個鏈都是一個小應用,當RouterChain接收用戶的輸入,其可以根據用戶輸入路由到和輸入最相關的子鏈上,并由子鏈產生輸出;
例如,用戶輸入是“請幫我寫一首詩”,當RouterChain接收后,會自動路由到“詩人”這個子鏈,由它來輸出結果。
2.RouterChain構成
根據Langchain的介紹,標準的RouterChain
應用應包含兩個標準組成部分:
- 路由鏈RouterChain:其本身就是一個chain應用,能夠根據用戶輸入進行下游子鏈的選擇;Langchain框架提供了多種RouterChain,其中著重介紹了
LLMRouterChain
和EmbeddingRouterChain
兩種:LLMRouterChain
將用戶輸入放進大語言模型,通過Prompt的形式讓大語言模型來進行路由EmbeddingRouterChain
通過向量搜索的方式,將用戶輸入
- 子鏈DestinationChain:直譯為目標鏈,即可路由到的鏈,按照上圖,我們會存在4個目標鏈,分別是lawyer chain,sales chain,english teacher chain 和 poet chain
3.MultiPromptChain構成
MultiPromptChain
應用應包含兩個標準組成部分
- router_chain:接收一個RouterChain實例,作為路由鏈進行路由
default_chain:接收一個LLMChain實例,當Router Chain無法找到合適的下游子鏈時,會自動路由到的默認鏈,可以認為是一個兜底備選鏈 - destination_chains:接收一個Mapping[str, LLMChain] 字典,key為可以路由到的destination chain的名稱,value為該destination chain的LLMChain實例
此外,還有其他主要的可選參數:
- memory: 接收一個BaseMemory實例,能為路由鏈添加上下文記憶
- verbose: bool值,若為True則會打印該鏈的調用過程
4.代碼示例
下面我們以“園丁” 和 “插花大師”為例,子鏈DestinationChain分別是 園丁的chain
和 插花大師的chain
《代碼流程》
1.【Step1】初始化語言模型("qwen:7b")
2.【Step2】構建提示信息(json格式),包括:key、description 和 template
- 【Step2.1】構建兩個場景的模板
- 【Step2.2】構建提示信息
3.【Step3】構建目標鏈chain_map(json格式),以提示信息prompt_infos中的key為key,以Chain為value
4.【Step4】構建路由鏈router_chain
5.【Step5】構建默認鏈 default_chain
6.【Step6】構建多提示鏈 MultiPromptChain
from langchain.chains.llm import LLMChain
from langchain.prompts import PromptTemplate
from langchain.chains.router.llm_router import LLMRouterChain, RouterOutputParser
from langchain.chains.router.multi_prompt_prompt import MULTI_PROMPT_ROUTER_TEMPLATE as RounterTemplate## 【Step1】初始化語言模型
# from langchain.llms import OpenAI
# llm = OpenAI()
# llm = AzureChatOpenAI(deployment_name="GPT-4", temperature=0)ollama_llm = Ollama(model="qwen:7b")## 【Step2】構建提示信息(json格式),包括:key、description 和 template
# 【Step2.1】構建兩個場景的模板
flower_care_template = """
你是一個經驗豐富的園丁,擅長解答關于養花育花的問題。
下面是需要你來回答的問題:
{input}
"""flower_deco_template = """
你是一位網紅插花大師,擅長解答關于鮮花裝飾的問題。
下面是需要你來回答的問題:
{input}
"""# 【Step2.2】構建提示信息
prompt_infos = [{"key": "flower_care","description": "適合回答關于鮮花護理的問題","template": flower_care_template,},{"key": "flower_decoration","description": "適合回答關于鮮花裝飾的問題","template": flower_deco_template,}
]## 【Step3】構建目標鏈chain_map(json格式),以提示信息prompt_infos中的key為key,以Chain為value
chain_map = {}for info in prompt_infos:prompt = PromptTemplate(template=info['template'],input_variables=["input"])print("目標提示:\n", prompt)chain = LLMChain(llm=ollama_llm,prompt=prompt,verbose=True)chain_map[info["key"]] = chain## 【Step4】構建路由鏈router_chain
destinations = [f"{p['key']}: {p['description']}" for p in prompt_infos]
router_template = RounterTemplate.format(destinations="\n".join(destinations))
print("路由模板:\n", router_template)router_prompt = PromptTemplate(template=router_template,input_variables=["input"],output_parser=RouterOutputParser(),
)
print("路由提示:\n", router_prompt)router_chain = LLMRouterChain.from_llm(ollama_llm,router_prompt,verbose=True
)## 【Step5】構建默認鏈 default_chain
from langchain.chains import ConversationChain
default_chain = ConversationChain(llm=ollama_llm,output_key="text",verbose=True
)## 【Step6】構建多提示鏈 MultiPromptChain
from langchain.chains.router import MultiPromptChainchain = MultiPromptChain(router_chain=router_chain,destination_chains=chain_map,default_chain=default_chain,verbose=True
)# 測試1
print(chain.run("如何為玫瑰澆水?"))
【參考鏈接】
- 【LangChain系列 31】Chains——基礎鏈:LLMChain和RouterChain
- Langchain Chain - RouterChain 根據輸入相關性進行路由的路由鏈
- 精華筆記:吳恩達 x LangChain《基于LangChain的大語言模型應用開發》(上)