From b20f09f9be8b77170aa97f696897a6353891d962 Mon Sep 17 00:00:00 2001 From: Riccardo Balbo Date: Mon, 22 Apr 2024 14:33:02 +0200 Subject: [PATCH] update proto --- requirements.txt | 2 +- src/OpenAgentsNode.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 22e8d2e..3358629 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,6 @@ grpcio==1.62.1 protobuf==5.26.1 -https://github.com/riccardobl/openagents-grpc-proto/releases/download/v0.6/openagents_grpc_proto-PYTHON.tar.gz +https://github.com/riccardobl/openagents-grpc-proto/releases/download/v0.7.2/openagents_grpc_proto-PYTHON.tar.gz faiss-cpu==1.8.0 numpy==1.21.2 packaging \ No newline at end of file diff --git a/src/OpenAgentsNode.py b/src/OpenAgentsNode.py index c592657..9ec9999 100644 --- a/src/OpenAgentsNode.py +++ b/src/OpenAgentsNode.py @@ -6,6 +6,7 @@ import traceback import json import asyncio +import pickle class BlobStorage: def __init__(self, id, url, node): @@ -85,8 +86,40 @@ def __init__(self, filters, meta, template, sockets): self._sockets = sockets + def cacheSet(self, path, value, version=0, expireAt=0): + try: + dataBytes = pickle.dumps(value) + client = self._node.getClient() + CHUNK_SIZE = 1024 + def write_data(): + for j in range(0, len(dataBytes), CHUNK_SIZE): + chunk = bytes(dataBytes[j:min(j+CHUNK_SIZE, len(dataBytes))]) + request = rpc_pb2.RpcCacheSetRequest( + key=path, + data=chunk, + expireAt=expireAt, + version=version + ) + yield request + res=client.cacheSet(write_data()) + return res.success + except Exception as e: + print("Error setting cache "+str(e)) + return False + def cacheGet(self, path, lastVersion = 0): + try: + client = self._node.getClient() + bytesOut = bytearray() + for chunk in client.cacheGet(rpc_pb2.RpcCacheGetRequest(key=path, lastVersion = lastVersion)): + if not chunk.exists: + return None + bytesOut.extend(chunk.data) + return pickle.loads(bytesOut) + except Exception as e: + print("Error getting cache "+str(e)) + return None def _setNode(self, node): self._node = node