Skip to content

Commit 9d027a1

Browse files
committed
AI Gateway
1 parent 26b04ec commit 9d027a1

File tree

1 file changed

+31
-70
lines changed

1 file changed

+31
-70
lines changed

src/content/docs/ai-gateway/evaluations/add-human-feedback-api.mdx

Lines changed: 31 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ sidebar:
55
order: 4
66
---
77

8+
import { APIRequest } from "~/components";
9+
810
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.
911

1012
If you prefer to add human feedback via the dashboard, refer to [Add Human Feedback](/ai-gateway/evaluations/add-human-feedback/).
@@ -19,21 +21,7 @@ If you prefer to add human feedback via the dashboard, refer to [Add Human Feedb
1921
2. Get your [Account ID](/fundamentals/account/find-account-and-zone-ids/).
2022
3. Using that API token and Account ID, send a [`POST` request](/api/resources/ai_gateway/methods/create/) to the Cloudflare API.
2123

22-
## 2. Using the API Token
23-
24-
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:
25-
26-
```bash
27-
curl "https://api.cloudflare.com/client/v4/accounts/{account_id}/ai-gateway/gateways/{gateway_id}/logs" \
28-
--header "Authorization: Bearer {your_api_token}"
29-
```
30-
31-
In the request above:
32-
33-
- Replace `{account_id}` and `{gateway_id}` with your specific Cloudflare account and gateway details.
34-
- Replace `{your_api_token}` with the API token you just created.
35-
36-
## 3. Retrieve the `cf-aig-log-id`
24+
## 2. Retrieve the `cf-aig-log-id`
3725

3826
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.
3927

@@ -64,22 +52,14 @@ In the example below, the `cf-aig-log-id` is `01JADMCQQQBWH3NXZ5GCRN98DP`.
6452

6553
If you do not have the `cf-aig-log-id` in the response body or you need to access it after the fact, you are able to retrieve it by querying the logs using the [Cloudflare API](/api/resources/ai_gateway/subresources/logs/methods/list/).
6654

67-
The steps below outline how to do this.
68-
69-
1. **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`.
70-
Here is an example request:
71-
72-
```bash
73-
GET https://api.cloudflare.com/client/v4/accounts/{account_id}/ai-gateway/gateways/{gateway_id}/logs
74-
```
55+
Send a `GET` request to get a list of logs and then find a specific ID
7556

76-
Replace `{account_id}` and `{gateway_id}` with your specific account and gateway details.
57+
<APIRequest
58+
path="/accounts/{account_id}/ai-gateway/gateways/{gateway_id}/logs"
59+
method="GET"
60+
/>
7761

78-
2. **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`.
79-
80-
In the example below, the `id` is `01JADMCQQQBWH3NXZ5GCRN98DP`.
81-
82-
```json
62+
```json output
8363
{
8464
"result": [
8565
{
@@ -103,24 +83,7 @@ In the example below, the `id` is `01JADMCQQQBWH3NXZ5GCRN98DP`.
10383
"tokens_in": 0,
10484
"tokens_out": 0
10585
}
106-
],
107-
"result_info": {
108-
"count": 0,
109-
"max_cost": 0,
110-
"max_duration": 0,
111-
"max_tokens_in": 0,
112-
"max_tokens_out": 0,
113-
"max_total_tokens": 0,
114-
"min_cost": 0,
115-
"min_duration": 0,
116-
"min_tokens_in": 0,
117-
"min_tokens_out": 0,
118-
"min_total_tokens": 0,
119-
"page": 0,
120-
"per_page": 0,
121-
"total_count": 0
122-
},
123-
"success": true
86+
]
12487
}
12588
```
12689

@@ -129,42 +92,40 @@ In the example below, the `id` is `01JADMCQQQBWH3NXZ5GCRN98DP`.
12992
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:
13093

13194
```js
132-
const resp = await env.AI.run('@cf/meta/llama-3-8b-instruct', {
133-
prompt: 'tell me a joke'
134-
}, {
95+
const resp = await env.AI.run(
96+
"@cf/meta/llama-3-8b-instruct",
97+
{
98+
prompt: "tell me a joke",
99+
},
100+
{
135101
gateway: {
136-
id: 'my_gateway_id'
137-
}
138-
})
102+
id: "my_gateway_id",
103+
},
104+
},
105+
);
139106

140-
const myLogId = env.AI.aiGatewayLogId
107+
const myLogId = env.AI.aiGatewayLogId;
141108
```
142109

143110
:::note[Note:]
144111

145-
146112
The `aiGatewayLogId` property, will only hold the last inference call log id.
147113

148-
149114
:::
150115

151-
## 4. Submit feedback via PATCH request
116+
## 3. Submit feedback via PATCH request
152117

153-
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:
118+
Once you have both the API token and the `cf-aig-log-id`, you can send a PATCH request to submit feedback.
154119

155-
```bash
156-
PATCH https://api.cloudflare.com/client/v4/accounts/{account_id}/ai-gateway/gateways/{gateway_id}/logs/{log_id}
157-
```
158-
159-
Add the following in the request body to submit positive feedback:
160-
161-
```json
162-
{
163-
"feedback": 1
164-
}
165-
```
120+
<APIRequest
121+
path="/accounts/{account_id}/ai-gateway/gateways/{gateway_id}/logs/{id}"
122+
method="PATCH"
123+
json={{
124+
feedback: 1,
125+
}}
126+
/>
166127

167-
Add the following in the request body to submit negative feedback:
128+
If you had negative feedback, adjust the body of the request to be `-1`.
168129

169130
```json
170131
{

0 commit comments

Comments
 (0)