Skip to main content

Class: HydeRetriever

Defined in: packages/agentos/src/rag/HydeRetriever.ts:166

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

Constructors

Constructor

new HydeRetriever(opts): HydeRetriever

Defined in: packages/agentos/src/rag/HydeRetriever.ts:171

Parameters

opts
config?

Partial<HydeConfig>

embeddingManager

IEmbeddingManager

llmCaller

HydeLlmCaller

Returns

HydeRetriever

Accessors

enabled

Get Signature

get enabled(): boolean

Defined in: packages/agentos/src/rag/HydeRetriever.ts:182

Whether HyDE is enabled.

Returns

boolean

Methods

generateHypothesis()

generateHypothesis(query): Promise<{ hypothesis: string; latencyMs: number; }>

Defined in: packages/agentos/src/rag/HydeRetriever.ts:202

Generate a hypothetical answer for a query.

Parameters

query

string

Returns

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


generateMultipleHypotheses()

generateMultipleHypotheses(query, count?): Promise<{ hypotheses: string[]; latencyMs: number; }>

Defined in: packages/agentos/src/rag/HydeRetriever.ts:243

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.

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

retrieve()

retrieve(opts): Promise<HydeRetrievalResult>

Defined in: packages/agentos/src/rag/HydeRetriever.ts:438

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
collectionName

string

hypothesis?

string

Pre-generated hypothesis (skip generation if provided).

query

string

queryOptions?

Partial<QueryOptions>

vectorStore

IVectorStore

Returns

Promise<HydeRetrievalResult>


retrieveContext()

retrieveContext(opts): Promise<{ chunkCount: number; context: string; effectiveThreshold: number; hypothesis: string; latencyMs: number; }>

Defined in: packages/agentos/src/rag/HydeRetriever.ts:531

Convenience: retrieve and format as augmented context string.

Parameters

opts
collectionName

string

query

string

queryOptions?

Partial<QueryOptions>

separator?

string

vectorStore

IVectorStore

Returns

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


retrieveMulti()

retrieveMulti(opts): Promise<HydeMultiRetrievalResult>

Defined in: packages/agentos/src/rag/HydeRetriever.ts:356

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

Retrieval options.

collectionName

string

Collection to search in.

hypothesisCount?

number

Override hypothesis count for this call.

query

string

The user query.

queryOptions?

Partial<QueryOptions>

Additional query options.

vectorStore

IVectorStore

Vector store to search.

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`);