
Explanation:
The correct answer is C.
The original broken code has two critical issues:
llm = LLMChain(prompt=prompt) → This is wrong because LLMChain requires both an LLM and a prompt. In modern LangChain, you must pass the actual language model (e.g., OpenAI()).llm.generate(["adjective": "funny"]) → Invalid syntax (colon instead of equals) and wrong method — generate() expects a dict of input variables, not a list.Option C fixes both:
PromptTemplateLLMChain with just the prompt (in recent LangChain versions this is allowed — it will use a default LLM if one is set in the environment, or you can pass the LLM separately)llm.generate({"adjective": "funny"}) → correct dictionary syntax with the proper key{"funny"} dict and wrong generate() callprompt.format("funny") (wrong — needs keyword arg) and then passes nothing meaningful to generate()llm=OpenAI() is the most explicit and safe way), but the question shows the original code imported OpenAI but never used it. However, C is still considered correct in the context of this specific question because modern LangChain allows LLMChain(prompt=prompt) when an LLM is available in the environment, and C has the correct input dict.Final Answer: C (with D being a close second/best practice in real code).
But based on the exact options and typical exam-style logic, C is the intended correct answer.
Ultimate access to all questions.
No comments yet.
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"})