

# 管理 A/B 測試
<a name="ab-testing-manage"></a>

更新 A/B 測試以啟動、暫停或停止它。您可以使用 `UpdateABTest`操作來變更執行狀態。

**注意**  
AgentCore CLI 生命週期命令 (`view`、`pause`、`resume``stop`、`promote`、`archive`) 會依與 一起傳遞**的任務 ID** 來識別測試`-i, --id`，而非名稱。從 `agentcore run ab-test --json`( `id` 欄位） 取得 ID，或使用 列出測試`agentcore view ab-test`。任務 ID 使用 格式 `name-suffix`。例如：`customerSupportTargetTest-Ab1Cd2Ef3G`。任務記錄會在本機以 JSON 形式儲存在專案`.cli/jobs/ab-tests/`中的 下。

## 程式碼範例
<a name="manage-ab-test-examples"></a>

### 檢視、暫停、繼續和停止
<a name="manage-ab-test-start-stop"></a>

**Example**  
列出所有 A/B 測試任務，或詳細檢視任務 （也顯示調用 URL 和結果）：  

```
agentcore view ab-test
agentcore view ab-test <ab-test-id>
```
繼續暫停的測試：  

```
agentcore resume ab-test -i <ab-test-id>
```
暫停執行中的測試。流量會停止分割，且所有請求都會路由至控制變體。可以繼續。  

```
agentcore pause ab-test -i <ab-test-id>
```
永久停止測試。所有流量都會還原至控制變體。這無法復原。  

```
agentcore stop ab-test -i <ab-test-id>
```
開始測試 （將執行狀態設定為 `RUNNING`)：  

```
import boto3
import uuid

client = boto3.client("bedrock-agentcore", region_name="us-west-2")

response = client.update_ab_test(
    abTestId="customerSupportPromptTest-Ab1Cd2Ef3G",
    executionStatus="RUNNING",
    clientToken=str(uuid.uuid4()),
)
print(f"Execution status: {response['executionStatus']}")
```
暫停執行中的測試：  

```
response = client.update_ab_test(
    abTestId="customerSupportPromptTest-Ab1Cd2Ef3G",
    executionStatus="PAUSED",
    clientToken=str(uuid.uuid4()),
)
print(f"Execution status: {response['executionStatus']}")
```
永久停止執行中的測試：  

```
response = client.update_ab_test(
    abTestId="customerSupportPromptTest-Ab1Cd2Ef3G",
    executionStatus="STOPPED",
    clientToken=str(uuid.uuid4()),
)
print(f"Execution status: {response['executionStatus']}")
```

### 提升處理變體
<a name="manage-ab-test-promote"></a>

當結果達到統計顯著性時，請提升處理變體。 會`promote`停止測試並將處理變體寫入 `agentcore.json`；`agentcore deploy`之後執行 將其推出。

**Example**  

```
agentcore promote ab-test -i <ab-test-id>
agentcore deploy
```
開發套件沒有單一的「提升」操作。停止測試，然後自行將獲勝變體套用到您的資源 （例如，更新控制組態套件或閘道目標以符合處理方式）。

### 刪除 A/B 測試
<a name="manage-ab-test-remove"></a>

刪除 A/B 測試及其相關聯的中繼資料。測試必須處於`STOPPED`執行狀態，才能將其刪除。刪除測試不會影響測試參考的組態套件、AgentCore Gateway 或線上評估組態。

**Example**  

```
agentcore archive ab-test -i <ab-test-id>
```
停止然後刪除：  

```
import boto3
import uuid

client = boto3.client("bedrock-agentcore", region_name="us-west-2")

# First stop the test if it's running
client.update_ab_test(
    abTestId="customerSupportPromptTest-Ab1Cd2Ef3G",
    executionStatus="STOPPED",
    clientToken=str(uuid.uuid4()),
)

# Then delete
response = client.delete_ab_test(
    abTestId="customerSupportPromptTest-Ab1Cd2Ef3G"
)
print(f"Deleted: {response['abTestId']}")
```

## 執行狀態轉換
<a name="manage-ab-test-execution-states"></a>


| 從 | 到 | 說明 | 
| --- | --- | --- | 
|  `NOT_STARTED`  |  `RUNNING`  | 開始測試。AgentCore Gateway 會開始分割流量。 | 
|  `RUNNING`  |  `PAUSED`  | 暫停測試。流量會停止分割；要控制的所有流量路由。 | 
|  `PAUSED`  |  `RUNNING`  | 繼續暫停的測試。 | 
|  `RUNNING`  |  `STOPPED`  | 永久停止測試。流量路由會立即結束。 | 
|  `PAUSED`  |  `STOPPED`  | 永久停止暫停的測試。 | 