Let's design a Production-Grade Document Question-Answering System using LLMs, similar to systems used by companies to help customers find information in their documentation or knowledge base.
AI System Design: Document QA System
1. Requirements & Goals
Functional Requirements:
Answer user questions based on a company's documentation/knowledge base
Provide source citations for answers
Handle questions outside the document scope gracefully
Support multiple document formats (PDF, HTML, Markdown, etc.)
Non-Functional Requirements:
Latency: Response time < 3 seconds
Cost: Optimize for LLM token usage
Accuracy: High precision in answers with minimal hallucination
Scale: Handle 100K+ documents and thousands of concurrent users
2. High-Level Architecture
The system follows a Retrieval-Augmented Generation (RAG) architecture with these main components:
def generate_response(query, context):
prompt = f"""
Context: {context}
Question: {query}
Instructions:
1. Answer based ONLY on the provided context
2. If the answer isn't in the context, say so
3. Cite specific sources for your answer
Answer:
"""
response = llm.generate(prompt)
return response
4. Technical Implementation Details
A. Vector Store Selection
Options:
Pinecone: Managed, scalable, but expensive
Weaviate: Open-source, good for hybrid search
FAISS + Redis: Custom solution, more control
Selection Criteria:
Query latency
Scale requirements
Cost considerations
Deployment complexity
B. LLM Integration
Model Selection:
Base Model Options:
GPT-4: Highest quality, expensive
Claude: Good balance
Llama-2: Self-hosted option
Fine-tuning strategy for domain adaptation
Prompt Engineering:
python
def construct_prompt(query, retrieved_chunks):
system_prompt = """You are a helpful assistant that answers questions based on provided documentation..."""
context = format_chunks(retrieved_chunks)
user_prompt = f"""
Context: {context}
Question: {query}
Answer the question using ONLY the provided context. Include citations.
"""
return system_prompt, user_prompt
C. Caching Strategy
Multi-Level Cache:
L1: In-memory cache for frequent queries
L2: Redis for distributed caching
L3: Vector store results cache
Cache Implementation:
python
class QueryCache:
def __init__(self):
self.local_cache = LRUCache(1000)
self.redis_client = Redis()
def get_cached_response(self, query):
# Check local cache
# Check Redis
# Return None if not found
pass