-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs: #4
- Loading branch information
Showing
7 changed files
with
130 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import argparse | ||
import os | ||
import sqlite3 | ||
import sys | ||
|
||
import chromadb | ||
import typer | ||
|
||
from chroma_ops.utils import validate_chroma_persist_dir, read_script | ||
|
||
|
||
def rebuild_fts(persist_dir: str) -> None: | ||
validate_chroma_persist_dir(persist_dir) | ||
sql_file = os.path.join(persist_dir, "chroma.sqlite3") | ||
conn = sqlite3.connect(sql_file) | ||
cursor = conn.cursor() | ||
script = read_script("scripts/drop_fts.sql") | ||
cursor.executescript(script) | ||
cursor.close() | ||
conn.close() | ||
typer.echo("Dropped FTS. Will try to start your Chroma now.", file=sys.stderr) | ||
typer.echo( | ||
"NOTE: Depending on the size of your documents in Chroma it may take a while for Chroma to start up again.", | ||
file=sys.stderr, | ||
color=typer.colors.YELLOW, | ||
) | ||
try: | ||
chromadb.PersistentClient(path=persist_dir) | ||
typer.echo("Chroma started successfully.", file=sys.stderr) | ||
except Exception as e: | ||
typer.echo( | ||
f"Chroma failed to start. Error: {repr(e)}", | ||
file=sys.stderr, | ||
color=typer.colors.RED, | ||
err=True, | ||
) | ||
|
||
|
||
def command( | ||
persist_dir: str = typer.Argument(..., help="The persist directory"), | ||
) -> None: | ||
rebuild_fts(persist_dir) | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("persist_dir", type=str) | ||
arg = parser.parse_args() | ||
rebuild_fts(arg.persist_dir) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
BEGIN TRANSACTION; | ||
DROP TABLE IF EXISTS embedding_fulltext_search; | ||
DROP TABLE IF EXISTS embedding_fulltext_search_config; | ||
DROP TABLE IF EXISTS embedding_fulltext_search_content; | ||
DROP TABLE IF EXISTS embedding_fulltext_search_data; | ||
DROP TABLE IF EXISTS embedding_fulltext_search_docsize; | ||
DROP TABLE IF EXISTS embedding_fulltext_search_idx; | ||
CREATE TABLE embedding_fulltext (id INTEGER PRIMARY KEY); | ||
DELETE FROM migrations WHERE dir='metadb' AND version='3' AND filename='00003-full-text-tokenize.sqlite.sql'; | ||
COMMIT TRANSACTION; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import os.path | ||
import shutil | ||
import sqlite3 | ||
import tempfile | ||
import uuid | ||
|
||
import chromadb | ||
import pytest | ||
|
||
from chroma_ops.rebuild_fts import rebuild_fts | ||
|
||
|
||
def test_basic_clean() -> None: | ||
records_to_add = 1 | ||
with tempfile.TemporaryDirectory() as temp_dir: | ||
client = chromadb.PersistentClient(path=temp_dir) | ||
col = client.create_collection("test") | ||
ids_documents = [ | ||
(f"{uuid.uuid4()}", f"document {i}", [0.1] * 1536) | ||
for i in range(records_to_add) | ||
] | ||
ids, documents, embeddings = zip(*ids_documents) | ||
col.add(ids=list(ids), documents=list(documents), embeddings=list(embeddings)) | ||
sql_file = os.path.join(temp_dir, "chroma.sqlite3") | ||
conn = sqlite3.connect(sql_file) | ||
cursor = conn.cursor() | ||
cursor.execute("DROP TABLE IF EXISTS embedding_fulltext_search;") | ||
conn.commit() | ||
cursor.close() | ||
with pytest.raises(Exception) as e: | ||
col.get(where_document={"$contains": "document 0"}) | ||
|
||
assert "no such table: embedding_fulltext_search" in str(e) | ||
rebuild_fts(temp_dir) | ||
fixed_temp_dir = os.path.join(temp_dir, "fixed") | ||
shutil.copytree(temp_dir, fixed_temp_dir) | ||
client = chromadb.PersistentClient(path=fixed_temp_dir) | ||
col = client.get_collection("test") | ||
col.get(where_document={"$contains": "document 0"}) |