Files
Chromy/chromy/chroma_functions.py
T

106 lines
2.7 KiB
Python
Raw Normal View History

2026-04-22 17:03:01 +02:00
from __future__ import annotations
from collections.abc import Sequence
from typing import cast
2026-04-21 15:28:20 +02:00
from uuid import uuid4
2026-04-21 17:13:43 +02:00
import chromadb
2026-04-21 18:24:49 +02:00
from chromadb.api import ClientAPI
2026-04-22 17:03:01 +02:00
from chromadb.api.types import QueryResult, Where
2026-04-21 17:13:43 +02:00
from chromadb.errors import NotFoundError
2026-04-22 15:47:46 +02:00
from chromy.embed import EmbeddingRecord
2026-04-21 14:32:10 +02:00
2026-04-21 18:24:49 +02:00
def _get_client_and_collection(
collection_name: str,
) -> tuple[ClientAPI, chromadb.Collection]:
client = chromadb.PersistentClient()
try:
collection = client.get_collection(name=collection_name)
except NotFoundError:
raise
return client, collection
2026-04-29 12:44:28 +02:00
def list_collections() -> list[str]:
2026-04-21 14:32:10 +02:00
client = chromadb.PersistentClient()
collections = client.list_collections()
if not collections:
return []
2026-04-29 12:44:28 +02:00
return [getattr(collection, "name", str(collection)) for collection in collections]
2026-04-21 14:32:10 +02:00
def create_collection(name: str) -> str:
client = chromadb.PersistentClient()
collection = client.create_collection(name=name)
2026-04-21 14:45:01 +02:00
2026-04-21 14:32:10 +02:00
return getattr(collection, "name", name)
def delete_collection(name: str) -> None:
client = chromadb.PersistentClient()
client.delete_collection(name=name)
2026-04-21 21:26:40 +02:00
def delete_data(collection_name: str, where: dict[str, str]) -> int:
_, collection = _get_client_and_collection(collection_name)
2026-04-22 17:03:01 +02:00
result = collection.delete(where=cast(Where, where))
2026-04-21 21:26:40 +02:00
return int(result.get("deleted", 0))
2026-04-29 14:46:41 +02:00
def has_data_for_file(collection_name: str, file_name: str) -> bool:
_, collection = _get_client_and_collection(collection_name)
result = collection.get(where=cast(Where, {"file_name": file_name}))
ids = result.get("ids", [])
return len(ids) > 0
2026-04-29 12:44:28 +02:00
def count_collection(collection_name: str) -> int:
2026-04-21 18:24:49 +02:00
_, collection = _get_client_and_collection(collection_name)
2026-04-29 12:44:28 +02:00
return collection.count()
2026-04-21 15:28:20 +02:00
2026-04-22 17:19:14 +02:00
def add_data(
collection_name: str,
data: Sequence[EmbeddingRecord],
file_name: str,
) -> None:
2026-04-21 15:28:20 +02:00
if not data:
return
2026-04-21 18:24:49 +02:00
_, collection = _get_client_and_collection(collection_name)
2026-04-21 15:28:20 +02:00
2026-04-24 18:23:02 +02:00
embeddings: list[Sequence[float]] = [record["embedding"] for record in data]
2026-04-22 17:03:01 +02:00
2026-04-21 18:24:49 +02:00
collection.add(
2026-04-21 15:28:20 +02:00
ids=[str(uuid4()) for _ in data],
2026-04-21 18:24:49 +02:00
metadatas=[{"file_name": file_name} for _ in data],
2026-04-21 15:28:20 +02:00
documents=[record["text"] for record in data],
2026-04-22 17:03:01 +02:00
embeddings=embeddings,
2026-04-21 15:28:20 +02:00
)
2026-04-21 17:13:43 +02:00
2026-04-22 17:19:14 +02:00
def query_data(collection_name: str, texts: Sequence[str]) -> QueryResult:
2026-04-21 17:20:45 +02:00
if not texts:
return {
"ids": [],
"documents": [],
"metadatas": [],
"distances": [],
2026-04-22 17:03:01 +02:00
"embeddings": None,
"uris": None,
"data": None,
"included": ["documents", "metadatas", "distances"],
2026-04-21 17:20:45 +02:00
}
2026-04-21 18:24:49 +02:00
_, collection = _get_client_and_collection(collection_name)
2026-04-21 17:20:45 +02:00
2026-04-22 17:19:14 +02:00
return collection.query(query_texts=list(texts))