Skip to main content
Utility modules providing shared functionality across all vector database integrations.

Evaluation Metrics

compute_recall_at_k

Compute Recall@k for a single query.
list[str]
required
List of retrieved document IDs in ranked order
set[str]
required
Set of ground truth relevant document IDs
int
required
Number of top results to consider
float
Recall score between 0 and 1. Formula: (relevant docs in top-k) / (total relevant docs)

compute_precision_at_k

Compute Precision@k for a single query.
list[str]
required
List of retrieved document IDs in ranked order
set[str]
required
Set of ground truth relevant document IDs
int
required
Number of top results to consider
float
Precision score between 0 and 1. Formula: (relevant docs in top-k) / k

compute_mrr

Compute Mean Reciprocal Rank for a single query.
list[str]
required
List of retrieved document IDs in ranked order
set[str]
required
Set of ground truth relevant document IDs
float
Reciprocal rank score between 0 and 1. Formula: 1 / (rank of first relevant document)

compute_ndcg_at_k

Compute Normalized Discounted Cumulative Gain at k.
list[str]
required
List of retrieved document IDs in ranked order
set[str]
required
Set of ground truth relevant document IDs
int
required
Number of top results to consider
float
NDCG score between 0 and 1. Formula: DCG@k / IDCG@k (Ideal DCG)

compute_hit_rate

Compute hit rate (binary success) for a single query.
list[str]
required
List of retrieved document IDs in ranked order
set[str]
required
Set of ground truth relevant document IDs
int
required
Number of top results to consider
float
1.0 if any relevant doc is in top-k, else 0.0

evaluate_retrieval

Evaluate retrieval quality over multiple queries.
list[QueryResult]
required
List of QueryResult objects with retrieved and relevant IDs
int
default:"5"
Cutoff for top-k metrics
RetrievalMetrics
RetrievalMetrics object with averaged scores across all queries

Sparse Embeddings

normalize_sparse

Normalize any sparse format to Haystack SparseEmbedding.
Union[SparseEmbedding, Dict[int, float], Dict[str, List], None]
required
Sparse embedding in any supported format:
  • SparseEmbedding object (passthrough)
  • Dict with int keys and float values (Milvus format)
  • Dict with indices and values lists (Pinecone format)
  • None (passthrough)
Optional[SparseEmbedding]
Normalized SparseEmbedding or None

to_milvus_sparse

Convert SparseEmbedding to Milvus format.
SparseEmbedding
required
Haystack SparseEmbedding object
Dict[int, float]
Dictionary mapping indices to values in Milvus format

to_pinecone_sparse

Convert SparseEmbedding to Pinecone sparse_values format.
SparseEmbedding
required
Haystack SparseEmbedding object
Dict[str, List]
Dictionary with ‘indices’ and ‘values’ keys in Pinecone format

to_qdrant_sparse

Convert SparseEmbedding to Qdrant SparseVector format.
SparseEmbedding
required
Haystack SparseEmbedding object
SparseVector
Qdrant SparseVector object

get_doc_sparse_embedding

Extract sparse embedding from Document, checking standard and legacy locations.
Any
required
Haystack Document object
str
default:"sparse_embedding"
Legacy meta key to check if doc.sparse_embedding is None
Optional[SparseEmbedding]
SparseEmbedding from doc.sparse_embedding or doc.meta[fallback_meta_key], or None

Document Converters

ChromaDocumentConverter

Utility class for converting between Haystack Documents and Chroma format.

prepare_haystack_documents_for_upsert

List[Document]
required
List of Haystack Document objects
Dict[str, Any]
Dictionary with keys: ids, texts, metadatas, embeddings formatted for Chroma upsert

PineconeDocumentConverter

Utility class for converting between Haystack Documents and Pinecone format.

prepare_haystack_documents_for_upsert

List[Document]
required
List of Haystack Document objects
List[Dict[str, Any]]
List of Pinecone vector dictionaries with id, values, metadata keys

convert_query_results_to_haystack_documents

Dict[str, Any]
required
Pinecone query results dictionary
bool
default:"False"
Whether to include vector embeddings in Documents
List[Document]
List of Haystack Document objects

Configuration

load_config

Load configuration from YAML file.
str
required
Path to YAML configuration file
Dict[str, Any]
Loaded configuration dictionary with environment variables resolved

resolve_env_vars

Resolve environment variable references in configuration.
Dict[str, Any]
required
Configuration dictionary potentially containing $ references
Dict[str, Any]
Configuration with all environment variables resolved