KatInterview

Design a Question-Answering System with Document Retrieval

Machine LearningModel answer
HardModel answerMachine Learningragllmvector-search

Last updated

Build a production-ready system that answers questions from local documents using an LLM API, vector search, and streaming responses.

Design and implement a question-answering system that can respond to queries about a collection of local documents using a large language model. Your system should retrieve relevant passages and generate answers with proper citations.

Core Requirements:

  1. Document Processing: Ingest a directory containing text files (Markdown and PDF formats), split them into manageable pieces, and create searchable representations
  2. Query Interface: Support both command-line usage and an HTTP API endpoint for asking questions
  3. Answer Generation: Use an LLM API to generate responses that reference the retrieved content, with streaming output and source attribution
  4. Reliability: Handle API failures gracefully with appropriate retry logic and error responses
  5. Configuration: Support environment-based configuration for credentials and settings
  6. Documentation: Provide setup instructions and basic validation tests

Technical Specifications:

  • Assume approximately 1,000 documents totaling around 5 million tokens
  • Target sub-2-second time to first response token
  • Use embedding dimensions of 1024 with cosine similarity
  • Retrieve 5 context passages per query
  • Stream generated tokens as they arrive

Deliverables:

Explain your approach to the following:

  • How you would structure the document chunks and why
  • Your retrieval strategy and the trade-offs involved
  • The prompting technique for generating cited answers
  • Failure handling for network and API errors
  • A lightweight method to assess whether answers are accurate and well-grounded

Include code sketches for the critical components and discuss production considerations.

Hints

Hint 1

Consider combining multiple retrieval methods rather than relying on embeddings alone

Hint 2

Think about how chunk size affects both retrieval quality and generation context

Hint 3

Design your retry logic to handle rate limits differently than transient failures