跳转到正文
文档导航: 决策模型 / 请求策略

请求策略

RightNow AI决策模型请求策略 更新于

本页内容

向 RightNow decision model 的决定请求添加可选的 policy 对象。低于置信度阈值的回答不带决定返回,可能的最大费用超过上限的请求会在任何工作开始前被拒绝。

decide.ts
import { RightNowClient } from '@rightnowai/rightnow';

const client = new RightNowClient({
  apiKey: process.env.RIGHTNOW_API_KEY,
  baseURL: "https://api.rightnowai.co/v1",
});
const reply = await client.decide({
  "model": "rightnow-decision",
  "state": "The blender I ordered arrived with a cracked jar. I need it working before Saturday.",
  "questions": {
    "damaged_in_transit": {
      "type": "boolean",
      "instructions": "Did the item arrive damaged?"
    },
    "team": {
      "type": "choice",
      "instructions": "Which team should handle this ticket?",
      "criteria": {
        "replacements": "Ship a new item or part",
        "refunds": "Return money to the customer",
        "support": null
      }
    },
    "urgency": {
      "type": "score",
      "instructions": "How urgent is this ticket?",
      "criteria": [
        "Routine",
        "Priority",
        "Immediate"
      ]
    }
  },
  "policy": {
    "min_confidence": 0.8,
    "on_low_confidence": "abstain",
    "max_cost_usd": 0.29
  }
});
console.log(JSON.stringify(reply, null, 2));

// Set RIGHTNOW_API_KEY to your API key before running this example.
// Run this example on your server.
// SDK defaults: The default is 5 retries, also the maximum, with 150 seconds
// per attempt and a 60-second total retry budget. A single wait longer than 60
// seconds raises the last error.
// Use Retry-After-Ms, or Retry-After when milliseconds are absent. The wait is
// never shorter than a random backoff floor: a random fraction of 0.5 seconds
// doubled for each attempt, capped at 8 seconds.

以 rnsk_ 开头的密钥通过普通 HTTP 调用决定路径。

API keys 页面上的密钥无法使用请求策略。

策略字段

min_confidence
可选。一个大于 0 且不超过 1 的有限数。confidence 低于此值的回答会弃权,相等的置信度通过。
on_low_confidence
可选。只接受 abstain。省略该值也会弃权。
max_cost_usd
可选。以美元计的上限,大于 0 且不超过 1,000,最多八位小数。

每个成员都是可选的,空对象也有效。在决定请求或流的启动请求中发送 policy。由网关应用,模型不会收到它。

on_low_confidence: "error" 和 require_calibrated 不可用。设置了其中任何一个的请求,包括 require_calibrated: false,都会返回 400 invalid_request。

已发布的 SDK 0.5.1 在决定请求和流启动中接受策略。TypeScript 声明 PolicyDecideRequest,并以 DecideRequest 导出,带有类型为 Policy 的可选 policy 成员。在 TypeScript 中用 decisionsFor(reply, request) 读取回复,在 Python 中用 decisions_for(reply, request)。弃权结果带有 kind: "abstained"、abstention_reason,且没有 decision。读取决定前请检查 kind。Playground 运行不发送策略。

校验

无效的策略返回 400 invalid_request,类型为 invalid_request_error,并带有 error.retryable: false。null 策略、数字字符串、非有限值、错误类型和未知成员都无效。

error.param 在值不是对象时为 policy,在已知成员上为 policy.<member>,在未知成员上为 policy.*。不接受策略的决定路径会以 error.param policy 拒绝它。

上限在展开指数后最多允许八位小数,末尾的零不计:0.29、0.07 和 3e-8 有效,1e-9 无效。网关读取原始 JSON 数字,所以 JSON 解析器会舍入掉的位数仍然计入。

弃权的回答

模型回复后,网关会把每个回答的 confidence 与 min_confidence 比较。低于阈值的回答保留 confidence 及其余字段作为证据,失去 decision,并获得 abstained: true 和 abstention_reason: "low_confidence"。

通过的回答和问题拒绝保持通常的形式。成功的回复会回显你发送的 policy,不添加任何默认值。在这个示例回复中,damaged_in_transit 通过,另外两个回答弃权。

answers and policy
JSON
{
  "answers": {
    "damaged_in_transit": {
      "type": "boolean",
      "probability": 0.93,
      "confidence": 0.93,
      "calibration_version": "cal-2026-11-a",
      "decision": true,
      "alternatives": [
        {
          "decision": false,
          "confidence": 0.07
        }
      ],
      "calibrated": false
    },
    "team": {
      "type": "choice",
      "choice": "replacements",
      "probabilities": {
        "replacements": 0.71,
        "refunds": 0.21,
        "support": 0.08
      },
      "confidence": 0.71,
      "calibration_version": "cal-2026-11-b",
      "alternatives": [
        {
          "decision": "refunds",
          "confidence": 0.21
        },
        {
          "decision": "support",
          "confidence": 0.08
        }
      ],
      "calibrated": false,
      "abstained": true,
      "abstention_reason": "low_confidence"
    },
    "urgency": {
      "type": "score",
      "score": 1.48,
      "legend": {
        "0": "Routine",
        "1": "Priority",
        "2": "Immediate"
      },
      "probabilities": {
        "0": 0.07,
        "1": 0.38,
        "2": 0.55
      },
      "confidence": 0.55,
      "calibration_version": "cal-2026-11-c",
      "alternatives": [
        {
          "decision": "1",
          "confidence": 0.38
        },
        {
          "decision": "0",
          "confidence": 0.07
        }
      ],
      "calibrated": false,
      "abstained": true,
      "abstention_reason": "low_confidence"
    }
  },
  "policy": {
    "min_confidence": 0.8,
    "on_low_confidence": "abstain",
    "max_cost_usd": 0.29
  }
}

绝不要对弃权的回答采取行动。它的 choice、score、probability、probabilities 和 alternatives 是模型证据,不是决定。读取 decision 前请检查 abstained。

费用上限

在准入之前,网关会把请求可能的最大费用与 max_cost_usd 比较。它按每美元 100,000,000 微美分精确换算上限,该界限包含媒体最低额。相等的费用通过。

超出上限的请求会在任何模型工作前返回 400 policy_max_cost_exceeded。SDK 会抛出 PolicyMaxCostExceededError。它不计入每月用量,也不会被收费。界限可能超过最终费用,所以接近预期费用的上限可能会拒绝一个本会更便宜的请求。

error.json
JSON
{
  "error": {
    "message": "The request's maximum possible cost exceeds max_cost_usd. The request was not dispatched, counted toward monthly usage or charged.",
    "type": "invalid_request_error",
    "param": "policy.max_cost_usd",
    "code": "policy_max_cost_exceeded",
    "retryable": false,
    "details": {
      "policy": {
        "max_cost_usd": 0.0001
      },
      "counted": false,
      "estimated_cost_microcents": 120000,
      "max_cost_microcents": 10000
    }
  },
  "request_id": "request-id-from-response"
}
error.details.counted
false:没有计入任何用量,也没有收费。
error.details.policy
你发送的策略。
error.details.estimated_cost_microcents
请求可能的最大费用,向上取整到整数微美分。
error.details.max_cost_microcents
以微美分计的上限。

这个拒绝带有 error.retryable: false。提高 max_cost_usd 或发送更小的请求。本示例中的金额仅作说明。

用量与计费

弃权的回答与任何已回答的问题一样计费。它计入每月上限,并随请求的其他费用一起支付通常的按答案问题费。请参阅价格。

问题拒绝保持未回答且不收费。被费用上限拒绝的请求既不计入也不收费。

流式

在流中,把 policy 发送在 start.request 内。它在状态变化之间保持不变,变更操作无法更改它。策略错误以带有 fatal: false 的 error 事件到达,不会关闭流。 请参阅流式。

重放与存储的数据

策略是 Idempotency-Key 指纹的一部分。在同一密钥下更改策略会返回 400 idempotency_key_reuse。

带密钥的重放会返回原始结果,包括弃权,不产生新的模型工作、计费或用量。请参阅幂等性。

存储的回复与其他回复一样,在相同的 24 小时回答窗口和清除限制下保留其策略和弃权。用量记录只保留弃权问题的数量,不含策略、阈值、问题名称、原因或回答。