Consider a LangChain chain that calls an LLM with a prompt template versus a direct API call to the same LLM with the same prompt. What difference in output behavior would you expect?
from langchain import LLMChain, PromptTemplate from langchain.llms import OpenAI llm = OpenAI(temperature=0) prompt = PromptTemplate(template="Translate '{text}' to French.", input_variables=["text"]) chain = LLMChain(llm=llm, prompt=prompt) result_chain = chain.run(text="Hello") # Direct API call result_direct = llm("Translate 'Hello' to French.")
Think about what LangChain adds on top of the API call.
LangChain's LLMChain formats the prompt and calls the LLM API under the hood. The output string is the same as the direct API call with the same prompt and parameters.
When you run multiple prompts in sequence, how does LangChain manage state compared to direct API calls?
from langchain import ConversationChain from langchain.llms import OpenAI llm = OpenAI(temperature=0) conv = ConversationChain(llm=llm) response1 = conv.predict(input="Hello!") response2 = conv.predict(input="How are you?") # Direct calls resp1 = llm("Hello!") resp2 = llm("How are you?")
Think about how conversation context is preserved.
LangChain's ConversationChain stores previous messages internally to provide context. Direct API calls are stateless unless you manually include prior messages.
Identify the correct code to create a LangChain LLMChain with a prompt template that asks for a summary.
Check the order and presence of required arguments in LLMChain constructor.
LLMChain requires both llm and prompt arguments. The correct order is llm first, then prompt.
Given this code, why does running chain.run() raise a KeyError?
from langchain import LLMChain, PromptTemplate from langchain.llms import OpenAI prompt = PromptTemplate(template="Answer: {question}", input_variables=["question"]) llm = OpenAI() chain = LLMChain(llm=llm, prompt=prompt) result = chain.run()
Check the input variables required by the prompt template.
The prompt template expects a 'question' variable. Calling run() without arguments causes a KeyError for missing 'question'.
Choose the best advantage LangChain offers compared to making direct API calls to language models.
Think about what LangChain adds beyond just calling the API.
LangChain provides tools to build chains, manage memory, use prompt templates, and integrate with other services, making complex app building easier.