Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[skip ci] breaking changes of store and index apis #322

Draft
wants to merge 33 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
s
  • Loading branch information
ouonline committed Oct 31, 2024
commit 1ec44ecb22942d7ea4546193d3a37070a1aca0bb
10 changes: 6 additions & 4 deletions lazyllm/tools/rag/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 143,20 @@ def register_similarity(
class EmbeddingIndex(IndexBase):
def __init__(self, backend_type: Optional[str] = None, *args, **kwargs):
if backend_type == 'milvus':
self._index = MilvusBackend(*args, **kwargs)
self._store = MilvusStore(*args, **kwargs)
elif backend_type == 'map':
self._store = MapStore(*args, **kwargs)
else:
raise ValueError(f'unsupported IndexWrapper backend [{backend_type}]')

@override
def update(self, nodes: List[DocNode]) -> None:
self._index.update(nodes)
self._store.update_nodes(nodes)

@override
def remove(self, uids: List[str], group_name: Optional[str] = None) -> None:
self._index.remove(uids, group_name)
self._index.remove_nodes(group_name, uids)

@override
def query(self, *args, **kwargs) -> List[DocNode]:
return self._index.query(*args, **kwargs)
return self._store.query(*args, **kwargs)
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 13,7 @@ def _remove_from_indices(name2index: Dict[str, IndexBase], uids: List[str],
for _, index in name2index.items():
index.remove(uids, group_name)

class MapBackend(StoreBase, IndexBase):
class MapStore(StoreBase, IndexBase):
def __init__(self, node_groups: List[str]):
super().__init__()
# Dict[group_name, Dict[uuid, DocNode]]
Expand All @@ -22,8 22,6 @@ def __init__(self, node_groups: List[str]):
}
self._name2index = {}

# ----- APIs for StoreBase ----- #

@override
def update_nodes(self, nodes: List[DocNode]) -> None:
for node in nodes:
Expand Down Expand Up @@ -78,22 76,6 @@ def get_index(self, type: str = 'default') -> Optional[IndexBase]:
return self
return self._name2index.get(type)

# ----- APIs for IndexBase ----- #

@override
def update(self, nodes: List[DocNode]) -> None:
self.update_nodes(nodes)

@override
def remove(self, uids: List[str], group_name: Optional[str] = None) -> None:
if group_name:
self.remove_nodes(group_name, uids)
else:
for _, docs in self._group2docs.items():
for uid in uids:
docs.pop(uid, None)
_remove_from_indices(self._name2index, uids)

@override
def query(self, group_name: str, uids: Optional[List[str]] = None) -> List[DocNode]:
return self.get_nodes(group_name, uids)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 25,7 @@ def __init__(self, name: str, data_type: int, index_type: Optional[str] = None,
self.max_length = max_length


class MilvusBackend(StoreBase, IndexBase):
class MilvusStore(StoreBase):
_type2milvus = [
pymilvus.DataType.VARCHAR, # DTYPE_VARCHAR
pymilvus.DataType.FLOAT_VECTOR, # DTYPE_FLOAT_VECTOR
Expand Down Expand Up @@ -84,8 84,6 @@ def __init__(self, uri: str, embed: Dict[str, Callable],
self._map_backend = MapBackend(list(group_fields.keys()))
self._load_all_nodes_to(self._map_backend)

# ----- APIs for Store ----- #

@override
def update_nodes(self, nodes: List[DocNode]) -> None:
parallel_do_embedding(self._embed, nodes)
Expand Down Expand Up @@ -127,16 125,6 @@ def get_index(self, type: str = 'default') -> Optional[IndexBase]:
return self
return self._map_backend.get_index(type)

# ----- APIs for Index ----- #

@override
def update(self, nodes: List[DocNode]) -> None:
self.update_nodes(nodes)

@override
def remove(self, uids: List[str], group_name: Optional[str] = None) -> None:
self.remove_nodes(group_name, uids)

@override
def query(self,
query: str,
Expand Down
4 changes: 4 additions & 0 deletions lazyllm/tools/rag/store_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 24,10 @@ def is_group_active(self, name: str) -> bool:
def all_groups(self) -> List[str]:
pass

@abstractmethod
def query(self, *args, **kwargs) -> List[DocNode]:
pass

@abstractmethod
def register_index(self, type: str, index: IndexBase) -> None:
pass
Expand Down