การกำหนดเส้นทางและสายงานแบบมีเงื่อนไข
สร้างสายงานที่เลือกเส้นทางแบบไดนามิกตามข้อมูลนำเข้า โดยส่งคำขอแต่ละรายการไปยังสายงานย่อยที่เหมาะสมที่สุดสำหรับกระบวนการทำงานแบบแตกแขนงที่ชาญฉลาดยิ่งขึ้น
การกำหนดเส้นทางและสายงานแบบมีเงื่อนไข เป็นบทเรียน AI Agents with LangChain & Autonomous Workflows ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส AI Agents with LangChain & Autonomous Workflows ให้อัปเกรดเป็น CoddyKit PRO คอร์ส AI Agents with LangChain & Autonomous Workflows มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การกำหนดเส้นทางและสายงานแบบมีเงื่อนไข”
สร้างสายงานที่เลือกเส้นทางแบบไดนามิกตามข้อมูลนำเข้า โดยส่งคำขอแต่ละรายการไปยังสายงานย่อยที่เหมาะสมที่สุดสำหรับกระบวนการทำงานแบบแตกแขนงที่ชาญฉลาดยิ่งขึ้น คุณปฏิบัติ AI Agents with LangChain & Autonomous Workflows ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน AI Agents with LangChain & Autonomous Workflows หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน AI Agents with LangChain & Autonomous Workflows บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การกำหนดเส้นทางและสายงานแบบมีเงื่อนไข” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน AI Agents with LangChain & Autonomous Workflows นี้ได้ไหม
ได้ บทเรียน AI Agents with LangChain & Autonomous Workflows ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- บทนำสู่เชนของ LangChain
- เชนแบบลำดับและเชนอย่างง่าย
- การปรับแต่งตรรกะของเชน
- การกำหนดเส้นทางและสายงานแบบมีเงื่อนไข