-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add agent proxy example (#229)
Co-authored-by: James Riehl <[email protected]>
- Loading branch information
Showing
5 changed files
with
165 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters