0Pricing
AI Agents with LangChain & Autonomous Workflows · Lekcja

Routing i łańcuchy warunkowe

Buduj łańcuchy, które dynamicznie wybierają ścieżkę na podstawie danych wejściowych, kierując każde żądanie do najlepiej dopasowanego podłańcucha na potrzeby inteligentniejszych, rozgałęzionych workflowów.

Routing i łańcuchy warunkowe to bezpłatna lekcja AI Agents with LangChain & Autonomous Workflows na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej AI Agents with LangChain & Autonomous Workflows, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs AI Agents with LangChain & Autonomous Workflows zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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

Często zadawane pytania

Czy lekcja „Routing i łańcuchy warunkowe” jest bezpłatna?

Tak — pełny tekst „Routing i łańcuchy warunkowe” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu AI Agents with LangChain & Autonomous Workflows, przejdź na CoddyKit PRO. Kurs AI Agents with LangChain & Autonomous Workflows zawiera 4 lekcji w sumie.

Co nauczysz się w „Routing i łańcuchy warunkowe”?

Buduj łańcuchy, które dynamicznie wybierają ścieżkę na podstawie danych wejściowych, kierując każde żądanie do najlepiej dopasowanego podłańcucha na potrzeby inteligentniejszych, rozgałęzionych workf… Ćwiczysz AI Agents with LangChain & Autonomous Workflows z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć AI Agents with LangChain & Autonomous Workflows?

Nie wymagamy żadnego doświadczenia. AI Agents with LangChain & Autonomous Workflows w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Routing i łańcuchy warunkowe”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji AI Agents with LangChain & Autonomous Workflows?

Tak. Każda lekcja AI Agents with LangChain & Autonomous Workflows zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Wprowadzenie do chains w LangChain
  2. Sekwencyjne i proste chains
  3. Dostosowywanie logiki chains
  4. Routing i łańcuchy warunkowe
← Powrót do AI Agents with LangChain & Autonomous Workflows