article_icon
article

Microagents - Creating Microagents

A guide helping you create your first microagent.

2023-03-240 min readjosh

Create your first uAgent

To build a microagent, you need to have installed a text editor application like PyCharm CE or VisualStudioCode. If you do not already have them on your computer, please follow the official guides:

Finalize the installation process, and then open your command prompt (Windows) or terminal (MacOS) on your computer.

  1. Navigate towards your microgents directory: you need to navigate to the directory you choose as reference for your microagent project. Here, we need to first create a Python script (.py). You can either do this directly on your text editor, or by running the following command:

    code-icon
    code-icon
    touch <filename_with_extension>
    

    In both cases, you need to open the folder you created for your microagent and inside it create the .py script file you desire which will contain all the needed code to create our first microagent. Let’s create a .py file named my_first_agent.py.

    code-icon
    code-icon
    touch my_first_agent.py 
    

    Now we can start writing the code for our uAgent.

  2. We use the uagents library to import the necessary classes from the microagents framework to create an agent named Alice.

    code-icon
    code-icon
    from uagent import Agent, Context
    alice = Agent(name="Alice", seed="Alice recovery phrase")
    

    Here, we create a new agent instance with a given name, Alice, and seed. The seed is used to generate a unique identity for the agent. It is optional but useful to include a seed parameter when creating an agent to set fixed addresses. Otherwise, random addresses will be generated every time you run the agent.

  3. We want our agent to log a message every 2 seconds using the say_hello function.

    code-icon
    code-icon
    @alice.on_interval(period=2.0)
    async def say_hello(ctx: Context):
        ctx.logger.info(f'hello, my name is {ctx.name}')
    
    • @agent.on_interval(period=2.0): this is a decorator syntax to define a periodic behavior for the agent. In this case, the agent will execute the say_hello function every 2 seconds.

    • async def say_hello(ctx: Context): this defines the behavior to be executed periodically by the agent. This function takes a Context object as an argument. The Context object is a collection of data and functions related to the agent. In our case, we just use the agent's name.

    • ctx.logger.info(f'Hello, my name is {ctx.name}'): this is the behavior to be executed by the agent. This line logs a message to the agent's logger, using the agent's name.

  4. Once you have defined the agent and its task, you can run it using the .run() method.

    code-icon
    code-icon
    if __name__ == "__main__":
        alice.run()
    

Save your script. It should look as follows:

code-icon
code-icon
from uagent import Agent, Context

alice = Agent(name="Alice", seed="Alice recovery phrase")
    
@alice.on_interval(period=2.0)
async def say_hello(ctx: Context):
    ctx.logger.info(f'hello, my name is {ctx.name}')
    
if __name__ == "__main__":
    alice.run()

Run your first uAgent.

To run the script, open your terminal, making sure you are in the right directory for your microagent project, then run the following command:

code-icon
code-icon
python <script_name>

That is, in our example:

code-icon
code-icon
python my_first_agent.py

When you run the script using the Python interpreter, you should see the agent's logs printed on your terminal, showing the task being performed at the specified intervals, as depicted below:

In summary, the above example demonstrates how to create and run a simple microagent using the Fetch.ai uAgents framework in Python. The agent is defined with a name and seed, and is programmed to execute a behavior every 2 seconds using the on_interval decorator. The agent's behavior simply logs a message to the agent's logger using its name. This code can serve as a starting point for building more complex and customizable agents that can interact with other agents on the network to perform a variety of tasks.

Overall, the uAgents library provides a simple and flexible framework for creating and running microagents in a decentralized network, enabling you to build complex and autonomous systems.


More from Fetch