> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Implementing Question Answering

> Guide to implementing context-based question answering with foundation models, covering RAG, prompt patterns, best practices, and examples to produce grounded, auditable answers from internal documents.

In this lesson you'll learn how to use foundation models (via Amazon Bedrock or similar services) to answer questions about your own data using natural language. We’ll cover:

* The problem: finding precise answers across many documents.
* The solution: context‑augmented question answering and retrieval‑augmented generation (RAG).
* Prompt design patterns and examples for reliable, grounded answers.
* Best practices and expected outcomes.

## Problem statement

Organizations accumulate large volumes of content—documentation, policies, knowledge bases, and reports. The core challenge is locating the single correct answer quickly when relevant facts are spread across multiple sources.

A practical solution is to use Amazon Bedrock with a chosen foundation model to answer questions that depend on your internal content. When you ask a model a question it will typically respond in one of two ways:

* From pretrained knowledge (general world knowledge).
* From context you supply (documents or snippets attached to the prompt).

If you don’t supply relevant context, the model may hallucinate or return outdated/generalized answers because it lacks access to your internal policies. If you include the relevant documents as context, the model can produce accurate, grounded responses.

Consider this example: “How many days of PTO does a level 4 employee get?” Without your HR policy text, the model will likely guess. With the HR policy included as prompt context, the model can answer precisely.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/tDsOIcBSOgU8BE1P/images/Introduction-to-Amazon-Bedrock/Working-With-Different-Task-Types/Implementing-Question-Answering/question-answering-bedrock-hr-level4-15days.jpg?fit=max&auto=format&n=tDsOIcBSOgU8BE1P&q=85&s=18e1a8e12af820019ea9e9de20f4a184" alt="An infographic titled &#x22;Solution: Question Answering&#x22; showing a prompt and an HR policy document as model inputs feeding into a &#x22;Bedrock Foundation Model.&#x22; The model output box states: &#x22;A level 4 gets 15 days PTO per calendar year.&#x22;" width="1920" height="1080" data-path="images/Introduction-to-Amazon-Bedrock/Working-With-Different-Task-Types/Implementing-Question-Answering/question-answering-bedrock-hr-level4-15days.jpg" />
</Frame>

## Open vs. context‑based question answering

* Open Q\&A: The model answers from its pretrained knowledge (what it learned during training). Useful for general questions, but unreliable for internal facts.
* Context‑based Q\&A: You provide documents or snippets and instruct the model to answer using only that content. This grounds the response in your sources and greatly reduces hallucination.

Context‑based Q\&A is a fundamental building block for document search systems and knowledge assistants. It is commonly combined with retrieval‑augmented generation (RAG), where a semantic search step fetches the most relevant documents before the model generates an answer.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/tDsOIcBSOgU8BE1P/images/Introduction-to-Amazon-Bedrock/Working-With-Different-Task-Types/Implementing-Question-Answering/open-vs-context-qa-rag-diagram.jpg?fit=max&auto=format&n=tDsOIcBSOgU8BE1P&q=85&s=c18b65856db0fd43da31f341d6407087" alt="A side-by-side diagram comparing Open Question Answering (LLMs answering from training data/general knowledge) with Context-Based Question Answering (LLMs that use retrieved documents and a user question to produce answers grounded in source content). A banner below highlights Retrieval-Augmented Generation (RAG) and components like document search, knowledge assistants, and RAG systems." width="1920" height="1080" data-path="images/Introduction-to-Amazon-Bedrock/Working-With-Different-Task-Types/Implementing-Question-Answering/open-vs-context-qa-rag-diagram.jpg" />
</Frame>

## Prompt pattern and structure

Use a simple, repeatable pattern when asking a model to answer from a provided document:

```text theme={null}
Using the HR policy text provided below, answer the following question.

Question: How many PTO days are allocated to a Level 4 employee?

Instructions:
Provide the answer in one short sentence.
Only use information from the provided document.
Do not include additional explanation.
```

Key elements to include:

* A clearly labeled question.
* Explicit instruction to use the provided input (grounding).
* A strict output format (for example: “one short sentence”).
* A scope limitation so the model does not introduce external knowledge.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/tDsOIcBSOgU8BE1P/images/Introduction-to-Amazon-Bedrock/Working-With-Different-Task-Types/Implementing-Question-Answering/prompt-design-qa-workflow-tips.jpg?fit=max&auto=format&n=tDsOIcBSOgU8BE1P&q=85&s=d22790ef4e0a30c9ca5c14f48deba740" alt="An infographic titled &#x22;Workflow: Prompt Design for Q&A&#x22; showing a laptop screen with an example prompt on it. To the left are four numbered tips: Be specific; Provide context if needed; Specify answer format; Limit response scope." width="1920" height="1080" data-path="images/Introduction-to-Amazon-Bedrock/Working-With-Different-Task-Types/Implementing-Question-Answering/prompt-design-qa-workflow-tips.jpg" />
</Frame>

## Prompt examples: poor vs. better

Poor prompts often mix multiple questions, leave the desired format unspecified, and do not provide grounding:

* Poor: “Tell me about AWS security and encryption, how it works, and what services use that.”
  * Problems: multiple questions in one prompt, unclear scope, no output format.

Focused prompts work much better:

* Better: “Explain how KMS is used to encrypt data in Amazon S3 in three bullets.”
  * Benefits: single focused question, explicit format, scoped task.

You can use a short checklist when converting a loose question into a production prompt:

| Problem with prompt | How to fix it | Example |
| - | -: | - |
| Multiple tasks in one prompt | Split into separate prompts | `Explain KMS + Explain S3 access logging` → two prompts |
| No response format | Specify the format (sentence, bullets, steps) | `Answer in three bullets` |
| Unbounded scope | Narrow the topic and timeframe | `Describe KMS for S3 encryption (no references to other services)` |

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/tDsOIcBSOgU8BE1P/images/Introduction-to-Amazon-Bedrock/Working-With-Different-Task-Types/Implementing-Question-Answering/workflow-avoiding-mistakes-aws-kms-s3.jpg?fit=max&auto=format&n=tDsOIcBSOgU8BE1P&q=85&s=640ab297db88dde38cf0af8029cc9f2d" alt="An infographic titled &#x22;Workflow: Avoiding Common Mistakes&#x22; that compares a &#x22;Poor Prompt&#x22; and a &#x22;Better Prompt&#x22; side‑by‑side, using AWS encryption examples. The left side lists issues like multiple questions and no context, while the right shows a focused single question asking about AWS KMS and S3." width="1920" height="1080" data-path="images/Introduction-to-Amazon-Bedrock/Working-With-Different-Task-Types/Implementing-Question-Answering/workflow-avoiding-mistakes-aws-kms-s3.jpg" />
</Frame>

## Best practices for reliable Q\&A

* Ask one question at a time; decompose complex requests into sub‑questions.

* Define scope clearly (timeframe, role, product, or document sections).

* Specify the desired output format (one sentence, numbered steps, 3 bullets).

* Provide grounding documents or passages. If the model must rely solely on those documents, state that explicitly.

* Add an explicit fallback: instruct the model to answer “I don’t know” or “Not found in the provided documents” when the information is not present.

* Monitor and log model responses, confidence signals, and the source document used for auditing.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/tDsOIcBSOgU8BE1P/images/Introduction-to-Amazon-Bedrock/Working-With-Different-Task-Types/Implementing-Question-Answering/prompting-workflow-avoid-common-mistakes.jpg?fit=max&auto=format&n=tDsOIcBSOgU8BE1P&q=85&s=a1166f6c4e3c4ae2e08cb3de0d09e870" alt="An infographic titled &#x22;Workflow: Avoiding Common Mistakes&#x22; that lists best practices for prompting. It shows four colored tip boxes — one question at a time, define the scope, specify response format, and provide context (grounding)." width="1920" height="1080" data-path="images/Introduction-to-Amazon-Bedrock/Working-With-Different-Task-Types/Implementing-Question-Answering/prompting-workflow-avoid-common-mistakes.jpg" />
</Frame>

<Callout icon="lightbulb" color="#1CB2FE">
  Include an explicit fallback instruction in your prompt such as: `If the answer is not present in the provided documents, reply “Not found in the provided documents.”` This simple rule significantly reduces hallucinations and makes behavior predictable for downstream systems.
</Callout>

<Callout icon="warning" color="#FF6B6B">
  Do not assume model outputs are authoritative without source attribution. Always surface the document or snippet used for the answer and validate high‑impact responses with a human reviewer.
</Callout>

## Quick prompt checklist

* Single, clearly labeled question.
* Explicit grounding instruction.
* Strict output format.
* Explicit fallback for missing information.
* (Optional) Provide citation format if you want the model to reference document IDs or line numbers.

## Expected outcomes

Implementing context‑based Q\&A will:

* Reduce time to answer by surfacing precise facts from your documents.
* Improve discoverability of internal knowledge.
* Minimize hallucination when prompts are grounded and include a fallback.
* Enable knowledge assistants and RAG systems to provide reliable, auditable responses.

## Key takeaway

Clear, focused, single‑question prompts that are explicitly grounded in the right source documents produce the most accurate answers.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/tDsOIcBSOgU8BE1P/images/Introduction-to-Amazon-Bedrock/Working-With-Different-Task-Types/Implementing-Question-Answering/key-takeaway-01-clear-focused-questions.jpg?fit=max&auto=format&n=tDsOIcBSOgU8BE1P&q=85&s=0f7967f91fb45f800ca965f63eec055d" alt="A presentation slide titled &#x22;Key Takeaway&#x22; with a blue badge labeled &#x22;01&#x22; and the text &#x22;Clear, focused questions produce more accurate answers.&#x22; The left side is a dark blue panel and the rest of the slide is light gray." width="1920" height="1080" data-path="images/Introduction-to-Amazon-Bedrock/Working-With-Different-Task-Types/Implementing-Question-Answering/key-takeaway-01-clear-focused-questions.jpg" />
</Frame>

This concludes the lesson on implementing question‑and‑answer prompts. Related topics include retrieval pipelines and sentiment analysis using foundation models in Amazon Bedrock.

Links and references

* Amazon Bedrock overview: [https://aws.amazon.com/bedrock/](https://aws.amazon.com/bedrock/)
* Retrieval‑Augmented Generation (RAG) — conceptual guide: [https://en.wikipedia.org/wiki/Retrieval-augmented\_generation](https://en.wikipedia.org/wiki/Retrieval-augmented_generation)
* Prompt engineering best practices: [https://developers.google.com/machine-learning/guides/text/prompting](https://developers.google.com/machine-learning/guides/text/prompting)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/introduction-to-amazon-bedrock/module/108362ba-e1ca-4ff5-ba48-8d7594429db7/lesson/efaa3b7f-1c60-4e4f-aac1-b812572e4612" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.