
Explanation:
D is the correct answer.
LLMChain requires both an llm (the actual model) and a prompt. The original code only passes prompt=prompt, so it fails to initialize (the import for OpenAI is there but unused).
D correctly does: LLMChain(llm=OpenAI(), prompt=prompt).
It also fixes the call to llm.generate({"adjective": "funny"}), which is the proper input format for a single example (a dict of variables). The original ["adjective": "funny"] has invalid syntax and the wrong structure.
A: Still missing the llm= argument and uses invalid .generate syntax.
B: Incorrectly does prompt.format("funny") (positional instead of keyword) and then passes a formatted string to LLMChain (wrong) + empty .generate().
C: Fixes only the .generate call but leaves the chain initialization broken (no LLM).
Newer LangChain code often uses LCEL instead:
from langchain_core.prompts import PromptTemplate
from langchain_openai import OpenAI # or ChatOpenAI
prompt = PromptTemplate(...)
llm = OpenAI()
chain = prompt | llm
result = chain.invoke({"adjective": "funny"})
from langchain_core.prompts import PromptTemplate
from langchain_openai import OpenAI # or ChatOpenAI
prompt = PromptTemplate(...)
llm = OpenAI()
chain = prompt | llm
result = chain.invoke({"adjective": "funny"})
Ultimate access to all questions.
A Generative AI Engineer is testing a simple prompt template in LangChain using the code below, but it is getting an error.
from langchain.chains import LLMChain
from langchain.community.llms import OpenAI
from langchain.core.prompts import PromptTemplate
prompt_template = "Tell me a {adjective} joke"
prompt = PromptTemplate(
input_variables=["adjective"],
template=prompt_template
)
llm = LLMChain(prompt=prompt)
llm.generate(["adjective": "funny"])
from langchain.chains import LLMChain
from langchain.community.llms import OpenAI
from langchain.core.prompts import PromptTemplate
prompt_template = "Tell me a {adjective} joke"
prompt = PromptTemplate(
input_variables=["adjective"],
template=prompt_template
)
llm = LLMChain(prompt=prompt)
llm.generate(["adjective": "funny"])
Assuming the API key was properly defined, what change does the Generative AI Engineer need to make to fix their chain?
A
prompt_template = "Tell me a {adjective} joke"
prompt = PromptTemplate(
input_variables=["adjective"],
template=prompt_template
)
llm = LLMChain(prompt=prompt)
llm.generate({"funny"})
prompt_template = "Tell me a {adjective} joke"
prompt = PromptTemplate(
input_variables=["adjective"],
template=prompt_template
)
llm = LLMChain(prompt=prompt)
llm.generate({"funny"})
B
prompt_template = "Tell me a {adjective} joke"
prompt = PromptTemplate(
input_variables=["adjective"],
template=prompt_template
)
llm = LLMChain(prompt=prompt.format("funny"))
llm.generate()
prompt_template = "Tell me a {adjective} joke"
prompt = PromptTemplate(
input_variables=["adjective"],
template=prompt_template
)
llm = LLMChain(prompt=prompt.format("funny"))
llm.generate()
C
prompt_template = "Tell me a {adjective} joke"
prompt = PromptTemplate(
input_variables=["adjective"],
template=prompt_template
)
llm = LLMChain(prompt=prompt)
llm.generate({"adjective": "funny"})
prompt_template = "Tell me a {adjective} joke"
prompt = PromptTemplate(
input_variables=["adjective"],
template=prompt_template
)
llm = LLMChain(prompt=prompt)
llm.generate({"adjective": "funny"})
D
prompt_template = "Tell me a {adjective} joke"
prompt = PromptTemplate(
input_variables=["adjective"],
template=prompt_template
)
llm = LLMChain(llm=OpenAI(), prompt=prompt)
llm.generate({"adjective": "funny"})
prompt_template = "Tell me a {adjective} joke"
prompt = PromptTemplate(
input_variables=["adjective"],
template=prompt_template
)
llm = LLMChain(llm=OpenAI(), prompt=prompt)
llm.generate({"adjective": "funny"})