Created
October 9, 2025 03:00
-
-
Save pzarzycki/b260d774e69e6e3de9848b3690e23391 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Install dependencies first (if not already installed): | |
# pip install chromadb sentence-transformers | |
import chromadb | |
from chromadb.utils import embedding_functions | |
# 1. Create or connect to a local ChromaDB client | |
client = chromadb.Client() | |
# 2. Define an embedding function (using a small local model) | |
embedding_func = embedding_functions.SentenceTransformerEmbeddingFunction( | |
model_name="all-MiniLM-L6-v2" | |
) | |
# 3. Create a collection (like a table) | |
collection = client.create_collection(name="my_collection", embedding_function=embedding_func) | |
# 4. Add some documents | |
collection.add( | |
ids=["doc1", "doc2", "doc3"], | |
documents=[ | |
"Chroma is a database for embeddings.", | |
"OpenAI builds powerful language models.", | |
"Python is a versatile programming language." | |
] | |
) | |
# 5. Run a similarity search | |
results = collection.query( | |
query_texts=["What is Chroma?"], | |
n_results=2 | |
) | |
print(results) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment