本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
在 Amazon Bedrock 中使用重新排名器模型
您可以直接使用重新排名器模型,或在知識庫查詢期間擷取結果時使用。選擇您偏好方法的索引標籤,然後遵循下列步驟:
- Console
-
您無法直接在 中使用重新排名器模型 AWS 管理主控台,但您可以在查詢知識庫時使用重新排名器模型,方法如下:
-
當您查詢知識庫時,請選擇
圖示以開啟組態窗格。 -
展開重新排名區段。
-
選擇選取模型,然後選取重新排名模型。
-
如果您的 Amazon Bedrock Knowledge Bases 服務角色缺少使用重新排名器模型的許可,請選取更新服務角色以修改具有適當許可的角色。
-
(選用) 在其他重新排名選項區段中,修改您需要的任何選項。
-
輸入提示,然後選取執行。回應是套用重新排名器模型之後的結果。
如需執行知識庫查詢的詳細說明,請參閱 查詢知識庫並擷取資料 和 查詢知識庫並根據擷取的資料產生回應。
-
- API
-
如需在知識庫查詢期間使用重新排名器模型的說明,請參閱 查詢知識庫並擷取資料和 查詢知識庫並根據擷取的資料產生回應。
若要直接搭配 Amazon Bedrock API 使用重新排名器模型,請傳送具有 Amazon Bedrock 代理人執行時期端點的重新排名請求。
下列是必要欄位:
欄位 基本描述 queries 一個 RerankQuery 物件的陣列。指定 TEXT做為type,並在textQuery欄位中包含查詢。sources 要提交至重新排名模型的 RerankSource 物件陣列。對於每個 RerankSource,指定INLINE做為type,並在inlineDocumentSource欄位中包含 RerankDocument 物件。請參閱下方以取得RerankDocument的相關詳細資訊。rerankingConfiguration 包含所要使用重新排名模型的 Amazon Resource Name (ARN),和重新排名後要傳回的結果數量,以及選擇性包含模型的推論組態。您可以將其他模型組態指定為鍵/值對。如需詳細資訊,請參閱 Cohere 文件網站上的重新排名 。 以下是選填欄位:
欄位 使用案例 nextToken 在上一個回應中傳回的字符,您可以包含此字符以提供下一批次的結果。 所包含
RerankSource物件的格式取決於文件的格式。若要查看不同RerankSource類型的格式,請選擇對應至文件格式的索引標籤:StringJSON object- String
如果文件是字串,請將 RerankDocument 物件
type的欄位值指定為TEXT,並在text欄位中包含文件。例如:{ "inlineDocumentSource": { "textDocument": { "text": "string" }, "type": "TEXT" }, "type": "INLINE" }- JSON object
如果文件是 JSON 物件,請將 RerankDocument 物件中的
type欄位值指定為JSON,並在jsonDocument欄位中包含此文件。例如:{ "inlineDocumentSource": { "jsonDocument": JSON value, "type": "JSON" }, "type": "INLINE" }
對
Rerank請求的回應會在results欄位中傳回 RerankResult 物件的清單。每個物件皆包含下列欄位:-
document– 包含您提交之文件的相關資訊。 -
relevanceScore– 文件的相關性分數,由重新排名模型指派。 -
index– 指出文件相對於清單中其他文件的排名。分數越低,排名越高。
如果顯示的結果太多,則回應會在
nextToken欄位中傳回值。在此情況下,若要查看下一批結果,請在後續請求中包含該字符。程式碼範例
下列範例示範如何使用 AWS SDKs呼叫 Rerank API。
PythonNode.js- 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}`); }