Skip to content

Commit c2d6948

Browse files
authored
feat: wipe per storage type (#1772)
1 parent 7d2de5c commit c2d6948

File tree

1 file changed

+76
-2
lines changed

1 file changed

+76
-2
lines changed

scripts/utils.py

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,35 @@
22
import os
33
import shutil
44

5+
from private_gpt.paths import local_data_path
6+
from private_gpt.settings.settings import settings
57

6-
def wipe():
7-
path = "local_data"
8+
9+
def wipe() -> None:
10+
WIPE_MAP = {
11+
"simple": wipe_simple, # node store
12+
"chroma": wipe_chroma, # vector store
13+
"postgres": wipe_postgres, # node, index and vector store
14+
}
15+
for dbtype in ("nodestore", "vectorstore"):
16+
database = getattr(settings(), dbtype).database
17+
func = WIPE_MAP.get(database)
18+
if func:
19+
func(dbtype)
20+
else:
21+
print(f"Unable to wipe database '{database}' for '{dbtype}'")
22+
23+
24+
def wipe_file(file: str) -> None:
25+
if os.path.isfile(file):
26+
os.remove(file)
27+
print(f" - Deleted {file}")
28+
29+
30+
def wipe_tree(path: str) -> None:
31+
if not os.path.exists(path):
32+
print(f"Warning: Path not found {path}")
33+
return
834
print(f"Wiping {path}...")
935
all_files = os.listdir(path)
1036

@@ -24,6 +50,54 @@ def wipe():
2450
continue
2551

2652

53+
def wipe_simple(dbtype: str) -> None:
54+
assert dbtype == "nodestore"
55+
from llama_index.core.storage.docstore.types import (
56+
DEFAULT_PERSIST_FNAME as DOCSTORE,
57+
)
58+
from llama_index.core.storage.index_store.types import (
59+
DEFAULT_PERSIST_FNAME as INDEXSTORE,
60+
)
61+
62+
for store in (DOCSTORE, INDEXSTORE):
63+
wipe_file(str((local_data_path / store).absolute()))
64+
65+
66+
def wipe_postgres(dbtype: str) -> None:
67+
try:
68+
import psycopg2
69+
except ImportError as e:
70+
raise ImportError("Postgres dependencies not found") from e
71+
72+
cur = conn = None
73+
try:
74+
tables = {
75+
"nodestore": ["data_docstore", "data_indexstore"],
76+
"vectorstore": ["data_embeddings"],
77+
}[dbtype]
78+
connection = settings().postgres.model_dump(exclude_none=True)
79+
schema = connection.pop("schema_name")
80+
conn = psycopg2.connect(**connection)
81+
cur = conn.cursor()
82+
for table in tables:
83+
sql = f"DROP TABLE IF EXISTS {schema}.{table}"
84+
cur.execute(sql)
85+
print(f"Table {schema}.{table} dropped.")
86+
conn.commit()
87+
except psycopg2.Error as e:
88+
print("Error:", e)
89+
finally:
90+
if cur:
91+
cur.close()
92+
if conn:
93+
conn.close()
94+
95+
96+
def wipe_chroma(dbtype: str):
97+
assert dbtype == "vectorstore"
98+
wipe_tree(str((local_data_path / "chroma_db").absolute()))
99+
100+
27101
if __name__ == "__main__":
28102
commands = {
29103
"wipe": wipe,

0 commit comments

Comments
 (0)