This module implements the Generation stage of a Cognitive Retrieval-Augmented Generation (CognitiveRAG) pipeline.
Its responsibility is to take a user query and a set of retrieved document chunks, then generate a grounded, deterministic answer strictly based on the provided context.
The module uses Amazon Bedrock with Claude 3 Haiku to ensure reliability, traceability, and compliance with AWS-based hackathon requirements.
This stage does not perform retrieval, embedding, or ranking. It assumes those steps have already been completed upstream.
{
"query": "What is the parental leave policy?",
"retrieved_chunks": [
"Employees are entitled to parental leave of up to 12 weeks...",
"Eligibility requirements include..."
],
"chunk_metadata": [
{
"chunk_id": "mtsamples_0_chunk_17",
"doc_id": "mtsamples_0",
"section": "Employee Benefits",
"page": null,
"score": 0.92
}
]
}
{
"answer": "Employees are entitled to parental leave of up to 12 weeks.",
"sources": [
{
"chunk_id": "mtsamples_0_chunk_17",
"doc_id": "mtsamples_0",
"section": "Employee Benefits",
"page": null,
"score": 0.92
}
]
}
This module uses Amazon Bedrock to invoke Anthropic’s Claude 3 Haiku model via the Bedrock Runtime API.
Amazon Bedrock was selected to provide a fully managed, serverless interface for foundation models while maintaining strong governance, security, and cost controls.
Hackathon compliance
Ensures the project meaningfully uses AWS-native services rather than external APIs.
Serverless inference
No infrastructure provisioning, scaling logic, or deployment management required.
Security & governance
IAM-based authentication (no hardcoded API keys), aligned with AWS best practices.
Cost efficiency
Claude 3 Haiku is optimized for fast, low-latency, low-cost question-answering workloads.
Model flexibility
Enables switching between Anthropic, Meta, or Mistral models without changing application logic.
Compared to alternatives such as OpenAI’s API, Amazon Bedrock provides:
anthropic.claude-3-haiku-20240307-v1:00500The system prompt explicitly enforces:
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
Create a .env file if you want to load AWS credentials from environment variables (otherwise rely on your standard AWS CLI config):
AWS_ACCESS_KEY_ID=your_key
AWS_SECRET_ACCESS_KEY=your_secret
AWS_DEFAULT_REGION=ca-central-1
python generate_answer.py
boto3python-dotenvEnvironment variables are loaded from .env, which is excluded from version control.
This generation stage prioritizes grounded, explainable answers over creativity.
The design focuses on determinism, traceability, and correctness, which are essential for enterprise-style use cases such as policy lookup, documentation QA, and internal knowledge systems.
Amazon Bedrock was selected to balance speed, cost, and governance, making this module suitable for both rapid prototyping and scalable production deployments.