Guardrails

TsgcAIGuardrails 位于您的应用程序与语言模型之间。个人数据在请求离开应用程序之前被占位符替换,并在答案中还原;工具可按名称允许、拒绝或送去批准;结构化答案会按 JSON schema 校验。

TsgcAIGuardrails

一个组件即可提供 AI 功能上线前所需的规则。把它赋给 TsgcAIChat.Guardrails,该聊天组件的每个请求以及基于该聊天组件的智能体的每次运行,无论由哪家提供商回答,都遵循同样的规则。

组件类

TsgcAIGuardrails

用途

PII 脱敏、工具规则和答案校验

平台

Windows、macOS、Linux、iOS 和 Android

版本。TsgcAIGuardrails 随 sgcAI、sgcWebSockets Enterprise 和 All-Access 一同提供,版本范围与 TsgcAIChat 相同。2026.11.0 新增。sgcAI 是自包含的:它包含该组件所基于的 sgcWebSockets Core 运行时。适用于 Delphi 7 至 Delphi 13 和 C++ Builder。

管住个人数据和高风险工具

开启脱敏,列出允许和拒绝的工具,设置 schema,把 guardrails 关联到聊天组件。

uses
  sgcAI, sgcAI_Chat, sgcAI_Guardrails;

procedure TForm1.ProtectChat;
var
  vText, vError: string;
begin
  // personal data never leaves the application
  sgcAIGuardrails1.PII.Enabled := True;
  sgcAIGuardrails1.PII.CustomPatterns.Add('EmployeeId=EMP-####');
  // read and search tools are allowed, deleting never is
  sgcAIGuardrails1.AllowedTools.Add('read_*');
  sgcAIGuardrails1.AllowedTools.Add('search_*');
  sgcAIGuardrails1.DeniedTools.Add('*delete*');
  sgcAIGuardrails1.ToolApproval := aigaNotReadOnly;
  // structured answers must match the schema
  sgcAIGuardrails1.Schema.Enabled := True;
  sgcAIGuardrails1.Schema.JSONSchema := '{"type":"object","properties":' +
    '{"price":{"type":"number","minimum":0}},"required":["price"]}';

  sgcAIChat1.Guardrails := sgcAIGuardrails1;
  sgcAIAgent1.Chat := sgcAIChat1; // the agent follows the same tool rules

  vText := sgcAIGuardrails1.Redact('Mail bob@example.com, call +34 600 111 222');
  Memo1.Lines.Add(vText); // Mail [EMAIL_1], call [PHONE_1]
  Memo1.Lines.Add(sgcAIGuardrails1.Restore(vText)); // the original text
  if not sgcAIGuardrails1.ValidateJSON('{"price":"cheap"}',
    sgcAIGuardrails1.Schema.JSONSchema, vError) then
    Memo1.Lines.Add(vError); // $.price: expected number
end;

关键属性与方法

您最常使用的成员。

PII 脱敏

启用 PII.Enabled 后,邮箱地址、电话号码、银行卡号(经 Luhn 校验)、IBAN(经 mod 97 校验)以及 IPv4 和 IPv6 地址会在请求发出之前被 [EMAIL_1] 这样的占位符替换,并在 PII.RestoreInAnswers 开启时于答案中还原。PII.Kinds 用于选择类型。

自定义模式

PII.CustomPatterns 以 Name=Pattern 的形式添加您自己的标识符,其中 # 表示数字,@ 表示字母,? 表示任意字符,* 表示单词的其余部分,例如 EmployeeId=EMP-#### 会得到 [EMPLOYEEID_1]。

工具规则

AllowedTools 和 DeniedTools 接受带 * 通配符的工具名称,比较时不区分大小写,被拒绝的名称始终优先。被拒绝的工具不会提供给模型,如果模型仍然调用它,也会再次被拒绝。OnToolBlocked 报告每次拒绝及其原因。

批准

ToolApproval 决定哪些工具在运行之前要询问 TsgcAIAgent.OnToolApproval:aigaNone、aigaNotReadOnly(默认)或 aigaAll。

Schema 校验

启用 Schema.Enabled 后,结构化答案会按 Schema.JSONSchema 校验:type、required、properties、additionalProperties、enum、const、minimum、maximum、长度和条目数。违反规则时会触发 OnSchemaViolation,并给出 $.price: expected number 这样的路径,聊天组件会报告错误,而不是保存该答案。

方法

Redact、Restore、ValidateJSON 和 IsToolAllowed 也可以单独使用,无需聊天组件。ClearRedactions 会清除占位符映射。

规范与参考资料

该组件所用服务和协议的权威资料来源。

继续探索

在线帮助sgcAI 组件的完整 API 参考和使用指南。
所有 sgcAI 组件浏览每个 sgcAI 组件的完整功能矩阵。
下载免费试用版用您自己的 API 密钥试用。
价格Single、Team 和 Site 授权,均含完整源代码。
超值之选:All-AccesseSeGeCe 全部产品,含高级支持,每年 €1,059 起。
查看 All-Access 价格

准备好开始了吗?

下载免费试用版,在语言模型触达用户之前为它设好规则。