Skip to main content

AgentLayerSDK

AgentLayerSDK is a Python library designed to streamline Agent development.

Install

Using Pip

pip install PyAgentlayer

Usage

faucet

Get Gas Token from: Faucet

set up environment

create .env file from .env.example, and edit .env file

HELLO_WORLD_PRIVATE_KEY=#Private Key#
HELLO_WORLD_AGENT_ID=#Agent ID#

HELLO_WORLD_PRIVATE_KEY is the private key of the agent, you can get it from Metamask. HELLO_WORLD_AGENT_ID is the agent id, you need to register agent with command python demo_helloworld.py register, then you will get an agent_id, place it in .env file.

Create an agent

demo_helloworld.py

import os
from datetime import datetime
from dotenv import load_dotenv
load_dotenv()

from pyagentlayer import LAgent, Model, Context, run_agent

# 1. create agent
agent = LAgent(name="helloworld",
               description="say hello",
               http_endpoint=f"http://agent.mydomain.xyz/hello_world",
               private_key=os.environ['HELLO_WORLD_PRIVATE_KEY'],
               agent_id=os.environ['HELLO_WORLD_AGENT_ID'])


# 2. define agent's protocol , including request and response schema 
class Param(Model):
    msg: str


class Response(Model):
    code: int
    data: str


@agent.on_message("hello", Param, Response)
def hello(ctx: Context, param: Param):
    return Response(code=0, data=f"hello {param.msg}. {datetime.ctime(datetime.now())}")


# 3. run agent
if __name__ == "__main__":
    run_agent(agent)

step 1, you need to create an agent with LAgent class, and provide agent's name, description, private_key, http_endpoint, agent_id.

NOTE:

  1. the infomation of agent, such as name, description, http_endpoint is unchangable once the agent is created.
  2. the http_endpoint should be a public endpoint, which is used to interact with our agent.

step 2, define agent's protocol, including request and response schema, and define a function to handle the message.

step 3, run agent with run_agent function.

Register

Before run agent, we should register agent with command: python demo_helloworld.py register

the output should be like this:

agent_id: 1
wallet_address:0xXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
smart_wallet_address:0xXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

now we get an agent_id, place the agent_id in .env file.

Run Agent

python demo_helloworld.py

after agent running, we can get agent api info with http://localhost:8000.

Call other agent

In the process of developing our Agent, we may need to utilize Agent services provided by others. In such cases, we can conveniently integrate and test by using the API interfaces provided by AgentSDK.

How can we view the services provided by the Agent? We can directly access the endpoint of the third-party Agent in a web browser to obtain the corresponding API documentation. The documentation will comprehensively list the paths provided, along with the request parameters and responses for each path.

For example, if we call the calc method of agent 1, we can call it with the following code:

target_agent_id = 1
res = agent.send(target_agent_id, "hello", {
    "msg": "hello, i am new agent"
})

or call by curl via openapi schema

curl http://localhost:8000/hello --header 'Content-Type:application/json' --data-raw '{"msg": "hello, i am new agent"}' 

Build a paid agent

We can create a Paid Agent that requires the caller to pay for its usage, thereby generating revenue. The paid agent can be easily developed and registered using the AgentSDK, and it can also subscribe to and invoke other paid agents through the SDK. Below, we demonstrate how to create a paid agent with a piece of code.

NOTE: Currently, Paid Agents only support payment through ERC20 Token Agent.

from pyagentlayer import LAgent, SubscriptionPlan, SubscriptionPeriodEnum

# define subscription plans, currently supporting three subscription types: weekly, monthly, yearly.
subscription_plans = [
    SubscriptionPlan(period=SubscriptionPeriodEnum.WEEKLY, price_in_agent=1),   # 1 Agent weekly
    SubscriptionPlan(period=SubscriptionPeriodEnum.MONTHLY, price_in_agent=4),  # 4 Agent monthly
    SubscriptionPlan(period=SubscriptionPeriodEnum.YEARLY, price_in_agent=40),  # 40 Agent yearly
]

agent = LAgent(name="plus",
               description="demo agent for plus",
               http_endpoint="http://agent.mydomain.xyz/plus",
               payable=True,  # payment represent this is a paid agent
               subscription_plan=subscription_plans,
               private_key=os.environ['PLUS_AGENT_PRIVATE_KEY'],
               agent_id=os.environ['PLUS_AGENT_AGENT_ID'])

Release files for PyAgentlayer 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for PyAgentlayer 0.1.0
File Interpreter ABI Platform
PyAgentlayer-0.1.0-py3-none-any.whl Python 3 none any Details

Release files / PyAgentlayer-0.1.0-py3-none-any.whl

Download URL PyAgentlayer-0.1.0-py3-none-any.whl
Size 28.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
779df81f4fa505d4c5686c00a96e98fa14fc7ad6a31ba68917566f9febfcc0e1
BLAKE2b-256 checksum
How to use checksums
5a8239f70941a0af027adf58d798e6529d83751a29ef95273e9a1cc85789c282
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.0

Release history Release notifications | RSS feed

This release

0.1.0 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page