diff --git a/pkgs/community/swarmauri_documentstore_communityredis/README.md b/pkgs/community/swarmauri_documentstore_communityredis/README.md new file mode 100644 index 00000000..cd26902a --- /dev/null +++ b/pkgs/community/swarmauri_documentstore_communityredis/README.md @@ -0,0 +1 @@ +# Swarmauri Example Community Package \ No newline at end of file diff --git a/pkgs/community/swarmauri_documentstore_communityredis/pyproject.toml b/pkgs/community/swarmauri_documentstore_communityredis/pyproject.toml new file mode 100644 index 00000000..03963d72 --- /dev/null +++ b/pkgs/community/swarmauri_documentstore_communityredis/pyproject.toml @@ -0,0 +1,57 @@ +[tool.poetry] +name = "swarmauri_documentstore_communityredis" +version = "0.6.0.dev1" +description = "Swarmauri Psutil Tool" +authors = ["Jacob Stewart "] +license = "Apache-2.0" +readme = "README.md" +repository = "http://github.com/swarmauri/swarmauri-sdk" +classifiers = [ + "License :: OSI Approved :: Apache Software License", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12" +] + +[tool.poetry.dependencies] +python = ">=3.10,<3.13" + +# Swarmauri +swarmauri_core = { path = "../../core" } +swarmauri_base = { path = "../../base" } + +# Dependencies +redis = "^4.0" + + +[tool.poetry.group.dev.dependencies] +flake8 = "^7.0" +pytest = "^8.0" +pytest-asyncio = ">=0.24.0" +pytest-xdist = "^3.6.1" +pytest-json-report = "^1.5.0" +python-dotenv = "*" +requests = "^2.32.3" + +[build-system] +requires = ["poetry-core>=1.0.0"] +build-backend = "poetry.core.masonry.api" + +[tool.pytest.ini_options] +norecursedirs = ["combined", "scripts"] + +markers = [ + "test: standard test", + "unit: Unit tests", + "integration: Integration tests", + "acceptance: Acceptance tests", + "experimental: Experimental tests" +] +log_cli = true +log_cli_level = "INFO" +log_cli_format = "%(asctime)s [%(levelname)s] %(message)s" +log_cli_date_format = "%Y-%m-%d %H:%M:%S" +asyncio_default_fixture_loop_scope = "function" + +[tool.poetry.plugins."swarmauri.documentstores"] +RedisDocumentStore = "swarmauri_documentstore_communityredis.RedisDocumentStore:RedisDocumentStore" \ No newline at end of file diff --git a/pkgs/community/swarmauri_documentstore_communityredis/swarmauri_documentstore_communityredis/RedisDocumentStore.py b/pkgs/community/swarmauri_documentstore_communityredis/swarmauri_documentstore_communityredis/RedisDocumentStore.py new file mode 100644 index 00000000..dc1e7d63 --- /dev/null +++ b/pkgs/community/swarmauri_documentstore_communityredis/swarmauri_documentstore_communityredis/RedisDocumentStore.py @@ -0,0 +1,72 @@ +from typing import List, Optional +from swarmauri_base.document_stores.DocumentStoreBase import DocumentStoreBase +from swarmauri_core.documents.IDocument import IDocument +import redis +import json +from redis.commands.search.field import TextField, NumericField, TagField +from redis.commands.search.indexDefinition import IndexDefinition, IndexType + + +class RedisDocumentStore(DocumentStoreBase): + def __init__(self, host, password, port, db): + """Store connection details without initializing the Redis client.""" + self._host = host + self._password = password + self._port = port + self._db = db + self._redis_client = None # Delayed initialization + + @property + def redis_client(self): + """Lazily initialize and return the Redis client using a factory method.""" + if self._redis_client is None: + print("here") + self._redis_client = redis.Redis( + host=self._host, password=self._password, port=self._port, db=self._db + ) + print("there") + return self._redis_client + + def add_document(self, document: IDocument) -> None: + + data = document.as_dict() + doc_id = data["id"] + del data["id"] + self.redis_client.json().set(doc_id, "$", json.dumps(data)) + + def add_documents(self, documents: List[IDocument]) -> None: + with self.redis_client.pipeline() as pipe: + for document in documents: + pipe.set(document.doc_id, document) + pipe.execute() + + def get_document(self, doc_id: str) -> Optional[IDocument]: + result = self.redis_client.json().get(doc_id) + if result: + return json.loads(result) + return None + + def get_all_documents(self) -> List[IDocument]: + keys = self.redis_client.keys("*") + documents = [] + for key in keys: + document_data = self.redis_client.get(key) + if document_data: + documents.append(json.loads(document_data)) + return documents + + def update_document(self, doc_id: str, updated_document: IDocument) -> None: + self.add_document(updated_document) + + def delete_document(self, doc_id: str) -> None: + self.redis_client.delete(doc_id) + + def __getstate__(self): + """Return the object state for serialization, excluding the Redis client.""" + state = self.__dict__.copy() + state["_redis_client"] = None # Exclude Redis client from serialization + return state + + def __setstate__(self, state): + """Restore the object state after serialization, reinitializing the Redis client.""" + self.__dict__.update(state) diff --git a/pkgs/community/swarmauri_documentstore_communityredis/swarmauri_documentstore_communityredis/__init__.py b/pkgs/community/swarmauri_documentstore_communityredis/swarmauri_documentstore_communityredis/__init__.py new file mode 100644 index 00000000..a061c021 --- /dev/null +++ b/pkgs/community/swarmauri_documentstore_communityredis/swarmauri_documentstore_communityredis/__init__.py @@ -0,0 +1,14 @@ +from .RedisDocumentStore import RedisDocumentStore + +__version__ = "0.6.0.dev26" +__long_desc__ = """ + +# Swarmauri Redis document store Plugin + +This repository includes a Redis document store of a Swarmauri Plugin. + +Visit us at: https://swarmauri.com +Follow us at: https://github.com/swarmauri +Star us at: https://github.com/swarmauri/swarmauri-sdk + +""" diff --git a/pkgs/community/swarmauri_vectorstore_communitymlm/pyproject.toml b/pkgs/community/swarmauri_vectorstore_communitymlm/pyproject.toml index 0c582df3..f2724923 100644 --- a/pkgs/community/swarmauri_vectorstore_communitymlm/pyproject.toml +++ b/pkgs/community/swarmauri_vectorstore_communitymlm/pyproject.toml @@ -1,7 +1,7 @@ [tool.poetry] name = "swarmauri_vectorstore_communitymlm" version = "0.6.0.dev1" -description = "Swarmauri Psutil Tool" +description = "Swarmauri MLM Vector Store" authors = ["Jacob Stewart "] license = "Apache-2.0" readme = "README.md" @@ -55,4 +55,4 @@ log_cli_date_format = "%Y-%m-%d %H:%M:%S" asyncio_default_fixture_loop_scope = "function" [tool.poetry.plugins."swarmauri.vectorstores"] -ExampleCommunityAgent = "swarmauri_vectorstore_communitymlm.ExampleCommunityAgent:ExampleCommunityAgent" \ No newline at end of file +ExampleCommunityAgent = "swarmauri_vectorstore_communitymlm.ExampleCommunityAgent:ExampleCommunityAgent"