本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 Amazon Nova 建置自訂 RAG 系統
注意
Amazon Nova Premier 尚未透過 RetrieveAndGenerate API 提供。若要搭配 Amazon Nova Premier 使用 RetrieveAndGenerate API,您需要在呼叫 RetrieveAndGenerate API 時提供自訂提示。這可透過在 RetrieveAndGenerate API 呼叫promptTemplate
的 generationConfiguration
引數中提供 來完成,如下所示:
'generationConfiguration': { 'promptTemplate': { 'textPromptTemplate': promptTemplate } }
若要建置自訂提示範本,請參閱 RAG 的提示指引。
您可以在自訂文字 RAG 系統中使用 Amazon Nova 模型做為 LLM。若要使用 Amazon Nova 建置自己的 RAG 系統,您可以將 RAG 系統設定為直接查詢知識庫,也可以將知識庫與 代理程式建立關聯 (如需詳細資訊,請參閱 使用 Amazon Nova 建置 AI 代理器)
在任何 RAG 系統中使用 Amazon Nova 時,有兩種一般方法
-
使用擷取工具做為工具 (建議):您可以在 對等 API 或 Invokemodel API 的 ToolParameter 中定義要做為工具使用的擷取工具。例如,您可以將 Bedrock Retrieve API 或任何其他擷取器定義為「工具」。
-
使用 RAG 系統的自訂指示:您可以定義自己的自訂指示,以建置自訂 RAG 系統。
使用擷取器做為工具
定義允許模型叫用擷取器的工具。工具的定義是您在 toolConfig
(ToolConfiguration) 請求參數中傳遞至 Converse
操作的 JSON 結構描述。
{ "tools": [ { "toolSpec": { "name": "Retrieve information tool", "description": "This tool retrieves information from a custom database", "inputSchema": { "json": { "type": "object", "properties": { "query": { "type": "string", "description": "This is the description of the query parameter" } }, "required": [ "query" ] } } } } ] }
定義工具之後,您可以將工具組態傳遞為 converse API 中的參數。
如何解譯回應元素
您將會在助理「角色」下收到來自模型的 JSON 回應,其中內容類型為「toolUse」,或者如果模型選擇不使用擷取工具,則內容類型為「文字」。如果模型選擇使用擷取工具,回應將識別工具 (tool_name)。有關如何使用請求工具的資訊,請參閱模型在 output
(ConverseOutput) 欄位中傳回的訊息。特別是 toolUse
(ToolUseBlock) 欄位。您可以在稍後的呼叫中使用 toolUseId
欄位來識別工具請求。
{ "output": { "message": { "role": "assistant", "content": [ { "toolUse": { "toolUseId": "tooluse_1234567", "name": "Retrieve information tool", "input": { "query": "Reformatted user query" #various arguments needed by the chosen tool } } } ] } }, "stopReason": "tool_use" }
從模型回應中的 toolUse
欄位,您可以使用 name
欄位來識別工具的名稱。然後呼叫 工具的實作,並從 input
欄位傳遞輸入參數。
如何將擷取的內容輸入回 Converse API
若要將擷取的結果重新執行回 Amazon Nova,您現在可以建構工具區塊訊息,其中包含使用者角色中的 toolResult
(ToolResultBlock) 內容區塊。在內容區塊中,包含來自 工具的回應,以及您在上一個步驟中取得的工具請求 ID。
{ "role": "user", "content": [ { "toolResult": { "toolUseId": "tooluse_1234567", "content": [ { "json": { "Text chunk 1": "retrieved information chunk 1", "Text chunk 2": "retrieved information chunk 2" } } ], "status": "success | error" } } ] }
toolResult 可以具有「內容」,可以具有「文字」、「JSON」和「影像」(取決於使用的模型)。如果工具中發生錯誤,例如請求不存在或錯誤的引數,您可以在 toolResult
欄位中將錯誤資訊傳送至模型。若要指出錯誤,請在 status
error
欄位中指定 。