Monday, October 21, 2024

ChromaDB Hybrid Search

Chroma has query method which does the document search. This is effective for doing hybrid search. 

 

import chromadb

# Initialize Chroma

client = chromadb.Client()


# Create a collection with metadata

collection = client.create_collection(name="my_collection")


# Add documents with vectors and metadata

collection.add(

    embeddings=[[0.1, 0.2, 0.3], [0.2, 0.1, 0.4]],  # Embeddings

    documents=["Document 1", "Document 2"],         # Documents

    metadatas=[{"category": "science", "author": "Alice"},

              {"category": "history", "author": "Bob"}],  # Metadata

    ids=["doc1", "doc2"]

)


# Perform a hybrid search (vector search + metadata filtering)

results = collection.query(

    query_embeddings=[[0.1, 0.2, 0.3]],  # Embedding query vector

    n_results=5,                         # Number of results

    where={"category": "science"}        # Metadata filter

)


# Output results

for result in results["documents"]:

    print(result)

No comments:

Post a Comment