0
0
Prompt Engineering / GenAIml~10 mins

Chains (sequential, router) in Prompt Engineering / GenAI - 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 sequential chain that runs two steps in order.

Prompt Engineering / GenAI
from langchain.chains import SequentialChain
chain = SequentialChain(chains=[chain1, [1]], input_variables=["input"], output_variables=["output"])
result = chain.run("Hello")
Drag options to blanks, or click blank then click option'
Achain2
Bchain3
Cchain4
Dchain5
Attempts:
3 left
💡 Hint
Common Mistakes
Using a chain variable that is not defined.
Swapping the order of chains.
2fill in blank
medium

Complete the code to define a router chain that selects chains based on input keys.

Prompt Engineering / GenAI
from langchain.chains.router import RouterChain
router = RouterChain(chains={'sales': sales_chain, 'support': [1], default_chain=default_chain)
response = router.run("support request")
Drag options to blanks, or click blank then click option'
Afinance_chain
Bmarketing_chain
Chr_chain
Dsupport_chain
Attempts:
3 left
💡 Hint
Common Mistakes
Using a chain that does not match the key.
Forgetting to include the correct chain in the dictionary.
3fill in blank
hard

Fix the error in the sequential chain code by filling the blank correctly.

Prompt Engineering / GenAI
from langchain.chains import SequentialChain
chain = SequentialChain(chains=[chain1, chain2], input_variables=["input"], output_variables=[[1]])
result = chain.run("Test")
Drag options to blanks, or click blank then click option'
A["result"]
B["answer"]
C["output"]
D["response"]
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name not matching the chain's output.
Not using a list for output_variables.
4fill in blank
hard

Fill both blanks to create a router chain that routes 'billing' and 'tech' requests correctly.

Prompt Engineering / GenAI
router = RouterChain(chains=[1], default_chain=default_chain)
response = router.run("billing issue")
Drag options to blanks, or click blank then click option'
A{'billing': tech_chain, 'tech': billing_chain}
B{'billing': billing_chain, 'tech': tech_chain}
C{'billing': default_chain, 'tech': tech_chain}
D{'billing': billing_chain, 'tech': default_chain}
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the chains for keys.
Using default_chain instead of specific chains.
5fill in blank
hard

Fill all three blanks to build a sequential chain with correct input and output variables.

Prompt Engineering / GenAI
chain = SequentialChain(chains=[chainA, chainB], input_variables=[[1]], output_variables=[[2]])
result = chain.run([3])
Drag options to blanks, or click blank then click option'
A"user_input"
B"final_output"
C{"user_input": "Hello"}
D"input_text"
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names for input or output.
Passing a string instead of a dictionary to run.