以编程方式使用文档

预签名反馈令牌让您可以收集 反馈 从客户端应用程序(浏览器、移动应用等)收集,而无需暴露您的 LangSmith API 密钥。每个令牌生成一个作用域限定为特定 运行 和反馈键的 URL。客户端通过直接调用该 URL 提交反馈,无需身份验证。

这在以下情况下很有用:

  • - Your frontend collects thumbs up/down or star ratings from end users.
  • - 您想在电子邮件、Slack 消息或其他外部渠道中嵌入反馈链接。
  • - 您需要将反馈收集与后端解耦。

创建预签名反馈令牌

使用 create_presigned_feedback_token() / createPresignedFeedbackToken 为特定的运行和反馈键生成令牌。返回的对象包含一个 url ,客户端可以调用它来提交反馈:

from langsmith import Client

client = Client()

run_id = "<run_id>"

token = client.create_presigned_feedback_token(
    run_id,
    feedback_key="user_score",
)

print(token.url)
# https://api.smith.langchain.com/api/v1/feedback/tokens/<token_id>
const client = new Client();

const runId = "<run_id>";

const token = await client.createPresignedFeedbackToken(runId, "user_score");

console.log(token.url);
// https://api.smith.langchain.com/api/v1/feedback/tokens/<token_id>

设置令牌过期时间

令牌默认在 3 小时后过期。传入 expiration 来自定义此设置,可以使用 timedelta (相对时间)或 datetime (绝对时间):

from langsmith import Client

client = Client()

run_id = "<run_id>"

token = client.create_presigned_feedback_token(
    run_id,
    feedback_key="user_score",
    expiration=datetime.timedelta(hours=24),
)
const client = new Client();

const runId = "<run_id>";

const token = await client.createPresignedFeedbackToken(runId, "user_score", {
  expiration: { hours: 24 },
});

限制反馈值

传入 feedback_config to restrict what values clients can submit. This is useful for enforcing a specific feedback schema (e.g., thumbs up/down, 1–5 stars, or categorical labels):

from langsmith import Client

client = Client()

run_id = "<run_id>"

token = client.create_presigned_feedback_token(
    run_id,
    feedback_key="user_score",
    feedback_config={
        "type": "continuous",
        "min": 0,
        "max": 1,
    },
)
const client = new Client();

const runId = "<run_id>";

const token = await client.createPresignedFeedbackToken(runId, "user_score", {
  feedbackConfig: {
    type: "continuous",
    min: 0,
    max: 1,
  },
});

批量创建令牌(仅 Python)

使用 create_presigned_feedback_tokens (复数形式)在单次调用中为多个反馈键生成令牌:

from langsmith import Client

client = Client()

run_id = "<run_id>"

tokens = client.create_presigned_feedback_tokens(
    run_id,
    feedback_keys=["thumbs_up", "thumbs_down"],
)

for token in tokens:
    print(f"{token.id}: {token.url}")

使用预签名 URL 提交反馈

获得预签名 URL 后,您的前端代码或电子邮件客户端通过向该 URL 发送 POST or GET 请求来提交反馈。该 URL 不需要 API 密钥或身份验证,因为令牌提供了授权。

POST 请求

当用户与反馈控件交互时(例如点击赞成按钮),从您的前端使用 POST 支持 POSTscore, value, comment, correctionGET 请求 metadata fields.

curl --request POST \
  --url "https://api.smith.langchain.com/api/v1/feedback/tokens/<token_id>" \
  --header "Content-Type: application/json" \
  --data '{
    "score": 1,
    "comment": "This response was helpful!"
  }'

当在电子邮件或 Slack 消息中嵌入反馈链接时使用

。用户点击会触发请求。 GET 支持 GETscore, value, comment作为查询参数。 correction 不支持与 metadata 一起使用 GET.

curl --request GET \
  --url "https://api.smith.langchain.com/api/v1/feedback/tokens/<token_id>?score=1&comment=This%20response%20was%20helpful!"

使用 SDK 提交反馈

您也可以使用 SDK 通过预签名令牌提交反馈,这对于收到来自其他服务的令牌 URL 的服务端工作流程非常有用。

from langsmith import Client

client = Client()

client.create_feedback_from_token(
    "<token_or_url>",
    score=1,
    comment="This response was helpful!",
)
// Use a direct HTTP request to the presigned URL
await fetch(tokenUrl, {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    score: 1,
    comment: "This response was helpful!",
  }),
});

列出现有令牌

使用以下方式检索运行的所有预签名反馈令牌 list_presigned_feedback_tokens / listPresignedFeedbackTokens.

from langsmith import Client

client = Client()

run_id = "<run_id>"

for token in client.list_presigned_feedback_tokens(run_id):
    print(f"ID: {token.id}, URL: {token.url}, Expires: {token.expires_at}")
const client = new Client();

const runId = "<run_id>";

for await (const token of client.listPresignedFeedbackTokens(runId)) {
  console.log(`URL: ${token.url}, Expires: ${token.expires_at}`);
}

相关