Skip to content

Commit

Permalink
fix(docs): fix on query handler example issue (#923)
Browse files Browse the repository at this point in the history
  • Loading branch information
gautamgambhir97 authored Sep 6, 2024
1 parent b1db9b4 commit 7b1cd0e
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions pages/guides/agents/intermediate/handlers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,10 @@ Let's explore the Proxy code script step-by-step:
```py copy
import json

from fastapi import FastAPI
from fastapi import FastAPI, Request
from uagents import Model
from uagents.query import query
from uagents.envelope import Envelope

AGENT_ADDRESS = "agent1qt6ehs6kqdgtrsduuzslqnrzwkrcn3z0cfvwsdj22s27kvatrxu8sy3vag0"

Expand All @@ -340,9 +341,11 @@ Let's explore the Proxy code script step-by-step:

```py copy
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"]
response = await query(destination=AGENT_ADDRESS, message=req, timeout=15)
if isinstance(response, Envelope):
data = json.loads(response.decode_payload())
return data["text"]
return response
```

5. Initialize a `FastAPI` app:
Expand All @@ -363,9 +366,10 @@ Let's explore the Proxy code script step-by-step:

```py copy
@app.post("/endpoint")
async def make_agent_call(req: TestRequest):
async def make_agent_call(req: Request):
model = TestRequest.parse_obj(await req.json())
try:
res = await agent_query(req)
res = await agent_query(model)
return f"successful call - agent response: {res}"
except Exception:
return "unsuccessful agent call"
Expand All @@ -378,19 +382,22 @@ The overall script should look as follows:
```py copy filename="on_query.py"
import json

from fastapi import FastAPI
from fastapi import FastAPI, Request
from uagents import Model
from uagents.query import query
from uagents.envelope import Envelope

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"]
response = await query(destination=AGENT_ADDRESS, message=req, timeout=15)
if isinstance(response, Envelope):
data = json.loads(response.decode_payload())
return data["text"]
return response

app = FastAPI()

Expand All @@ -399,9 +406,10 @@ def read_root():
return "Hello from the Agent controller"

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

0 comments on commit 7b1cd0e

Please sign in to comment.