-
Notifications
You must be signed in to change notification settings - Fork 202
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
feat: add RedisKVStorage, MilvusVectorStorage, and NebulaGraphStorage #71
Open
utopia2077
wants to merge
3
commits into
gusye1234:main
Choose a base branch
from
utopia2077:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import json | ||
from dataclasses import dataclass, field | ||
|
||
from ..base import BaseKVStorage | ||
import redis | ||
from redis.exceptions import ConnectionError | ||
from .._utils import get_workdir_last_folder_name, logger | ||
|
||
@dataclass | ||
class RedisKVStorage(BaseKVStorage): | ||
_redis: redis.Redis = field(init=False, repr=False, compare=False) | ||
def __post_init__(self): | ||
try: | ||
host = self.global_config["addon_params"].get("redis_host", "localhost") | ||
port = self.global_config["addon_params"].get("redis_port", "6379") | ||
user = self.global_config["addon_params"].get("redis_user", None) | ||
password = self.global_config["addon_params"].get("redis_password", None) | ||
db = self.global_config["addon_params"].get("redis_db", 0) | ||
self._redis = redis.Redis(host=host, port=port, username=user, password=password, db=db) | ||
self._redis.ping() | ||
logger.info(f"Connected to Redis at {host}:{port}") | ||
except ConnectionError: | ||
logger.error(f"Failed to connect to Redis at {host}:{port}") | ||
raise | ||
|
||
self._namespace = f"kv_store_{get_workdir_last_folder_name(self.global_config['working_dir'])}" | ||
logger.info(f"Initialized Redis KV storage for namespace: {self._namespace}") | ||
|
||
async def all_keys(self) -> list[str]: | ||
return [key.decode().split(':', 1)[1] for key in self._redis.keys(f"{self._namespace}:*")] | ||
|
||
async def index_done_callback(self): | ||
# Redis automatically persists data, so no explicit action needed | ||
pass | ||
|
||
async def get_by_id(self, id): | ||
value = self._redis.get(f"{self._namespace}:{id}") | ||
return json.loads(value) if value else None | ||
|
||
async def get_by_ids(self, ids, fields=None): | ||
pipeline = self._redis.pipeline() | ||
for id in ids: | ||
pipeline.get(f"{self._namespace}:{id}") | ||
values = pipeline.execute() | ||
|
||
results = [] | ||
for value in values: | ||
if value: | ||
data = json.loads(value) | ||
if fields: | ||
results.append({k: v for k, v in data.items() if k in fields}) | ||
else: | ||
results.append(data) | ||
else: | ||
results.append(None) | ||
return results | ||
|
||
async def filter_keys(self, data: list[str]) -> set[str]: | ||
pipeline = self._redis.pipeline() | ||
for key in data: | ||
pipeline.exists(f"{self._namespace}:{key}") | ||
exists = pipeline.execute() | ||
return set([key for key, exists in zip(data, exists) if not exists]) | ||
|
||
async def upsert(self, data: dict[str, dict]): | ||
pipeline = self._redis.pipeline() | ||
for key, value in data.items(): | ||
pipeline.set(f"{self._namespace}:{key}", json.dumps(value,ensure_ascii=False)) | ||
pipeline.execute() | ||
|
||
async def drop(self): | ||
keys = self._redis.keys(f"{self._namespace}:*") | ||
if keys: | ||
self._redis.delete(*keys) | ||
|
||
def __getstate__(self): | ||
state = self.__dict__.copy() | ||
del state['_redis'] | ||
return state | ||
|
||
def __setstate__(self, state): | ||
self.__dict__.update(state) | ||
self.__post_init__() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import asyncio | ||
import os | ||
from dataclasses import dataclass | ||
import numpy as np | ||
from pymilvus import MilvusClient | ||
|
||
from .._utils import get_workdir_last_folder_name, logger | ||
from ..base import BaseVectorStorage | ||
|
||
|
||
@dataclass | ||
class MilvusVectorStorage(BaseVectorStorage): | ||
|
||
@staticmethod | ||
def create_collection_if_not_exist(client, collection_name: str,max_id_length: int, dimension: int,**kwargs): | ||
if client.has_collection(collection_name): | ||
return | ||
client.create_collection( | ||
collection_name, max_length=max_id_length, id_type="string", dimension=dimension, **kwargs | ||
) | ||
|
||
|
||
def __post_init__(self): | ||
self.milvus_uri = self.global_config["addon_params"].get("milvus_uri", "") | ||
if self.milvus_uri: | ||
self.milvus_user = self.global_config["addon_params"].get("milvus_user", "") | ||
self.milvus_password = self.global_config["addon_params"].get("milvus_password", "") | ||
self.collection_name = get_workdir_last_folder_name(self.global_config["working_dir"]) | ||
self._client = MilvusClient(self.milvus_uri, self.milvus_user, self.milvus_password) | ||
else: | ||
self._client_file_name = os.path.join( | ||
self.global_config["working_dir"], "milvus_lite.db" | ||
) | ||
self._client = MilvusClient(self._client_file_name) | ||
|
||
self.cosine_better_than_threshold: float = 0.2 | ||
self._max_batch_size = self.global_config["embedding_batch_num"] | ||
self.max_id_length = 256 | ||
MilvusVectorStorage.create_collection_if_not_exist( | ||
self._client, self.collection_name,max_id_length=self.max_id_length,dimension=self.embedding_func.embedding_dim, | ||
) | ||
|
||
async def upsert(self, data: dict[str, dict]): | ||
logger.info(f"Inserting {len(data)} vectors to {self.collection_name}") | ||
list_data = [ | ||
{ | ||
"id": k, | ||
**{k1: v1 for k1, v1 in v.items() if k1 in self.meta_fields}, | ||
} | ||
for k, v in data.items() | ||
] | ||
contents = [v["content"] for v in data.values()] | ||
batches = [ | ||
contents[i : i + self._max_batch_size] | ||
for i in range(0, len(contents), self._max_batch_size) | ||
] | ||
embeddings_list = await asyncio.gather( | ||
*[self.embedding_func(batch) for batch in batches] | ||
) | ||
embeddings = np.concatenate(embeddings_list) | ||
for i, d in enumerate(list_data): | ||
d["vector"] = embeddings[i] | ||
batch_size = 1024 | ||
results = [] | ||
for i in range(0, len(list_data), batch_size): | ||
batch = list_data[i:i+batch_size] | ||
batch_result = self._client.upsert(collection_name=self.collection_name, data=batch) | ||
results.append(batch_result) | ||
|
||
total_upsert_count = sum(result.get('upsert_count', 0) for result in results) | ||
results = {'upsert_count': total_upsert_count} | ||
return results | ||
|
||
async def query(self, query: str, top_k=5): | ||
embedding = await self.embedding_func([query]) | ||
results = self._client.search( | ||
collection_name=self.collection_name, | ||
data=embedding, | ||
limit=top_k, | ||
output_fields=list(self.meta_fields), | ||
search_params={"metric_type": "COSINE", "params": {"radius": self.cosine_better_than_threshold}}, | ||
) | ||
return [ | ||
{**dp["entity"], "id": dp["id"], "distance": dp["distance"]} | ||
for dp in results[0] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,8 @@ hnswlib | |
xxhash | ||
tenacity | ||
dspy-ai | ||
neo4j | ||
neo4j | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the milvus dep, so those who want to use this component will handle the cross platform. Let's not introduce the install problems for the rest. |
||
pymilvus | ||
redis | ||
nebula3-python | ||
ng_nx |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe remove this line, so the Milvus is not imported by default?