Copilot 额度用于支付您在 Power Platform 环境中使用 Copilot Studio 构建的智能体和 Copilot 的使用费用。 作为管理员,你向需要它们的环境分配信用额度,并决定当环境耗尽其分配的信用额度时会发生什么情况。 除了通过 Power Platform 管理中心 进行操作外,还可以使用 Power Platform API 和管理 (Admin) SDK 通过代码来管理 Copilot 点数分配。 在跨多个环境分配容量、标准化超额行为或将分配作为部署管道的一部分进行管理时,自动化信用分配非常有用。
在本教程中,你将学习如何:
- 在 Power Platform 管理中心管理 Copilot 积分。
- 使用 Power Platform API 进行身份验证。
- 查看可用于分配的Copilot信用额度。
- 将 Copilot 额度分配给某个环境。
- 控制环境是否从租户配额中调用资源。
Copilot 额度由权益 ID MCSMessages 表示,并通过 Power Platform API 的 licensing/allocationsV2 操作进行管理。
allocationsV2/availability 操作会报告可用于分配的容量有多少,而 allocationsV2 操作会写入环境的分配信息。 本文中的所有编程示例都使用 API 版本 2024-10-01。
每个环境分配都有一组 强制规则 ,用于确定环境使用其分配的信用额度时会发生什么情况。 管理中心中的“从我的租户中的可用容量提取”选项对应于 TenantPool 强制执行规则:启用后,环境在其自身分配用尽后会继续从租户中未分配的容量提取;禁用后,环境将被限制在其分配的容量内。
Warning
如果已发布的环境组规则控制从我的租户可用容量中提取,则无法为单个环境覆盖该设置。 通过 Power Platform 管理中心、Power Platform API 或管理 SDK 进行的更新会因 TenantPoolLockedByPolicy 错误而被拒绝。 更改并重新发布环境组的规则,或者 从组中删除环境 以独立管理设置。 删除后,环境将保留组上次应用的值,但该设置将变为可编辑。 有关可用组规则的详细信息,请参阅 环境组的规则。
先决条件
一个配置为 Power Platform API 的应用程序注册。 请注意应用注册 的应用程序(客户端)ID 和 目录(租户)ID。
管理许可证和容量的权限。 使用 Power Platform 管理员 或 全局管理员 角色或可管理许可和容量的等效角色以用户身份登录。
对于 SDK 示例,请安装其公共库中每月发布的 SDK:
dotnet add package Microsoft.PowerPlatform.Management
pip install powerplatform-management
管理Copilot额度分配
本部分展示了在 Power Platform 管理中心和各个 SDK 中执行的相同端到端流程:查看可用容量、将额度分配给环境,以及控制该环境是否使用租户容量。 使用选项卡在管理中心和首选语言之间切换。
登录到 Power Platform 管理中心。
在导航窗格中,选择许可。
在“产品”下,选择“Copilot Studio”,然后选择“管理Copilot额度”。
选择要管理的环境。 使用 “搜索 ”框按名称查找环境。
在“分配容量”下,输入要分配给该环境的 Copilot 积分数量。 该窗格显示已消耗的额度数量、已分配的额度数量,以及可从您的租户中分配的额度数量。
在配额超额下,选中或取消选中从我的租户可用配额中调用,以控制环境用尽其分配额度时的处理方式:
-
已选中:环境分配的配额用尽后,将继续从租户的未分配配额中提取。
-
已清零:环境的使用量会被限制在其分配额度内,并在额度耗尽后停止消耗资源。
选择“保存”。
以下示例将以当前用户身份交互式登录,读取可供分配的 Copilot 配额,将配额分配给某个环境,并关闭从我的租户可用配额中提取选项,从而使环境的配额上限保持在已分配额度内。
# Requires the MSAL.PS module: Install-Module MSAL.PS -Scope CurrentUser
Import-Module "MSAL.PS"
$clientId = "<application (client) ID of your app registration>"
$tenantId = "<directory (tenant) ID>"
$environmentId = "<environment ID>"
$apiBaseUrl = "https://api.powerplatform.com"
$apiVersion = "2024-10-01"
# Sign in interactively and request a token for the Power Platform API
$auth = Get-MsalToken -ClientId $clientId -TenantId $tenantId -Scope "https://api.powerplatform.com/.default" -Interactive
$headers = @{ Authorization = "Bearer $($auth.AccessToken)" }
# 1. View the Copilot Credits available to allocate for the environment
$filter = [uri]::EscapeDataString("environmentId eq '$environmentId' and EntitlementId in ('MCSMessages')")
$availability = Invoke-RestMethod -Method Get `
-Uri "$apiBaseUrl/licensing/allocationsV2/availability?api-version=$apiVersion&`$filter=$filter" `
-Headers $headers
$availability.entitlementAllocationsAvailable |
ForEach-Object { Write-Host "$($_.entitlementId): $($_.availableQuantity) available" }
# 2. Allocate Copilot Credits to the environment and turn off tenant capacity draw
$allocationBody = @{
scope = @{ environmentId = $environmentId }
allocatedEntitlements = @(
@{
entitlementId = "MCSMessages"
allocation = @{ quantity = 5000 }
enforcementRules = @(
# Disable TenantPool so the environment is capped at its allocation.
# Set enabled = $true to let the environment draw from tenant capacity.
@{ ruleType = "TenantPool"; enabled = $false }
)
}
)
} | ConvertTo-Json -Depth 10
Invoke-RestMethod -Method Put `
-Uri "$apiBaseUrl/licensing/allocationsV2?api-version=$apiVersion" `
-Headers $headers -ContentType "application/json" -Body $allocationBody
Write-Host "Updated Copilot credit allocation for environment $environmentId"
以下示例通过使用您的应用注册的客户端 ID 进行交互式登录,读取可用于分配的 Copilot 配额,将配额分配给一个环境,并关闭从我的租户可用容量中提取选项,从而使环境的配额上限保持在已分配额度内。
using Microsoft.PowerPlatform.Management;
using Microsoft.PowerPlatform.Management.Models;
// Create an interactive client using your app registration's client ID.
// A browser window opens for sign-in.
var factory = new ServiceClientFactory();
var client = factory.Create("YOUR_CLIENT_ID");
var environmentId = "<environment ID>";
var apiVersion = "2024-10-01";
// 1. View the Copilot Credits available to allocate for the environment
var availability = await client.Licensing.AllocationsV2.Availability.GetAsync(config =>
{
config.QueryParameters.ApiVersion = apiVersion;
config.QueryParameters.Filter =
$"environmentId eq '{environmentId}' and EntitlementId in (MCSMessages)";
});
foreach (var entitlement in availability.EntitlementAllocationsAvailable)
{
Console.WriteLine($"{entitlement.EntitlementId}: {entitlement.AvailableQuantity} available");
}
// 2. Allocate Copilot Credits to the environment and turn off tenant capacity draw
var request = new AllocationPutRequestModel
{
Scope = new ScopeModel { EnvironmentId = environmentId },
AllocatedEntitlements = new List<EntitlementAllocationModel>
{
new EntitlementAllocationModel
{
EntitlementId = "MCSMessages",
Allocation = new AllocationModel { Quantity = 5000 },
EnforcementRules = new List<AllocationEnforcementRule>
{
// Disable TenantPool so the environment is capped at its allocation.
// Set Enabled = true to let the environment draw from tenant capacity.
new AllocationEnforcementRule
{
RuleType = AllocationEnforcementRuleTypes.TenantPool,
Enabled = false
}
}
}
}
};
await client.Licensing.AllocationsV2.PutAsync(request, config =>
{
config.QueryParameters.ApiVersion = apiVersion;
});
Console.WriteLine($"Updated Copilot credit allocation for environment {environmentId}");
以下示例通过使用您的应用注册的客户端 ID 进行交互式登录,读取可用于分配的 Copilot 配额,将配额分配给一个环境,并关闭从我的租户可用容量中提取选项,从而使环境的配额上限保持在已分配额度内。
import asyncio
from azure.identity import InteractiveBrowserCredential
from kiota_abstractions.base_request_configuration import RequestConfiguration
from kiota_authentication_azure.azure_identity_authentication_provider import (
AzureIdentityAuthenticationProvider,
)
from kiota_http.httpx_request_adapter import HttpxRequestAdapter
from mspp_management.service_client_base import ServiceClientBase
from mspp_management.licensing.allocations_v2.allocations_v2_request_builder import (
AllocationsV2RequestBuilder,
)
from mspp_management.licensing.allocations_v2.availability.availability_request_builder import (
AvailabilityRequestBuilder,
)
from mspp_management.models.allocation_put_request_model import AllocationPutRequestModel
from mspp_management.models.scope_model import ScopeModel
from mspp_management.models.entitlement_allocation_model import EntitlementAllocationModel
from mspp_management.models.allocation_model import AllocationModel
from mspp_management.models.allocation_enforcement_rule import AllocationEnforcementRule
from mspp_management.models.allocation_enforcement_rule_types import (
AllocationEnforcementRuleTypes,
)
API_VERSION = "2024-10-01"
async def manage_copilot_credits(client: ServiceClientBase, environment_id: str):
# 1. View the Copilot Credits available to allocate for the environment
query_params = AvailabilityRequestBuilder.AvailabilityRequestBuilderGetQueryParameters(
api_version=API_VERSION,
filter=f"environmentId eq '{environment_id}' and EntitlementId in (MCSMessages)",
)
availability = await client.licensing.allocations_v2.availability.get(
request_configuration=RequestConfiguration(query_parameters=query_params)
)
for entitlement in availability.entitlement_allocations_available:
print(f"{entitlement.entitlement_id}: {entitlement.available_quantity} available")
# 2. Allocate Copilot Credits to the environment and turn off tenant capacity draw
# Disable TenantPool so the environment is capped at its allocation.
# Set enabled = True to let the environment draw from tenant capacity.
tenant_pool_rule = AllocationEnforcementRule()
tenant_pool_rule.rule_type = AllocationEnforcementRuleTypes.TenantPool
tenant_pool_rule.enabled = False
entitlement = EntitlementAllocationModel()
entitlement.entitlement_id = "MCSMessages"
entitlement.allocation = AllocationModel()
entitlement.allocation.quantity = 5000
entitlement.enforcement_rules = [tenant_pool_rule]
request = AllocationPutRequestModel()
request.scope = ScopeModel()
request.scope.environment_id = environment_id
request.allocated_entitlements = [entitlement]
put_params = AllocationsV2RequestBuilder.AllocationsV2RequestBuilderPutQueryParameters(
api_version=API_VERSION,
)
await client.licensing.allocations_v2.put(
request, request_configuration=RequestConfiguration(query_parameters=put_params)
)
print(f"Updated Copilot credit allocation for environment {environment_id}")
async def main():
# Sign in interactively using your app registration's client ID.
credential = InteractiveBrowserCredential(client_id="YOUR_CLIENT_ID")
auth_provider = AzureIdentityAuthenticationProvider(
credentials=credential,
scopes=["https://api.powerplatform.com/.default"],
)
adapter = HttpxRequestAdapter(authentication_provider=auth_provider)
client = ServiceClientBase(adapter)
await manage_copilot_credits(client, environment_id="<environment ID>")
asyncio.run(main())