article_icon
article

Coding Your First AI Agent

The 'Hello World' of Fetch.ai's Agent Platform.

2023-07-253 min readFetch.ai

The world of AI Agents is fascinating. At Fetch.ai we believe these little pieces of code have the potential to disrupt the entire world economy. From mundane tasks to solving complex problems, these digital emissaries can be programmed to make decisions and carry out tasks on your behalf.

AI Agent technology is opening up new possibilities for developers who wish to build the next generation of intelligent applications. At Fetch.ai, we are dedicated to providing the tooling for developers to get ahead in AI. We believe that whether you’re an experienced programmer, or someone new to the field, you should be able to harness the power of AI agents.

So, here’s a comprehensive guide that will walk you through the installation and creation of your first AI agent in five simple steps.

Let’s dive in.

Prerequisites

  • Python 3.8, 3.9, or 3.10
  • Poetry for the virtual environment (if you don’t have it in your machine, you can follow this simple guide to install it)

Step One: Create a Virtual Environment

Virtual environments are a recommended best practice. They can provide you with a controlled space where all your project dependencies can avoid conflicts. If you have Poetry installed, you can execute the following in your terminal:

code-icon
code-icon
poetry init

Step Two: Install AI Agents Package

We can now start introducing AI agents to our development ecosystem. Execute the following poetry command:

code-icon
code-icon
poetry add uagents

The installation process may take a few moments. If your installation keeps failing, and you’re unable to debug - you can install directly from the source code.

Step Three: Create the Agent

Create a new Python file (let's call it my_first_uagent.py) and open it in your favorite code editor.

Now comes the fun part. Let’s create your first AI agent using the following snippet:

code-icon
code-icon
# Import the necessary classes from the uagents package
from uagents import Agent, Context

# Create a new agent named "bond" and set his recovery phrase for cryptographic key generation.
bond = Agent(name="bond", seed="007") 

Note that it is completely optional, but useful to include a seed parameter when creating an agent to set fixed addresses. If you don’t have a seed parameter, random addresses will be generated every time you run the agent.

Step Four: Give it some work

We will now give our agent something to do.

code-icon
code-icon
@bond.on_interval(period=2.0)
async def say_hello(ctx: Context):
    ctx.logger.info(f'the name is {ctx.name}, james {ctx.name}')

if __name__ == "__main__":
    bond.run()

Let’s take a minute to understand this code. In this snippet, we have a decorator @bond.on_interval(period=2.0) that sets up an interval-based action for our agent, bond. We have applied it to the function say_hello(), and have asked bond to say a famous quote every 2 seconds. ‘ctx’ is used to provide a context-specific method to say_hello().

Step Five: Run your agent!

Once you’re done, your entire code should look like this:

code-icon
code-icon
from uagents import Agent, Context
bond = Agent(name="bond", seed="007") 
bond = Agent(name="bond", seed="007") 

@bond.on_interval(period=2.0)
async def say_hello(ctx: Context):
    ctx.logger.info(f'the name is {ctx.name}, james {ctx.name}')

if __name__ == "__main__":
    bond.run()

Let’s see it in action. Assuming you’re in the directory my_first_uagent.py is in, execute the following:

code-icon
code-icon
poetry run python my_first_uagent.py

After a few lines in the agent's logs, you should see the following in your terminal:

Screenshot - 2023-07-17T172119.716.png

Congratulations! You have successfully created your first agent in just five steps. Feel free to play around with your code, and ask bond to say some other things. You can drop a message on our Telegram if you have any questions!


More from Fetch