pythonintermediate
ChromaDB Vector Database Operations
Store and query vector embeddings using ChromaDB for semantic search and RAG applications.
pythonPress ⌘/Ctrl + Shift + C to copy
import chromadb
from chromadb.utils import embedding_functions
client = chromadb.PersistentClient(path="./chroma_db")
openai_ef = embedding_functions.OpenAIEmbeddingFunction(
api_key="sk-...",
model_name="text-embedding-3-small"
)
collection = client.get_or_create_collection(
name="documents",
embedding_function=openai_ef,
metadata={"hnsw:space": "cosine"}
)
# Add documents (embeddings auto-generated)
collection.add(
documents=[
"Python is a versatile programming language",
"JavaScript powers the modern web",
"Rust provides memory safety without GC",
],
metadatas=[
{"lang": "python", "type": "general"},
{"lang": "javascript", "type": "web"},
{"lang": "rust", "type": "systems"},
],
ids=["doc1", "doc2", "doc3"]
)
# Semantic search
results = collection.query(
query_texts=["Which language is best for web?"],
n_results=2,
where={"type": "web"},
)
print(results["documents"])
print(results["distances"])
# Update
collection.update(
ids=["doc1"],
documents=["Python is widely used in AI and data science"],
metadatas=[{"lang": "python", "type": "ai"}]
)
# Delete
collection.delete(ids=["doc3"])
print(f"Total: {collection.count()}")Use Cases
- semantic search
- RAG context retrieval
- document similarity
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptadvanced
RAG Pipeline Implementation
Build a retrieval-augmented generation pipeline that grounds LLM answers in your own documents.
Best for: Grounding LLM answers in private documents
#ai#rag
pythonadvanced
Build a RAG Pipeline with LangChain
Implement retrieval-augmented generation using LangChain, embeddings, and a vector store.
Best for: Knowledge base Q&A
#ai#langchain
typescriptintermediate
Text Chunking Strategies for RAG
Implement different text chunking strategies for RAG pipelines — fixed, recursive, and semantic.
Best for: RAG pipeline preprocessing
#ai#chunking
pythonintermediate
ChromaDB Persistent Vector Store
Create, persist, and query a ChromaDB vector store for semantic document retrieval.
Best for: local vector DB
#chromadb#vector-db