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.
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:
poetry init
We can now start introducing AI agents to our development ecosystem. Execute the following poetry command:
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.
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:
# 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.
We will now give our agent something to do.
@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().
Once you’re done, your entire code should look like this:
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:
poetry run python my_first_uagent.py
After a few lines in the agent's logs, you should see the following in your terminal:
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!