0
0
LangChainframework~10 mins

Why model abstraction matters in LangChain - Test Your Understanding

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

Complete the code to create a simple model abstraction using LangChain.

LangChain
from langchain.llms import OpenAI

llm = [1](temperature=0.7)

print(llm("Say hello"))
Drag options to blanks, or click blank then click option'
AOpenAI
BPromptTemplate
CBaseLLM
DChatOpenAI
Attempts:
3 left
💡 Hint
Common Mistakes
Using ChatOpenAI instead of OpenAI here
Using PromptTemplate which is for prompts, not models
2fill in blank
medium

Complete the code to use model abstraction to switch between models easily.

LangChain
def get_model(model_name):
    if model_name == "openai":
        return [1](temperature=0)
    elif model_name == "chat":
        return ChatOpenAI(temperature=0)

model = get_model("openai")
print(model("Hello!"))
Drag options to blanks, or click blank then click option'
AOpenAI
BBaseLLM
CPromptTemplate
DChatOpenAI
Attempts:
3 left
💡 Hint
Common Mistakes
Returning BaseLLM which is abstract
Returning PromptTemplate which is not a model
3fill in blank
hard

Fix the error in the code by choosing the correct method to generate text from the model abstraction.

LangChain
llm = OpenAI(temperature=0.5)

response = llm.[1](["What is AI?"])
print(response.generations[0][0].text)
Drag options to blanks, or click blank then click option'
Arun
Bgenerate
Ccall
Dexecute
Attempts:
3 left
💡 Hint
Common Mistakes
Using call which does not exist on the LLM instance
Using run or execute which do not exist here
4fill in blank
hard

Fill both blanks to create a flexible prompt and generate a response using model abstraction.

LangChain
from langchain.prompts import [1]
from langchain.llms import OpenAI

prompt = [2](template="Say something about {topic}.")
llm = OpenAI(temperature=0.3)

text = prompt.format(topic="LangChain")
response = llm(text)
print(response)
Drag options to blanks, or click blank then click option'
APromptTemplate
BChatPromptTemplate
CBasePrompt
DSimplePrompt
Attempts:
3 left
💡 Hint
Common Mistakes
Using ChatPromptTemplate which is for chat models
Using BasePrompt which is abstract
5fill in blank
hard

Fill all three blanks to build a chain that uses model abstraction and prompt templates.

LangChain
from langchain.chains import LLMChain
from langchain.llms import [1]
from langchain.prompts import [2]

llm = [1](temperature=0.2)
prompt = [2](template="Explain {concept} simply.")
chain = LLMChain(llm=llm, prompt=prompt)

result = chain.run(concept="model abstraction")
print(result)
Drag options to blanks, or click blank then click option'
AOpenAI
BChatOpenAI
CPromptTemplate
DBaseLLM
Attempts:
3 left
💡 Hint
Common Mistakes
Using ChatOpenAI with PromptTemplate which may mismatch
Using BaseLLM which is abstract and cannot be instantiated