View a markdown version of this page

速率限制 API 示例 - 亚马逊基岩 AgentCore

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

速率限制 API 示例

以下示例显示了如何使用 AWS CLI 和 AWS Python 软件开发工具包 (Boto3) 管理速率限制。

创建每秒每个目标的请求数限制

以下示例创建了一个速率限制,该速率限制以每个目标为基础控制每秒的请求数。一个特定目标获得 100 个 RPS,所有其他目标都获得自己的 10 个 RPS 存储桶。

例
AWS CLI
  1. 运行以下命令:

    aws bedrock-agentcore-control create-gateway-rate-limit \ --gateway-identifier my-gateway-abc1234567 \ --dimension-keys '["targetName"]' \ --description "Per-target RPS limit" \ --entries '[ { "dimensions": {"targetName": "my-high-traffic-target"}, "requests": [ { "rate": 100, "period": "second" } ] }, { "dimensions": {"targetName": "*"}, "requests": [ { "rate": 10, "period": "second" } ] } ]'
AWS Python SDK (Boto3)
  1. import boto3 client = boto3.client("bedrock-agentcore-control", region_name="us-west-2") response = client.create_gateway_rate_limit( gatewayIdentifier="my-gateway-abc1234567", dimensionKeys=["targetName"], description="Per-target RPS limit", entries=[ { "dimensions": {"targetName": "my-high-traffic-target"}, "requests": [ { "rate": 100, "period": "second", } ], }, { "dimensions": {"targetName": "*"}, "requests": [ { "rate": 10, "period": "second", } ], }, ], ) print(f"Rate Limit ID: {response['rateLimitId']}")

设置每位呼叫者每分钟请求次数上限

以下示例根据调用者的 JWT sub 声明创建了速率限制。高级用户获得 300 RPM,所有其他用户获得 60 RPM。

例
AWS CLI
  1. 运行以下命令:

    aws bedrock-agentcore-control create-gateway-rate-limit \ --gateway-identifier my-gateway-abc1234567 \ --dimension-keys '["$.context.jwt.sub"]' \ --description "Per-caller RPM by subscription tier" \ --entries '[ { "dimensions": {"$.context.jwt.sub": "premium-user-001"}, "requests": [ { "rate": 300, "period": "minute" } ] }, { "dimensions": {"$.context.jwt.sub": "premium-user-002"}, "requests": [ { "rate": 300, "period": "minute" } ] }, { "dimensions": {"$.context.jwt.sub": "*"}, "requests": [ { "rate": 60, "period": "minute" } ] } ]'
AWS Python SDK (Boto3)
  1. import boto3 client = boto3.client("bedrock-agentcore-control", region_name="us-west-2") response = client.create_gateway_rate_limit( gatewayIdentifier="my-gateway-abc1234567", dimensionKeys=["$.context.jwt.sub"], description="Per-caller RPM by subscription tier", entries=[ { "dimensions": {"$.context.jwt.sub": "premium-user-001"}, "requests": [ { "rate": 300, "period": "minute", } ], }, { "dimensions": {"$.context.jwt.sub": "premium-user-002"}, "requests": [ { "rate": 300, "period": "minute", } ], }, { "dimensions": {"$.context.jwt.sub": "*"}, "requests": [ { "rate": 60, "period": "minute", } ], }, ], ) print(f"Rate Limit ID: {response['rateLimitId']}")

使用代币创建多维限制

以下示例使用三维密钥创建了速率限制,限制了每分钟的请求和令牌。每个来电者都有自己的预算范围,适用于特定的目标和模型。

例
AWS CLI
  1. 运行以下命令:

    aws bedrock-agentcore-control create-gateway-rate-limit \ --gateway-identifier my-gateway-abc1234567 \ --dimension-keys '["targetName", "qualifiedModelId", "$.context.jwt.sub"]' \ --description "Per-caller token and request budget per target and model" \ --entries '[ { "dimensions": {"targetName": "my-inference-target", "qualifiedModelId": "anthropic.claude-3-sonnet-20240229-v1:0", "$.context.jwt.sub": "*"}, "requests": [ { "rate": 100, "period": "minute" } ], "tokens": [ { "rate": 50000, "period": "minute" } ] }, { "dimensions": {"targetName": "*", "qualifiedModelId": "*", "$.context.jwt.sub": "*"}, "requests": [ { "rate": 30, "period": "minute" } ], "tokens": [ { "rate": 10000, "period": "minute" } ] } ]'
AWS Python SDK (Boto3)
  1. import boto3 client = boto3.client("bedrock-agentcore-control", region_name="us-west-2") response = client.create_gateway_rate_limit( gatewayIdentifier="my-gateway-abc1234567", dimensionKeys=["targetName", "qualifiedModelId", "$.context.jwt.sub"], description="Per-caller token and request budget per target and model", entries=[ { "dimensions": {"targetName": "my-inference-target", "qualifiedModelId": "anthropic.claude-3-sonnet-20240229-v1:0", "$.context.jwt.sub": "*"}, "requests": [ { "rate": 100, "period": "minute", } ], "tokens": [ { "rate": 50000, "period": "minute", } ], }, { "dimensions": {"targetName": "*", "qualifiedModelId": "*", "$.context.jwt.sub": "*"}, "requests": [ { "rate": 30, "period": "minute", } ], "tokens": [ { "rate": 10000, "period": "minute", } ], }, ], ) print(f"Rate Limit ID: {response['rateLimitId']}")

创建连接速率限制

以下示例为每个模型创建了连接速率限制。这限制了对每个模型的并发进行中请求的数量,这对于保护并发容量有限的目标很有用。

例
AWS CLI
  1. 运行以下命令:

    aws bedrock-agentcore-control create-gateway-rate-limit \ --gateway-identifier my-gateway-abc1234567 \ --dimension-keys '["qualifiedModelId"]' \ --description "Connection rate limit per model" \ --entries '[ { "dimensions": {"qualifiedModelId": "anthropic.claude-3-sonnet-20240229-v1:0"}, "connections": [ { "rate": 50, "period": "second" } ] }, { "dimensions": {"qualifiedModelId": "*"}, "connections": [ { "rate": 20, "period": "second" } ] } ]'
AWS Python SDK (Boto3)
  1. import boto3 client = boto3.client("bedrock-agentcore-control", region_name="us-west-2") response = client.create_gateway_rate_limit( gatewayIdentifier="my-gateway-abc1234567", dimensionKeys=["qualifiedModelId"], description="Connection rate limit per model", entries=[ { "dimensions": {"qualifiedModelId": "anthropic.claude-3-sonnet-20240229-v1:0"}, "connections": [ { "rate": 50, "period": "second", } ], }, { "dimensions": {"qualifiedModelId": "*"}, "connections": [ { "rate": 20, "period": "second", } ], }, ], ) print(f"Rate Limit ID: {response['rateLimitId']}")

屏蔽费率为零的来电者

以下示例通过将特定呼叫者的费率设置为 0 来屏蔽该来电者。一旦更改传播(最多 30 秒),这将拒绝来自被封锁调用方的所有请求。

例
AWS CLI
  1. 运行以下命令:

    aws bedrock-agentcore-control create-gateway-rate-limit \ --gateway-identifier my-gateway-abc1234567 \ --dimension-keys '["$.context.jwt.sub"]' \ --description "Block abusive caller" \ --entries '[ { "dimensions": {"$.context.jwt.sub": "blocked-user-789"}, "requests": [ { "rate": 0, "period": "second" } ] }, { "dimensions": {"$.context.jwt.sub": "*"}, "requests": [ { "rate": 100, "period": "second" } ] } ]'
AWS Python SDK (Boto3)
  1. import boto3 client = boto3.client("bedrock-agentcore-control", region_name="us-west-2") response = client.create_gateway_rate_limit( gatewayIdentifier="my-gateway-abc1234567", dimensionKeys=["$.context.jwt.sub"], description="Block abusive caller", entries=[ { "dimensions": {"$.context.jwt.sub": "blocked-user-789"}, "requests": [ { "rate": 0, "period": "second", } ], }, { "dimensions": {"$.context.jwt.sub": "*"}, "requests": [ { "rate": 100, "period": "second", } ], }, ], ) print(f"Rate Limit ID: {response['rateLimitId']}")

设定速率限制

以下示例检索特定速率限制的详细信息。

例
AWS CLI
  1. 运行以下命令:

    aws bedrock-agentcore-control get-gateway-rate-limit \ --gateway-identifier my-gateway-abc1234567 \ --rate-limit-id rl-abc123def456
AWS Python SDK (Boto3)
  1. import boto3 client = boto3.client("bedrock-agentcore-control", region_name="us-west-2") response = client.get_gateway_rate_limit( gatewayIdentifier="my-gateway-abc1234567", rateLimitId="rl-abc123def456", ) print(f"Status: {response['status']}") print(f"Dimension Keys: {response['dimensionKeys']}") print(f"Entries: {len(response['entries'])}")

更新速率限制

以下示例更新现有速率限制的条目以提高特定目标的速率。

注意

该dimensionKeys字段在创建后是不可变的。要更改维度密钥,请删除速率限制并创建一个新的速率限制。

例
AWS CLI
  1. 运行以下命令:

    aws bedrock-agentcore-control update-gateway-rate-limit \ --gateway-identifier my-gateway-abc1234567 \ --rate-limit-id rl-abc123def456 \ --description "Updated per-target RPS limit" \ --entries '[ { "dimensions": {"targetName": "my-high-traffic-target"}, "requests": [ { "rate": 200, "period": "second" } ] }, { "dimensions": {"targetName": "*"}, "requests": [ { "rate": 20, "period": "second" } ] } ]'
AWS Python SDK (Boto3)
  1. import boto3 client = boto3.client("bedrock-agentcore-control", region_name="us-west-2") response = client.update_gateway_rate_limit( gatewayIdentifier="my-gateway-abc1234567", rateLimitId="rl-abc123def456", description="Updated per-target RPS limit", entries=[ { "dimensions": {"targetName": "my-high-traffic-target"}, "requests": [ { "rate": 200, "period": "second", } ], }, { "dimensions": {"targetName": "*"}, "requests": [ { "rate": 20, "period": "second", } ], }, ], ) print(f"Status: {response['status']}")

列出速率限制

以下示例列出了网关的所有速率限制。

例
AWS CLI
  1. 运行以下命令:

    aws bedrock-agentcore-control list-gateway-rate-limits \ --gateway-identifier my-gateway-abc1234567
AWS Python SDK (Boto3)
  1. import boto3 client = boto3.client("bedrock-agentcore-control", region_name="us-west-2") paginator = client.get_paginator("list_gateway_rate_limits") for page in paginator.paginate(gatewayIdentifier="my-gateway-abc1234567"): for rate_limit in page["rateLimits"]: print( f"ID: {rate_limit['rateLimitId']}, " f"Status: {rate_limit['status']}, " f"Dimensions: {rate_limit['dimensionKeys']}" )
注意

结果可能会分页。使用响应中的nextToken值来检索其他页面,或使用 Boto3 分页器,如上所示。

删除速率限制

以下示例从网关中删除了速率限制。

例
AWS CLI
  1. 运行以下命令:

    aws bedrock-agentcore-control delete-gateway-rate-limit \ --gateway-identifier my-gateway-abc1234567 \ --rate-limit-id rl-abc123def456
AWS Python SDK (Boto3)
  1. import boto3 client = boto3.client("bedrock-agentcore-control", region_name="us-west-2") response = client.delete_gateway_rate_limit( gatewayIdentifier="my-gateway-abc1234567", rateLimitId="rl-abc123def456", ) print(f"Status: {response['status']}")

批量看跌速率限制

以下示例在一次呼叫中创建或更新多个速率限制。批量放置使用向上插入语义——如果存在具有相同维度键的速率限制,则会对其进行更新;否则,将创建新的速率限制。

注意

批量放置是等效的。您可以安全地重试失败的呼叫,而无需创建重复的速率限制。

例
AWS CLI
  1. 运行以下命令:

    aws bedrock-agentcore-control batch-put-gateway-rate-limits \ --gateway-identifier my-gateway-abc1234567 \ --rate-limits '[ { "dimensionKeys": ["targetName"], "description": "Per-target RPS", "entries": [ { "dimensions": {"targetName": "*"}, "requests": [{"rate": 50, "period": "second"}] } ] }, { "dimensionKeys": ["$.context.jwt.sub"], "description": "Per-caller RPM", "entries": [ { "dimensions": {"$.context.jwt.sub": "*"}, "requests": [{"rate": 120, "period": "minute"}] } ] } ]'
AWS Python SDK (Boto3)
  1. import boto3 client = boto3.client("bedrock-agentcore-control", region_name="us-west-2") response = client.batch_put_gateway_rate_limits( gatewayIdentifier="my-gateway-abc1234567", rateLimits=[ { "dimensionKeys": ["targetName"], "description": "Per-target RPS", "entries": [ { "dimensions": {"targetName": "*"}, "requests": [ {"rate": 50, "period": "second"} ], } ], }, { "dimensionKeys": ["$.context.jwt.sub"], "description": "Per-caller RPM", "entries": [ { "dimensions": {"$.context.jwt.sub": "*"}, "requests": [ {"rate": 120, "period": "minute"} ], } ], }, ], ) for result in response["rateLimits"]: print(f"ID: {result['rateLimitId']}, Status: {result['status']}")