LangChain 54 深入理解LangChain 表达式语言十七 Chains Route动态路由 LangChain Expression Language (LCEL)

LangChain系列文章

  1. LangChain 36 深入理解LangChain 表达式语言优势一 LangChain Expression Language (LCEL)
  2. LangChain 37 深入理解LangChain 表达式语言二 实现prompt+model+output parser LangChain Expression Language (LCEL)
  3. LangChain 38 深入理解LangChain 表达式语言三 实现RAG检索增强生成 LangChain Expression Language (LCEL)
  4. LangChain 39 深入理解LangChain 表达式语言四 为什么要用LCEL LangChain Expression Language (LCEL)
  5. LangChain 40 实战Langchain访问OpenAI ChatGPT API Account deactivated的另类方法,访问跳板机API
  6. LangChain 41 深入理解LangChain 表达式语言五 为什么要用LCEL调用大模型LLM LangChain Expression Language (LCEL)
  7. LangChain 42 深入理解LangChain 表达式语言六 Runtime调用不同大模型LLM LangChain Expression Language (LCEL)
  8. LangChain 43 深入理解LangChain 表达式语言七 日志和Fallbacks异常备选方案 LangChain Expression Language (LCEL)
  9. LangChain 44 深入理解LangChain 表达式语言八 Runnable接口输入输出模式 LangChain Expression Language (LCEL)
  10. LangChain 45 深入理解LangChain 表达式语言九 Runnable 调用、流输出、批量调用、异步处理 LangChain Expression Language (LCEL)
  11. LangChain 46 深入理解LangChain 表达式语言十 Runnable 调用中间状态调试日志 LangChain Expression Language (LCEL)
  12. LangChain 47 深入理解LangChain 表达式语言十一 Runnable 并行处理 LangChain Expression Language (LCEL)
  13. LangChain 48 终极解决 实战Langchain访问OpenAI ChatGPT API Account deactivated的另类方法,访问跳板机API
  14. LangChain 49 深入理解LangChain 表达式语言十二 Runnable 透传数据保持输入不变 LangChain Expression Language (LCEL)
  15. LangChain 50 深入理解LangChain 表达式语言十三 自定义pipeline函数 LangChain Expression Language (LCEL)
  16. LangChain 51 深入理解LangChain 表达式语言十四 自动修复配置RunnableConfig LangChain Expression Language (LCEL)
  17. LangChain 52 深入理解LangChain 表达式语言十五 Bind runtime args绑定运行时参数 LangChain Expression Language (LCEL)
  18. LangChain 53 深入理解LangChain 表达式语言十六 Dynamically route动态路由 LangChain Expression Language (LCEL)

在这里插入图片描述

1. Dynamically route根据输入动态路由逻辑

本笔记本涵盖了如何在LangChain表达语言中进行路由。

路由允许您创建非确定性链,其中上一步的输出定义了下一步。路由有助于在与LLMs的交互中提供结构和一致性。

有两种方法可以执行路由:

  1. 使用RunnableBranch。
  2. 编写自定义工厂函数,该函数获取上一步的输入并返回一个可运行的函数。重要的是,这应该返回一个可运行的函数,而不是实际执行。

我们将使用两个步骤的序列来说明这两种方法,其中第一步将输入问题分类为关于LangChain、ChatGPT或其他,然后路由到相应的提示链。

2. 创建三个子链:

from langchain_core.runnables import RunnablePassthrough
from langchain.prompts import ChatPromptTemplate
from langchain.chat_models import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnableParallel
from langchain.prompts import PromptTemplate

from dotenv import load_dotenv
load_dotenv()
# from langchain.globals import set_debug
# set_debug(True)

langchain_chain = (
    PromptTemplate.from_template(
        """You are an expert in langchain. \
Always answer questions starting with "As Harrison Chase told me". \
Respond to the following question:

Question: {question}
Answer:"""
    )
    | ChatOpenAI()
)
chatgpt_chain = (
    PromptTemplate.from_template(
        """You are an expert in ChatGPT. \
Always answer questions starting with "As Sam Altman told me". \
Respond to the following question:

Question: {question}
Answer:"""
    )
    | ChatOpenAI()
)
general_chain = (
    PromptTemplate.from_template(
        """Respond to the following question:

Question: {question}
Answer:"""
    )
    | ChatOpenAI()
)

from langchain_core.runnables import RunnableBranch

branch = RunnableBranch(
    (lambda x: "chatgpt" in x["topic"].lower(), chatgpt_chain),
    (lambda x: "langchain" in x["topic"].lower(), langchain_chain),
    general_chain,
)

full_chain = {"topic": chain, "question": lambda x: x["question"]} | branch

response = full_chain.invoke({"question": "how do I use ChatGPT ?"})
print('response >> ', response)

response = full_chain.invoke({"question": "how do I use LangChain ?"})
print('response >> ', response)

response = full_chain.invoke({"question": "whats 2 + 2"})
print('response >> ', response)

2.1 ChatGPT 问题 response

zgpeace@zgpeaces-MacBook-Pro ~/W/L/langchain-llm-app> python LCEL/router.py                                              [8:50:46] develop?
response >>  content='As Sam Altman told me, to use ChatGPT, you can access it through OpenAI\'s API. You need to make API calls to send a series of messages as input to the model. Start with a system message to set the behavior of the assistant, followed by user messages to have a conversation. Each message should have a \'role\' (either "system", "user", or "assistant") and \'content\' (the text of the message). You can continue the conversation by extending the list of messages and making additional API calls as needed.'

2.2 Langchain 问题 response

zgpeace@zgpeaces-MacBook-Pro ~/W/L/langchain-llm-app> python LCEL/router.py                                              [8:49:42] develop?
response >>  content='As Harrison Chase told me, to use LangChain, you can follow these steps:\n\n1. Visit the LangChain website and create an account.\n2. Once you have created an account, log in to your LangChain account.\n3. On the dashboard, you will find various options and tools to use the LangChain platform.\n4. To start using LangChain, you can either request a translation or offer your translation services.\n5. If you want to request a translation, provide the necessary details such as the source and target languages, the document or text you need translating, and any specific instructions.\n6. LangChain will then connect you with a suitable translator who will work on your request.\n7. If you want to offer your translation services, you can create a profile showcasing your language skills and expertise.\n8. Clients looking for translators will be able to find and hire you based on your profile and qualifications.\n9. Once you have completed a translation or received a translated document, you can communicate with the client through the LangChain platform for any further clarifications or revisions.\n10. LangChain also provides a secure payment system for both clients and translators, ensuring a smooth and reliable transaction process.\n\nRemember to always refer to the LangChain platform for any specific instructions or guidance as they may have updated features or processes.'

2.3 通用问题response

zgpeace@zgpeaces-MacBook-Pro ~/W/L/langchain-llm-app> python LCEL/router.py                                              [8:48:19] develop?
response >>  content='The sum of 2 and 2 is 4.'

代码

https://github.com/zgpeace/pets-name-langchain/tree/develop

参考

https://python.langchain.com/docs/expression_language/how_to/functions

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值