网关配置对象

config 对象用于配置与各种提供者的 API 交互。它支持多种模式,如单一提供者访问、提供者之间的负载均衡和回退策略。

以下 JSON 架构用于验证配置对象:

JSON 架构
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "strategy": {
      "type": "object",
      "properties": {
        "mode": {
          "type": "string",
          "enum": [
            "single",
            "loadbalance",
            "fallback"
          ]
        },
        "on_status_codes": {
          "type": "array",
          "items": {
            "type": "integer"
          },
          "optional": true
        }
      }
    },
    "provider": {
      "type": "string",
      "enum": [
        "openai",
        "anthropic",
        "azure-openai",
        "anyscale",
        "cohere",
        "palm"
      ]
    },
    "resource_name": {
      "type": "string",
      "optional": true
    },
    "deployment_id": {
      "type": "string",
      "optional": true
    },
    "api_version": {
      "type": "string",
      "optional": true
    },
    "override_params": {
      "type": "object"
    },
    "api_key": {
      "type": "string"
    },
    "virtual_key": {
      "type": "string"
    },
    "cache": {
      "type": "object",
      "properties": {
        "mode": {
          "type": "string",
          "enum": [
            "simple",
            "semantic"
          ]
        },
        "max_age": {
          "type": "integer",
          "optional": true
        }
      },
      "required": [
        "mode"
      ]
    },
    "retry": {
      "type": "object",
      "properties": {
        "attempts": {
          "type": "integer"
        },
        "on_status_codes": {
          "type": "array",
          "items": {
            "type": "number"
          },
          "optional": true
        }
      },
      "required": [
        "attempts"
      ]
    },
    "weight": {
      "type": "number"
    },
    "on_status_codes": {
      "type": "array",
      "items": {
        "type": "integer"
      }
    },
    "targets": {
      "type": "array",
      "items": {
        "$ref": "#"
      }
    }
  },
  "anyOf": [
    {
      "required": [
        "provider",
        "api_key"
      ]
    },
    {
      "required": [
        "virtual_key"
      ]
    },
    {
      "required": [
        "strategy",
        "targets"
      ]
    },
    {
      "required": [
        "cache"
      ]
    },
    {
      "required": [
        "retry"
      ]
    }
  ],
  "additionalProperties": false
}

示例配置

// Simple config with cache and retry
{
  "virtual_key": "***", // Your Virtual Key
  "cache": { // Optional
    "mode": "semantic",
    "max_age": 10000
  },
  "retry": { // Optional
    "attempts": 5,
    "on_status_codes": []
  }
}

// Load balancing with 2 OpenAI keys
{
  "strategy": {
      "mode": "loadbalance"
    },
  "targets": [
    {
      "provider": "openai",
      "api_key": "sk-***"
    },
    {
      "provider": "openai",
      "api_key": "sk-***"
    }
  ]
}

您可以在 下面 找到更多模式的示例。

Schema Details

键名
描述
类型
是否必需
枚举值
附加信息

strategy

配置或任何单独目标的操作策略

object

是(如果没有 providervirtual_key

-

查看策略对象详情

provider

服务提供商的名称

string

是(如果没有 modevirtual_key

"openai", "anthropic", "azure-openai", "anyscale", "cohere"

-

api_key

服务提供商的API密钥

string

是(如果指定了 provider

-

-

virtual_key

虚拟密钥标识符

string

是(如果没有 modeprovider

-

-

cache

缓存配置

object

-

查看缓存对象详情

retry

重试配置

object

-

查看重试对象详情

weight

负载均衡权重

number

-

loadbalance 模式中使用

on_status_codes

触发回退的状态码

array of strings

-

fallback 模式中使用

targets

目标配置列表

array

是(如果指定了 mode

-

每个项目遵循配置模式

request_timeout

请求超时配置

number

-

-

custom_host

路由到私有托管模型

string

-

provider + api_key 结合使用

forward_headers

直接转发敏感头信息

array of strings

-

-

override_params

传递模型名称和其他超参数

object

"model", "temperature", "frequency_penalty", "logit_bias", "logprobs", "top_logprobs", "max_tokens", "n", "presence_penalty", "response_format", "seed", "stop", "top_p", 等等

传递通常是有效负载一部分的所有内容

策略对象详细信息

键名称
描述
类型
是否必需
枚举值
附加信息

mode

配置的策略模式

string

"loadbalance", "fallback"

on_status_codes

应用策略的状态码。此字段仅在策略模式为 "fallback" 时使用

数字数组

可选

缓存对象详细信息

键名
描述
类型
必需
枚举值
额外信息

mode

缓存模式

string

"simple", "semantic"

-

max_age

缓存条目的最大年龄

integer

-

可选

重试对象详细信息

键名
描述
类型
必需
枚举值
附加信息

attempts

重试次数

整数

-

-

on_status_codes

触发重试的状态码

字符串数组

-

可选

云服务提供商参数 (Azure OpenAI, Google Vertex, AWS Bedrock)

Azure OpenAI

键名
类型
必需

azure_resource_name

string

azure_deployment_id

string

azure_api_version

string

azure_model_name

string

Authorization

string ("Bearer $API_KEY")

Google Vertex AI

键名
类型
必需

vertex_project_id

string

vertex_region

string

AWS Bedrock

键名
类型
必需

aws_access_key_id

string

aws_secret_access_key

string

aws_region

string

aws_session_token

string

注意事项

  • 策略 mode 键决定配置的操作模式。如果未指定策略 mode,则假定为单一提供者模式,需要 providerapi_keyvirtual_key

  • loadbalancefallback 模式下,targets 数组指定每个目标的配置。

  • cacheretry 对象分别提供缓存和重试策略的附加配置。

示例

单一提供者与API密钥
传递模型及超参数并带覆盖选项
单一提供者与虚拟密钥
单一提供者与虚拟密钥、缓存和重试
使用两个OpenAI API密钥的负载均衡
负载均衡与后备组合

Last updated