Serve by file index, not full path
This commit is contained in:
25
db.py
25
db.py
@@ -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]
|
||||
Reference in New Issue
Block a user