成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

五分鐘了解 LangChain 的路由鏈

開發 人工智能
本文主要介紹了LangChain中的路由鏈(RouterChain)的概念,它主要用在不確定性的場景下,根據提示詞,選擇具體的某個鏈去執行。

上篇文章《5分鐘理透LangChain的Chain》里用到了順序鏈SequentialChain,它可以將多個鏈按順序串起來。本文介紹LangChain里的另外1個重要的鏈:路由鏈。

1. 路由鏈概念

路由鏈(RouterChain)是由LLM根據輸入的Prompt去選擇具體的某個鏈。路由鏈中一般會存在多個Prompt,Prompt結合LLM決定下一步選擇哪個鏈。

2. 路由鏈的使用場景

路由鏈一般涉及到2個核心類,LLMRouterChain和MultiPromptChain,一起看看官網介紹:

  • LLMRouterChain:使用LLM路由到可能的選項中。
  • MultiPromptChain:該鏈可用于在多個提示詞之間路由輸入,當你有多個提示詞并且只想路由到其中一個時,可以用這個鏈。

一般使用路由鏈時,有固定的幾個步驟:

  • 準備多個鏈的Prompt提示詞,然后各自封裝成鏈。
  • 將可能路由到的鏈封裝到destination_chains里。
  • 構建多提示詞和RouterChain ,負責選擇下一個要調用的鏈。
  • 構建默認鏈。
  • 使用MultiPromptChain選擇某個鏈,然后再去執行此鏈。

3. 使用路由鏈的案例

假設我們有一個常見的場景,根據用戶的輸入內容選擇不同的處理路徑,如果沒有選到合適的鏈,則使用默認鏈。比如:根據用戶的輸入問題,選擇不同的鏈去處理,如果沒選到合適的,則走默認鏈。

具體代碼如下:

from langchain_openai import ChatOpenAI

model = ChatOpenAI(
    model_name="gpt-3.5-turbo",
    openai_api_key="sk-xxxx",
    openai_api_base="https://api.302.ai/v1",
)


from langchain.chains.router import LLMRouterChain, MultiPromptChain
from langchain.chains.router.llm_router import RouterOutputParser
from langchain.chains.router.multi_prompt_prompt import MULTI_PROMPT_ROUTER_TEMPLATE
from langchain.chains import LLMChain, ConversationChain
from langchain.prompts import PromptTemplate

# 準備2條目的鏈:一條物理鏈,一條數學鏈
# 1. 物理鏈
physics_template = """
你是一位物理學家,擅長回答物理相關的問題,當你不知道問題的答案時,你就回答不知道。
具體問題如下:
{input}
"""
physics_prompt = PromptTemplate.from_template(physics_template)
physics_chain = LLMChain(llm=model, prompt=physics_prompt)

# 2. 數學鏈
math_template = """
你是一個數學家,擅長回答數學相關的問題,當你不知道問題的答案時,你就回答不知道。
具體問題如下:
{input}
"""
math_prompt = PromptTemplate.from_template(math_template)
math_chain = LLMChain(llm=model, prompt=math_prompt)

# 3. 英語鏈
english_template = """
你是一個非常厲害的英語老師,擅長回答英語相關的問題,當你不知道問題的答案時,你就回答不知道。
具體問題如下:
{input}
"""
english_prompt = PromptTemplate.from_template(english_template)
english_chain = LLMChain(llm=model, prompt=english_prompt)


######### 所有可能的目的鏈
destination_chains = {}
destination_chains["physics"] = physics_chain
destination_chains["math"] = math_chain
destination_chains["english"] = english_chain


######### 默認鏈
default_chain = ConversationChain(llm=model, output_key="text")

# 讓多路由模板 能找到合適的 提示詞模板
destinations_template_str = """
physics:擅長回答物理問題
math:擅長回答數學問題
english:擅長回答英語問題
"""
router_template = MULTI_PROMPT_ROUTER_TEMPLATE.format(
    destinations=destinations_template_str
)

# 通過路由提示詞模板,構建路由提示詞
router_prompt = PromptTemplate(
    template=router_template,
    input_variables=["input"],
    output_parser=RouterOutputParser(),
)

######### 路由鏈
router_chain = LLMRouterChain.from_llm(llm=model, prompt=router_prompt)

######### 最終的鏈
multi_prompt_chain = MultiPromptChain(
    router_chain=router_chain,
    destination_chains=destination_chains,
    default_chain=default_chain,
    verbose=True,
)



# multi_prompt_chain.invoke({"input": "重力加速度是多少?"})
# multi_prompt_chain.invoke("y=x^2+2x+1的導數是多少?")
multi_prompt_chain.invoke("將以下英文翻譯成中文,只輸出中文翻譯結果:\n The largest community building the future of LLM apps.")
# multi_prompt_chain.invoke("你是怎么理解java的面向對象的思想的?")

執行結果跟我們預想的一致,執行結果如下:

4. 總結

這篇博客主要介紹了LangChain中的路由鏈(RouterChain)的概念,它主要用在不確定性的場景下,根據提示詞,選擇具體的某個鏈去執行。還聊了它的使用場景和具體案例,希望對你有幫助!

責任編輯:趙寧寧 來源: 程序員半支煙
相關推薦

2024-06-19 10:41:06

2018-03-12 14:37:50

區塊鏈比特幣架構

2021-10-19 07:27:08

HTTP代理網絡

2009-11-05 14:53:54

Visual Stud

2022-12-16 09:55:50

網絡架構OSI

2023-09-07 23:52:50

Flink代碼

2024-09-18 08:21:24

JavaScriptTypeScriptprototype

2018-03-23 11:23:14

2019-11-22 11:10:26

區塊鏈技術

2024-08-13 11:13:18

2009-11-06 10:25:34

WCF元數據交換

2024-09-23 17:05:44

2009-10-26 15:45:43

VB.NET類構造

2020-02-19 19:26:27

K8S開源平臺容器技術

2020-05-12 09:10:24

瀏覽器服務器網絡

2024-04-28 12:55:46

redis頻道機制

2020-03-06 10:45:48

機器學習人工智能神經網絡

2009-11-02 18:07:58

Oracle數據庫

2018-03-12 21:31:24

區塊鏈

2021-09-18 11:36:38

混沌工程云原生故障
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 中文字幕一区二区三区在线视频 | 亚洲免费精品 | 国产精品99精品久久免费 | 精品美女视频在免费观看 | 日日碰狠狠躁久久躁96avv | 欧美三级在线 | 一区二区三区四区国产 | 久久综合伊人一区二区三 | 久久99精品视频 | 五月激情久久 | 99久久精品国产一区二区三区 | 日韩一区二区三区在线 | 国产精品视频免费播放 | av在线播放不卡 | 久久久精品高清 | 欧美美女爱爱视频 | 在线91| 精品av天堂毛片久久久借种 | 中文字幕免费视频 | 草久久 | 日本不卡免费新一二三区 | 日韩一区二区在线视频 | 国产精品永久免费视频 | 欧美视频三区 | 国产91视频免费 | 99国内精品久久久久久久 | 男女羞羞视频在线免费观看 | 亚洲视频免费在线观看 | 久久综合伊人 | 久久99精品久久久久久 | 亚洲传媒在线 | 91av免费看 | 久久av网| 国产精品免费小视频 | 欧美成人免费在线视频 | 国产成人影院 | 精品久久1 | 国产精品一区视频 | av片免费 | 三级成人在线观看 | 国产香蕉视频在线播放 |