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

refactor(example): examples overhaul #309

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
113 changes: 113 additions & 0 deletions integrations/tortoise-ORM/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Room Booking Example

Welcome to the Room Booking example! This guide demonstrates how to create a distributed agent system where a guest can request a room booking from a hotel. This example focuses on using Tortoise ORM for database management within the agents to handle room availability and booking processes.

To prepare for running the example, ensure you're in the proper directory and have configured your environment with the necessary dependencies:



```
poetry install
poetry shell
```


## Using Tortoise ORM for Database Management

### Defining the Models

- **Description**: Models are used to define the structure of the data exchanged between agents. In this example, we have `RoomRequest` and `RoomResponse` models for the booking request and response, and an `Availability` model for managing room availability and pricing.

```
from uagents import Model
from tortoise import fields, models

class RoomRequest(Model):
max_price: int

class RoomResponse(Model):
success: bool

class Availability(models.Model):
id = fields.IntField(pk=True)
room_available = fields.BooleanField(default=True)
min_price = fields.FloatField(default=0.0)
```

### Hotel Agent with Tortoise ORM

- **Description**: The hotel agent uses Tortoise ORM to manage room availability in a SQLite database. On startup, the database is initialized, and the room availability is set. The agent processes room booking requests and updates the database accordingly.

```
from uagents import Agent, Context
from tortoise import Tortoise
from models import Availability, RoomRequest, RoomResponse

HOTEL_SEED = "put_hotel_seed_phrase_here"

hotel = Agent(
name="hotel",
port=8001,
seed=HOTEL_SEED,
endpoint=["http://127.0.0.1:8001/submit"],
)

print(f"hotel's agent address: {hotel.address}")

@hotel.on_event("startup")
async def startup(_ctx: Context):
await Tortoise.init(
db_url="sqlite://db.sqlite3", modules={"models": ["models"]}
)
await Tortoise.generate_schemas()

await Availability.create(
room_available=True,
min_price=50,
)

@hotel.on_event("shutdown")
async def shutdown(_ctx: Context):
await Tortoise.close_connections()

@hotel.on_message(model=RoomRequest)
async def message_handler(ctx: Context, sender: str, msg: RoomRequest):
availability = await Availability.first()
success = False
if availability.room_available:
ctx.logger.info(f"Room available, attempting to book")
if availability.min_price <= msg.max_price:
success = True
ctx.logger.info(f"Offer of ${msg.max_price} accepted!")
availability.room_available = False
await availability.save()
else:
ctx.logger.info(f"Offer of ${msg.max_price} was too low, won't accept")
else:
ctx.logger.info(f"Room unavailable")

await ctx.send(sender, RoomResponse(success=success))

if __name__ == "__main__":
hotel.run()
```

## Running the Example

To observe the room booking interaction between the guest and hotel agents, run:

```
python guest.py
python hotel.py
```


This command starts both agents. The guest agent periodically sends a room booking request to the hotel agent, which processes the request based on room availability and price, and responds accordingly.

## Key Points

- Ensure the guest and hotel agents are configured with appropriate seed phrases and endpoints.
- The hotel agent manages room availability using Tortoise ORM and responds to booking requests based on predefined criteria.
- The guest agent continuously sends booking requests until a successful booking is achieved.

By following this example, you can implement a simple room booking system with distributed agents, demonstrating the potential of agent-based architectures in automating tasks and interactions.
41 changes: 41 additions & 0 deletions integrations/tortoise-ORM/guest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from uagents import Agent, Context
from models import RoomRequest, RoomResponse

HOTEL_ADDRESS = "put_HOTEL_ADDRESS_here"
GUEST_SEED = "put_guest_seed_phrase_here"

guest = Agent(
name="guest",
port=8000,
seed=GUEST_SEED,
endpoint=["http://127.0.0.1:8000/submit"],
)

request = RoomRequest(max_price=70)

@guest.on_event("startup")
async def startup(ctx: Context):
ctx.storage.set("completed", False)

@guest.on_interval(period=8.0)
async def send_message(ctx: Context):

completed = ctx.storage.get("completed")

if not completed:
ctx.logger.info(f"Sending room booking request: {request}")
await ctx.send(HOTEL_ADDRESS, request)


@guest.on_message(RoomResponse, replies=set())
async def booking_handler(ctx: Context, _sender: str, msg: RoomResponse):
if msg.success:
ctx.logger.info("Booking was successful")
else:
ctx.logger.info("Booking was unsuccessful")

ctx.storage.set("completed", True)


if __name__ == "__main__":
guest.run()
54 changes: 54 additions & 0 deletions integrations/tortoise-ORM/hotel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from uagents import Agent, Context, Model
from tortoise import Tortoise
from models import Availability, RoomRequest, RoomResponse


HOTEL_SEED = "put_hotel_seed_phrase_here"

hotel = Agent(
name="hotel",
port=8001,
seed=HOTEL_SEED ,
endpoint=["http://127.0.0.1:8001/submit"],
)

print(f"hotel's agent address: {hotel.address}")

@hotel.on_event("startup")
async def startup(_ctx: Context):
await Tortoise.init(
db_url="sqlite://db.sqlite3", modules={"models": ["models"]}
)
await Tortoise.generate_schemas()

await Availability.create(
room_available=True,
min_price=50,
)


@hotel.on_event("shutdown")
async def shutdown(_ctx: Context):
await Tortoise.close_connections()

@hotel.on_message(model=RoomRequest)
async def message_handler(ctx: Context, sender: str, msg: RoomRequest):
availability = await Availability.first()
success = False
if availability.room_available:
ctx.logger.info(f"Room available, attempting to book")
if availability.min_price <= msg.max_price:
success = True
ctx.logger.info(f"Offer of ${msg.max_price} accepted!")
availability.room_available = False
await availability.save()
else:
ctx.logger.info(f"Offer of ${msg.max_price} was to low, won't accept ")
else:
ctx.logger.info(f"Room unavailable")

await ctx.send(sender, RoomResponse(success=success))


if __name__ == "__main__":
hotel.run()
14 changes: 14 additions & 0 deletions integrations/tortoise-ORM/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from uagents import Model
from tortoise import fields, models

class RoomRequest(Model):
max_price: int

class RoomResponse(Model):
success: bool

class Availability(models.Model):
id = fields.IntField(pk=True)
room_available = fields.BooleanField(default=True)
min_price = fields.FloatField(default=0.0)

Loading
Loading