將 Amazon Nova 用於多模態 RAG - Amazon Nova

將 Amazon Nova 用於多模態 RAG

您可以使用多模態 RAG 來搜尋 PDF、影像或影片等文件 (適用於 Amazon Nova Lite 和 Amazon Nova Pro)。透過 Amazon Nova 多模態理解功能,您可以使用包含文字和影像的混合資料來建置 RAG 系統。您可以透過 Amazon Bedrock 知識庫或透過建置自訂多模態 RAG 系統來執行此操作。

若要建立多模態 RAG 系統:

  1. 建立多模態內容的資料庫。

  2. 在 Amazon Nova 的多模態 RAG 系統中執行推論。

    1. 讓使用者能夠查詢內容

    2. 將內容傳回 Amazon Nova

    3. 啓用 Amazon Nova 以回應原始使用者查詢。

使用 Amazon Nova 建立自訂多模態 RAG 系統

若要使用 Amazon Nova 建立多模態內容的資料庫,可以使用兩種常見方法中的一種。這兩種方法的準確性取決於您的具體應用程式。

使用多模態嵌入建立向量資料庫。

您可以透過使用 Titan 多模態嵌入等嵌入模型,來建立多模態資料的向量資料庫。若要這樣做,您首先需要有效率地將文件解析為文字、資料表和影像。然後,若要建立向量資料庫,請將解析的內容傳遞至所選的多模態嵌入模型。我們建議以原始模態將嵌入連接至文件的部分,以便檢索器可以以原始內容模態傳回搜尋結果。

使用文字嵌入建立向量資料庫。

若要使用文字嵌入模型,您可以使用 Amazon Nova 將影像轉換為文字。然後,您可以使用文字嵌入模型建立向量資料庫,例如 Titan 文字嵌入 V2 模型

對於投影片和資訊圖表等文件,您可以將文件的每個部分轉換為文字描述,然後使用該文字描述建立向量資料庫。若要建立文字描述,請透過 Converse API,搭配類似下列提示詞使用 Amazon Nova:

You are a story teller and narrator who will read an image and tell all the details of the image as a story. Your job is to scan the entire image very carefully. Please start to scan the image from top to the bottom and retrieve all important parts of the image. In creating the story, you must first pay attention to all the details and extract relevant resources. Here are some important sources: 1. Please identify all the textual information within the image. Pay attention to text headers, sections/subsections anecdotes, and paragraphs. Especially, extract those pure-textual data not directly associated with graphs. 2. please make sure to describe every single graph you find in the image 3. please include all the statistics in the graph and describe each chart in the image in detail 4. please do NOT add any content that are not shown in the image in the description. It is critical to keep the description truthful 5. please do NOT use your own domain knowledge to infer and conclude concepts in the image. You are only a narrator and you must present every single data-point available in the image. Please give me a detailed narrative of the image. While you pay attention to details, you MUST give the explanation in a clear English that is understandable by a general user.

然後,Amazon Nova 將以所提供之影像的文字描述來回應。然後,該文字描述可以傳送到文字嵌入模型以建立向量資料庫。

或者,對於 pdf 等文字密集型文件,最好從文字中解析影像 (這取決於您的具體資料和應用程式)。若要這樣做,您首先需要有效率地將文件解析為文字、資料表和影像。然後,可以使用如上所示的提示詞將產生的影像轉換為文字。然後,可以將影像和任何其他文字所產生的文字描述傳送到文字嵌入模型,以建立向量資料庫。建議以原始模態將嵌入連接至文件的部分,以便檢索器可以以原始內容模態傳回搜尋結果。

在針對 Amazon Nova 的 RAG 系統中執行推論

設定向量資料庫之後,您現在可以啟用使用者查詢來搜尋資料庫、將擷取的內容傳回 Amazon Nova,然後使用擷取的內容和使用者查詢,使 Amazon Nova 模型能夠回應原始使用者查詢。

若要使用文字或多模態使用者查詢來查詢向量資料庫,請與遵循您在執行 RAG 進行文字理解和產生時相同的設計選擇。您可以搭配 Amazon Bedrock 知識庫使用 Amazon Nova,也可以使用 Amazon Nova 和 Converse API 建置自訂 RAG 系統

當檢索器將內容傳回模型時,建議您以其原始模態使用該內容。因此,如果原始輸入是影像,則即使您出於建立文字嵌入的目的將影像轉換為了文字,也請將影像傳回 Amazon Nova。若要更有效地傳回影像,建議您使用此範本來設定擷取的內容,以用於 Converse API:

doc_template = """Image {idx} : """ messages = [] for item in search_results: messages += [ { "text": doc_template.format(idx=item.idx) }, { "image": { "format": "jpeg", # image source is not actually used in offline inference # images input are provided to inferencer separately "source": { "bytes": BASE64_ENCODED_IMAGE } } } ] messages.append({"text": question}) system_prompt = """ In this session, you are provided with a list of images and a user's question, your job is to answer the user's question using only information from the images. When give your answer, make sure to first quote the images (by mentioning image title or image ID) from which you can identify relevant information, then followed by your reasoning steps and answer. If the images do not contain information that can answer the question, please state that you could not find an exact answer to the question. Remember to add citations to your response using markers like %[1]%, %[2]% and %[3]% for the corresponding images."""

透過在 Converse API 中使用擷取的內容和使用者查詢,您可以調用 Converse API,Amazon Nova 則會產生回應或請求額外的搜尋。發生的情況取決於您的指示,或擷取的內容是否有效回答了使用者查詢。