HyDE retriever: generates a hypothetical answer, embeds it, and searches the vector store with adaptive thresholding.

Accessors

Constructors

Methods

  • Generate a hypothetical answer for a query.

    Parameters

    • query: string

    Returns Promise<{
        hypothesis: string;
        latencyMs: number;
    }>

  • Generate multiple hypothetical documents from different perspectives.

    Each hypothesis approaches the query from a different angle, improving recall by covering more of the semantic space. Uses chain-of-thought prompting to ensure diverse, high-quality hypotheses.

    The system prompt asks the LLM to generate N diverse hypotheses:

    • Hypothesis 1: Technical/formal perspective
    • Hypothesis 2: Practical/example perspective
    • Hypothesis 3: Overview/summary perspective
    • (Additional hypotheses explore further angles)

    Parameters

    • query: string

      The user query to generate hypotheses for.

    • Optional count: number

      Number of hypotheses to generate. Default: config.hypothesisCount (3).

    Returns Promise<{
        hypotheses: string[];
        latencyMs: number;
    }>

    Generated hypotheses and timing.

    Throws

    If the LLM call fails.

    Example

    const { hypotheses, latencyMs } = await retriever.generateMultipleHypotheses(
    'How does BM25 scoring work?',
    3,
    );
    // hypotheses[0]: Technical explanation with formulas
    // hypotheses[1]: Practical example with code
    // hypotheses[2]: High-level conceptual overview
  • Multi-hypothesis retrieval: generates N diverse hypotheses, searches with each, and merges results by deduplication (keeping the highest score per document).

    This dramatically improves recall compared to single-hypothesis HyDE because one bad hypothesis doesn't ruin everything — other hypotheses can still find relevant documents from different angles.

    Pipeline:

    1. Generate N hypotheses via generateMultipleHypotheses
    2. Embed each hypothesis
    3. Search the vector store with each embedding
    4. Union all results, deduplicate by document ID, keep highest score

    Parameters

    • opts: {
          query: string;
          vectorStore: IVectorStore;
          collectionName: string;
          queryOptions?: Partial<QueryOptions>;
          hypothesisCount?: number;
      }

      Retrieval options.

      • query: string

        The user query.

      • vectorStore: IVectorStore

        Vector store to search.

      • collectionName: string

        Collection to search in.

      • Optional queryOptions?: Partial<QueryOptions>

        Additional query options.

      • Optional hypothesisCount?: number

        Override hypothesis count for this call.

    Returns Promise<HydeMultiRetrievalResult>

    Deduplicated results from all hypotheses.

    Example

    const result = await retriever.retrieveMulti({
    query: 'How does BM25 work?',
    vectorStore: myStore,
    collectionName: 'knowledge-base',
    hypothesisCount: 3,
    });
    console.log(`Found ${result.queryResult.documents.length} unique docs from ${result.hypothesisCount} hypotheses`);
  • Embed the hypothesis and search the vector store. Uses adaptive thresholding: starts at initialThreshold, steps down until results are found or minThreshold is reached.

    Parameters

    • opts: {
          query: string;
          vectorStore: IVectorStore;
          collectionName: string;
          queryOptions?: Partial<QueryOptions>;
          hypothesis?: string;
      }
      • query: string
      • vectorStore: IVectorStore
      • collectionName: string
      • Optional queryOptions?: Partial<QueryOptions>
      • Optional hypothesis?: string

        Pre-generated hypothesis (skip generation if provided).

    Returns Promise<HydeRetrievalResult>

  • Convenience: retrieve and format as augmented context string.

    Parameters

    • opts: {
          query: string;
          vectorStore: IVectorStore;
          collectionName: string;
          queryOptions?: Partial<QueryOptions>;
          separator?: string;
      }
      • query: string
      • vectorStore: IVectorStore
      • collectionName: string
      • Optional queryOptions?: Partial<QueryOptions>
      • Optional separator?: string

    Returns Promise<{
        context: string;
        hypothesis: string;
        effectiveThreshold: number;
        chunkCount: number;
        latencyMs: number;
    }>