Routing und bedingte Chains
Erstellen Sie Chains, die abhängig von der Eingabe dynamisch einen Pfad auswählen und jede Anfrage für intelligentere, verzweigte Workflows an die passende Sub-Chain senden.
Routing und bedingte Chains ist eine kostenlose AI Agents with LangChain & Autonomous Workflows-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des AI Agents with LangChain & Autonomous Workflows-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der AI Agents with LangChain & Autonomous Workflows-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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
Häufig gestellte Fragen
Ist die Lektion „Routing und bedingte Chains“ kostenlos?
Ja — der vollständige Text von „Routing und bedingte Chains“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des AI Agents with LangChain & Autonomous Workflows-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der AI Agents with LangChain & Autonomous Workflows-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Routing und bedingte Chains“?
Erstellen Sie Chains, die abhängig von der Eingabe dynamisch einen Pfad auswählen und jede Anfrage für intelligentere, verzweigte Workflows an die passende Sub-Chain senden. Du übst AI Agents with LangChain & Autonomous Workflows mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um AI Agents with LangChain & Autonomous Workflows zu starten?
Keine Vorkenntnisse erforderlich. AI Agents with LangChain & Autonomous Workflows auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „Routing und bedingte Chains“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser AI Agents with LangChain & Autonomous Workflows-Lektion Code schreiben und ausführen?
Ja. Jede AI Agents with LangChain & Autonomous Workflows-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Einführung in LangChain Chains
- Sequenzielle und einfache Chains
- Die Logik von Chains anpassen
- Routing und bedingte Chains