Using agents storage function
Introduction
Agents will need to store data for later use, uAgents has an inbuilt function to store data to json file, this is done with ctx.storage
. Data is stored in key:value
format. ctx
object’s storage
attribute has different function like get
and set
to use agent’s storage.
Overview
We can store simple integers like :
ctx.storage.set("Messages_sent", 1)
Or strings:
ctx.storage.set("Passkey", "#-eiwfwrign")
Complex data can also be stored:
Record1 = {"name":ctx.name, "address":ctx.address} ctx.storage.set("Record1", Record1)
We can access these values like so:
ctx.logger.info(ctx.storage.get("Record1")) ctx.logger.info(ctx.storage.get("Messages_sent")) ctx.logger.info(ctx.storage.get("Passkey"))
Expected Output:
INFO: [Receiver]: {"name": "Receiver", "address": "agent1qdlcar9glcm6f9rpzway3xqaw4clfltxdhtwflxrjz3ahy98ep5dx7xy6x7"} INFO: [Receiver]: 2 INFO: [Receiver]: "#-eiwfwrign"
Locally, this will be stored as a json file:
{ "Messages_sent" : 2, "Record1" : {"name" : "Receiver", "address" : "agent1qdlcar9glcm6f9rpzway3xqaw4clfltxdhtwflxrjz3ahy98ep5dx7xy6x7"}, "Passkey" : "#-eiwfwrign" }
Generally, any object that is compatible with pydantic Models will be fine with uAgents.
Prerequisites
Make sure you have read the following resources before going on with this guide:
Imports needed
Walk-through
In this walkthrough, we want to show how storage functions are called and how to use them. We want to create an agent which gets a value from the storage (starting from 0) every second. Then prints it, and puts the new value back into the storage but increased by 1 unit.
-
To start let's create a Python script and name it
storage.py
, we can do this in terminal with the following command:windowsecho. > storage.py
-
Then, we need to open the script in the text editor of choice and import the necessary classes,
Agent
andContext
, from theuagents
library. -
Let's then create an agent named
alice
which logs a message every second using the.on_interval()
decorator, indicating the current count. Theon_interval()
function takes aContext
object as a parameter: theContext
object contains astorage
attribute, which is used to store and retrieve data between method calls:
storage.pyfrom uagents import Agent, Context alice = Agent(name="alice", seed="alice recovery phrase", port=8000, endpoint=["http://127.0.0.1:8000/submit"]) @alice.on_interval(period=1.0) async def on_interval(ctx: Context): current_count = ctx.storage.get("count") or 0 ctx.logger.info(f"My count is: {current_count}") ctx.storage.set("count", current_count + 1) if __name__ == "__main__": alice.run()
Here, the on_interval()
function retrieves the current count from the storage attribute using the ctx.storage.get()
method. It prints the current_count
value, and then increments it by 1
, and stores the updated count back to the storage attribute using the ctx.storage.set()
method. The current count is then logged using the ctx.logger.info()
method.
- Save the script.
Run the script
On your terminal, make sure you activated the virtual environment.
Run the script: python storage.py
The output should look as follows:
INFO: [alice]: Registration on Almanac API successful INFO: [alice]: Registering on almanac contract... INFO: [alice]: Registering on almanac contract...complete INFO: [alice]: Agent inspector available at https://agentverse.ai/inspect/?uri=http%3A//127.0.0.1%3A8000&address=agent1qww3ju3h6kfcuqf54gkghvt2pqe8qp97a7nzm2vp8plfxflc0epzcjsv79t INFO: [alice]: Starting server on http://0.0.0.0:8000 (Press CTRL+C to quit) INFO: [alice]: My count is: 0 INFO: [alice]: My count is: 1 INFO: [alice]: My count is: 2 INFO: [alice]: My count is: 3 ...