| name | vector-database |
| description | Work with vector databases for RAG, embeddings, and semantic search using ChromaDB or similar. Use when building knowledge bases for PSI Engine or AI-powered search. |
🧮 Vector Database Skill
ChromaDB Setup
Installation
pip install chromadb
Initialize
import chromadb
client = chromadb.PersistentClient(path="./chroma_db")
client = chromadb.Client()
collection = client.get_or_create_collection(
name="knowledge_base",
metadata={"hnsw:space": "cosine"}
)
CRUD Operations
Add Documents
collection.add(
documents=["How to fix null pointer exception in Python"],
metadatas=[{
"source": "agent_1",
"category": "debugging",
"language": "python",
"date": "2026-01-14"
}],
ids=["doc_001"]
)
Query (Semantic Search)
results = collection.query(
query_texts=["null reference error"],
n_results=5,
where={"category": "debugging"},
include=["documents", "metadatas", "distances"]
)
for i, doc in enumerate(results['documents'][0]):
print(f"Score: {results['distances'][0][i]}")
print(f"Doc: {doc}")
Update
collection.update(
ids=["doc_001"],
documents=["Updated solution for null pointer"],
metadatas=[{"updated": True}]
)
Delete
collection.delete(ids=["doc_001"])
collection.delete(where={"category": "outdated"})
RAG Pattern
def rag_query(question: str, context_limit: int = 5) -> str:
results = collection.query(
query_texts=[question],
n_results=context_limit
)
context = "\n\n".join(results['documents'][0])
prompt = f"""Based on this context:
{context}
Answer this question: {question}"""
return llm.generate(prompt)
Custom Embeddings
from chromadb.utils import embedding_functions
openai_ef = embedding_functions.OpenAIEmbeddingFunction(
api_key="your-key",
model_name="text-embedding-ada-002"
)
st_ef = embedding_functions.SentenceTransformerEmbeddingFunction(
model_name="all-MiniLM-L6-v2"
)
collection = client.create_collection(
name="custom_embeddings",
embedding_function=st_ef
)
Filtering
results = collection.query(
query_texts=["error handling"],
where={
"$and": [
{"category": {"$eq": "debugging"}},
{"language": {"$in": ["python", "javascript"]}}
]
}
)
results = collection.query(
query_texts=["error"],
where_document={"$contains": "exception"}
)
PSI Engine Integration
class KnowledgeHarvester:
def __init__(self):
self.client = chromadb.PersistentClient("./knowledge")
self.collection = self.client.get_or_create_collection("learnings")
def harvest(self, task_result: dict):
self.collection.add(
documents=[task_result['solution']],
metadatas=[{
'task': task_result['task'],
'agent': task_result['agent_id'],
'timestamp': datetime.now().isoformat()
}],
ids=[f"learning_{uuid.uuid4()}"]
)
def find_similar(self, query: str, limit: int = 5):
return self.collection.query(
query_texts=[query],
n_results=limit
)