Answers with their sources
Task: a page that answers questions about a store from its policy documents, and shows which document each answer came from.
# 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: phi3Three Markdown files in knowledge/:
# 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 reads the folder, splits it into chunks and embeds them. q:llm knowledge="docs" retrieves the chunks closest to the question and sends them to the model numbered, with the instruction to answer only from them and cite them like [1]. answer_result.sources lists what was retrieved; answer_result.grounded says whether the answer cites any of it.
<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><!-- 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>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 failedTested: in CI these tests run against a stand-in model server, which answers from the first source it is given; before every release they run against a real model (tests/live_ai/test_cookbook_ai.py). That is why they check structure — which source, which tool, what the page shows on failure — and never the model's words.
See IA-2, IA-6 and the AI guide.