跳至主要內容

回饋

新增於:@mastra/core@1.18.0

回饋 API 可儲存及查詢人工介入的可觀測性 signal,例如評分、拇指評價、留言與修正。如需使用模式,請參閱回饋指南

使用範例
「使用範例」的直接連結

以下範例透過可觀測性進入點,為已持久化的 trace 記錄評分。addFeedback() 在此進入點上為選用,因此呼叫前請先檢查作用中的可觀測性實作是否支援。

if (!mastra.observability.addFeedback) {
throw new Error('Feedback is not supported by the active observability implementation')
}

await mastra.observability.addFeedback({
traceId: 'trace-123',
spanId: 'span-456',
feedback: {
feedbackSource: 'user',
feedbackType: 'rating',
value: 1,
comment: 'Helpful answer.',
},
})

建立回饋
「建立回饋」的直接連結

addFeedback(args)
「addfeedbackargs」的直接連結

透過可觀測性進入點,將回饋新增至已持久化的 trace 或 span。

await mastra.observability.addFeedback?.({
traceId: 'trace-123',
spanId: 'span-456',
feedback: {
feedbackSource: 'user',
feedbackType: 'rating',
value: 1,
comment: 'Helpful answer.',
},
})

traceId?:

string
作為回饋目標定位依據的 trace。

spanId?:

string
作為回饋目標定位依據的 span。

correlationContext?:

CorrelationContext
用於產生事件的即時 span 或 trace context,不必從儲存空間重新載入目標。

feedback:

FeedbackInput
要新增的回饋 payload。

createFeedback(args)
「createfeedbackargs」的直接連結

透過可觀測性 storage domain 建立一筆回饋記錄。儲存層級呼叫會直接寫入儲存區,因此請包含 timestamp

await observability.createFeedback({
feedback: {
feedbackId: 'feedback-1',
timestamp: new Date(),
traceId: 'trace-123',
spanId: 'span-456',
feedbackSource: 'user',
feedbackType: 'rating',
value: 1,
comment: 'Helpful answer.',
},
})

HTTP 與 client SDK 的建立 route 接受 CreateFeedbackBody,並由 server 端設定 timestamp。省略 feedbackId 時會自動產生:

await mastraClient.createFeedback({
feedback: {
traceId: 'trace-123',
spanId: 'span-456',
feedbackSource: 'user',
feedbackType: 'rating',
value: 1,
},
})

batchCreateFeedback(args)
「batchcreatefeedbackargs」的直接連結

透過可觀測性 storage domain 建立多筆回饋記錄。HTTP route 與 @mastra/client-js 不會公開此方法。

await observability.batchCreateFeedback({
feedbacks: [
{
feedbackId: 'feedback-1',
timestamp: new Date(),
traceId: 'trace-123',
feedbackSource: 'user',
feedbackType: 'rating',
value: 1,
},
{
feedbackId: 'feedback-2',
timestamp: new Date(),
traceId: 'trace-123',
feedbackSource: 'qa',
feedbackType: 'comment',
value: 'Needs a citation before shipping.',
},
],
})

列出回饋
「列出回饋」的直接連結

listFeedback(args?)
「listfeedbackargs」的直接連結

以分頁模式或 delta 模式傳回回饋記錄。

const response = await mastraClient.listFeedback({
filters: {
feedbackType: 'rating',
feedbackSource: 'studio',
},
pagination: { page: 0, perPage: 20 },
orderBy: { field: 'timestamp', direction: 'DESC' },
})

mode?:

'page' | 'delta'
列出模式。預設為 'page'。

filters?:

FeedbackFilter
回饋記錄的 filter。

orderBy?:

{ field?: 'timestamp'; direction?: 'ASC' | 'DESC' }
分頁模式的排序設定。

after?:

string
用於增量輪詢的 delta cursor。只適用於 delta 模式。

limit?:

number
Delta 模式中傳回的更新數量上限。

OLAP 查詢
「OLAP 查詢」的直接連結

OLAP 回饋查詢會針對數值型 value 欄位運作。

getFeedbackAggregate(args)
「getfeedbackaggregateargs」的直接連結

傳回單一彙總回饋值。

const response = await mastraClient.getFeedbackAggregate({
feedbackType: 'rating',
feedbackSource: 'user',
aggregation: 'avg',
comparePeriod: 'previous_day',
})

feedbackType:

string
要彙總的回饋類型。

feedbackSource?:

string
要彙總的回饋來源。

aggregation:

'sum' | 'avg' | 'min' | 'max' | 'count' | 'count_distinct' | 'last'
要套用的彙總方式。

filters?:

FeedbackFilter
其他 filter。

comparePeriod?:

'previous_period' | 'previous_day' | 'previous_week'
選用的逐期比較。

getFeedbackBreakdown(args)
「getfeedbackbreakdownargs」的直接連結

傳回依維度分組的回饋值。

const response = await mastraClient.getFeedbackBreakdown({
feedbackType: 'rating',
groupBy: ['entityName'],
aggregation: 'avg',
})

getFeedbackTimeSeries(args)
「getfeedbacktimeseriesargs」的直接連結

傳回依時間間隔分桶的回饋值。

const response = await mastraClient.getFeedbackTimeSeries({
feedbackType: 'rating',
interval: '1h',
aggregation: 'avg',
groupBy: ['feedbackSource'],
})

getFeedbackPercentiles(args)
「getfeedbackpercentilesargs」的直接連結

傳回依時間間隔分桶的百分位數值。

const response = await mastraClient.getFeedbackPercentiles({
feedbackType: 'rating',
percentiles: [0.5, 0.95],
interval: '1d',
})

型別
「型別」的直接連結

FeedbackRecord
「feedbackrecord」的直接連結

feedbackId?:

string | null
此回饋事件的唯一 ID。省略時,由 server route 產生。

timestamp:

Date
記錄回饋的時間。

traceId?:

string | null
可用時,作為回饋目標定位依據的 trace。

spanId?:

string | null
可用時,作為回饋目標定位依據的 span。

feedbackSource?:

string | null
選用的來源 metadata,例如 'user'、'qa'、'studio' 或 'system'。

source?:

string | null
feedbackSource 的已淘汰 alias。

feedbackType:

string
回饋類型,例如 'rating'、'thumbs'、'comment' 或 'correction'。

value:

number | string
回饋值。數值型值支援彙總、細分、時間序列與百分位數查詢。

comment?:

string | null
回饋的其他留言或 context。

feedbackUserId?:

string | null
提供回饋的使用者。

sourceId?:

string | null
此回饋連結之來源記錄的 ID,例如實驗結果 ID。

metadata?:

Record<string, unknown> | null
回饋記錄的使用者自訂 metadata。

共用 context 欄位
「共用 context 欄位」的直接連結

回饋記錄可包含共用的可觀測性 context 欄位,用於篩選、分組,以及與 trace、log、指標和分數建立關聯。

entityType?:

EntityType | null
產生 signal 的實體類型。

entityId?:

string | null
產生 signal 之實體的 ID。

entityName?:

string | null
產生 signal 之實體的名稱。

parentEntityType?:

EntityType | null
父實體的實體類型。

parentEntityId?:

string | null
父實體的 ID。

parentEntityName?:

string | null
父實體的名稱。

rootEntityType?:

EntityType | null
根實體的實體類型。

rootEntityId?:

string | null
根實體的 ID。

rootEntityName?:

string | null
根實體的名稱。

userId?:

string | null
觸發執行的人類終端使用者。

organizationId?:

string | null
多租戶組織或帳戶。

resourceId?:

string | null
較廣泛的資源 context。

runId?:

string | null
執行 run 識別碼。

sessionId?:

string | null
用於將 trace 分組的 Session 識別碼。

threadId?:

string | null
對話 thread 識別碼。

requestId?:

string | null
用於關聯的 HTTP request ID。

environment?:

string | null
部署環境。

serviceName?:

string | null
服務名稱。

scope?:

Record<string, unknown> | null
Package、應用程式版本或部署 metadata。

entityVersionId?:

string | null
產生 signal 之實體的版本 ID。

parentEntityVersionId?:

string | null
父實體的版本 ID。

rootEntityVersionId?:

string | null
根實體的版本 ID。

experimentId?:

string | null
實驗或 eval run 識別碼。

executionSource?:

string | null
執行來源,例如本機、cloud 或 CI。

tags?:

string[] | null
用於篩選的標籤。

FeedbackInput
「feedbackinput」的直接連結

請搭配 mastra.observability.addFeedback()recordedTrace.addFeedback()recordedSpan.addFeedback() 使用 FeedbackInput

feedbackSource?:

string
回饋的選用來源 metadata。

source?:

string
feedbackSource 的已淘汰 alias。

feedbackType:

string
要記錄的回饋類型。

value:

number | string
要記錄的回饋值。

comment?:

string
其他留言或 context。

feedbackUserId?:

string
提供回饋的使用者。

userId?:

string
feedbackUserId 的已淘汰 alias。

metadata?:

Record<string, unknown>
其他回饋專屬 metadata。

experimentId?:

string
實驗或 eval run 識別碼。

sourceId?:

string
此回饋連結之來源記錄的 ID。

FeedbackFilter
「feedbackfilter」的直接連結

請在 listFeedback() 與 OLAP 查詢的 filters 中使用 FeedbackFilter

timestamp?:

{ start?: Date; end?: Date; startExclusive?: boolean; endExclusive?: boolean }
依時間戳記範圍篩選。

traceId?:

string
依 trace ID 篩選。

spanId?:

string
依 span ID 篩選。

feedbackType?:

string | string[]
依一或多個回饋類型篩選。

feedbackSource?:

string
依回饋來源篩選。

source?:

string
feedbackSource 的已淘汰 alias。

feedbackUserId?:

string
依提供回饋的使用者篩選。

entityType?:

EntityType
依實體類型篩選。

entityName?:

string
依實體名稱篩選。

entityVersionId?:

string
依實體版本 ID 篩選。

parentEntityType?:

EntityType
依父實體類型篩選。

parentEntityName?:

string
依父實體名稱篩選。

parentEntityVersionId?:

string
依父實體版本 ID 篩選。

rootEntityType?:

EntityType
依根實體類型篩選。

rootEntityName?:

string
依根實體名稱篩選。

rootEntityVersionId?:

string
依根實體版本 ID 篩選。

userId?:

string
依人類終端使用者 ID 篩選。

organizationId?:

string
依組織 ID 篩選。

resourceId?:

string
依資源 ID 篩選。

runId?:

string
依 run ID 篩選。

sessionId?:

string
依 session ID 篩選。

threadId?:

string
依 thread ID 篩選。

requestId?:

string
依 request ID 篩選。

serviceName?:

string
依服務名稱篩選。

environment?:

string
依環境篩選。

executionSource?:

string
依執行來源篩選。

experimentId?:

string
依實驗或 eval run 識別碼篩選。

tags?:

string[]
依標籤篩選。相符記錄必須包含所有指定標籤。

HTTP route
「HTTP route」的直接連結

方法路徑用途權限
GET/api/observability/feedback列出回饋記錄無衍生權限
POST/api/observability/feedback建立回饋記錄無衍生權限
POST/api/observability/feedback/aggregate傳回單一彙總值observability:read
POST/api/observability/feedback/breakdown依維度將回饋分組observability:read
POST/api/observability/feedback/timeseries依時間間隔將回饋分桶observability:read
POST/api/observability/feedback/percentiles傳回百分位數序列observability:read