带来源的回答
机器翻译
本页由英文原文机器翻译而来,尚未经过母语审校,欢迎在 GitHub 上提出修改。内容如有出入,以英文原文为准。
任务: 一个根据商店的政策文档回答问题的页面,并显示每个回答来自哪份文档。
yaml
# Recipe: answers from your own documents, with the sources they came from.
paths:
components: ./components
# The model server is Ollama at http://localhost:11434 unless
# QUANTUM_LLM_BASE_URL says otherwise; the model is this one unless
# QUANTUM_LLM_DEFAULT_MODEL says otherwise.
llm:
model: phi3knowledge/ 中的三个 Markdown 文件:
md
# Returns
Returns are accepted within 30 days of delivery, with the receipt. The refund
goes back to the card used for the order.q:knowledge 读取这个文件夹,把它拆分成文本块并计算嵌入。q:llm knowledge="docs" 检索与问题最接近的文本块,编号后发送给模型,并指示它只根据这些文本块回答、像 [1] 这样引用它们。answer_result.sources 列出检索到的内容;answer_result.grounded 说明回答是否引用了其中任何一项。
xml
<q:component name="Ask">
<!-- The documents in knowledge/, split into chunks and embedded when the
page runs. persist="false" keeps the index in memory; without it, it is
stored in ./.quantum/knowledge and reused until a document changes. -->
<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">
<!-- The question retrieves the closest chunks; they reach the model
numbered, with the instruction to answer only from them and cite
them like [1]. -->
<q:llm name="answer" knowledge="docs" top="2">
<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">
<ui:text>{answer}</ui:text>
<q:if condition="answer_result.grounded">
<ui:text>Sources:</ui:text>
<q:loop items="{answer_result.sources}" var="s">
<ui:text>[{s.n}] {s.name}</ui:text>
</q:loop>
<q:else>
<ui:alert variant="warning">This answer cites none of the documents.</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="the answer comes with the document it is from" page="/">
<test:visit q="How many days do I have to return an order?" />
<test:expect text="Sources:" />
<test:expect text="[1] returns.md" />
</q:test>
<q:test name="another question, another document" page="/">
<test:visit q="Is shipping free for my order?" />
<test:expect text="[1] shipping.md" />
</q:test>
<q:test name="no question, no model call" page="/">
<test:visit />
<test:expect no-text="Sources:" />
</q:test>text
tests/ask.test.q
PASS the answer comes with the document it is from
PASS another question, another document
PASS no question, no model call
3 passed, 0 failed已测试: 在 CI 中,这些测试针对一个替身模型服务器运行,它根据收到的第一个来源作答;每次发布前,它们针对真实模型运行(tests/live_ai/test_cookbook_ai.py)。因此它们检查的是结构——哪个来源、哪个工具、失败时页面显示什么——而从不检查模型的措辞。