Add Human Feedback using API
This guide will walk you through the steps of adding human feedback to an AI Gateway request using the Cloudflare API. You will learn how to retrieve the relevant request logs, and submit feedback using the API.
If you prefer to add human feedback via the dashboard, refer to Add Human Feedback.
- Create an API token with the following permissions:
AI Gateway - Read
AI Gateway - Edit
- Get your Account ID.
- Using that API token and Account ID, send a
POST
request to the Cloudflare API.
Once you have the token, you can use it in API requests by adding it to the authorization header as a bearer token. Here is an example of how to use it in a request:
In the request above:
- Replace
{account_id}
and{gateway_id}
with your specific Cloudflare account and gateway details. - Replace
{your_api_token}
with the API token you just created.
The cf-aig-log-id
is a unique identifier for the specific log entry to which you want to add feedback. Below are two methods to obtain this identifier.
This method allows you to directly find the cf-aig-log-id
within the header of the response returned by the AI Gateway. This is the most straightforward approach if you have access to the original API response.
The steps below outline how to do this.
- Make a Request to the AI Gateway: This could be a request your application sends to the AI Gateway. Once the request is made, the response will contain various pieces of metadata.
- Check the Response Headers: The response will include a header named
cf-aig-log-id
. This is the identifier you will need to submit feedback.
In the example below, the cf-aig-log-id
is 01JADMCQQQBWH3NXZ5GCRN98DP
.
If you don’t have the cf-aig-log-id
in the response body or you need to access it after the fact, you can retrieve it by querying the logs using the Cloudflare API.
The steps below outline how to do this.
- Send a GET Request to Retrieve Logs: You can query the AI Gateway logs for a specific time frame or for a specific request. The request will return a list of logs, each containing its own
id
. Here is an example request:
Replace {account_id}
and {gateway_id}
with your specific account and gateway details.
- Search for the Relevant Log: In the response from the GET request, locate the specific log entry for which you would like to submit feedback. Each log entry will include the
id
.
In the example below, the id
is 01JADMCQQQBWH3NXZ5GCRN98DP
.
You can also retrieve the cf-aig-log-id
using a binding, which streamlines the process. Here’s how to retrieve the log ID directly:
Once you have both the API token and the cf-aig-log-id
, you can send a PATCH request to submit feedback. Use the following URL format, replacing the {account_id}
, {gateway_id}
, and {log_id}
with your specific details:
Add the following in the request body to submit positive feedback:
Add the following in the request body to submit negative feedback:
You can verify the feedback submission in two ways:
- Through the Cloudflare dashboard ↗: check the updated feedback on the AI Gateway interface.
- Through the API: Send another GET request to retrieve the updated log entry and confirm the feedback has been recorded.