0
0
LangChainframework~10 mins

Prompt composition and chaining 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 prompt template with a variable called 'name'.

LangChain
from langchain.prompts import PromptTemplate

prompt = PromptTemplate(input_variables=["name"], template="Hello, [1]!")
Drag options to blanks, or click blank then click option'
Alocation
Bage
Cname
Dgreeting
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name in the template that is not listed in input_variables.
Forgetting to use curly braces around the variable in the template.
2fill in blank
medium

Complete the code to chain two prompts where the output of the first is input to the second.

LangChain
from langchain.chains import SimpleSequentialChain

chain = SimpleSequentialChain(chains=[chain1, [1]])
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.
Mixing the order of chains.
3fill in blank
hard

Fix the error in the prompt template by completing the missing input variable.

LangChain
prompt = PromptTemplate(input_variables=[[1]], template="What is the capital of {country}?")
Drag options to blanks, or click blank then click option'
A"city"
B"state"
C"capital"
D"country"
Attempts:
3 left
💡 Hint
Common Mistakes
Listing a variable in input_variables that is not used in the template.
Forgetting to include the variable used in the template.
4fill in blank
hard

Fill both blanks to create a chain that takes input 'question' and outputs 'answer'.

LangChain
from langchain.chains import LLMChain

chain = LLMChain(llm=llm, prompt=prompt, output_key=[1], input_key=[2])
Drag options to blanks, or click blank then click option'
A"answer"
B"question"
C"response"
D"query"
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping input_key and output_key values.
Using keys that don't match the prompt variables.
5fill in blank
hard

Fill all three blanks to create a prompt template with variables 'topic' and 'style', and chain them correctly.

LangChain
from langchain.prompts import PromptTemplate
from langchain.chains import SimpleSequentialChain

prompt1 = PromptTemplate(input_variables=[[1]], template="Write about [2].")
prompt2 = PromptTemplate(input_variables=["style"], template="Make it [3].")

chain1 = LLMChain(llm=llm, prompt=prompt1)
chain2 = LLMChain(llm=llm, prompt=prompt2)

chain = SimpleSequentialChain(chains=[chain1, chain2])
Drag options to blanks, or click blank then click option'
A"topic"
Btopic
C"style"
Dstyle
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting variable names in input_variables list.
Mismatching variable names between template and input_variables.