流式输出回答
机器翻译
本页由英文原文机器翻译而来,尚未经过母语审校,欢迎在 GitHub 上提出修改。内容如有出入,以英文原文为准。
任务: 模型可能要几秒钟才能回答;立即显示页面,回答到达时逐步显示。
yaml
# Recipe: the page renders at once and the answer appears as it is written.
paths:
components: ./components
llm:
model: phi3使用 stream="true" 时,q:llm 不会等待:检索已经完成,所以来源已经在页面上, <ui:stream for="answer"> 随着模型的输出逐步填入回答——通过框架自带的脚本, 不需要编写 JavaScript。没有 JavaScript 时,它是一个链接;quantum console 也会显示逐步到达的回答。
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这个流属于提问的访问者,只能读取一次,十分钟后过期。
已测试: 在 CI 中,这些测试针对一个替身模型服务器运行,它根据收到的第一个来源作答;每次发布前,它们针对真实模型运行(tests/live_ai/test_cookbook_ai.py)。因此它们检查的是结构——哪个来源、哪个工具、失败时页面显示什么——而从不检查模型的措辞。
参见 IA-7。