

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 在 Amazon Bedrock 中使用重新排名器模型
<a name="rerank-use"></a>

您可以直接使用重新排名器模型，或在知識庫查詢期間擷取結果時使用。選擇您偏好方法的索引標籤，然後遵循下列步驟：

------
#### [ Console ]

您無法直接在 中使用重新排名器模型 AWS 管理主控台，但您可以在查詢知識庫時使用重新排名器模型，方法如下：

1. 當您查詢知識庫時，請選擇 ![Icon showing three horizontal sliders at different positions for adjusting settings.](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/images/icons/configurations.png) 圖示以開啟**組態**窗格。

1. 展開**重新排名**區段。

1. 選擇**選取模型**，然後選取重新排名模型。

1. 如果您的 Amazon Bedrock Knowledge Bases 服務角色缺少[使用重新排名器模型的許可](rerank-prereq.md)，請選取**更新服務角色**以修改具有適當許可的角色。

1. (選用) 在**其他重新排名選項**區段中，修改您需要的任何選項。

1. 輸入提示，然後選取**執行**。回應是套用重新排名器模型之後的結果。

如需執行知識庫查詢的詳細說明，請參閱 [查詢知識庫並擷取資料](kb-test-retrieve.md) 和 [查詢知識庫並根據擷取的資料產生回應](kb-test-retrieve-generate.md)。

------
#### [ API ]

如需在知識庫查詢期間使用重新排名器模型的說明，請參閱 [查詢知識庫並擷取資料](kb-test-retrieve.md)和 [查詢知識庫並根據擷取的資料產生回應](kb-test-retrieve-generate.md)。

若要直接搭配 Amazon Bedrock API 使用重新排名器模型，請傳送具有 [Amazon Bedrock 代理人執行時期端點](https://docs.aws.amazon.com/general/latest/gr/bedrock.html#bra-rt)的[重新排名](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_Rerank.html)請求。

下列是必要欄位：


****  

| 欄位 | 基本描述 | 
| --- | --- | 
| queries | 一個 [RerankQuery](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RerankQuery.html) 物件的陣列。指定 TEXT 做為 type，並在 textQuery 欄位中包含查詢。 | 
| sources | 要提交至重新排名模型的 [RerankSource](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RerankSource.html) 物件陣列。對於每個 RerankSource，指定 INLINE 做為 type，並在 inlineDocumentSource 欄位中包含 [RerankDocument](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RerankDocument.html) 物件。請參閱下方以取得 RerankDocument 的相關詳細資訊。 | 
| rerankingConfiguration | 包含所要使用重新排名模型的 Amazon Resource Name (ARN)，和重新排名後要傳回的結果數量，以及選擇性包含模型的推論組態。您可以將其他模型組態指定為鍵/值對。如需詳細資訊，請參閱 Cohere 文件網站上的[重新排名](https://docs.cohere.com/reference/rerank)。 | 

以下是選填欄位：


****  

| 欄位 | 使用案例 | 
| --- | --- | 
| nextToken | 在上一個回應中傳回的字符，您可以包含此字符以提供下一批次的結果。 | 

所包含 `RerankSource` 物件的格式取決於文件的格式。若要查看不同 `RerankSource` 類型的格式，請選擇對應至文件格式的索引標籤：

------
#### [ String ]

如果文件是字串，請將 [RerankDocument](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RerankDocument.html) 物件 `type` 的欄位值指定為 `TEXT`，並在 `text` 欄位中包含文件。例如：

```
{
    "inlineDocumentSource": {
        "textDocument": {
            "text": "string"
        },
        "type": "TEXT"
    },
    "type": "INLINE"
}
```

------
#### [ JSON object ]

如果文件是 JSON 物件，請將 [RerankDocument](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RerankDocument.html) 物件中的 `type` 欄位值指定為 `JSON`，並在 `jsonDocument` 欄位中包含此文件。例如：

```
{
    "inlineDocumentSource": {
        "jsonDocument": JSON value,
        "type": "JSON"
    },
    "type": "INLINE"
}
```

------

對 `Rerank` 請求的回應會在 `results` 欄位中傳回 [RerankResult](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RerankResult.html) 物件的清單。每個物件皆包含下列欄位：
+ `document` – 包含您提交之文件的相關資訊。
+ `relevanceScore` – 文件的相關性分數，由重新排名模型指派。
+ `index` – 指出文件相對於清單中其他文件的排名。分數越低，排名越高。

如果顯示的結果太多，則回應會在 `nextToken` 欄位中傳回值。在此情況下，若要查看下一批結果，請在後續請求中包含該字符。

**程式碼範例**

下列範例示範如何使用 AWS SDKs呼叫 Rerank API。

------
#### [ Python ]

```
import boto3

client = boto3.client('bedrock-agent-runtime', region_name='us-east-1')
response = client.rerank(
    queries=[{
        'type': 'TEXT',
        'textQuery': {'text': 'What is Amazon Bedrock?'}
    }],
    sources=[
        {
            'type': 'INLINE',
            'inlineDocumentSource': {
                'type': 'TEXT',
                'textDocument': {'text': 'Amazon Bedrock is a fully managed service for foundation models.'}
            }
        },
        {
            'type': 'INLINE',
            'inlineDocumentSource': {
                'type': 'TEXT',
                'textDocument': {'text': 'Amazon S3 is an object storage service.'}
            }
        }
    ],
    rerankingConfiguration={
        'type': 'BEDROCK_RERANKING_MODEL',
        'bedrockRerankingConfiguration': {
            'modelConfiguration': {
                'modelArn': 'arn:aws:bedrock:us-east-1::foundation-model/cohere.rerank-v3-5:0'
            },
            'numberOfResults': 2
        }
    }
)
for result in response['results']:
    print(f'Index: {result["index"]}, Score: {result["relevanceScore"]}')
```

------
#### [ Node.js ]

```
import { BedrockAgentRuntimeClient, RerankCommand } from "@aws-sdk/client-bedrock-agent-runtime";

const client = new BedrockAgentRuntimeClient({ region: "us-east-1" });
const response = await client.send(new RerankCommand({
    queries: [{
        type: "TEXT",
        textQuery: { text: "What is Amazon Bedrock?" }
    }],
    sources: [
        {
            type: "INLINE",
            inlineDocumentSource: {
                type: "TEXT",
                textDocument: { text: "Amazon Bedrock is a fully managed service for foundation models." }
            }
        },
        {
            type: "INLINE",
            inlineDocumentSource: {
                type: "TEXT",
                textDocument: { text: "Amazon S3 is an object storage service." }
            }
        }
    ],
    rerankingConfiguration: {
        type: "BEDROCK_RERANKING_MODEL",
        bedrockRerankingConfiguration: {
            modelConfiguration: {
                modelArn: "arn:aws:bedrock:us-east-1::foundation-model/cohere.rerank-v3-5:0"
            },
            numberOfResults: 2
        }
    }
}));
for (const result of response.results) {
    console.log(`Index: ${result.index}, Score: ${result.relevanceScore}`);
}
```

------

------