text stringlengths 0 59.1k |
|---|
}); |
}, |
}, |
}, |
}, |
}); |
``` |
Notes: |
- Requires a configured VoltOps client (keys) so feedback can be persisted. |
- `feedback.save` uses the scorer trace id by default; you can override via `traceId`. |
## useChat integration |
When you use the `/agents/:id/chat` endpoint (AI SDK useChat compatible), the assistant message includes feedback metadata under `message.metadata.feedback`. You can render a thumbs up/down UI and submit feedback to `feedback.url`. |
```ts |
import { useChat } from "@ai-sdk/react"; |
import { DefaultChatTransport } from "ai"; |
const apiUrl = "http://localhost:3141"; // your VoltAgent server base URL |
const agentId = "support-agent-id"; // from route param or app config |
const userId = "user-1"; // from your auth/session layer |
const conversationId = "conv-1"; // current conversation id from your app state |
const transport = new DefaultChatTransport({ |
api: `${apiUrl}/agents/${agentId}/chat`, |
prepareSendMessagesRequest({ messages }) { |
const lastMessage = messages[messages.length - 1]; |
return { |
body: { |
input: [lastMessage], |
options: { |
feedback: { |
key: "satisfaction", |
feedbackConfig: { |
type: "categorical", |
categories: [ |
{ value: 1, label: "Satisfied" }, |
{ value: 0, label: "Unsatisfied" }, |
], |
}, |
}, |
}, |
}, |
}; |
}, |
}); |
const { messages } = useChat({ transport }); |
const isFeedbackProvided = (feedback: any): boolean => |
Boolean(feedback?.provided || feedback?.providedAt || feedback?.feedbackId); |
const isFeedbackExpired = (feedback: any): boolean => |
typeof feedback?.expiresAt === "string" && new Date(feedback.expiresAt).getTime() <= Date.now(); |
const shouldShowFeedback = (message: any): boolean => { |
const feedback = message?.metadata?.feedback; |
if (!feedback?.url) return false; |
if (isFeedbackExpired(feedback)) return false; |
if (isFeedbackProvided(feedback)) return false; |
return true; |
}; |
// Example usage while rendering messages: |
// {messages.filter(shouldShowFeedback).map(renderFeedbackButtons)} |
async function submitFeedback(message: any, score: number) { |
const feedback = message?.metadata?.feedback; |
if (!feedback?.url || isFeedbackProvided(feedback)) return; |
const response = await fetch(feedback.url, { |
method: "POST", |
headers: { "Content-Type": "application/json" }, |
body: JSON.stringify({ |
score, |
comment: "Helpful response", |
feedback_source_type: "app", |
}), |
}); |
if (!response.ok) return; |
const { id: feedbackId } = (await response.json()) as { id?: string }; |
// Persist "already submitted" state for reloads |
await markFeedbackProvided({ |
agentId, |
userId, |
conversationId, |
messageId: message.id, |
feedbackId, // optional: feedback id returned by ingestion API |
}); |
} |
async function markFeedbackProvided(input: { |
agentId: string; |
userId: string; |
conversationId: string; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.