The ChromaHandshake class provides seamless integration between Chonkie’s chunking system and ChromaDB, a popular vector database.Embed and store your Chonkie chunks in ChromaDB without ever leaving the Chonkie SDK.
from chonkie import ChromaHandshake# Initialize with default settings (in-memory ChromaDB)handshake = ChromaHandshake()# Or specify a persistent storage pathhandshake = ChromaHandshake(path="./chroma_db")# Or use an existing Chroma clientimport chromadbclient = chromadb.Client()handshake = ChromaHandshake(client=client, collection_name="my_collection")
from chonkie import ChromaHandshake, SemanticChunkerhandshake = ChromaHandshake() # Initializes a new Chroma clientchunker = SemanticChunker()chunks = chunker("Chonkie is the best chonker ever!")handshake.write(chunks)
You can retrieve the most similar chunks from your ChromaDB collection using the search method:
from chonkie import ChromaHandshake# Initialize the handshakehandshake = ChromaHandshake(collection_name="my_documents")results = handshake.search(query="best chonker", limit=2)for result in results: print(result["score"], result["text"])
from chonkie import ChromaHandshake# Initialize the handshakehandshake = ChromaHandshake(collection_name="my_documents")# Generate an embedding using the handshake's configured embedding functionembedding = handshake.embedding_function("best chonker").tolist()results = handshake.search(embedding=embedding, limit=2)for result in results: print(result["score"], result["text"])
from chonkie import ChromaHandshake, SemanticChunker# Initialize the handshakehandshake = ChromaHandshake(collection_name="my_documents")# Create some chunkschunker = SemanticChunker()chunks = chunker.chunk("Chonkie is the best chonker ever!")# The chunker creates chunks but doesn't store their embeddings on the object.# We can generate an embedding from a chunk's text to use for the search.if chunks: search_embedding = handshake.embedding_function(chunks.text).tolist() # Search the handshake using the generated embedding results = handshake.search( embedding=search_embedding, limit=2, ) for result in results: print(result["score"], result["text"])