Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add agent proxy example #229

Merged
merged 4 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions python/examples/16-on-query-proxy/agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from uagents import Agent, Context, Model


class TestRequest(Model):
message: str


class Response(Model):
text: str


agent = Agent(
name="your_agent_name_here",
seed="your_agent_seed_here",
port=8001,
endpoint="http://localhost:8001/submit",
)


@agent.on_event("startup")
async def startup(ctx: Context):
ctx.logger.info(f"Starting up {agent.name}")
ctx.logger.info(f"With address: {agent.address}")
ctx.logger.info(f"And wallet address: {agent.wallet.address()}")


@agent.on_query(model=TestRequest, replies={Response})
async def query_handler(ctx: Context, sender: str, _query: TestRequest):
ctx.logger.info("Query received")
try:
# do something here
await ctx.send(sender, Response(text="success"))
except Exception:
await ctx.send(sender, Response(text="fail"))


if __name__ == "__main__":
agent.run()
34 changes: 34 additions & 0 deletions python/examples/16-on-query-proxy/proxy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import json

from fastapi import FastAPI
from uagents import Model
from uagents.query import query

AGENT_ADDRESS = "agent1qt6ehs6kqdgtrsduuzslqnrzwkrcn3z0cfvwsdj22s27kvatrxu8sy3vag0"


class TestRequest(Model):
message: str


async def agent_query(req):
response = await query(destination=AGENT_ADDRESS, message=req, timeout=15.0)
data = json.loads(response.decode_payload())
return data["text"]


app = FastAPI()


@app.get("/")
def read_root():
return "Hello from the Agent controller"


@app.post("/endpoint")
async def make_agent_call(req: TestRequest):
try:
res = await agent_query(req)
return f"successful call - agent response: {res}"
except Exception:
return "unsuccessful agent call"
Loading