llama-index-vector-stores-deeplake 0.0.1__tar.gz

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.
@@ -0,0 +1,18 @@
1
+ Metadata-Version: 2.1
2
+ Name: llama-index-vector-stores-deeplake
3
+ Version: 0.0.1
4
+ Summary: llama-index vector_stores deeplake integration
5
+ License: MIT
6
+ Author: Your Name
7
+ Author-email: you@example.com
8
+ Requires-Python: >=3.8.1,<3.12
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 3.9
12
+ Classifier: Programming Language :: Python :: 3.10
13
+ Classifier: Programming Language :: Python :: 3.11
14
+ Requires-Dist: llama-index-core (>=0.9.32,<0.10.0)
15
+ Description-Content-Type: text/markdown
16
+
17
+ # LlamaIndex Vector_Stores Integration: Deeplake
18
+
@@ -0,0 +1 @@
1
+ # LlamaIndex Vector_Stores Integration: Deeplake
@@ -0,0 +1,4 @@
1
+ from llama_index.vector_stores.deeplake.base import DeepLakeVectorStore
2
+
3
+
4
+ __all__ = ["DeepLakeVectorStore"]
@@ -0,0 +1,204 @@
1
+ """DeepLake vector store index.
2
+
3
+ An index that is built within DeepLake.
4
+
5
+ """
6
+ import logging
7
+ from typing import Any, List, Optional, cast
8
+
9
+ from llama_index.core.schema import BaseNode, MetadataMode
10
+ from llama_index.core.vector_stores.types import VectorStore as VectorStoreBase
11
+ from llama_index.core.vector_stores.types import (
12
+ VectorStoreQuery,
13
+ VectorStoreQueryResult,
14
+ )
15
+ from llama_index.core.vector_stores.utils import (
16
+ metadata_dict_to_node,
17
+ node_to_metadata_dict,
18
+ )
19
+
20
+ try:
21
+ from deeplake.core.vectorstore.deeplake_vectorstore import VectorStore
22
+
23
+ DEEPLAKE_INSTALLED = True
24
+ except ImportError:
25
+ DEEPLAKE_INSTALLED = False
26
+
27
+ logger = logging.getLogger(__name__)
28
+
29
+
30
+ class DeepLakeVectorStore(VectorStoreBase):
31
+ """The DeepLake Vector Store.
32
+
33
+ In this vector store we store the text, its embedding and
34
+ a few pieces of its metadata in a deeplake dataset. This implemnetation
35
+ allows the use of an already existing deeplake dataset if it is one that was created
36
+ this vector store. It also supports creating a new one if the dataset doesn't
37
+ exist or if `overwrite` is set to True.
38
+ """
39
+
40
+ stores_text: bool = True
41
+ flat_metadata: bool = True
42
+
43
+ def __init__(
44
+ self,
45
+ dataset_path: str = "llama_index",
46
+ token: Optional[str] = None,
47
+ read_only: Optional[bool] = False,
48
+ ingestion_batch_size: int = 1024,
49
+ ingestion_num_workers: int = 4,
50
+ overwrite: bool = False,
51
+ exec_option: Optional[str] = None,
52
+ verbose: bool = True,
53
+ **kwargs: Any,
54
+ ):
55
+ """
56
+ Args:
57
+ dataset_path (str): Path to the deeplake dataset, where data will be
58
+ stored. Defaults to "llama_index".
59
+ overwrite (bool, optional): Whether to overwrite existing dataset with same
60
+ name. Defaults to False.
61
+ token (str, optional): the deeplake token that allows you to access the
62
+ dataset with proper access. Defaults to None.
63
+ read_only (bool, optional): Whether to open the dataset with read only mode.
64
+ ingestion_batch_size (int): used for controlling batched data
65
+ injestion to deeplake dataset. Defaults to 1024.
66
+ ingestion_num_workers (int): number of workers to use during data injestion.
67
+ Defaults to 4.
68
+ overwrite (bool): Whether to overwrite existing dataset with the
69
+ new dataset with the same name.
70
+ exec_option (str): Default method for search execution. It could be either
71
+ It could be either ``"python"``, ``"compute_engine"`` or
72
+ ``"tensor_db"``. Defaults to ``"python"``.
73
+ - ``python`` - Pure-python implementation that runs on the client and
74
+ can be used for data stored anywhere. WARNING: using this option
75
+ with big datasets is discouraged because it can lead to memory
76
+ issues.
77
+ - ``compute_engine`` - Performant C++ implementation of the Deep Lake
78
+ Compute Engine that runs on the client and can be used for any data
79
+ stored in or connected to Deep Lake. It cannot be used with
80
+ in-memory or local datasets.
81
+ - ``tensor_db`` - Performant and fully-hosted Managed Tensor Database
82
+ that is responsible for storage and query execution. Only available
83
+ for data stored in the Deep Lake Managed Database. Store datasets in
84
+ this database by specifying runtime = {"tensor_db": True} during
85
+ dataset creation.
86
+ verbose (bool): Specify if verbose output is enabled. Default is True.
87
+ **kwargs (Any): Additional keyword arguments.
88
+
89
+ Raises:
90
+ ImportError: Unable to import `deeplake`.
91
+ """
92
+ self.ingestion_batch_size = ingestion_batch_size
93
+ self.num_workers = ingestion_num_workers
94
+ self.token = token
95
+ self.read_only = read_only
96
+ self.dataset_path = dataset_path
97
+
98
+ if not DEEPLAKE_INSTALLED:
99
+ raise ImportError(
100
+ "Could not import deeplake python package. "
101
+ "Please install it with `pip install deeplake`."
102
+ )
103
+
104
+ self.vectorstore = VectorStore(
105
+ path=dataset_path,
106
+ ingestion_batch_size=ingestion_batch_size,
107
+ num_workers=ingestion_num_workers,
108
+ token=token,
109
+ read_only=read_only,
110
+ exec_option=exec_option,
111
+ overwrite=overwrite,
112
+ verbose=verbose,
113
+ **kwargs,
114
+ )
115
+ self._id_tensor_name = "ids" if "ids" in self.vectorstore.tensors() else "id"
116
+
117
+ @property
118
+ def client(self) -> Any:
119
+ """Get client.
120
+
121
+ Returns:
122
+ Any: DeepLake vectorstore dataset.
123
+ """
124
+ return self.vectorstore.dataset
125
+
126
+ def add(self, nodes: List[BaseNode], **add_kwargs: Any) -> List[str]:
127
+ """Add the embeddings and their nodes into DeepLake.
128
+
129
+ Args:
130
+ nodes (List[BaseNode]): List of nodes with embeddings
131
+ to insert.
132
+
133
+ Returns:
134
+ List[str]: List of ids inserted.
135
+ """
136
+ embedding = []
137
+ metadata = []
138
+ id_ = []
139
+ text = []
140
+
141
+ for node in nodes:
142
+ embedding.append(node.get_embedding())
143
+ metadata.append(
144
+ node_to_metadata_dict(
145
+ node, remove_text=False, flat_metadata=self.flat_metadata
146
+ )
147
+ )
148
+ id_.append(node.node_id)
149
+ text.append(node.get_content(metadata_mode=MetadataMode.NONE))
150
+
151
+ kwargs = {
152
+ "embedding": embedding,
153
+ "metadata": metadata,
154
+ self._id_tensor_name: id_,
155
+ "text": text,
156
+ }
157
+
158
+ return self.vectorstore.add(
159
+ return_ids=True,
160
+ **kwargs,
161
+ )
162
+
163
+ def delete(self, ref_doc_id: str, **delete_kwargs: Any) -> None:
164
+ """
165
+ Delete nodes using with ref_doc_id.
166
+
167
+ Args:
168
+ ref_doc_id (str): The doc_id of the document to delete.
169
+
170
+ """
171
+ self.vectorstore.delete(filter={"metadata": {"doc_id": ref_doc_id}})
172
+
173
+ def query(self, query: VectorStoreQuery, **kwargs: Any) -> VectorStoreQueryResult:
174
+ """Query index for top k most similar nodes.
175
+
176
+ Args:
177
+ query (VectorStoreQuery): VectorStoreQuery class input, it has
178
+ the following attributes:
179
+ 1. query_embedding (List[float]): query embedding
180
+ 2. similarity_top_k (int): top k most similar nodes
181
+ deep_memory (bool): Whether to use deep memory for query execution.
182
+
183
+ Returns:
184
+ VectorStoreQueryResult
185
+ """
186
+ query_embedding = cast(List[float], query.query_embedding)
187
+ exec_option = kwargs.get("exec_option")
188
+ deep_memory = kwargs.get("deep_memory")
189
+ data = self.vectorstore.search(
190
+ embedding=query_embedding,
191
+ exec_option=exec_option,
192
+ k=query.similarity_top_k,
193
+ filter=query.filters,
194
+ deep_memory=deep_memory,
195
+ )
196
+
197
+ similarities = data["score"]
198
+ ids = data[self._id_tensor_name]
199
+ metadatas = data["metadata"]
200
+ nodes = []
201
+ for metadata in metadatas:
202
+ nodes.append(metadata_dict_to_node(metadata))
203
+
204
+ return VectorStoreQueryResult(nodes=nodes, similarities=similarities, ids=ids)
@@ -0,0 +1,49 @@
1
+ [build-system]
2
+ requires = ["poetry-core"]
3
+ build-backend = "poetry.core.masonry.api"
4
+
5
+ [tool.codespell]
6
+ check-filenames = true
7
+ check-hidden = true
8
+ # Feel free to un-skip examples, and experimental, you will just need to
9
+ # work through many typos (--write-changes and --interactive will help)
10
+ skip = "*.csv,*.html,*.json,*.jsonl,*.pdf,*.txt,*.ipynb"
11
+
12
+ [tool.mypy]
13
+ disallow_untyped_defs = true
14
+ # Remove venv skip when integrated with pre-commit
15
+ exclude = ["_static", "build", "examples", "notebooks", "venv"]
16
+ ignore_missing_imports = true
17
+ python_version = "3.8"
18
+
19
+ [tool.poetry]
20
+ name = "llama-index-vector-stores-deeplake"
21
+ version = "0.0.1"
22
+ description = "llama-index vector_stores deeplake integration"
23
+ authors = ["Your Name <you@example.com>"]
24
+ license = "MIT"
25
+ readme = "README.md"
26
+ packages = [{include = "llama_index/"}]
27
+
28
+ [tool.poetry.dependencies]
29
+ python = ">=3.8.1,<3.12"
30
+ llama-index-core = "^0.9.32"
31
+
32
+ [tool.poetry.group.dev.dependencies]
33
+ black = {extras = ["jupyter"], version = "<=23.9.1,>=23.7.0"}
34
+ codespell = {extras = ["toml"], version = ">=v2.2.6"}
35
+ ipython = "8.10.0"
36
+ jupyter = "^1.0.0"
37
+ mypy = "0.991"
38
+ pre-commit = "3.2.0"
39
+ pylint = "2.15.10"
40
+ pytest = "7.2.1"
41
+ pytest-mock = "3.11.1"
42
+ ruff = "0.0.292"
43
+ tree-sitter-languages = "^1.8.0"
44
+ types-Deprecated = ">=0.1.0"
45
+ types-PyYAML = "^6.0.12.12"
46
+ types-protobuf = "^4.24.0.4"
47
+ types-redis = "4.5.5.0"
48
+ types-requests = "2.28.11.8" # TODO: unpin when mypy>0.991
49
+ types-setuptools = "67.1.0.0"