Skip to content

[EXAMPLES][DOCS] review basic webhooks example #5710

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

Merged
merged 5 commits into from
Nov 27, 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
36 changes: 30 additions & 6 deletions examples/webhooks/basic-webhooks/README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,44 @@
## Description

This is a basic webhook example to show how to setup webhook listeners using the argilla SDK
This is a basic webhook example to show how to configure webhook listeners using the argilla SDK

The application defines three webhook listeners for the following events:

- Record events: `record.deleted`, `record.completed`
- Dataset events: `dataset.created`, `dataset.updated`, `dataset.published`, `dataset.deleted`
- Response events: `response.created`, `response.updated`

You can visit the [Argilla documentation](https://docs.argilla.io/dev/how_to_guides/webhooks) for more information.

## Running the app

1. Start argilla server and argilla worker
This example is intended to be used locally. You can check [this space](https://huggingface.co/spaces/argilla/argilla-webhooks)
for a remote example.

First, you must start the argilla server. We recommend you to use the docker installation. You can run the following commands to start the argilla server:
```bash
pdm server start
pdm worker
mkdir argilla && cd argilla
curl https://raw.githubusercontent.com/argilla-io/argilla/main/examples/deployments/docker/docker-compose.yaml -o docker-compose.yaml
docker compose up -d
```

2. Start the app
For more information on how to install the argilla server, please refer to the [argilla documentation](https://docs.argilla.io/latest/getting_started).

Once the argilla server is up and running, start the webhook server by running the following command:

```bash
ARGILLA_API_KEY=argilla.apikey \
WEBHOOK_SERVER_URL=http://host.docker.internal:8000 \
uvicorn main:server
```

The `ARGILLA_API_KEY` environment variable should be set to the API key of the argilla server.
The `WEBHOOK_SERVER_URL` environment variable should be set to the URL where the webhook server is running.
In this case, we are using `http://host.docker.internal:8000` because the webhook calls will be done inside a docker container.

The application will remove all existing webhook listeners and create new ones for the events mentioned above.

## Testing the app

You can see in se server logs traces when working with dataset, records and responses in the argilla server
When you start working with the argilla server, you can see the logs in the webhook server.
You can test the webhook listeners by creating, updating, and deleting datasets, responses and records in the argilla server.
52 changes: 19 additions & 33 deletions examples/webhooks/basic-webhooks/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

# Show the existing webhooks in the argilla server
for webhook in client.webhooks:
print(webhook.url)
print("Deleting webhook with url", webhook.url)
webhook.delete()


# Create a webhook listener using the decorator
Expand All @@ -31,46 +32,31 @@
# Related resources will be passed as keyword arguments to the decorated function
# (for example the dataset for a record-related event, or the record for a response-related event)
# When a resource is deleted
@rg.webhook_listener(events=["record.created", "record.completed"])
async def listen_record(
record: rg.Record, dataset: rg.Dataset, type: str, timestamp: datetime
):
print(f"Received record event of type {type} at {timestamp}")
@rg.webhook_listener(events=["record.deleted", "record.completed"])
async def records_listener(record: rg.Record, type: str, timestamp: datetime):
print(f"Received event of type {type} at {timestamp} for record {record}")

action = "completed" if type == "record.completed" else "created"
print(f"A record with id {record.id} has been {action} for dataset {dataset.name}!")

@rg.webhook_listener(events=["response.created", "response.updated"])
async def responses_listener(response: rg.UserResponse, type: str, timestamp: datetime):
print(f"Received event of type {type} at {timestamp} for response {response}")

@rg.webhook_listener(events="response.updated")
async def trigger_something_on_response_updated(response: rg.UserResponse, **kwargs):
print(
f"The user response {response.id} has been updated with the following responses:"
)
print([response.serialize() for response in response.responses])


@rg.webhook_listener(events=["dataset.created", "dataset.updated", "dataset.published"])
async def with_raw_payload(
type: str,
timestamp: datetime,
dataset: rg.Dataset,
**kwargs,
):
print(f"Event type {type} at {timestamp}")
print(dataset.settings)


@rg.webhook_listener(events="dataset.deleted")
async def on_dataset_deleted(
data: dict,
**kwargs,
):
print(f"Dataset {data} has been deleted!")
@rg.webhook_listener(
events=[
"dataset.created",
"dataset.updated",
"dataset.published",
"dataset.deleted",
]
)
async def datasets_listener(type: str, timestamp: datetime, dataset: rg.Dataset):
print(f"Received event of type {type} at {timestamp} for dataset {dataset}")


# Set the webhook server. The server is a FastAPI instance, so you need to expose it in order to run it using uvicorn:
# ```bash
# uvicorn main:webhook_server --reload
# uvicorn main:server --reload
# ```

server = rg.get_webhook_server()
2 changes: 1 addition & 1 deletion examples/webhooks/basic-webhooks/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
argilla @ git+https://github.com/argilla-io/argilla.git@feat/argilla/working-with-webhooks#subdirectory=argilla
fastapi
uvicorn[standard]
argilla ~= 2.5.0