跳转到内容
本站为社区维护的非官方中文翻译,与 Cloudflare, Inc. 无关联。本站不提供账户登录,请勿在任何非官方站点输入您的 Cloudflare 账户信息。官方文档:developers.cloudflare.com

Visual Studio Code

最后更新 查看 MarkdownAgent 设置

Visual Studio Code ↗ 支持为聊天使用自定义端点模型。将自定义端点指向受 Cloudflare Access 保护的 AI Gateway 自定义域。Visual Studio Code 使用存储在其密钥存储中的 Access 服务令牌进行身份验证。

此集成使用静态凭据。Visual Studio Code 无法使用 cloudflared 生成短期 Access 令牌。

前提条件

开始之前,你需要:

  1. 在 Visual Studio Code 中打开命令面板,运行 Chat: Manage Language Models。

  2. 选择 Add Models > Custom Endpoint。

  3. 输入组名称,例如 AI Gateway。

  4. 输入显示名称和 API 密钥。对于 API 密钥,使用以下单标头服务令牌值。将 <CLIENT_ID> 和 <CLIENT_SECRET> 替换为你的 Access 服务令牌值。

    {"cf-access-client-id":"<CLIENT_ID>","cf-access-client-secret":"<CLIENT_SECRET>"}
  5. 将 API 类型选为 Chat Completions。

  6. 在打开的 chatLanguageModels.json 文件中配置模型。将 ai.example.com 替换为你的 AI Gateway 自定义域。将模型 ID 和 token 限制替换为你的模型所支持的值。

    chatLanguageModels.jsonjson
    [
      {
        "name": "AI Gateway",
        "vendor": "customendpoint",
        "apiKey": "${input:cloudflareAccessServiceToken}",
        "apiType": "chat-completions",
        "models": [
          {
            "id": "openai/gpt-4.1-mini",
            "name": "GPT-4.1 mini",
            "url": "https://ai.example.com/compat/chat/completions",
            "toolCalling": true,
            "vision": true,
            "maxInputTokens": 1000000,
            "maxOutputTokens": 32768,
            "requestHeaders": {
              "Authorization": "${apiKey}"
            }
          }
        ]
      }
    ]

    Visual Studio Code 会将 ${apiKey} 替换为密钥存储中保存的服务令牌值。覆盖 Authorization 可防止 Visual Studio Code 的默认身份验证行为添加 Bearer 前缀。

  7. 保存文件,然后从聊天模型选择器中选择该模型。

  8. 发送提示。请求现在会通过 AI Gateway 路由。

有关自定义端点设置的更多信息,请参阅 AI language models in Visual Studio Code ↗。

配置 WAF 例外

使用 AI Gateway 自定义域时,WAF 规则 (Command Injection - Common Attack Commands)可能会阻止包含 shell 命令的提示。

如果该规则阻止了 Visual Studio Code 请求,请创建 WAF 例外,仅针对你的 AI Gateway 自定义域跳过此规则。请尽可能缩小例外范围。

要确认流量已到达 AI Gateway,请参阅验证是否生效。

这篇文档对您有帮助吗?