Store
The Store is a fundamental abstraction in Agentswarm that decouples state persistence from agent logic. It allows agents to share data (like task results, intermediate variables, or long-term memory) without knowing how or where that data is stored.
The Abstract Concept
The Store class defines a simple key-value interface. By designing your agents to rely on this interface, you make them portable and adaptable to different environments.
- Abstraction: Your agent simply calls
store.set("key", value)orstore.get("key"). - Flexibility: In a local script, this might just write to a Python dictionary. In a production cloud deployment, the underlying implementation could speak to Redis, a SQL database, or a cloud bucket.
agentswarm.datamodels.Store
Bases: ABC
The Store class defines a simple key/value API to access the store. The implementation can vary from a local dictionary, to a distributed remote store.
Source code in src/agentswarm/datamodels/store.py
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | |
get(key)
abstractmethod
Obtains the value associated with the given key.
Source code in src/agentswarm/datamodels/store.py
10 11 12 13 14 15 | |
has(key)
abstractmethod
Checks if the store has a value associated with the given key.
Source code in src/agentswarm/datamodels/store.py
24 25 26 27 28 29 | |
items()
abstractmethod
Returns all key-value pairs in the store. (Primary used for nice tracing)
Source code in src/agentswarm/datamodels/store.py
31 32 33 34 35 36 37 | |
recreate(config)
abstractmethod
classmethod
Recreates a Store instance from a configuration dictionary.
Source code in src/agentswarm/datamodels/store.py
47 48 49 50 51 52 53 | |
set(key, value)
abstractmethod
Sets the value associated with the given key.
Source code in src/agentswarm/datamodels/store.py
17 18 19 20 21 22 | |
to_dict()
abstractmethod
Returns the configuration needed to recreate the store. WARNING: Never share keys or other secret during this process.
Source code in src/agentswarm/datamodels/store.py
39 40 41 42 43 44 45 | |
Custom Implementations
You are encouraged to create your own Store implementations for your specific needs. For example, if you need persistence across reboots, you might implement a FileStore or a RedisStore.
Example: Creating a Redis Store
from agentswarm.datamodels import Store
import redis
class RedisStore(Store):
def __init__(self, host='localhost', port=6379, db=0):
self.r = redis.Redis(host=host, port=port, db=db)
def get(self, key: str) -> any:
return self.r.get(key)
def set(self, key: str, value: any):
self.r.set(key, value)
def has(self, key: str) -> bool:
return self.r.exists(key)
def items(self) -> dict:
# Implementation to fetch all keys...
pass
Local Store
The library comes with a ready-to-use LocalStore which implements the interface using an in-memory dictionary. This is perfect for testing, scripts, and single-instance applications where persistence is not required.
agentswarm.datamodels.LocalStore
Bases: Store
The LocalStore class implements a simple key-value store in memory.
Source code in src/agentswarm/datamodels/local_store.py
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |