使用 Amazon Nova 建置自訂 RAG 系統
注意
Amazon Nova Premier 尚不能透過 RetrieveAndGenerate API 使用。若要將 RetrieveAndGenerate API 與 Amazon Nova Premier 搭配使用,您需要在呼叫 RetrieveAndGenerate API 時提供自訂提示詞。這可透過在 RetrieveAndGenerate API 呼叫的 generationConfiguration 引數中提供 promptTemplate 來完成,如下所示:
'generationConfiguration': { 'promptTemplate': { 'textPromptTemplate': promptTemplate } }
若要建置自訂提示詞範本,請參閱 RAG 提示指引。
您可以使用 Amazon Nova 模型作為自訂文字 RAG 系統中的 LLM。若要使用 Amazon Nova 建置您自己的 RAG 系統,您可以設定 RAG 系統直接查詢知識庫,也可以將知識庫與代理程式建立關聯 (如需詳細資訊,請參閱 使用 Amazon Nova 建置 AI 代理程式)
在任何 RAG 系統中使用 Amazon Nova 時,有兩種一般方法
-
使用檢索器作為工具 (建議方法):您可以在 Converse 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 回應,其上下文類型為「工具使用」,或者如果模型選擇不使用檢索器工具,則上下文類型為「文字」。如果模型選擇使用檢索器工具,回應將識別工具 (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。