Serve by file index, not full path

This commit is contained in:
Jan Mrna
2025-11-06 10:45:42 +01:00
parent e624db1ce7
commit 788eebc916
3 changed files with 29 additions and 16 deletions

25
db.py
View File

@@ -26,7 +26,7 @@ class Record:
class QueryResult:
record: Record
distance: float
document: Path
document_name: str
@dataclass(slots=True)
class Database:
@@ -197,7 +197,7 @@ def query(db: Database | Path, text: str, record_count: int = 10) -> list[QueryR
# Look up the corresponding record
if vector_bytes in db.records:
record = db.records[vector_bytes]
results.append(QueryResult(record, distance, db.documents[record.document_index]))
results.append(QueryResult(record, distance, db.documents[record.document_index].name))
return results
@@ -272,4 +272,23 @@ def add_document(db: Database | Path, file: Path, max_workers: int = 4) -> None:
# Save database if we loaded it from file
if save_to_file and database_file_path:
save(db, database_file_path)
print(f"Database saved to {database_file_path}")
print(f"Database saved to {database_file_path}")
def get_document_path(db: Database | Path, document_index: int) -> Path:
"""
Get the file path of the document at the given index in the database.
Args:
db: Database object or path to database file
document_index: Index of the document to retrieve
Returns:
Path to the document file
"""
if isinstance(db, Path):
db = load(db)
if document_index < 0 or document_index >= len(db.documents):
raise IndexError(f"Document index out of range: {document_index}")
return db.documents[document_index]