Ir al contenido

Respuestas con sus fuentes ​

Traducción automática

Esta página se tradujo automáticamente del inglés y todavía no la revisó un hablante nativo; las correcciones son bienvenidas en GitHub. Si algo no coincide, vale el original en inglés.

Tarea: una página que responde preguntas sobre una tienda a partir de sus documentos de políticas, y muestra de qué documento salió cada respuesta.

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: phi3

Tres archivos Markdown en knowledge/:

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 lee la carpeta, la divide en fragmentos y calcula sus embeddings. q:llm knowledge="docs" recupera los fragmentos más cercanos a la pregunta y se los envía al modelo numerados, con la instrucción de responder solo a partir de ellos y citarlos como [1]. answer_result.sources lista lo que se recuperó; answer_result.grounded dice si la respuesta cita algo de eso.

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

Probado: en CI estas pruebas se ejecutan contra un servidor de modelo de reemplazo, que responde a partir de la primera fuente que recibe; antes de cada versión se ejecutan contra un modelo real (tests/live_ai/test_cookbook_ai.py). Por eso verifican la estructura — qué fuente, qué herramienta, qué muestra la página cuando algo falla — y nunca las palabras del modelo.

Ver IA-2, IA-6 y la guía de IA (en inglés).

Licencia MIT · Hecho con VitePress