Ir al contenido

Cuando los documentos no lo saben ​

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: cuando los documentos no cubren una pregunta, decirlo — en lugar de dejar que el modelo responda de memoria.

yaml
# Recipe: when the documents do not cover the question, say so — do not guess.
paths:
  components: ./components

llm:
  model: phi3

Los mismos tres documentos que en Respuestas con sus fuentes. Sin minRelevance, los fragmentos más cercanos siempre vuelven, estén relacionados con la pregunta o no. Con él, un fragmento menos relevante que el mínimo se descarta; cuando no queda ninguno, answer_result.found es false y el modelo nunca se llama.

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

El mínimo depende del modelo de embeddings y del tamaño de los fragmentos. Imprime s.relevance para algunas preguntas que tus documentos responden y algunas que no, y pon el mínimo entre ellas — por eso la página de arriba lo muestra junto a cada fuente.

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-9.

Licencia MIT · Hecho con VitePress