Export Chonkie’s Chunks into a MongoDB collection.
The MongoDBHandshake class provides integration between Chonkie’s chunking system and MongoDB, a popular NoSQL database.Embed and store your Chonkie chunks in MongoDB directly from the Chonkie SDK.
from chonkie import MongoDBHandshake, SemanticChunker # Initialize the handshakehandshake = MongoDBHandshake( uri="mongodb://localhost:27017", db_name="my_documents", collection_name="my_collection")# Create some chunkschunker = SemanticChunker()chunks = chunker.chunk("Chonkie loves to chonk your texts!")# Write chunks to MongoDBhandshake.write(chunks)
You can retrieve the most similar chunks from your MongoDB collection using the search method:
from chonkie import MongoDBHandshake# Initialize the handshakehandshake = MongoDBHandshake( uri="mongodb://localhost:27017", db_name="my_documents", collection_name="my_collection")results = handshake.search(query="chonk your texts", limit=2)for result in results: print(result["score"], result["text"])
from chonkie import MongoDBHandshake# Initialize the handshakehandshake = MongoDBHandshake( uri="mongodb://localhost:27017", db_name="my_documents", collection_name="my_collection")embedding = handshake.embedding_model.embed("chonk your texts").tolist()results = handshake.search(embedding=embedding, limit=2)for result in results: print(result["score"], result["text"])
from chonkie import MongoDBHandshake, SemanticChunker# Initialize the handshakehandshake = MongoDBHandshake( uri="mongodb://localhost:27017", db_name="my_documents", collection_name="my_collection")# Create some chunkschunker = SemanticChunker(embedding_model=handshake.embedding_model)chunks = chunker.chunk("Chonkie loves to chonk your texts!")# Search the handshakeresults = handshake.search( embedding=chunks[0].sentences[0].embedding, limit=2,)for result in results: print(result["score"], result["text"])