# Feature Design: Query Expansion & HyDE Integration

This document outlines the design for improving search relevance in Lumina using Query Expansion (Multi-Query) and Hypothetical Document Embeddings (HyDE).

## Problem Statement
The current search implementation relies on the user's original query. Simple vector search can sometimes fail to match relevant documents due to:
1.  **Keyword Mismatch**: The user might use different terminology than the document.
2.  **Semantic Gap**: The query might be too brief to capture the full semantic context required for a good vector match.

## Proposed Solution

### 1. Query Expansion (Multi-Query)
We will use an LLM to generate 3 unique variations of the user's query. This helps to:
- Capture different facets of the user's intent.
- Increase the probability of hitting relevant segments in the knowledge base.

### 2. HyDE (Hypothetical Document Embeddings)
We will use an LLM to generate a brief "hypothetical" answer to the user's query.
- Instead of embedding the question, we embed the hypothetical answer.
- This often results in better vector matches because we are comparing "answer-like" vectors with "document-like" segments.

## Technical Implementation

### Backend Changes

#### `RagService` (server/src/rag/rag.service.ts)
- **New Methods**:
    - `expandQuery(query: string, userId: string): Promise<string[]>`: Generates 3 variations of the query.
    - `generateHyDE(query: string, userId: string): Promise<string>`: Generates a hypothetical document.
- **Update `searchKnowledge`**:
    - Add `enableQueryExpansion` and `enableHyDE` parameters.
    - Implement logic to handle multiple search requests (concurrently) and deduplicate results.

#### `ChatService` (server/src/chat/chat.service.ts)
- Pass the new search options from user settings or request parameters.

### Frontend Changes

#### `types.ts` (web/types.ts)
- Update `AppSettings` to include `enableQueryExpansion` and `enableHyDE`.

#### `SettingsDrawer.tsx`
- Add UI toggles for these new search enhancement features.

## Verification Plan

### Backend Logs
- Verify that LLM calls for expansion and HyDE are being made.
- Log the generated queries and hypothetical documents for debugging.

### Manual Verification
- Compare search results with and without these features enabled for complex queries.
