Skip to main content

python sdk for agentlayer

Project description

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'])

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

PyAgentlayer-0.1.0-py3-none-any.whl (28.9 kB view details)

Uploaded Python 3

File details

Details for the file PyAgentlayer-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: PyAgentlayer-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 28.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.0

File hashes

Hashes for PyAgentlayer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 779df81f4fa505d4c5686c00a96e98fa14fc7ad6a31ba68917566f9febfcc0e1
MD5 db872c915a8eabed2fa2d396bd25a19f
BLAKE2b-256 5a8239f70941a0af027adf58d798e6529d83751a29ef95273e9a1cc85789c282

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page