-
Notifications
You must be signed in to change notification settings - Fork 700
/
chromadb_vector.py
257 lines (214 loc) 路 8.59 KB
/
chromadb_vector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import json
from typing import List
import chromadb
import pandas as pd
from chromadb.config import Settings
from chromadb.utils import embedding_functions
from ..base import VannaBase
from ..utils import deterministic_uuid
default_ef = embedding_functions.DefaultEmbeddingFunction()
class ChromaDB_VectorStore(VannaBase):
def __init__(self, config=None):
VannaBase.__init__(self, config=config)
if config is None:
config = {}
path = config.get("path", ".")
self.embedding_function = config.get("embedding_function", default_ef)
curr_client = config.get("client", "persistent")
collection_metadata = config.get("collection_metadata", None)
self.n_results_sql = config.get("n_results_sql", config.get("n_results", 10))
self.n_results_documentation = config.get("n_results_documentation", config.get("n_results", 10))
self.n_results_ddl = config.get("n_results_ddl", config.get("n_results", 10))
if curr_client == "persistent":
self.chroma_client = chromadb.PersistentClient(
path=path, settings=Settings(anonymized_telemetry=False)
)
elif curr_client == "in-memory":
self.chroma_client = chromadb.EphemeralClient(
settings=Settings(anonymized_telemetry=False)
)
elif isinstance(curr_client, chromadb.api.client.Client):
# allow providing client directly
self.chroma_client = curr_client
else:
raise ValueError(f"Unsupported client was set in config: {curr_client}")
self.documentation_collection = self.chroma_client.get_or_create_collection(
name="documentation",
embedding_function=self.embedding_function,
metadata=collection_metadata,
)
self.ddl_collection = self.chroma_client.get_or_create_collection(
name="ddl",
embedding_function=self.embedding_function,
metadata=collection_metadata,
)
self.sql_collection = self.chroma_client.get_or_create_collection(
name="sql",
embedding_function=self.embedding_function,
metadata=collection_metadata,
)
def generate_embedding(self, data: str, **kwargs) -> List[float]:
embedding = self.embedding_function([data])
if len(embedding) == 1:
return embedding[0]
return embedding
def add_question_sql(self, question: str, sql: str, **kwargs) -> str:
question_sql_json = json.dumps(
{
"question": question,
"sql": sql,
},
ensure_ascii=False,
)
id = deterministic_uuid(question_sql_json) + "-sql"
self.sql_collection.add(
documents=question_sql_json,
embeddings=self.generate_embedding(question_sql_json),
ids=id,
)
return id
def add_ddl(self, ddl: str, **kwargs) -> str:
id = deterministic_uuid(ddl) + "-ddl"
self.ddl_collection.add(
documents=ddl,
embeddings=self.generate_embedding(ddl),
ids=id,
)
return id
def add_documentation(self, documentation: str, **kwargs) -> str:
id = deterministic_uuid(documentation) + "-doc"
self.documentation_collection.add(
documents=documentation,
embeddings=self.generate_embedding(documentation),
ids=id,
)
return id
def get_training_data(self, **kwargs) -> pd.DataFrame:
sql_data = self.sql_collection.get()
df = pd.DataFrame()
if sql_data is not None:
# Extract the documents and ids
documents = [json.loads(doc) for doc in sql_data["documents"]]
ids = sql_data["ids"]
# Create a DataFrame
df_sql = pd.DataFrame(
{
"id": ids,
"question": [doc["question"] for doc in documents],
"content": [doc["sql"] for doc in documents],
}
)
df_sql["training_data_type"] = "sql"
df = pd.concat([df, df_sql])
ddl_data = self.ddl_collection.get()
if ddl_data is not None:
# Extract the documents and ids
documents = [doc for doc in ddl_data["documents"]]
ids = ddl_data["ids"]
# Create a DataFrame
df_ddl = pd.DataFrame(
{
"id": ids,
"question": [None for doc in documents],
"content": [doc for doc in documents],
}
)
df_ddl["training_data_type"] = "ddl"
df = pd.concat([df, df_ddl])
doc_data = self.documentation_collection.get()
if doc_data is not None:
# Extract the documents and ids
documents = [doc for doc in doc_data["documents"]]
ids = doc_data["ids"]
# Create a DataFrame
df_doc = pd.DataFrame(
{
"id": ids,
"question": [None for doc in documents],
"content": [doc for doc in documents],
}
)
df_doc["training_data_type"] = "documentation"
df = pd.concat([df, df_doc])
return df
def remove_training_data(self, id: str, **kwargs) -> bool:
if id.endswith("-sql"):
self.sql_collection.delete(ids=id)
return True
elif id.endswith("-ddl"):
self.ddl_collection.delete(ids=id)
return True
elif id.endswith("-doc"):
self.documentation_collection.delete(ids=id)
return True
else:
return False
def remove_collection(self, collection_name: str) -> bool:
"""
This function can reset the collection to empty state.
Args:
collection_name (str): sql or ddl or documentation
Returns:
bool: True if collection is deleted, False otherwise
"""
if collection_name == "sql":
self.chroma_client.delete_collection(name="sql")
self.sql_collection = self.chroma_client.get_or_create_collection(
name="sql", embedding_function=self.embedding_function
)
return True
elif collection_name == "ddl":
self.chroma_client.delete_collection(name="ddl")
self.ddl_collection = self.chroma_client.get_or_create_collection(
name="ddl", embedding_function=self.embedding_function
)
return True
elif collection_name == "documentation":
self.chroma_client.delete_collection(name="documentation")
self.documentation_collection = self.chroma_client.get_or_create_collection(
name="documentation", embedding_function=self.embedding_function
)
return True
else:
return False
@staticmethod
def _extract_documents(query_results) -> list:
"""
Static method to extract the documents from the results of a query.
Args:
query_results (pd.DataFrame): The dataframe to use.
Returns:
List[str] or None: The extracted documents, or an empty list or
single document if an error occurred.
"""
if query_results is None:
return []
if "documents" in query_results:
documents = query_results["documents"]
if len(documents) == 1 and isinstance(documents[0], list):
try:
documents = [json.loads(doc) for doc in documents[0]]
except Exception as e:
return documents[0]
return documents
def get_similar_question_sql(self, question: str, **kwargs) -> list:
return ChromaDB_VectorStore._extract_documents(
self.sql_collection.query(
query_texts=[question],
n_results=self.n_results_sql,
)
)
def get_related_ddl(self, question: str, **kwargs) -> list:
return ChromaDB_VectorStore._extract_documents(
self.ddl_collection.query(
query_texts=[question],
n_results=self.n_results_ddl,
)
)
def get_related_documentation(self, question: str, **kwargs) -> list:
return ChromaDB_VectorStore._extract_documents(
self.documentation_collection.query(
query_texts=[question],
n_results=self.n_results_documentation,
)
)