0
0
LangChainframework~10 mins

Conditional routing in graphs in LangChain - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a conditional routing node that directs based on input.

LangChain
from langchain.graphs import ConditionalRouter

router = ConditionalRouter(condition=[1])
Drag options to blanks, or click blank then click option'
Alambda x: x == 5
Blambda x: x > 10
Clambda x: x < 0
Dlambda x: x != 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using a condition that does not return a boolean.
Forgetting to use a lambda function.
2fill in blank
medium

Complete the code to add two branches to the conditional router.

LangChain
router.add_branch('yes', [1])
router.add_branch('no', lambda x: False)
Drag options to blanks, or click blank then click option'
Alambda x: x == 5
Blambda x: x < 0
Clambda x: x > 10
Dlambda x: x != 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using a condition that conflicts with the router's main condition.
Not using a lambda function.
3fill in blank
hard

Fix the error in the conditional routing function to correctly route inputs.

LangChain
def route_input(input_value):
    if input_value [1] 5:
        return 'yes'
    else:
        return 'no'
Drag options to blanks, or click blank then click option'
A=>
B=
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of '==' causes syntax errors.
Using '=>' is invalid syntax in Python.
4fill in blank
hard

Fill both blanks to define a conditional router with two branches for positive and negative inputs.

LangChain
router = ConditionalRouter(condition=[1])
router.add_branch('positive', [2])
Drag options to blanks, or click blank then click option'
Alambda x: x >= 0
Blambda x: x > 0
Clambda x: x < 0
Dlambda x: x <= 0
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing >= and > operators.
Using the same condition for both router and branch.
5fill in blank
hard

Fill all three blanks to create a conditional router with branches for even, odd, and zero inputs.

LangChain
router = ConditionalRouter(condition=[1])
router.add_branch('even', [2])
router.add_branch('odd', [3])
Drag options to blanks, or click blank then click option'
Alambda x: x != 0
Blambda x: x % 2 == 0 and x != 0
Clambda x: x % 2 != 0
Dlambda x: x == 0
Attempts:
3 left
💡 Hint
Common Mistakes
Not excluding zero in the router condition.
Mixing up even and odd conditions.