mem0ai-azure-mysql 0.1.115__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. mem0/__init__.py +6 -0
  2. mem0/client/__init__.py +0 -0
  3. mem0/client/main.py +1535 -0
  4. mem0/client/project.py +860 -0
  5. mem0/client/utils.py +29 -0
  6. mem0/configs/__init__.py +0 -0
  7. mem0/configs/base.py +90 -0
  8. mem0/configs/dbs/__init__.py +4 -0
  9. mem0/configs/dbs/base.py +41 -0
  10. mem0/configs/dbs/mysql.py +25 -0
  11. mem0/configs/embeddings/__init__.py +0 -0
  12. mem0/configs/embeddings/base.py +108 -0
  13. mem0/configs/enums.py +7 -0
  14. mem0/configs/llms/__init__.py +0 -0
  15. mem0/configs/llms/base.py +152 -0
  16. mem0/configs/prompts.py +333 -0
  17. mem0/configs/vector_stores/__init__.py +0 -0
  18. mem0/configs/vector_stores/azure_ai_search.py +59 -0
  19. mem0/configs/vector_stores/baidu.py +29 -0
  20. mem0/configs/vector_stores/chroma.py +40 -0
  21. mem0/configs/vector_stores/elasticsearch.py +47 -0
  22. mem0/configs/vector_stores/faiss.py +39 -0
  23. mem0/configs/vector_stores/langchain.py +32 -0
  24. mem0/configs/vector_stores/milvus.py +43 -0
  25. mem0/configs/vector_stores/mongodb.py +25 -0
  26. mem0/configs/vector_stores/opensearch.py +41 -0
  27. mem0/configs/vector_stores/pgvector.py +37 -0
  28. mem0/configs/vector_stores/pinecone.py +56 -0
  29. mem0/configs/vector_stores/qdrant.py +49 -0
  30. mem0/configs/vector_stores/redis.py +26 -0
  31. mem0/configs/vector_stores/supabase.py +44 -0
  32. mem0/configs/vector_stores/upstash_vector.py +36 -0
  33. mem0/configs/vector_stores/vertex_ai_vector_search.py +27 -0
  34. mem0/configs/vector_stores/weaviate.py +43 -0
  35. mem0/dbs/__init__.py +4 -0
  36. mem0/dbs/base.py +68 -0
  37. mem0/dbs/configs.py +21 -0
  38. mem0/dbs/mysql.py +321 -0
  39. mem0/embeddings/__init__.py +0 -0
  40. mem0/embeddings/aws_bedrock.py +100 -0
  41. mem0/embeddings/azure_openai.py +43 -0
  42. mem0/embeddings/base.py +31 -0
  43. mem0/embeddings/configs.py +30 -0
  44. mem0/embeddings/gemini.py +39 -0
  45. mem0/embeddings/huggingface.py +41 -0
  46. mem0/embeddings/langchain.py +35 -0
  47. mem0/embeddings/lmstudio.py +29 -0
  48. mem0/embeddings/mock.py +11 -0
  49. mem0/embeddings/ollama.py +53 -0
  50. mem0/embeddings/openai.py +49 -0
  51. mem0/embeddings/together.py +31 -0
  52. mem0/embeddings/vertexai.py +54 -0
  53. mem0/graphs/__init__.py +0 -0
  54. mem0/graphs/configs.py +96 -0
  55. mem0/graphs/neptune/__init__.py +0 -0
  56. mem0/graphs/neptune/base.py +410 -0
  57. mem0/graphs/neptune/main.py +372 -0
  58. mem0/graphs/tools.py +371 -0
  59. mem0/graphs/utils.py +97 -0
  60. mem0/llms/__init__.py +0 -0
  61. mem0/llms/anthropic.py +64 -0
  62. mem0/llms/aws_bedrock.py +270 -0
  63. mem0/llms/azure_openai.py +114 -0
  64. mem0/llms/azure_openai_structured.py +76 -0
  65. mem0/llms/base.py +32 -0
  66. mem0/llms/configs.py +34 -0
  67. mem0/llms/deepseek.py +85 -0
  68. mem0/llms/gemini.py +201 -0
  69. mem0/llms/groq.py +88 -0
  70. mem0/llms/langchain.py +65 -0
  71. mem0/llms/litellm.py +87 -0
  72. mem0/llms/lmstudio.py +53 -0
  73. mem0/llms/ollama.py +94 -0
  74. mem0/llms/openai.py +124 -0
  75. mem0/llms/openai_structured.py +52 -0
  76. mem0/llms/sarvam.py +89 -0
  77. mem0/llms/together.py +88 -0
  78. mem0/llms/vllm.py +89 -0
  79. mem0/llms/xai.py +52 -0
  80. mem0/memory/__init__.py +0 -0
  81. mem0/memory/base.py +63 -0
  82. mem0/memory/graph_memory.py +632 -0
  83. mem0/memory/main.py +1843 -0
  84. mem0/memory/memgraph_memory.py +630 -0
  85. mem0/memory/setup.py +56 -0
  86. mem0/memory/storage.py +218 -0
  87. mem0/memory/telemetry.py +90 -0
  88. mem0/memory/utils.py +133 -0
  89. mem0/proxy/__init__.py +0 -0
  90. mem0/proxy/main.py +194 -0
  91. mem0/utils/factory.py +132 -0
  92. mem0/vector_stores/__init__.py +0 -0
  93. mem0/vector_stores/azure_ai_search.py +383 -0
  94. mem0/vector_stores/baidu.py +368 -0
  95. mem0/vector_stores/base.py +58 -0
  96. mem0/vector_stores/chroma.py +229 -0
  97. mem0/vector_stores/configs.py +60 -0
  98. mem0/vector_stores/elasticsearch.py +235 -0
  99. mem0/vector_stores/faiss.py +473 -0
  100. mem0/vector_stores/langchain.py +179 -0
  101. mem0/vector_stores/milvus.py +245 -0
  102. mem0/vector_stores/mongodb.py +293 -0
  103. mem0/vector_stores/opensearch.py +281 -0
  104. mem0/vector_stores/pgvector.py +294 -0
  105. mem0/vector_stores/pinecone.py +373 -0
  106. mem0/vector_stores/qdrant.py +240 -0
  107. mem0/vector_stores/redis.py +295 -0
  108. mem0/vector_stores/supabase.py +237 -0
  109. mem0/vector_stores/upstash_vector.py +293 -0
  110. mem0/vector_stores/vertex_ai_vector_search.py +629 -0
  111. mem0/vector_stores/weaviate.py +316 -0
  112. mem0ai_azure_mysql-0.1.115.data/data/README.md +169 -0
  113. mem0ai_azure_mysql-0.1.115.dist-info/METADATA +224 -0
  114. mem0ai_azure_mysql-0.1.115.dist-info/RECORD +116 -0
  115. mem0ai_azure_mysql-0.1.115.dist-info/WHEEL +4 -0
  116. mem0ai_azure_mysql-0.1.115.dist-info/licenses/LICENSE +201 -0
@@ -0,0 +1,245 @@
1
+ import logging
2
+ from typing import Dict, Optional
3
+
4
+ from pydantic import BaseModel
5
+
6
+ from mem0.configs.vector_stores.milvus import MetricType
7
+ from mem0.vector_stores.base import VectorStoreBase
8
+
9
+ try:
10
+ import pymilvus # noqa: F401
11
+ except ImportError:
12
+ raise ImportError("The 'pymilvus' library is required. Please install it using 'pip install pymilvus'.")
13
+
14
+ from pymilvus import CollectionSchema, DataType, FieldSchema, MilvusClient
15
+
16
+ logger = logging.getLogger(__name__)
17
+
18
+
19
+ class OutputData(BaseModel):
20
+ id: Optional[str] # memory id
21
+ score: Optional[float] # distance
22
+ payload: Optional[Dict] # metadata
23
+
24
+
25
+ class MilvusDB(VectorStoreBase):
26
+ def __init__(
27
+ self,
28
+ url: str,
29
+ token: str,
30
+ collection_name: str,
31
+ embedding_model_dims: int,
32
+ metric_type: MetricType,
33
+ ) -> None:
34
+ """Initialize the MilvusDB database.
35
+
36
+ Args:
37
+ url (str): Full URL for Milvus/Zilliz server.
38
+ token (str): Token/api_key for Zilliz server / for local setup defaults to None.
39
+ collection_name (str): Name of the collection (defaults to mem0).
40
+ embedding_model_dims (int): Dimensions of the embedding model (defaults to 1536).
41
+ metric_type (MetricType): Metric type for similarity search (defaults to L2).
42
+ """
43
+ self.collection_name = collection_name
44
+ self.embedding_model_dims = embedding_model_dims
45
+ self.metric_type = metric_type
46
+ self.client = MilvusClient(uri=url, token=token)
47
+ self.create_col(
48
+ collection_name=self.collection_name,
49
+ vector_size=self.embedding_model_dims,
50
+ metric_type=self.metric_type,
51
+ )
52
+
53
+ def create_col(
54
+ self,
55
+ collection_name: str,
56
+ vector_size: str,
57
+ metric_type: MetricType = MetricType.COSINE,
58
+ ) -> None:
59
+ """Create a new collection with index_type AUTOINDEX.
60
+
61
+ Args:
62
+ collection_name (str): Name of the collection (defaults to mem0).
63
+ vector_size (str): Dimensions of the embedding model (defaults to 1536).
64
+ metric_type (MetricType, optional): etric type for similarity search. Defaults to MetricType.COSINE.
65
+ """
66
+
67
+ if self.client.has_collection(collection_name):
68
+ logger.info(f"Collection {collection_name} already exists. Skipping creation.")
69
+ else:
70
+ fields = [
71
+ FieldSchema(name="id", dtype=DataType.VARCHAR, is_primary=True, max_length=512),
72
+ FieldSchema(name="vectors", dtype=DataType.FLOAT_VECTOR, dim=vector_size),
73
+ FieldSchema(name="metadata", dtype=DataType.JSON),
74
+ ]
75
+
76
+ schema = CollectionSchema(fields, enable_dynamic_field=True)
77
+
78
+ index = self.client.prepare_index_params(
79
+ field_name="vectors", metric_type=metric_type, index_type="AUTOINDEX", index_name="vector_index"
80
+ )
81
+ self.client.create_collection(collection_name=collection_name, schema=schema, index_params=index)
82
+
83
+ def insert(self, ids, vectors, payloads, **kwargs: Optional[dict[str, any]]):
84
+ """Insert vectors into a collection.
85
+
86
+ Args:
87
+ vectors (List[List[float]]): List of vectors to insert.
88
+ payloads (List[Dict], optional): List of payloads corresponding to vectors.
89
+ ids (List[str], optional): List of IDs corresponding to vectors.
90
+ """
91
+ for idx, embedding, metadata in zip(ids, vectors, payloads):
92
+ data = {"id": idx, "vectors": embedding, "metadata": metadata}
93
+ self.client.insert(collection_name=self.collection_name, data=data, **kwargs)
94
+
95
+ def _create_filter(self, filters: dict):
96
+ """Prepare filters for efficient query.
97
+
98
+ Args:
99
+ filters (dict): filters [user_id, agent_id, run_id]
100
+
101
+ Returns:
102
+ str: formated filter.
103
+ """
104
+ operands = []
105
+ for key, value in filters.items():
106
+ if isinstance(value, str):
107
+ operands.append(f'(metadata["{key}"] == "{value}")')
108
+ else:
109
+ operands.append(f'(metadata["{key}"] == {value})')
110
+
111
+ return " and ".join(operands)
112
+
113
+ def _parse_output(self, data: list):
114
+ """
115
+ Parse the output data.
116
+
117
+ Args:
118
+ data (Dict): Output data.
119
+
120
+ Returns:
121
+ List[OutputData]: Parsed output data.
122
+ """
123
+ memory = []
124
+
125
+ for value in data:
126
+ uid, score, metadata = (
127
+ value.get("id"),
128
+ value.get("distance"),
129
+ value.get("entity", {}).get("metadata"),
130
+ )
131
+
132
+ memory_obj = OutputData(id=uid, score=score, payload=metadata)
133
+ memory.append(memory_obj)
134
+
135
+ return memory
136
+
137
+ def search(self, query: str, vectors: list, limit: int = 5, filters: dict = None) -> list:
138
+ """
139
+ Search for similar vectors.
140
+
141
+ Args:
142
+ query (str): Query.
143
+ vectors (List[float]): Query vector.
144
+ limit (int, optional): Number of results to return. Defaults to 5.
145
+ filters (Dict, optional): Filters to apply to the search. Defaults to None.
146
+
147
+ Returns:
148
+ list: Search results.
149
+ """
150
+ query_filter = self._create_filter(filters) if filters else None
151
+ hits = self.client.search(
152
+ collection_name=self.collection_name,
153
+ data=[vectors],
154
+ limit=limit,
155
+ filter=query_filter,
156
+ output_fields=["*"],
157
+ )
158
+ result = self._parse_output(data=hits[0])
159
+ return result
160
+
161
+ def delete(self, vector_id):
162
+ """
163
+ Delete a vector by ID.
164
+
165
+ Args:
166
+ vector_id (str): ID of the vector to delete.
167
+ """
168
+ self.client.delete(collection_name=self.collection_name, ids=vector_id)
169
+
170
+ def update(self, vector_id=None, vector=None, payload=None):
171
+ """
172
+ Update a vector and its payload.
173
+
174
+ Args:
175
+ vector_id (str): ID of the vector to update.
176
+ vector (List[float], optional): Updated vector.
177
+ payload (Dict, optional): Updated payload.
178
+ """
179
+ schema = {"id": vector_id, "vectors": vector, "metadata": payload}
180
+ self.client.upsert(collection_name=self.collection_name, data=schema)
181
+
182
+ def get(self, vector_id):
183
+ """
184
+ Retrieve a vector by ID.
185
+
186
+ Args:
187
+ vector_id (str): ID of the vector to retrieve.
188
+
189
+ Returns:
190
+ OutputData: Retrieved vector.
191
+ """
192
+ result = self.client.get(collection_name=self.collection_name, ids=vector_id)
193
+ output = OutputData(
194
+ id=result[0].get("id", None),
195
+ score=None,
196
+ payload=result[0].get("metadata", None),
197
+ )
198
+ return output
199
+
200
+ def list_cols(self):
201
+ """
202
+ List all collections.
203
+
204
+ Returns:
205
+ List[str]: List of collection names.
206
+ """
207
+ return self.client.list_collections()
208
+
209
+ def delete_col(self):
210
+ """Delete a collection."""
211
+ return self.client.drop_collection(collection_name=self.collection_name)
212
+
213
+ def col_info(self):
214
+ """
215
+ Get information about a collection.
216
+
217
+ Returns:
218
+ Dict[str, Any]: Collection information.
219
+ """
220
+ return self.client.get_collection_stats(collection_name=self.collection_name)
221
+
222
+ def list(self, filters: dict = None, limit: int = 100) -> list:
223
+ """
224
+ List all vectors in a collection.
225
+
226
+ Args:
227
+ filters (Dict, optional): Filters to apply to the list.
228
+ limit (int, optional): Number of vectors to return. Defaults to 100.
229
+
230
+ Returns:
231
+ List[OutputData]: List of vectors.
232
+ """
233
+ query_filter = self._create_filter(filters) if filters else None
234
+ result = self.client.query(collection_name=self.collection_name, filter=query_filter, limit=limit)
235
+ memories = []
236
+ for data in result:
237
+ obj = OutputData(id=data.get("id"), score=None, payload=data.get("metadata"))
238
+ memories.append(obj)
239
+ return [memories]
240
+
241
+ def reset(self):
242
+ """Reset the index by deleting and recreating it."""
243
+ logger.warning(f"Resetting index {self.collection_name}...")
244
+ self.delete_col()
245
+ self.create_col(self.collection_name, self.embedding_model_dims, self.metric_type)
@@ -0,0 +1,293 @@
1
+ import logging
2
+ from typing import Any, Dict, List, Optional
3
+
4
+ from pydantic import BaseModel
5
+
6
+ try:
7
+ from pymongo import MongoClient
8
+ from pymongo.errors import PyMongoError
9
+ from pymongo.operations import SearchIndexModel
10
+ except ImportError:
11
+ raise ImportError("The 'pymongo' library is required. Please install it using 'pip install pymongo'.")
12
+
13
+ from mem0.vector_stores.base import VectorStoreBase
14
+
15
+ logger = logging.getLogger(__name__)
16
+ logging.basicConfig(level=logging.INFO)
17
+
18
+
19
+ class OutputData(BaseModel):
20
+ id: Optional[str]
21
+ score: Optional[float]
22
+ payload: Optional[dict]
23
+
24
+
25
+ class MongoDB(VectorStoreBase):
26
+ VECTOR_TYPE = "knnVector"
27
+ SIMILARITY_METRIC = "cosine"
28
+
29
+ def __init__(self, db_name: str, collection_name: str, embedding_model_dims: int, mongo_uri: str):
30
+ """
31
+ Initialize the MongoDB vector store with vector search capabilities.
32
+
33
+ Args:
34
+ db_name (str): Database name
35
+ collection_name (str): Collection name
36
+ embedding_model_dims (int): Dimension of the embedding vector
37
+ mongo_uri (str): MongoDB connection URI
38
+ """
39
+ self.collection_name = collection_name
40
+ self.embedding_model_dims = embedding_model_dims
41
+ self.db_name = db_name
42
+
43
+ self.client = MongoClient(mongo_uri)
44
+ self.db = self.client[db_name]
45
+ self.collection = self.create_col()
46
+
47
+ def create_col(self):
48
+ """Create new collection with vector search index."""
49
+ try:
50
+ database = self.client[self.db_name]
51
+ collection_names = database.list_collection_names()
52
+ if self.collection_name not in collection_names:
53
+ logger.info(f"Collection '{self.collection_name}' does not exist. Creating it now.")
54
+ collection = database[self.collection_name]
55
+ # Insert and remove a placeholder document to create the collection
56
+ collection.insert_one({"_id": 0, "placeholder": True})
57
+ collection.delete_one({"_id": 0})
58
+ logger.info(f"Collection '{self.collection_name}' created successfully.")
59
+ else:
60
+ collection = database[self.collection_name]
61
+
62
+ self.index_name = f"{self.collection_name}_vector_index"
63
+ found_indexes = list(collection.list_search_indexes(name=self.index_name))
64
+ if found_indexes:
65
+ logger.info(f"Search index '{self.index_name}' already exists in collection '{self.collection_name}'.")
66
+ else:
67
+ search_index_model = SearchIndexModel(
68
+ name=self.index_name,
69
+ definition={
70
+ "mappings": {
71
+ "dynamic": False,
72
+ "fields": {
73
+ "embedding": {
74
+ "type": self.VECTOR_TYPE,
75
+ "dimensions": self.embedding_model_dims,
76
+ "similarity": self.SIMILARITY_METRIC,
77
+ }
78
+ },
79
+ }
80
+ },
81
+ )
82
+ collection.create_search_index(search_index_model)
83
+ logger.info(
84
+ f"Search index '{self.index_name}' created successfully for collection '{self.collection_name}'."
85
+ )
86
+ return collection
87
+ except PyMongoError as e:
88
+ logger.error(f"Error creating collection and search index: {e}")
89
+ return None
90
+
91
+ def insert(
92
+ self, vectors: List[List[float]], payloads: Optional[List[Dict]] = None, ids: Optional[List[str]] = None
93
+ ) -> None:
94
+ """
95
+ Insert vectors into the collection.
96
+
97
+ Args:
98
+ vectors (List[List[float]]): List of vectors to insert.
99
+ payloads (List[Dict], optional): List of payloads corresponding to vectors.
100
+ ids (List[str], optional): List of IDs corresponding to vectors.
101
+ """
102
+ logger.info(f"Inserting {len(vectors)} vectors into collection '{self.collection_name}'.")
103
+
104
+ data = []
105
+ for vector, payload, _id in zip(vectors, payloads or [{}] * len(vectors), ids or [None] * len(vectors)):
106
+ document = {"_id": _id, "embedding": vector, "payload": payload}
107
+ data.append(document)
108
+ try:
109
+ self.collection.insert_many(data)
110
+ logger.info(f"Inserted {len(data)} documents into '{self.collection_name}'.")
111
+ except PyMongoError as e:
112
+ logger.error(f"Error inserting data: {e}")
113
+
114
+ def search(
115
+ self, query: str, query_vector: List[float], limit=5, filters: Optional[Dict] = None
116
+ ) -> List[OutputData]:
117
+ """
118
+ Search for similar vectors using the vector search index.
119
+
120
+ Args:
121
+ query (str): Query string
122
+ query_vector (List[float]): Query vector.
123
+ limit (int, optional): Number of results to return. Defaults to 5.
124
+ filters (Dict, optional): Filters to apply to the search.
125
+
126
+ Returns:
127
+ List[OutputData]: Search results.
128
+ """
129
+
130
+ found_indexes = list(self.collection.list_search_indexes(name=self.index_name))
131
+ if not found_indexes:
132
+ logger.error(f"Index '{self.index_name}' does not exist.")
133
+ return []
134
+
135
+ results = []
136
+ try:
137
+ collection = self.client[self.db_name][self.collection_name]
138
+ pipeline = [
139
+ {
140
+ "$vectorSearch": {
141
+ "index": self.index_name,
142
+ "limit": limit,
143
+ "numCandidates": limit,
144
+ "queryVector": query_vector,
145
+ "path": "embedding",
146
+ }
147
+ },
148
+ {"$set": {"score": {"$meta": "vectorSearchScore"}}},
149
+ {"$project": {"embedding": 0}},
150
+ ]
151
+ results = list(collection.aggregate(pipeline))
152
+ logger.info(f"Vector search completed. Found {len(results)} documents.")
153
+ except Exception as e:
154
+ logger.error(f"Error during vector search for query {query}: {e}")
155
+ return []
156
+
157
+ output = [OutputData(id=str(doc["_id"]), score=doc.get("score"), payload=doc.get("payload")) for doc in results]
158
+ return output
159
+
160
+ def delete(self, vector_id: str) -> None:
161
+ """
162
+ Delete a vector by ID.
163
+
164
+ Args:
165
+ vector_id (str): ID of the vector to delete.
166
+ """
167
+ try:
168
+ result = self.collection.delete_one({"_id": vector_id})
169
+ if result.deleted_count > 0:
170
+ logger.info(f"Deleted document with ID '{vector_id}'.")
171
+ else:
172
+ logger.warning(f"No document found with ID '{vector_id}' to delete.")
173
+ except PyMongoError as e:
174
+ logger.error(f"Error deleting document: {e}")
175
+
176
+ def update(self, vector_id: str, vector: Optional[List[float]] = None, payload: Optional[Dict] = None) -> None:
177
+ """
178
+ Update a vector and its payload.
179
+
180
+ Args:
181
+ vector_id (str): ID of the vector to update.
182
+ vector (List[float], optional): Updated vector.
183
+ payload (Dict, optional): Updated payload.
184
+ """
185
+ update_fields = {}
186
+ if vector is not None:
187
+ update_fields["embedding"] = vector
188
+ if payload is not None:
189
+ update_fields["payload"] = payload
190
+
191
+ if update_fields:
192
+ try:
193
+ result = self.collection.update_one({"_id": vector_id}, {"$set": update_fields})
194
+ if result.matched_count > 0:
195
+ logger.info(f"Updated document with ID '{vector_id}'.")
196
+ else:
197
+ logger.warning(f"No document found with ID '{vector_id}' to update.")
198
+ except PyMongoError as e:
199
+ logger.error(f"Error updating document: {e}")
200
+
201
+ def get(self, vector_id: str) -> Optional[OutputData]:
202
+ """
203
+ Retrieve a vector by ID.
204
+
205
+ Args:
206
+ vector_id (str): ID of the vector to retrieve.
207
+
208
+ Returns:
209
+ Optional[OutputData]: Retrieved vector or None if not found.
210
+ """
211
+ try:
212
+ doc = self.collection.find_one({"_id": vector_id})
213
+ if doc:
214
+ logger.info(f"Retrieved document with ID '{vector_id}'.")
215
+ return OutputData(id=str(doc["_id"]), score=None, payload=doc.get("payload"))
216
+ else:
217
+ logger.warning(f"Document with ID '{vector_id}' not found.")
218
+ return None
219
+ except PyMongoError as e:
220
+ logger.error(f"Error retrieving document: {e}")
221
+ return None
222
+
223
+ def list_cols(self) -> List[str]:
224
+ """
225
+ List all collections in the database.
226
+
227
+ Returns:
228
+ List[str]: List of collection names.
229
+ """
230
+ try:
231
+ collections = self.db.list_collection_names()
232
+ logger.info(f"Listing collections in database '{self.db_name}': {collections}")
233
+ return collections
234
+ except PyMongoError as e:
235
+ logger.error(f"Error listing collections: {e}")
236
+ return []
237
+
238
+ def delete_col(self) -> None:
239
+ """Delete the collection."""
240
+ try:
241
+ self.collection.drop()
242
+ logger.info(f"Deleted collection '{self.collection_name}'.")
243
+ except PyMongoError as e:
244
+ logger.error(f"Error deleting collection: {e}")
245
+
246
+ def col_info(self) -> Dict[str, Any]:
247
+ """
248
+ Get information about the collection.
249
+
250
+ Returns:
251
+ Dict[str, Any]: Collection information.
252
+ """
253
+ try:
254
+ stats = self.db.command("collstats", self.collection_name)
255
+ info = {"name": self.collection_name, "count": stats.get("count"), "size": stats.get("size")}
256
+ logger.info(f"Collection info: {info}")
257
+ return info
258
+ except PyMongoError as e:
259
+ logger.error(f"Error getting collection info: {e}")
260
+ return {}
261
+
262
+ def list(self, filters: Optional[Dict] = None, limit: int = 100) -> List[OutputData]:
263
+ """
264
+ List vectors in the collection.
265
+
266
+ Args:
267
+ filters (Dict, optional): Filters to apply to the list.
268
+ limit (int, optional): Number of vectors to return.
269
+
270
+ Returns:
271
+ List[OutputData]: List of vectors.
272
+ """
273
+ try:
274
+ query = filters or {}
275
+ cursor = self.collection.find(query).limit(limit)
276
+ results = [OutputData(id=str(doc["_id"]), score=None, payload=doc.get("payload")) for doc in cursor]
277
+ logger.info(f"Retrieved {len(results)} documents from collection '{self.collection_name}'.")
278
+ return results
279
+ except PyMongoError as e:
280
+ logger.error(f"Error listing documents: {e}")
281
+ return []
282
+
283
+ def reset(self):
284
+ """Reset the index by deleting and recreating it."""
285
+ logger.warning(f"Resetting index {self.collection_name}...")
286
+ self.delete_col()
287
+ self.collection = self.create_col(self.collection_name)
288
+
289
+ def __del__(self) -> None:
290
+ """Close the database connection when the object is deleted."""
291
+ if hasattr(self, "client"):
292
+ self.client.close()
293
+ logger.info("MongoClient connection closed.")