当文档不知道答案时
机器翻译
本页由英文原文机器翻译而来,尚未经过母语审校,欢迎在 GitHub 上提出修改。内容如有出入,以英文原文为准。
任务: 当文档没有涵盖某个问题时,如实说明——而不是让模型凭记忆回答。
yaml
# Recipe: when the documents do not cover the question, say so — do not guess.
paths:
components: ./components
llm:
model: phi3与带来源的回答中相同的三份文档。没有 minRelevance 时, 最接近的文本块总会返回,不管它们与问题是否相关。有了它,相关度低于下限的文本块会被丢弃; 一个都不剩时,answer_result.found 为 false,模型根本不会被调用。
xml
<q:component name="Ask">
<q:knowledge name="docs" persist="false" chunkSize="300" chunkOverlap="30">
<q:source type="directory" path="knowledge" pattern="*.md" />
</q:knowledge>
<q:set name="question" value="{query.q}" default="" />
<q:if condition="question">
<!-- minRelevance: chunks less relevant than 0.8 are not retrieved. When
none remains, the model is not asked at all — it would answer from
memory — and answer_result.found is false. The right floor depends
on the embedding model and the chunk size: look at the relevance of
a few sources before choosing it. -->
<q:llm name="answer" knowledge="docs" top="2" minRelevance="0.8">
<q:message role="user">{question}</q:message>
</q:llm>
</q:if>
<ui:window title="Ask the store">
<ui:form>
<ui:input bind="q" value="{question}" placeholder="Your question" />
<ui:button variant="primary">Ask</ui:button>
</ui:form>
<q:if condition="question">
<q:if condition="answer_result.found">
<ui:text>{answer}</ui:text>
<q:loop items="{answer_result.sources}" var="s">
<ui:text>[{s.n}] {s.name} ({round(s.relevance, 2)})</ui:text>
</q:loop>
<q:else>
<ui:alert variant="info">Our documents do not answer that. Write to us at help@example.com.</ui:alert>
</q:else>
</q:if>
</q:if>
</ui:window>
</q:component>xml
<!-- Structural checks, never the model's exact words: the same tests run
against a real model before every release. -->
<q:test name="a question the documents answer" page="/">
<test:visit q="How many days do I have to return an order?" />
<test:expect text="[1] returns.md" />
<test:expect no-text="do not answer that" />
</q:test>
<q:test name="a question they do not answer" page="/">
<test:visit q="What is the capital of France?" />
<test:expect text="Our documents do not answer that." />
<test:expect no-text="[1]" />
</q:test>text
tests/honest.test.q
PASS a question the documents answer
PASS a question they do not answer
2 passed, 0 failed下限取决于嵌入模型和文本块大小。对一些你的文档能回答的问题和一些不能回答的问题, 打印 s.relevance,然后把下限设在两者之间——正因如此,上面的页面在每个来源旁边都显示了它。
已测试: 在 CI 中,这些测试针对一个替身模型服务器运行,它根据收到的第一个来源作答;每次发布前,它们针对真实模型运行(tests/live_ai/test_cookbook_ai.py)。因此它们检查的是结构——哪个来源、哪个工具、失败时页面显示什么——而从不检查模型的措辞。
参见 IA-9。