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

fix(docs): added dir structure in cleaning and booking demo #1080

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
102 changes: 89 additions & 13 deletions pages/examples/intermediate/agents-cleaning-demo.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { CodeGroup, DocsCode } from "../../../components/code"
## Introduction

This file can be run on any platform supporting Python, with the necessary install permissions.
This example shows how to set up a cleaning service using Agents and uAgents library tools.
This example shows how to set up a cleaning service using the uAgents library tools.

### Supporting documentation
## Supporting documentation

- [Creating an agent ↗️](/guides/agents/create-a-uagent)
- [Creating an interval task ↗️](/guides/agents/interval-task)
Expand All @@ -18,6 +18,28 @@ This example shows how to set up a cleaning service using Agents and uAgents lib
- [Agents address ↗️](/guides/agents/getting-started/getting-uagent-address)
- [How to use agents to send tokens ↗️](/guides/agents/intermediate/send-tokens)

## Pre-requisites

- **Python:** Download and install from [Python official website ↗️](https://www.python.org/downloads/).
- **Poetry:** Install by following the instructions on [Poetry's official website ↗️](https://python-poetry.org/docs/#installation).

## Project Structure

Outline of basic structure of the project:

```
cleaning-demo/
.
├── protocols/
│ └── cleaning/
│ ├── __init__.py
│ └── models.py
└── agents/
├── cleaner.py
└── user.py
```

### The Protocols
#### __init__.py

Expand All @@ -44,6 +66,7 @@ from .models import Availability, Provider, User
PROTOCOL_NAME = "cleaning"
PROTOCOL_VERSION = "0.1.0"


class ServiceRequest(Model):
user: str
location: str
Expand All @@ -52,22 +75,27 @@ class ServiceRequest(Model):
services: List[int]
max_price: float


class ServiceResponse(Model):
accept: bool
price: float


class ServiceBooking(Model):
location: str
time_start: datetime
duration: timedelta
services: List[int]
price: float


class BookingResponse(Model):
success: bool


cleaning_proto = Protocol(name=PROTOCOL_NAME, version=PROTOCOL_VERSION)


def in_service_region(
location: str, availability: Availability, provider: Provider
) -> bool:
Expand All @@ -90,9 +118,10 @@ def in_service_region(

return in_range


@cleaning_proto.on_message(model=ServiceRequest, replies=ServiceResponse)
async def handle_query_request(ctx: Context, sender: str, msg: ServiceRequest):
provider = await Provider.filter(name=ctx.name).first()
provider = await Provider.filter(name=ctx.agent.name).first()
availability = await Availability.get(provider=provider)
services = [int(service.type) for service in await provider.services]
markup = provider.markup
Expand All @@ -102,11 +131,11 @@ async def handle_query_request(ctx: Context, sender: str, msg: ServiceRequest):
ctx.logger.info(f"Received service request from user `{user.name}`")

if (
set(msg.services) <= set(services)
and in_service_region(msg.location, availability, provider)
and availability.time_start <= msg.time_start
and availability.time_end >= msg.time_start + msg.duration
and availability.min_hourly_price * msg_duration_hours < msg.max_price
set(msg.services) <= set(services)
and in_service_region(msg.location, availability, provider)
and availability.time_start <= msg.time_start
and availability.time_end >= msg.time_start + msg.duration
and availability.min_hourly_price * msg_duration_hours < msg.max_price
):
accept = True
price = markup * availability.min_hourly_price * msg_duration_hours
Expand All @@ -118,9 +147,10 @@ async def handle_query_request(ctx: Context, sender: str, msg: ServiceRequest):

await ctx.send(sender, ServiceResponse(accept=accept, price=price))


@cleaning_proto.on_message(model=ServiceBooking, replies=BookingResponse)
async def handle_book_request(ctx: Context, sender: str, msg: ServiceBooking):
provider = await Provider.filter(name=ctx.name).first()
provider = await Provider.filter(name=ctx.agent.name).first()
availability = await Availability.get(provider=provider)
services = [int(service.type) for service in await provider.services]

Expand All @@ -129,10 +159,10 @@ async def handle_book_request(ctx: Context, sender: str, msg: ServiceBooking):
ctx.logger.info(f"Received booking request from user `{user.name}`")

success = (
set(msg.services) <= set(services)
and availability.time_start <= msg.time_start
and availability.time_end >= msg.time_start + msg.duration
and msg.price <= availability.min_hourly_price * msg_duration_hours
set(msg.services) <= set(services)
and availability.time_start <= msg.time_start
and availability.time_end >= msg.time_start + msg.duration
and msg.price <= availability.min_hourly_price * msg_duration_hours
)

if success:
Expand Down Expand Up @@ -342,3 +372,49 @@ if __name__ == "__main__":
```
</DocsCode>
</CodeGroup>

### Poetry Dependencies

```pyproject.toml copy filename="pyproject.toml"
[tool.poetry.dependencies]
python = ">=3.9,<3.13"
uagents = "^0.17.1"
tortoise-orm = "^0.22.1"
geopy = "^2.4.1"
```

#### Instructions to execute the example.

- Navigate to the root folder of the example.
- Install dependencies by running `poetry install`.
- Run the cleaner agent with `python cleaner.py`.
- Run the user agent with `python user.py`.

### Expected Output

- Cleaner Agent

```
INFO: [cleaner]: Registration on Almanac API successful
INFO: [cleaner]: Registering on almanac contract...
INFO: [cleaner]: Registering on almanac contract...complete
INFO: [cleaner]: Agent inspector available at https://agentverse.ai/inspect/?uri=http%3A//127.0.0.1%3A8001&address=agent1qdfdx6952trs028fxyug7elgcktam9f896ays6u9art4uaf75hwy2j9m87w
INFO: [cleaner]: Starting server on http://0.0.0.0:8001 (Press CTRL+C to quit)
INFO: [cleaner]: Received service request from user `user`
INFO: [cleaner]: I am available! Proposing price: 22.0.
INFO: [cleaner]: Received booking request from user `user`
INFO: [cleaner]: Accepted task and updated availability.
```

- User Agent:

```
INFO: [ user]: Registration on Almanac API successful
INFO: [ user]: Registering on almanac contract...
INFO: [ user]: Registering on almanac contract...complete
INFO: [ user]: Agent inspector available at https://agentverse.ai/inspect/?uri=http%3A//127.0.0.1%3A8000&address=agent1qvrskj36y7urk2j9g4gu5hjgwvgr8v6jegm5druawmrpztmjjnep6ssn45p
INFO: [ user]: Starting server on http://0.0.0.0:8000 (Press CTRL+C to quit)
INFO: [ user]: Requesting cleaning service: user='user' location='London Kings Cross' time_start=datetime.datetime(2023, 4, 10, 16, 0, tzinfo=<UTC>) duration=datetime.timedelta(seconds=14400) services=[<ServiceType.WINDOW: 2>, <ServiceType.LAUNDRY: 3>] max_price=60.0
INFO: [ user]: Cleaner is available, attempting to book now
INFO: [ user]: Booking was successful
```
22 changes: 21 additions & 1 deletion pages/examples/intermediate/table-booking-demo.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { CodeGroup, DocsCode } from "../../../components/code"
This file can be run on any platform supporting Python, with the necessary install permissions.
This example shows how to use Agents to create a table booking service at a restaurant.

### Supporting documentation
## Supporting documentation

- [Creating an agent ↗️](/guides/agents/create-a-uagent)
- [Creating an interval task ↗️](/guides/agents/interval-task)
Expand All @@ -17,6 +17,22 @@ This example shows how to use Agents to create a table booking service at a rest
- [Protocols ↗️](/references/uagents/uagents-protocols/agent-protocols)
- [Agents address ↗️](/guides/agents/getting-started/getting-uagent-address)

## Project Structure

Outline of basic structure of the project:

```
table-booking-demo/
.
├── protocols/
│ ├── query.py
│ └── book.py
└── agents/
├── restaurant.py
└── user.py
```

### The Protocols

#### Query table
Expand Down Expand Up @@ -416,17 +432,21 @@ This example shows how to use Agents to create a table booking service at a rest
- Restaurant:

```
INFO: [restaurant]: Registration on Almanac API successful
INFO: [restaurant]: Registering on almanac contract...
INFO: [restaurant]: Registering on almanac contract...complete
INFO: [cleaner]: Agent inspector available at https://agentverse.ai/inspect/?uri=http%3A//127.0.0.1%3A8001&address=agent1qdfdx6952trs028fxyug7elgcktam9f896ayu9art4uaf75hwy2j9m87w
INFO: [restaurant]: Starting server on http://0.0.0.0:8001 (Press CTRL+C to quit)
INFO: [restaurant]: Query: guests=3 time_start=19 duration=2. Available tables: [2].
```

- User:

```
INFO: [ user]: Registration on Almanac API successful
INFO: [ user]: Registering on almanac contract...
INFO: [ user]: Registering on almanac contract...complete
INFO: [ user]: Agent inspector available at https://agentverse.ai/inspect/?uri=http%3A//127.0.0.1%3A8000&address=agent1qvrskj36y7urk2j9g4gujgwvgr8v6jegm5druawmrpztmjjnep6ssn45p
INFO: [ user]: Starting server on http://0.0.0.0:8000 (Press CTRL+C to quit)
INFO: [ user]: There is a free table, attempting to book one now
INFO: [ user]: Table reservation was successful
Expand Down
Loading