Databricks Generative AI Engineer Associate Question 194
Single answerYou are tasked with creating a simple chain in Databricks that consists of two steps. The first step prompts a user for input, and the second step generates a response using a pre-trained language model. Which code snippet best implements this functionality?
- A
from langchain.chains import SimpleChain from langchain.prompts import PromptTemplate
input_prompt = PromptTemplate(input_variables=['input'], template='Your input: {input}') chain = SimpleChain.from_prompts(input_prompt, None)
- B
from langchain.chains import SimpleChain from langchain.prompts import PromptTemplate from langchain.llms import OpenAI
input_prompt = PromptTemplate(input_variables=['input'], template='Your input: {input}') llm = OpenAI() chain = SimpleChain.from_prompts(input_prompt, llm)
- C
from langchain.chains import SequentialChain from langchain.prompts import PromptTemplate from langchain.llms import OpenAI
input_prompt = PromptTemplate(input_variables=['input'], template='Your input: {input}') llm = OpenAI() chain = SequentialChain.from_prompts(input_prompt, llm)
- D
from langchain.chains import LLMChain from langchain.prompts import PromptTemplate from langchain.llms import OpenAI
input_prompt = PromptTemplate(input_variables=['input'], template='Your input: {input}') llm = OpenAI() chain = LLMChain(prompt=input_prompt, llm=llm)
Show answer and explanation
Correct answer: D
Explanation
The LLMChain class in LangChain is specifically designed for creating simple chains that involve a prompt and a language model. It is the appropriate choice for implementing this two-step functionality. Other options either misuse classes or do not properly define the components needed for the chain.
- A. Incorrect.
This option incorrectly uses the
SimpleChainclass without providing a valid language model for the response generation step. TheNonevalue for the second step is not valid. - B. Incorrect.
While this option correctly includes a prompt and a language model, the
SimpleChainclass is not the correct choice for chaining a prompt with an LLM. - C. Incorrect.
This option uses
SequentialChaininstead ofLLMChain.SequentialChainis designed for multiple chained steps with dependencies, which is unnecessary for this simple two-step chain. - D. Correct.
This option correctly uses the
LLMChainclass, which is specifically designed for chains that include a prompt and an LLM. It correctly defines both the input prompt and the language model, making it the best choice for this task.