在 Azure AI 搜索中返回语义答案

在调用语义排序和标题时,您可以选择从最佳匹配的文档中提取直接回答查询的内容。 响应中可以包含一个或多个答案,然后可以在搜索页上呈现这些答案,以提高应用的用户体验。

语义答案是搜索索引中的逐字内容,阅读理解模型已将其识别为请求中提出的查询的答案。 这不是生成的答案。 有关使用生成式 AI 根据内容撰写答案的聊天式用户交互模型的指导,请参阅检索增强生成 (RAG)

在本文中,了解如何请求语义答案、解压缩响应,并找出哪些内容特征最有利于生成高质量的答案。

先决条件

适用于 语义查询 的所有先决条件也适用于答案,包括 服务层级和区域

  • 查询逻辑必须包括语义查询参数“queryType=semantic”,以及“answers”参数。 本文将讨论所需的参数。

  • 用户输入的查询字符串必须能够识别为一个问题(什么、何处、何时、如何)。

  • 索引中的搜索文档必须包含具有答案特征的文本,并且该文本必须存在于语义配置中列出的某个字段中。 例如,给定查询“什么是哈希表”,如果语义配置中没有字段包含包含“哈希表为...”的段落,则不太可能返回答案。

什么是语义答案?

语义答案是 语义查询响应的子结构。 它由搜索文档中的一个或多个字面段落构成,表述为对类似于问题的查询给出的答案。 若要返回答案,短语或句子必须存在于具有答案语言特征的搜索文档中,并且查询本身必须作为问题提出。

Azure AI 搜索使用计算机阅读理解模型来识别和选取最佳答案。 该模型从可用内容生成一组潜在的答案,当它达到足够高的置信度时,它将建议一个作为答案。

答案在查询响应有效负载中作为独立的顶级对象返回,你可以选择在搜索页面上呈现,并附带搜索结果。 在结构上,它是响应中的数组元素,由文本、文档键和置信度分数组成。

为“答案”构建 REST 查询

若要返回语义答案,查询必须具有语义性的 "queryType""queryLanguage""semanticConfiguration""answers" 参数。 指定这些参数不能保证答案,但请求必须包含它们才能进行应答处理。

{
    "search": "how do clouds form",
    "queryType": "semantic",
    "queryLanguage": "en-us",
    "semanticConfiguration": "my-semantic-config",
    "answers": "extractive|count-3",
    "captions": "extractive|highlight-true",
    "count": "true"
}
  • 查询字符串不得为 null,应作为问题进行表述。

  • "queryType" 必须设置为 "semantic"。

  • "queryLanguage" 必须是 受支持语言列表(REST API)中的值之一。

  • "semanticConfiguration" 确定哪些字符串字段向提取模型提供标记。 生成标题的相同字段也会生成答案。

  • 对于 "answers",参数构造为 "answers": "extractive",其中,返回的默认答案数为 1。 可通过添加 count(如上例所示)来增加答案的数量(最多 10 个)。 是否需要多个答案取决于应用的用户体验,以及呈现结果的方式。

解开响应中的 "answer"

数组中 "@search.answers" 提供了答案,该数组首先显示在查询响应中。 数组中的每个答案包括:

  • 文档密钥
  • 答案文本或内容,以纯文本或格式化方式
  • 置信度分数

如果答案不确定,响应将显示为 "@search.answers": []。 答案数组后跟值数组,这是语义查询中的标准响应。

鉴于查询“云如何形成”,该查询可以定向到基于 美国宇航局地球手册的内容构建的索引,以下示例演示了一个逐字答案(在第 38 页中找到):

{
    "@search.answers": [
        {
            "key": "4123",
            "text": "Sunlight heats the land all day, warming that moist air and causing it to rise high into the   atmosphere until it cools and condenses into water droplets. Clouds generally form where air is ascending (over land in this case),   but not where it is descending (over the river).",
            "highlights": "Sunlight heats the land all day, warming that moist air and causing it to rise high into the   atmosphere until it cools and condenses into water droplets. Clouds generally form<em> where air is ascending</em> (over land in this case),   but not where it is<em> descending</em> (over the river).",
            "score": 0.94639826
        }
    ],
    "value": [
        {
            "@search.score": 0.5479723,
            "@search.rerankerScore": 1.0321671911515296,
            "@search.captions": [
                {
                    "text": "Like all clouds, it forms when the air reaches its dew point—the temperature at which an air mass is cool enough for its water vapor to condense into liquid droplets. This false-color image shows valley fog, which is common in the Pacific Northwest of North America.",
                    "highlights": "Like all<em> clouds</em>, it<em> forms</em> when the air reaches its dew point—the temperature at    which an air mass is cool enough for its water vapor to condense into liquid droplets. This false-color image shows valley<em> fog</em>, which is common in the Pacific Northwest of North America."
                }
            ],
            "title": "Earth Atmosphere",
            "content": "Fog is essentially a cloud lying on the ground. Like all clouds, it forms when the air reaches its dew point—the temperature at  \n\nwhich an air mass is cool enough for its water vapor to condense into liquid droplets.\n\nThis false-color image shows valley fog, which is common in the Pacific Northwest of North America. On clear winter nights, the \n\nground and overlying air cool off rapidly, especially at high elevations. Cold air is denser than warm air, and it sinks down into the \n\nvalleys. The moist air in the valleys gets chilled to its dew point, and fog forms. If undisturbed by winds, such fog may persist for \n\ndays. The Terra satellite captured this image of foggy valleys northeast of Vancouver in February 2010.\n\n\n",
            "locations": [
                "Pacific Northwest",
                "North America",
                "Vancouver"
            ]
        }
    ]
}

设计包含答案的搜索结果页时,请务必处理找不到答案的情况。

在@search.answers中:

  • “key” 是匹配的文档密钥或 ID。 给定文档密钥后,可以使用 查找文档 API 检索搜索文档的任何或所有部分,以包含在搜索页或详细信息页上。

  • 在纯文本和突出显示中,“text”“highlights”提供相同的内容。

    默认情况下,突出显示样式为 <em>,可以使用现有的 highlightPreTag 和 highlightPostTag 参数替代这些突出显示。 如其他地方所述,答案的实质内容是搜索文档中的逐字内容。 提取模型查找答案的特征来查找相应的内容,但不在响应中撰写新语言。

  • “分数” 是反映答案强度的置信度分数。 如果响应中有多个答案,则此分数用于确定顺序。 顶部答案和顶部标题可以派生自不同的搜索文档,其中顶部答案源自一个文档,顶部标题来自另一个文档,但一般情况下,相同的文档显示在每个数组的顶部位置。

答案之后紧跟 “value” 数组,该数组始终包括分数、标题和任何默认可检索的字段。 如果指定了 select 参数,则“value”数组仅限于指定的字段。

生成高质量答案的建议

为获得最佳结果,请返回具有以下特征的文档语料库的语义答案:

  • "semanticConfiguration" 必须包含提供足够文本(可以在其中找到答案)的字段。 应首先在“优先内容字段”中列出可能包含答案的字段。 只有文档中的逐字文本才能显示为答案。

  • 查询字符串不得为 null(search=*),字符串应具有问题的特征,例如“what is”或“how to”,而不是任意顺序包含字词或短语的关键字搜索。 如果查询字符串似乎不是问题,则会跳过答案处理,即使请求将“answers”指定为查询参数也是如此。

  • 语义提取和汇总对于每个文档中可以及时分析的标记数有限制。 实际上,如果你有成百页的大型文档,请尝试先将内容分解成较小的文档。

后续步骤