0Pricing
AI Agents with LangChain & Autonomous Workflows · 课时

路由与条件链

构建能够根据输入动态选择路径的链,将每个请求发送到最合适的子链,从而实现更智能的分支式工作流。

路由与条件链 是 CoddyKit 上的免费 AI Agents with LangChain & Autonomous Workflows 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 AI Agents with LangChain & Autonomous Workflows 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 AI Agents with LangChain & Autonomous Workflows 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Beyond Straight-Line Chains

Sequential chains always run the same steps in order. But real workflows branch: a billing question and a coding question need different handling.

This lesson covers routing chains that choose a path based on the input.

What a Router Does

A router inspects the input, decides which destination chain best fits, and forwards the input there. It is a dispatcher in front of several specialist chains.

input -> router -> { billing_chain | code_chain | general_chain }

Defining Destination Chains

Each destination is a normal chain tuned for one kind of task, with its own prompt and configuration.

billing = LLMChain(llm=llm, prompt=billing_prompt)
coding  = LLMChain(llm=llm, prompt=coding_prompt)

Describing Each Route

The router needs to know what each destination is for. Provide a name and a short description so it can match an input to the right one.

routes = [
  {'name': 'billing', 'description': 'invoices, refunds, payments'},
  {'name': 'coding',  'description': 'programming and debugging help'}
]

LLM-Based Routing

One approach asks the LLM itself to classify the input and name the destination. It is flexible and handles fuzzy intent, but adds a model call and some unpredictability.

# router prompt asks model to output: {'destination': 'billing', 'input': ...}

Rule-Based Routing

When intent is clear from structure, a deterministic function can route faster and cheaper than an LLM.

def route(query):
    if 'refund' in query.lower():
        return 'billing'
    return 'general'

The Default Route

Some inputs match no specialist. Always provide a default destination so the chain never fails on an unexpected request.

default_chain = LLMChain(llm=llm, prompt=general_prompt)

Composing the Router Chain

LangChain's routing chain ties the router, the destination map, and the default together into a single callable that picks the path automatically.

from langchain.chains.router import MultiPromptChain
chain = MultiPromptChain(
    router_chain=router,
    destination_chains={'billing': billing, 'coding': coding},
    default_chain=default_chain)

Handling Misroutes

Routing is a prediction and can be wrong. Log the chosen destination, let a destination decline and fall back, and monitor misroute rates so you can refine descriptions over time.

LLM vs Rule-Based Tradeoffs

Choose your router by the situation:

  • Rules: fast, cheap, deterministic; brittle for fuzzy intent
  • LLM: flexible, handles nuance; slower, costs a call, less predictable

Hybrids use rules first and fall back to the LLM.

A Routing Workflow

Putting it together:

  • Build specialist destination chains
  • Describe each route clearly
  • Choose rule-based, LLM-based, or hybrid routing
  • Always include a default and monitor misroutes

Quick Check

Test your understanding of routing chains.

Recap

You learned to build branching, conditional chains.

  • A router dispatches input to specialist destination chains
  • Routing can be rule-based, LLM-based, or hybrid
  • Clear route descriptions improve accuracy
  • Always include a default and monitor misroutes

常见问题解答

「路由与条件链」课时是免费的吗?

是的 — 「路由与条件链」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 AI Agents with LangChain & Autonomous Workflows 课程的其余内容,请升级到 CoddyKit PRO。 AI Agents with LangChain & Autonomous Workflows 课程共包含 4 节课。

「路由与条件链」这节课中我会学到什么?

构建能够根据输入动态选择路径的链,将每个请求发送到最合适的子链,从而实现更智能的分支式工作流。 你通过在浏览器中直接运行的动手代码来练习 AI Agents with LangChain & Autonomous Workflows,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 AI Agents with LangChain & Autonomous Workflows 需要有经验吗?

无需任何先前经验。CoddyKit 上的 AI Agents with LangChain & Autonomous Workflows 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「路由与条件链」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 AI Agents with LangChain & Autonomous Workflows 课中编写并运行代码吗?

能。每节 AI Agents with LangChain & Autonomous Workflows 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. LangChain 链简介
  2. 顺序链与简单链
  3. 自定义链逻辑
  4. 路由与条件链
← 返回 AI Agents with LangChain & Autonomous Workflows