Databricks Generative AI Engineer Associate Question 198
Single answerYou are building a simple chain using the LangChain framework to create a conversational agent. The chain should first take a user input, summarize the input, and then generate a response based on the summarized input. Which of the following code snippets correctly implements this chain?
- A
from langchain.chains import SimpleSequentialChain from langchain.prompts import PromptTemplate from langchain.llms import OpenAI
llm = OpenAI() summarize_prompt = PromptTemplate(input_variables=['text'], template='Summarize the following: {text}') summarize_chain = LLMChain(llm=llm, prompt=summarize_prompt) response_prompt = PromptTemplate(input_variables=['summary'], template='Respond to the following summary: {summary}') response_chain = LLMChain(llm=llm, prompt=response_prompt) chain = SimpleSequentialChain(chains=[summarize_chain, response_chain])
- B
from langchain.chains import SimpleSequentialChain from langchain.prompts import PromptTemplate from langchain.llms import OpenAI
llm = OpenAI() summarize_prompt = PromptTemplate(input_variables=['text'], template='Summarize the following: {text}') response_prompt = PromptTemplate(input_variables=['summary'], template='Respond to the following summary: {summary}') chain = SimpleSequentialChain(chains=[summarize_prompt, response_prompt])
- C
from langchain.chains import SimpleSequentialChain from langchain.prompts import PromptTemplate from langchain.llms import OpenAI
llm = OpenAI() summarize_prompt = PromptTemplate(input_variables=['text'], template='Summarize the following: {text}') summarize_chain = LLMChain(llm=llm, prompt=summarize_prompt) response_prompt = PromptTemplate(input_variables=['summary'], template='Respond to the following summary: {summary}') response_chain = LLMChain(llm=llm, prompt=response_prompt) chain = SimpleSequentialChain(chains=[summarize_prompt, response_chain])
- D
from langchain.chains import SequentialChain from langchain.prompts import PromptTemplate from langchain.llms import OpenAI
llm = OpenAI() summarize_prompt = PromptTemplate(input_variables=['text'], template='Summarize the following: {text}') summarize_chain = LLMChain(llm=llm, prompt=summarize_prompt) response_prompt = PromptTemplate(input_variables=['summary'], template='Respond to the following summary: {summary}') response_chain = LLMChain(llm=llm, prompt=response_prompt) chain = SequentialChain(chains=[summarize_chain, response_chain])
Show answer and explanation
Correct answer: A
Explanation
The SimpleSequentialChain in LangChain is used to sequentially execute multiple chains where the output of one chain serves as the input to the next. In this scenario, each step (summarization and response generation) is implemented as an LLMChain, and the two LLMChains are combined using SimpleSequentialChain. The correct code snippet adheres to this requirement by using LLMChain instances for each step and combining them appropriately.
- A. Correct.
This code snippet correctly uses two LLMChain instances for summarization and response generation, and combines them into a SimpleSequentialChain to process user input in the required sequence.
- B. Incorrect.
This code snippet is incorrect because it attempts to include PromptTemplate instances directly in the SimpleSequentialChain instead of using LLMChain instances, which are required for processing prompts with language models.
- C. Incorrect.
This code snippet is incorrect because it mixes a PromptTemplate with an LLMChain in the SimpleSequentialChain, which is not valid. The SimpleSequentialChain expects all elements in the chain to be LLMChain instances.
- D. Incorrect.
This code snippet is incorrect because it uses SequentialChain instead of SimpleSequentialChain. While both are valid constructs, the question specifically requires the use of SimpleSequentialChain for this implementation.