Get startedGet started for free

Prompt templates and chaining

In this exercise, you'll begin using two of the core components in LangChain: prompt templates and chains!

The classes necessary for completing this exercise, including ChatOpenAI, have been pre-loaded for you.

This exercise is part of the course

Developing LLM Applications with LangChain

View Course

Exercise instructions

  • Convert the template text provided into a standard (non-chat) prompt template.
  • Create a chain to pass the prompt template into the LLM.
  • Invoke the chain on the question variable provided.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Create a prompt template from the template string
template = "You are an artificial intelligence assistant, answer the question. {question}"
prompt = PromptTemplate.____(
    template=____
)

llm = ChatOpenAI(model="gpt-4o-mini", api_key='')	

# Create a chain to integrate the prompt template and LLM
llm_chain = ____ | ____

# Invoke the chain on the question
question = "How does LangChain make LLM application development easier?"
print(llm_chain.invoke({"question": ____}))
Edit and Run Code