Files
Chromy/main.py
T

90 lines
2.2 KiB
Python
Raw Normal View History

2026-04-21 14:32:10 +02:00
from __future__ import annotations
2026-04-21 15:28:20 +02:00
from chromadb.errors import InternalError, NotFoundError
from dotenv import load_dotenv
2026-04-21 14:45:01 +02:00
from chroma_functions import (
2026-04-21 15:28:20 +02:00
add_data,
2026-04-21 14:45:01 +02:00
count_collection,
create_collection,
delete_collection,
list_collections,
)
2026-04-21 15:28:20 +02:00
from chunk_functions import chunk_file
2026-04-21 14:32:10 +02:00
from cli_parser import build_parser
2026-04-21 15:28:20 +02:00
from embed import embed
load_dotenv()
2026-04-21 14:32:10 +02:00
def main() -> int:
args = build_parser().parse_args()
if args.command in {"list-collections", "lc"}:
collections = list_collections()
if not collections:
print("No collections found.")
return 0
for name in collections:
print(name)
2026-04-21 14:45:01 +02:00
2026-04-21 14:32:10 +02:00
return 0
if args.command in {"create-collection", "cc"}:
try:
2026-04-21 14:45:01 +02:00
collection = create_collection(args.name)
2026-04-21 14:32:10 +02:00
except InternalError:
print(f"Collection '{args.name}' already exists.")
return 1
2026-04-21 14:45:01 +02:00
print(f"Created collection '{collection}'.")
2026-04-21 14:32:10 +02:00
return 0
if args.command in {"delete-collection", "dc"}:
try:
delete_collection(args.name)
except NotFoundError:
print(f"Collection '{args.name}' does not exist.")
return 1
print(f"Deleted collection '{args.name}'.")
2026-04-21 14:45:01 +02:00
2026-04-21 14:32:10 +02:00
return 0
2026-04-21 14:45:01 +02:00
if args.command in {"count", "co"}:
try:
count = count_collection(args.name)
except NotFoundError:
print(f"Collection '{args.name}' does not exist.")
return 1
print(count)
return 0
2026-04-21 14:32:10 +02:00
2026-04-21 15:28:20 +02:00
if args.command in {"add-data", "ad"}:
try:
chunks = chunk_file(args.file)
embeddings = embed(chunks)
add_data(args.collection, embeddings)
except NotFoundError:
print(f"Collection '{args.collection}' does not exist.")
return 1
except FileNotFoundError:
print(f"The file {args.file} was not found.")
return 1
print(f"Added {len(embeddings)} records to collection '{args.collection}'.")
return 0
2026-04-21 14:32:10 +02:00
print("Nothing to do. Use -h to see available commands.")
2026-04-21 14:45:01 +02:00
2026-04-21 14:32:10 +02:00
return 0
if __name__ == "__main__":
raise SystemExit(main())