A question alone may not give a chatbot enough information to answer. Context is the background text or information placed in the prompt alongside the question. The model uses both to formulate an answer. section 6.1.1
In a structured prompt, context explains the situation surrounding a request. Keep it distinct from the instruction, which says what to do, and from specific input, such as a passage or list of points to process. Include the parts that help the model understand the request; the prompt structure is a checklist, not a requirement to fill every field. section 2.6
Ask about a document
To ask about the temples at Paestum, put a question about their number, builders, and architectural style before a passage describing the site. The passage supplies context; the question identifies what to extract from it. A follow-up question can narrow an initial answer. section 6.1.1
Listing 7.3 Initial RAG setup before incorporating message history
from langchain_core.runnables import RunnablePassthrough
from langchain_core.prompts import PromptTemplate
from langchain_openai import ChatOpenAI
rag_prompt_template = """Use the following pieces of context
to answer the question at the end.
If you don't know the answer, just say that you don't know,
don't try to make up an answer.
Use three sentences maximum and keep the
answer as concise as possible.
{context}
Question: {question}
Helpful Answer:"""
rag_prompt = PromptTemplate.from_template(rag_prompt_template)
retriever = vector_db.as_retriever()
question_feeder = RunnablePassthrough()
chatbot = ChatOpenAI(openai_api_key=OPENAI_API_KEY,
model_name="gpt-5-nano")
rag_chain = {"context": retriever,
"question": question_feeder}|rag_prompt|chatbot
def execute_chain(chain, question):
answer = rag_chain.invoke(question)
return answer
The RAG prompt has separate {context} and {question} slots. A retriever supplies context, while the user's question flows into the question slot. The shown setup precedes the addition of message history. section 7.4
For an ongoing chat, the current document is not the only useful context. Earlier questions and answers can help the chatbot interpret a new prompt, so a RAG chat setup can be amended to include message history. section 7.4

















