Skip to content

Commit

Permalink
feat: add agent proxy example (#229)
Browse files Browse the repository at this point in the history
Co-authored-by: James Riehl <[email protected]>
  • Loading branch information
Archento and jrriehl authored Feb 18, 2024
1 parent 1eb190c commit 173e9d9
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 3 deletions.
18 changes: 18 additions & 0 deletions python/examples/16-on-query-proxy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## Example of how to query an agent using a proxy API

In separate terminals:

1. Run the FastAPI proxy:
```bash
uvicorn proxy:app
```

2. Run the agent:
```bash
python agent.py
```

3. Query the agent via the proxy:
```bash
curl -d '{"message": "test"}' -H "Content-Type: application/json" -X POST http://localhost:8000/endpoint
```
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"
74 changes: 72 additions & 2 deletions python/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ fetchai-babble = {version = "^0.4.0", optional = true}
tortoise-orm = {version = "^0.19.2", optional = true}
geopy = {version = "^2.3.0", optional = true}
pyngrok = {version = "^5.2.3", optional = true}
fastapi = {version = "^0.97.0", optional = true}

[tool.poetry.dev-dependencies]
aioresponses = "^0.7.4"
Expand All @@ -34,11 +35,12 @@ mkdocs-material = "^9.1.13"


[tool.poetry.extras]
all = ["tortoise-orm", "geopy", "fetchai-babble", "pyngrok"]
all = ["tortoise-orm", "geopy", "fetchai-babble", "pyngrok", "fastapi"]
wallet = ["fetchai-babble"]
orm = ["tortoise-orm"]
geo = ["geopy"]
remote-agents = ["pyngrok"]
proxy = ["fastapi"]

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down

0 comments on commit 173e9d9

Please sign in to comment.