Creating your first agent
Introduction
Once you've installed the uAgents library, it's quite simple to get a minimal use case running.
The uAgents Framework streamlines agent creation by offering tools for communication, discovery, and publication within the Fetch.ai network. It empowers users and developers with flexibility, allowing them to build agents using anything from cutting-edge Large Language Models (LLMs) to simple APIs.
Let our first agent be a simple initialization and printing out the agent's name and address.
Prerequisites
Make sure you have read the following resources before going on with this guide:
Imports needed
The agent
-
Let's create a Python script for this task, and name it by running:
windowsecho. > first_agent.py
-
We then need to import the
Agent
andContext
classes from theuagents
library, and then create an agent using the classAgent
:
first_agent.pyfrom uagents import Agent, Context agent = Agent(name="alice", seed="secret_seed_phrase", port=8000, endpoint=["http://localhost:8000/submit"])
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. Your address is kind of important, as this is how other agents will identify you.
-
Let's define a
say_hello()
function for our agent to print a message periodically sayinghello, my name is ...
:
first_agent.py@agent.on_event("startup") async def introduce_agent(ctx: Context): ctx.logger.info(f"Hello, I'm agent {agent.name} and my address is {agent.address}.") if __name__ == "__main__": agent.run()
The .on_event("startup")
decorator defines a behavior for this agent when it is run. In this case, the agent will execute the say_hello()
function when the agent starts. The Context
object is a collection of data and functions related to the agent. In this case, we just use the agent's name
, alice
. The agent executes the function and uses the ctx.logger.info()
method to print the message.
- Save the script.
The overall script should look as follows:
first_agent.pyfrom uagents import Agent, Context agent = Agent(name="alice", seed="secret_seed_phrase", port=8000, endpoint=["http://localhost:8000/submit"]) @agent.on_event("startup") async def introduce_agent(ctx: Context): ctx.logger.info(f"Hello, I'm agent {agent.name} and my address is {agent.address}.") if __name__ == "__main__": agent.run()
Run your agent
Make sure to have activated your virtual environment correctly.
Run the script: python first_agent.py
The output would be:
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=agent1qtu6wt5jphhmdjau0hdhc002ashzjnueqe89gvvuln8mawm3m0xrwmn9a76 INFO: [alice]: Starting server on http://0.0.0.0:8000 (Press CTRL+C to quit) INFO: [alice]: Hello, I'm agent alice and my address is agent1qtu6wt5jphhmdjau0hdhc002ashzjnueqe89gvvuln8mawm3m0xrwmn9a76.
Congratulations, you have just created your first Agent!