[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
@@ -1,8 +1,11 @@
|
|||||||
|
# Copyright (C) 2025 Intel Corporation
|
||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
import json
|
import json
|
||||||
from typing import Any, Dict, List, Optional, Tuple
|
from typing import Any, Dict, List, Optional, Tuple
|
||||||
|
|
||||||
from redis.asyncio import Redis as AsyncRedis
|
|
||||||
from redis import Redis
|
from redis import Redis
|
||||||
|
from redis.asyncio import Redis as AsyncRedis
|
||||||
|
|
||||||
DEFAULT_COLLECTION = "data"
|
DEFAULT_COLLECTION = "data"
|
||||||
DEFAULT_BATCH_SIZE = 1
|
DEFAULT_BATCH_SIZE = 1
|
||||||
@@ -22,32 +25,24 @@ class RedisPersistence:
|
|||||||
raise ValueError(f"Redis failed to connect: {e}")
|
raise ValueError(f"Redis failed to connect: {e}")
|
||||||
|
|
||||||
def put(self, key: str, val: dict, collection: str = DEFAULT_COLLECTION) -> None:
|
def put(self, key: str, val: dict, collection: str = DEFAULT_COLLECTION) -> None:
|
||||||
"""
|
"""Put a key-value pair into the store.
|
||||||
Put a key-value pair into the store.
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
key (str): key
|
key (str): key
|
||||||
val (dict): value
|
val (dict): value
|
||||||
collection (str): collection name
|
collection (str): collection name
|
||||||
|
|
||||||
"""
|
"""
|
||||||
self._redis_client.hset(name=collection, key=key, value=json.dumps(val))
|
self._redis_client.hset(name=collection, key=key, value=json.dumps(val))
|
||||||
|
|
||||||
async def aput(
|
async def aput(self, key: str, val: dict, collection: str = DEFAULT_COLLECTION) -> None:
|
||||||
self, key: str, val: dict, collection: str = DEFAULT_COLLECTION
|
"""Put a key-value pair into the store.
|
||||||
) -> None:
|
|
||||||
"""
|
|
||||||
Put a key-value pair into the store.
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
key (str): key
|
key (str): key
|
||||||
val (dict): value
|
val (dict): value
|
||||||
collection (str): collection name
|
collection (str): collection name
|
||||||
|
|
||||||
"""
|
"""
|
||||||
await self._async_redis_client.hset(
|
await self._async_redis_client.hset(name=collection, key=key, value=json.dumps(val))
|
||||||
name=collection, key=key, value=json.dumps(val)
|
|
||||||
)
|
|
||||||
|
|
||||||
def put_all(
|
def put_all(
|
||||||
self,
|
self,
|
||||||
@@ -55,13 +50,11 @@ class RedisPersistence:
|
|||||||
collection: str = DEFAULT_COLLECTION,
|
collection: str = DEFAULT_COLLECTION,
|
||||||
batch_size: int = DEFAULT_BATCH_SIZE,
|
batch_size: int = DEFAULT_BATCH_SIZE,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""
|
"""Put a dictionary of key-value pairs into the store.
|
||||||
Put a dictionary of key-value pairs into the store.
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
kv_pairs (List[Tuple[str, dict]]): key-value pairs
|
kv_pairs (List[Tuple[str, dict]]): key-value pairs
|
||||||
collection (str): collection name
|
collection (str): collection name
|
||||||
|
|
||||||
"""
|
"""
|
||||||
with self._redis_client.pipeline() as pipe:
|
with self._redis_client.pipeline() as pipe:
|
||||||
cur_batch = 0
|
cur_batch = 0
|
||||||
@@ -77,29 +70,23 @@ class RedisPersistence:
|
|||||||
pipe.execute()
|
pipe.execute()
|
||||||
|
|
||||||
def get(self, key: str, collection: str = DEFAULT_COLLECTION) -> Optional[dict]:
|
def get(self, key: str, collection: str = DEFAULT_COLLECTION) -> Optional[dict]:
|
||||||
"""
|
"""Get a value from the store.
|
||||||
Get a value from the store.
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
key (str): key
|
key (str): key
|
||||||
collection (str): collection name
|
collection (str): collection name
|
||||||
|
|
||||||
"""
|
"""
|
||||||
val_str = self._redis_client.hget(name=collection, key=key)
|
val_str = self._redis_client.hget(name=collection, key=key)
|
||||||
if val_str is None:
|
if val_str is None:
|
||||||
return None
|
return None
|
||||||
return json.loads(val_str)
|
return json.loads(val_str)
|
||||||
|
|
||||||
async def aget(
|
async def aget(self, key: str, collection: str = DEFAULT_COLLECTION) -> Optional[dict]:
|
||||||
self, key: str, collection: str = DEFAULT_COLLECTION
|
"""Get a value from the store.
|
||||||
) -> Optional[dict]:
|
|
||||||
"""
|
|
||||||
Get a value from the store.
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
key (str): key
|
key (str): key
|
||||||
collection (str): collection name
|
collection (str): collection name
|
||||||
|
|
||||||
"""
|
"""
|
||||||
val_str = await self._async_redis_client.hget(name=collection, key=key)
|
val_str = await self._async_redis_client.hget(name=collection, key=key)
|
||||||
if val_str is None:
|
if val_str is None:
|
||||||
@@ -123,25 +110,21 @@ class RedisPersistence:
|
|||||||
return collection_kv_dict
|
return collection_kv_dict
|
||||||
|
|
||||||
def delete(self, key: str, collection: str = DEFAULT_COLLECTION) -> bool:
|
def delete(self, key: str, collection: str = DEFAULT_COLLECTION) -> bool:
|
||||||
"""
|
"""Delete a value from the store.
|
||||||
Delete a value from the store.
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
key (str): key
|
key (str): key
|
||||||
collection (str): collection name
|
collection (str): collection name
|
||||||
|
|
||||||
"""
|
"""
|
||||||
deleted_num = self._redis_client.hdel(collection, key)
|
deleted_num = self._redis_client.hdel(collection, key)
|
||||||
return bool(deleted_num > 0)
|
return bool(deleted_num > 0)
|
||||||
|
|
||||||
async def adelete(self, key: str, collection: str = DEFAULT_COLLECTION) -> bool:
|
async def adelete(self, key: str, collection: str = DEFAULT_COLLECTION) -> bool:
|
||||||
"""
|
"""Delete a value from the store.
|
||||||
Delete a value from the store.
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
key (str): key
|
key (str): key
|
||||||
collection (str): collection name
|
collection (str): collection name
|
||||||
|
|
||||||
"""
|
"""
|
||||||
deleted_num = await self._async_redis_client.hdel(collection, key)
|
deleted_num = await self._async_redis_client.hdel(collection, key)
|
||||||
return bool(deleted_num > 0)
|
return bool(deleted_num > 0)
|
||||||
@@ -152,8 +135,7 @@ class RedisPersistence:
|
|||||||
host: str,
|
host: str,
|
||||||
port: int,
|
port: int,
|
||||||
) -> "RedisPersistence":
|
) -> "RedisPersistence":
|
||||||
"""
|
"""Load a RedisPersistence from a Redis host and port.
|
||||||
Load a RedisPersistence from a Redis host and port.
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
host (str): Redis host
|
host (str): Redis host
|
||||||
@@ -161,4 +143,3 @@ class RedisPersistence:
|
|||||||
"""
|
"""
|
||||||
url = f"redis://{host}:{port}".format(host=host, port=port)
|
url = f"redis://{host}:{port}".format(host=host, port=port)
|
||||||
return cls(redis_uri=url)
|
return cls(redis_uri=url)
|
||||||
|
|
||||||
|
|||||||
@@ -22,14 +22,6 @@ pandas
|
|||||||
prometheus_fastapi_instrumentator
|
prometheus_fastapi_instrumentator
|
||||||
pyarrow
|
pyarrow
|
||||||
pydantic #==1.10.13
|
pydantic #==1.10.13
|
||||||
shortuuid
|
|
||||||
tavily-python
|
|
||||||
|
|
||||||
# used by agents
|
|
||||||
transformers
|
|
||||||
transformers[sentencepiece]
|
|
||||||
|
|
||||||
uvicorn
|
|
||||||
|
|
||||||
# used by document loader
|
# used by document loader
|
||||||
# beautifulsoup4
|
# beautifulsoup4
|
||||||
@@ -46,3 +38,11 @@ uvicorn
|
|||||||
# virtualenv
|
# virtualenv
|
||||||
|
|
||||||
redis
|
redis
|
||||||
|
shortuuid
|
||||||
|
tavily-python
|
||||||
|
|
||||||
|
# used by agents
|
||||||
|
transformers
|
||||||
|
transformers[sentencepiece]
|
||||||
|
|
||||||
|
uvicorn
|
||||||
|
|||||||
Reference in New Issue
Block a user