共用方式為


資訊檢索

在 Retrieval-Augmented 產生 (RAG) 解決方案的上一個步驟中,您為區塊產生內嵌。 在此步驟中,您會在向量資料庫中產生索引,並實驗以判斷最佳搜尋。 本文涵蓋搜尋索引的組態選項、搜尋類型,以及重新編製策略。

本文是一系列文章的一部分。 閱讀 簡介

設定搜尋索引

注意

本節說明 Azure AI 搜尋的特定建議。 如果您使用不同的商店,請檢閱適當的文件,以尋找該服務的關鍵配置。

您的存放區的搜尋索引中,每個數據欄位都對應一個資料列。 搜尋存放區通常支援 非向量數據類型,例如字串、布爾值、整數、單精度浮點數、雙精度浮點數和日期時間。 它們也支援集合,例如單一型別集合和 向量資料類型。 對於每個數據行,您必須 設定的資訊,例如數據類型,以及欄位是否可篩選、可擷取或可搜尋。

請考慮下列 向量搜尋組態, 可以應用於向量欄位:

  • 向量搜尋演算法:向量搜尋演算法 搜尋相對匹配項目。 AI 搜尋具有一種暴力演算法選項,稱為窮盡的 k 近鄰(KNN),可以掃描整個向量空間。 它也具有更高效能的演算法選項,稱為階層式導覽小型世界(HNSW),可執行 近似近鄰 (ANN) 搜尋。

  • 相似度計量: 演算法會使用 相似度計量 來計算接近度。 AI 搜尋中的計量類型包括餘弦、點產品和歐幾里得。 如果您使用 Azure OpenAI 服務內嵌模型,請選擇餘弦值。

  • efConstruction 參數: 此參數是在建構 HNSW 索引期間使用。 它決定在索引期間與某個向量連接的最近鄰居的數量。 較大的 efConstruction 值會導致比小的數值更好的品質索引。 但較大的值需要更多時間、記憶體和計算。 針對大量區塊,請設定較高的 efConstruction 值。 若為少量區塊,請設定較低的值。 若要判斷最佳值,請試驗您的數據和預期的查詢。

  • efSearch 參數: 在查詢期間使用此參數來設定搜尋所使用的近鄰數或類似區塊數目。

  • m 參數: 此參數是雙向連結計數。 範圍是 4 到 10。 較低的數字會產生較少的結果雜訊。

在 AI 搜尋中,向量組態會封裝在 vectorSearch 組態中。 當您設定向量數據行時,會參考該向量數據行的適當設定,並設定維度數目。 向量數據行的 dimensions 屬性代表您內嵌模型產生的維度數目。 例如,儲存優化 文字內嵌-3-small 模型會產生 1,536 個維度。

選擇您的搜尋方法

當您從提示協調器對搜尋存放區執行查詢時,請考慮下列因素:

  • 您想要執行的搜尋類型,例如 vector、keyword 或 hybrid

  • 您想查詢一個或多個欄位嗎?

  • 是否要手動執行多個查詢,例如關鍵詞查詢和向量搜尋

  • 您是否需要將查詢細分為子查詢

  • 您是否應該在查詢中使用篩選

您的提示調度器可能會使用靜態方法或根據提示的內容線索動態結合不同的方法。 下列各節將說明這些選項,以協助您尋找適合您工作負載的方法。

搜尋類型

搜尋平臺通常支援全文檢索和向量搜尋。 某些平臺,例如 AI 搜尋,支援混合式搜尋。

向量搜尋 比較向量查詢(提示)與向量欄位之間的相似度。 如需詳細資訊,請參閱 選擇 Azure 服務進行向量搜尋

重要

在內嵌查詢之前,您應該先執行您在區塊上進行的相同 清除作業。 例如,如果您將內嵌區塊中的每個單字都小寫,則應該先將查詢中的每個單字小寫,然後再內嵌。

注意

您可以針對相同查詢中的多個向量欄位執行向量搜尋。 在 AI 搜尋中,這種做法會被視為混合式搜尋。 如需詳細資訊,請參閱 混合式搜尋

下列範例程式代碼會針對 contentVector 字段執行向量搜尋。

embedding = embedding_model.generate_embedding(
    chunk=str(pre_process.preprocess(query))
)

vector = RawVectorQuery(
    k=retrieve_num_of_documents,
    fields="contentVector",
    vector=embedding,
)

results = client.search(
    search_text=None,
    vector_queries=[vector],
    top=retrieve_num_of_documents,
    select=["title", "content", "summary"],
)

內嵌查詢的程式代碼會先處理查詢。 該前置處理應該是在內嵌之前預先處理區塊的相同程序代碼。 您必須使用相同的嵌入模型來嵌入區塊。

全文搜索 與儲存在索引中的純文本匹配。 從查詢擷取關鍵詞並針對一或多個索引數據行,在全文搜索中使用這些擷取的關鍵詞是常見的作法。 您可以設定全文搜索,在任何字詞或所有字詞相符時傳回相符結果。

實驗,以判斷要針對哪些欄位執行全文搜索。 如 擴充階段文章所述,您應該在內容具有類似語意意義但實體或關鍵詞不同的情況下,使用關鍵詞和實體元數據欄位進行全文搜索。 要考慮進行全文搜索的其他常見欄位包括標題、摘要和區塊文字。

下列範例程式代碼會針對標題、內容和摘要欄位執行全文搜索。

formatted_search_results = []

results = client.search(
    search_text=query,
    top=retrieve_num_of_documents,
    select=["title", "content", "summary"],
)

formatted_search_results = format_results(results)

AI 搜尋支援 混合式查詢, 包含一或多個文字搜尋和一或多個向量搜尋。 平臺會執行每個查詢,取得中間結果,使用 倒數排序融合重新排名這些結果,並返回前 N 個結果。

下列範例程式代碼會針對標題、內容和摘要欄位執行全文搜索。 它也會對 contentVector 和 questionVector 欄位執行向量搜尋。 AI 搜尋會以平行方式執行所有查詢、重新排序結果,並傳回最前面 retrieve_num_of_documents

 embedding = embedding_model.generate_embedding(
    chunk=str(pre_process.preprocess(query))
)
vector1 = RawVectorQuery(
    k=retrieve_num_of_documents,
    fields="contentVector",
    vector=embedding,
)
vector2 = RawVectorQuery(
    k=retrieve_num_of_documents,
    fields="questionVector",
    vector=embedding,
)

results = client.search(
    search_text=query,
    vector_queries=[vector1, vector2],
    top=retrieve_num_of_documents,
    select=["title", "content", "summary"],
)

手動執行多個查詢

您可以手動執行多個查詢,例如向量搜尋和關鍵詞全文搜索。 您可以匯總結果,手動重新 結果,並傳回最上層的結果。 請考慮以下手動執行多重查詢的使用案例:

  • 您可以使用不支援混合式搜尋的搜尋平臺。 您可以使用手動多個查詢來執行您自己的混合式搜尋。

  • 您要對不同的查詢執行全文搜索。 例如,您可以從查詢擷取關鍵詞,並針對關鍵詞元數據欄位執行全文搜索。 然後,您可以擷取實體,並針對實體元數據欄位執行查詢。

  • 您想要控制重新排序過程。

  • 查詢會要求您執行 分解子查詢 擷取多個來源的地面數據。

查詢翻譯

查詢轉譯是RAG解決方案資訊擷取階段的選擇性步驟。 此步驟會將查詢轉換成優化的形式,以獲取更好的結果。 查詢翻譯方法包括增強、分解、重寫和假設性文件嵌入(HyDE)。

查詢增強

查詢擴增是一個轉譯步驟,可讓查詢更簡單且更容易使用,並增強內容。 如果您的查詢很小或模糊,您應該考慮擴增。 例如,請考慮查詢「比較Microsoft收益」。該查詢不包含要比較的時間範圍或時間單位,而且只會指定收益。 請考慮增強版本的查詢,例如「比較今年與去年 Microsoft 在每一季度的收益和收入。」新的查詢是清楚且具體的。

當您增強查詢時,您會維護原始查詢,但新增更多內容。 請勿移除或改變原始查詢,也不會變更查詢的性質。

您可以使用語言模型來增強查詢。 但是您無法擴增所有查詢。 如果您有內容,您可以將它傳遞至語言模型,以增強查詢。 如果您沒有內容,您必須判斷語言模型是否有可用來增強查詢的資訊。 例如,如果您使用大型語言模型,例如 GPT 模型,您可以判斷查詢的相關信息是否可在因特網上取得。 若是如此,您可以使用模型來增強查詢。 否則,您不應該擴大查詢。

在下列提示中,語言模型會擴展查詢。 此提示包括查詢有內容和無內容時的範例。 如需詳細資訊,請參閱 RAG 實驗加速器 GitHub 存放庫

Input Processing:

Analyze the input query to identify the core concept or topic.
Check whether the query provides context.
If context is provided, use it as the primary basis for augmentation and explanation.
If no context is provided, determine the likely domain or field, such as science, technology, history, or arts, based on the query.

Query Augmentation:

If context is provided:

Use the given context to frame the query more specifically.
Identify other aspects of the topic not covered in the provided context that enrich the explanation.

If no context is provided, expand the original query by adding the following elements, as applicable:

Include definitions about every word, such as adjective or noun, and the meaning of each keyword, concept, and phrase including synonyms and antonyms.
Include historical context or background information, if relevant.
Identify key components or subtopics within the main concept.
Request information about practical applications or real-world relevance.
Ask for comparisons with related concepts or alternatives, if applicable.
Inquire about current developments or future prospects in the field.

Other Guidelines:

Prioritize information from provided context when available.
Adapt your language to suit the complexity of the topic, but aim for clarity.
Define technical terms or jargon when they're first introduced.
Use examples to illustrate complex ideas when appropriate.
If the topic is evolving, mention that your information might not reflect the very latest developments.
For scientific or technical topics, briefly mention the level of scientific consensus if relevant.
Use Markdown formatting for better readability when appropriate.

Example Input-Output:

Example 1 (With provided context):

Input: "Explain the impact of the Gutenberg Press"
Context Provided: "The query is part of a discussion about revolutionary inventions in medieval Europe and their long-term effects on society and culture."
Augmented Query: "Explain the impact of the Gutenberg Press in the context of revolutionary inventions in medieval Europe. Cover its role in the spread of information, its effects on literacy and education, its influence on the Reformation, and its long-term impact on European society and culture. Compare it to other medieval inventions in terms of societal influence."

Example 2 (Without provided context):

Input: "Explain CRISPR technology"
Augmented Query: "Explain CRISPR technology in the context of genetic engineering and its potential applications in medicine and biotechnology. Cover its discovery, how it works at a molecular level, its current uses in research and therapy, ethical considerations surrounding its use, and potential future developments in the field."
Now, provide a comprehensive explanation based on the appropriate augmented query.

Context: {context}

Query: {query}

Augmented Query:

分解

複雜的查詢需要一個以上的數據收集來將模型系結在一起。 例如,查詢「電動汽車如何運作,並與內燃機(ICE)車輛相比有什麼不同?」可能需要從多個來源獲取可靠數據。 一位消息人士可能會描述電動汽車的運作方式,其中另一個將電動汽車與ICE車輛進行比較。

分解是將複雜查詢分解成多個較小且更簡單子查詢的程式。 您可以獨立執行每個分解的查詢,並將所有已分解查詢的最佳結果匯總為累積的上下文。 接著,您會執行原始查詢,其會將累積的內容傳遞至語言模型。

您應該在執行任何搜尋之前,先判斷查詢是否需要多個搜尋。 如果您需要多個子查詢,您可以針對所有查詢執行 手動多個查詢。 使用語言模型來判斷是否建議多個子查詢。

下列提示會將查詢分類為簡單或複雜。 如需詳細資訊,請參閱 RAG 實驗加速器 GitHub 存放庫

Consider the given question to analyze and determine whether it falls into one of these categories:

1. Simple, factual question
  a. The question asks for a straightforward fact or piece of information.
  b. The answer can likely be found stated directly in a single passage of a relevant document.
  c. Breaking the question down further is unlikely to be beneficial.
  Examples: "What year did World War 2 end?", "What is the capital of France?", "What are the features of productX?"

2. Complex, multipart question
  a. The question has multiple distinct components or asks for information about several related topics.
  b. Different parts of the question likely need to be answered by separate passages or documents.
  c. Breaking the question down into subquestions for each component provides better results.
  d. The question is open-ended and likely to have a complex or nuanced answer.
  e. Answering the question might require synthesizing information from multiple sources.
  f. The question might not have a single definitive answer and could warrant analysis from multiple angles.
  Examples: "What were the key causes, major battles, and outcomes of the American Revolutionary War?", "How do electric cars work and how do they compare to gas-powered vehicles?"

Based on this rubric, does the given question fall under category 1 (simple) or category 2 (complex)? The output should be in strict JSON format. Ensure that the generated JSON is 100% structurally correct, with proper nesting, comma placement, and quotation marks. There shouldn't be a comma after the last element in the JSON.

Example output:
{
  "category": "simple"
}

您也可以使用語言模型來分解複雜的查詢。 下列提示會分解複雜的查詢。 如需詳細資訊,請參閱 RAG 實驗加速器 GitHub 儲存庫

Analyze the following query:

For each query, follow these specific instructions:

- Expand the query to be clear, complete, fully qualified, and concise.
- Identify the main elements of the sentence, typically a subject, an action or relationship, and an object or complement. Determine which element is being asked about or emphasized (usually the unknown or focus of the question). Invert the sentence structure. Make the original object or complement the new subject. Transform the original subject into a descriptor or qualifier. Adjust the verb or relationship to fit the new structure.
- Break the query down into a set of subqueries that have clear, complete, fully qualified, concise, and self-contained propositions.
- Include another subquery by using one more rule: Identify the main subject and object. Swap their positions in the sentence. Adjust the wording to make the new sentence grammatically correct and meaningful. Ensure that the new sentence asks about the original subject.
- Express each idea or fact as a standalone statement that can be understood with the help of the given context.
- Break down the query into ordered subquestions, from least to most dependent.
- The most independent subquestion doesn't require or depend on the answer to any other subquestion or prior knowledge.
- Try having a complete subquestion that has all information only from the base query. There's no other context or information available.
- Separate complex ideas into multiple simpler propositions when appropriate.
- Decontextualize each proposition by adding necessary modifiers to nouns or entire sentences. Replace pronouns, such as it, he, she, they, this, and that, with the full name of the entities that they refer to.
- If you still need more questions, the subquestion isn't relevant and should be removed.

Provide your analysis in the following YAML format, and strictly adhere to the following structure. Don't output anything extra, including the language itself.

type: interdependent
queries:
- [First query or subquery]
- [Second query or subquery, if applicable]
- [Third query or subquery, if applicable]
- ...

Examples:

1. Query: "What is the capital of France?"
type: interdependent
queries:
    - What is the capital of France?

2. Query: "Who is the current CEO of the company that created the iPhone?"
type: interdependent
queries:
    - Which company created the iPhone?
    - Who is the current CEO of Apple? (identified in the previous question)

3. Query: "What is the population of New York City, and what is the tallest building in Tokyo?"
type: multiple_independent
queries:
    - What is the population of New York City?
    - What is the tallest building in Tokyo?

Now, analyze the following query:

{query}

重寫

輸入查詢可能不是擷取基礎數據的最佳形式。 您可以使用語言模型來重寫查詢,並取得更好的結果。 重寫查詢以解決下列挑戰:

  • 含糊
  • 遺漏關鍵詞
  • 不必要的單字
  • 不清楚的語意

下列提示會使用語言模型來重寫查詢。 如需詳細資訊,請參閱 RAG 實驗加速器 GitHub 存放庫

Rewrite the given query to optimize it for both keyword-based and semantic-similarity search methods. Follow these guidelines:

- Identify the core concepts and intent of the original query.
- Expand the query by including relevant synonyms, related terms, and alternate phrasings.
- Maintain the original meaning and intent of the query.
- Include specific keywords that are likely to appear in relevant documents.
- Incorporate natural language phrasing to capture semantic meaning.
- Include domain-specific terminology if applicable to the query's context.
- Ensure that the rewritten query covers both broad and specific aspects of the topic.
- Remove ambiguous or unnecessary words that might confuse the search.
- Combine all elements into a single, coherent paragraph that flows naturally.
- Aim for a balance between keyword richness and semantic clarity.

Provide the rewritten query as a single paragraph that incorporates various search aspects, such as keyword-focused, semantically focused, or domain-specific aspects.

query: {original_query}

HyDE 技術

HyDE 是 RAG 解決方案的替代資訊擷取技術。 HyDE 使用語言模型從查詢中生成答案,而不是將查詢轉換為嵌入向量,並使用這些嵌入向量在向量資料庫中尋找最相似的相符項。 這些答案會轉換成嵌入向量,用來尋找最相近的匹配。 此過程可讓 HyDE 執行答覆對答覆的嵌入相似性搜尋。

將查詢翻譯合併到流程

可以使用多個翻譯查詢。 您甚至可以同時使用這四個翻譯。 下圖展示了一個範例,說明如何將這些翻譯合併成一個管線。

顯示具有查詢轉換器的RAG管線的圖表。

管線具有下列步驟:

  1. 可選的查詢增強步驟會接收原始查詢。 此步驟會輸出原始查詢和增強查詢。

  2. 選擇性查詢分解步驟會接收增強的查詢。 此步驟會輸出原始查詢、增強查詢和分解的查詢。

  3. 每個分解的查詢都會執行三個子步驟。 所有分解的查詢經過子步驟後,輸出會包含原始查詢、增強查詢、分解查詢,以及累積的上下文。 累積的上下文包含通過子步驟處理的所有分解查詢中最前面的 N 個結果的匯總。 子步驟包含下列工作:

    1. 選擇性查詢重寫器會重寫已分解的查詢。

    2. 搜尋索引會處理重寫的查詢或原始查詢。 它會使用搜尋類型來執行查詢,例如向量、全文檢索、混合式或手動多重。 搜尋索引也可以使用進階查詢功能,例如 HyDE。

    3. 結果會重新排名。 最高 N 重新排序的結果會新增至累積的上下文。

  4. 原始查詢及其累積的上下文會經歷與每個分解查詢相同的三個子步驟。 但只有一個查詢會經歷這些步驟,而呼叫者會收到前 N 個 結果。

在查詢中傳遞影像

某些多模式模型,例如 GPT-4V 和 GPT-4o,可以解譯影像。 如果您使用這些模型,您可以避免將影像區塊化,並將影像當做提示的一部分傳遞至多模式模型。 您應該進行實驗,以判斷此方法在傳遞和不傳遞額外內容時,影象區塊化的表現如何。 您也應該比較成本差異,並執行成本效益分析。

篩選查詢

若要篩選查詢,您可以在設定為可篩選的搜尋存放區中使用字段。 請考慮使用這些欄位來篩選查詢的關鍵詞和實體,以便幫助縮小結果範圍。 使用篩選來消除不相關的數據。 只擷取符合索引中特定條件的數據。 這種做法可改善查詢的整體效能,並提供更相關的結果。 若要判斷篩選是否有利於您的案例,請執行實驗和測試。 請考慮沒有關鍵詞或關鍵詞不正確、縮寫或縮略字等因素。

權重欄位

在 AI 搜尋中,您可以加權欄位,以根據準則影響結果的排名。

注意

本節說明 AI 搜尋加權功能。 如果您使用不同的數據平臺,請研究該平臺的加權功能。

AI 搜尋支援評分模型,其中包含用於加權字段的 參數,以及用於數值資料的函數。 評分配置檔僅適用於非向量欄位。 對向量和混合式搜尋的支持處於預覽狀態。 您可以在索引上建立多個評分配置檔,並選擇性地選擇依每個查詢使用一個。

您加權的欄位取決於查詢類型和使用案例。 例如,如果查詢是以關鍵詞為中心,例如「Microsoft的總部在哪裡?」,您希望評分配置提高實體或關鍵詞欄位的權重。 您可以將不同的設定檔用於不同的使用者,允許使用者選擇他們的焦點,或根據應用程式選擇設定檔。

在生產系統中,您應該只維護在生產環境中實際使用的設定檔。

使用重新排序

使用重新排序來執行一或多個查詢,整合結果,並對這些結果進行排名。 請考慮下列受益於重新排序搜尋結果的情境:

  • 手動執行多個搜尋,而且您想要匯總結果並加以排名。

  • 向量和關鍵詞搜尋不一定正確。 您想要增加您搜尋結果中傳回的文件數量,這其中可能包含原本可能被忽略的有效結果,並使用重新排序來評估結果。

您可以使用語言模型或交叉編碼器來執行重新排序。 某些平臺,例如 AI 搜尋,擁有重新排序結果的專屬方法。 您可以評估數據的這些選項,以判斷最適合您案例的選項。 下列各節提供這些方法的詳細數據。

語言模型重新排序

下列範例語言模型提示會重新排序結果。 如需詳細資訊,請參閱 RAG實驗加速器

Each document in the following list has a number next to it along with a summary of the document. A question is also provided.
Respond with the numbers of the documents that you should consult to answer the question, in order of relevance, and the relevance score as a JSON string based on JSON format as shown in the schema section. The relevance score is a number from 1 to 10 based on how relevant you think the document is to the question. The relevance score can be repetitive. Don't output any other text, explanation, or metadata apart from the JSON string. Just output the JSON string, and strip every other text. Strictly remove the last comma from the nested JSON elements if it's present.
Don't include any documents that aren't relevant to the question. There should be exactly one document element.

Example format:
Document 1:
content of document 1
Document 2:
content of document 2
Document 3:
content of document 3
Document 4:
content of document 4
Document 5:
content of document 5
Document 6:
content of document 6
Question: user-defined question

schema:
{
    "documents": {
        "document_1": "Relevance",
        "document_2": "Relevance"
    }
}

跨編碼器重新排序

下列範例會使用 Hugging Face 所提供的 交叉編碼器來載入 Roberta 模型。 它會逐一查看每個區塊,並使用模型來計算相似度,以提供值。 它會將結果排序,並回傳排名前 N 名的 個結果。 如需詳細資訊,請參閱 RAG 實驗加速器 GitHub 存放庫

from sentence_transformers import CrossEncoder
...

model_name = 'cross-encoder/stsb-roberta-base'
model = CrossEncoder(model_name)

cross_scores_ques = model.predict(
    [[user_prompt, item] for item in documents],
    apply_softmax=True,
    convert_to_numpy=True,
)

top_indices_ques = cross_scores_ques.argsort()[-k:][::-1]
sub_context = []
for idx in list(top_indices_ques):
    sub_context.append(documents[idx])

語義排序

AI 搜尋具有稱為 語意排名的專屬功能。 這項功能會使用從 Microsoft Bing 改編的深度學習模型,以提升最語意上相關的結果。 如需詳細資訊,請參閱 語意排名器的運作方式

考慮其他搜尋建議

當您實作搜尋解決方案時,請考慮下列一般指引:

  • 從搜尋傳回標題、摘要、來源和未經清除的內容欄位。

  • 預先判斷您是否需要將查詢分解成子查詢。

  • 在多個字段上執行向量和文字查詢。 當您收到查詢時,您不知道向量搜尋還是文字搜尋是否更好。 而且您不知道向量搜尋或關鍵詞搜尋應該搜尋的理想字段。 您可以搜尋多個字段,也許需要進行多次查詢,然後重新排序結果,並傳回具有最高分數的結果。

  • 篩選關鍵詞和實體欄位以縮小結果範圍。

  • 使用關鍵詞進行向量搜尋。 關鍵詞會將結果篩選為較小的子集。 向量存放區會針對該子集工作,以尋找最佳的相符項目。

評估您的搜尋結果

在準備階段中,您會 收集的測試查詢,以及測試檔資訊。 您可以使用您在該階段中收集的下列資訊來評估搜尋結果:

  • 查詢:範例查詢
  • 上下文:測試檔中回應範例查詢之所有文字的集合。

若要評估搜尋解決方案,您可以使用下列建立完善的擷取評估方法:

  • K 的準確率: 在搜尋結果總數中正確識別相關項目的百分比。 此計量著重於搜尋結果的正確性。

  • 在 K 召回: 在前 K 中相關項目的百分比,相對於可能的相關項目總數。 此計量著重於搜尋結果涵蓋範圍。

  • 平均對等排名(MRR): 排名搜尋結果中第一個相關答案的倒數排名平均值。 此計量著重於搜尋結果中發生第一個相關結果的位置。

您應該測試正面和負面範例。 針對正面範例,您希望計量盡可能接近 1。 針對負面範例,您的數據不應該能夠回答查詢,您希望指標盡可能接近 0。 您應該測試所有測試查詢。 平均正查詢結果和負面查詢結果,以了解搜尋結果在匯總中的執行方式。

下一步