Ir al contenido

Transmitir una respuesta ​

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: un modelo puede tardar segundos en responder; mostrar la página de inmediato y la respuesta a medida que llega.

yaml
# Recipe: the page renders at once and the answer appears as it is written.
paths:
  components: ./components

llm:
  model: phi3

Con stream="true", q:llm no espera: la recuperación ya se hizo, así que las fuentes están en la página, y <ui:stream for="answer"> va completando la respuesta a medida que el modelo la escribe — con el propio script del framework, sin JavaScript que escribir. Sin JavaScript es un enlace, y quantum console también muestra la respuesta a medida que llega.

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">
    <!-- stream="true": the page does not wait for the model. The sources are
         known at once; the answer is read from /_stream/… as it is written. -->
    <q:llm name="answer" knowledge="docs" top="2" stream="true">
      <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">
      <!-- The framework's own script fills it in; without JavaScript, a link
           opens the answer. -->
      <ui:stream for="answer" />
      <q:loop items="{answer_result.sources}" var="s">
        <ui:text>[{s.n}] {s.name}</ui:text>
      </q:loop>
    </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 page renders with its sources before the answer" page="/">
  <test:visit q="How many days do I have to return an order?" />
  <test:expect text="[1] returns.md" />
  <test:expect text="Read the answer" />
</q:test>
text
tests/stream.test.q
  PASS  the page renders with its sources before the answer
1 passed, 0 failed

El stream pertenece al visitante que preguntó, se lee una vez y vence en diez minutos.

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

Licencia MIT · Hecho con VitePress