跳到正文

当模型失败时 ​

机器翻译

本页由英文原文机器翻译而来,尚未经过母语审校,欢迎在 GitHub 上提出修改。内容如有出入,以英文原文为准。

任务: 使用模型的页面在模型服务器宕机、变慢或缺少模型时,仍然应该能工作——并说明发生了什么。

yaml
# Recipe: the page goes on, and says so, when the model cannot answer.
paths:
  components: ./components

llm:
  model: phi3

没有 onerror 时,AI 失败会停止页面,并给出指明服务器和原因的错误。有了 onerror="continue",页面会继续:summary_result.success 为 false, summary_result.error.message 说明原因,summary 为空。这个示例把 endpoint= 指向一个没有运行的服务器,所以它在任何地方都以同样的方式失败。

xml
<q:component name="Summary">
  <q:set name="text" value="Returns are accepted within 30 days of delivery, with the receipt." />

  <!-- endpoint= sends this one call to another server — here one that is not
       running, to show the failure. onerror="continue": instead of stopping
       the page, the failure is in summary_result and the value is empty. -->
  <q:llm name="summary" endpoint="http://127.0.0.1:9" timeout="5" onerror="continue">
    <q:prompt>Summarize in five words: {text}</q:prompt>
  </q:llm>

  <ui:window title="Policy">
    <ui:text>{text}</ui:text>
    <q:if condition="summary_result.success">
      <ui:text>In short: {summary}</ui:text>
      <q:else>
        <ui:alert variant="info">No summary right now — the assistant is unavailable.</ui:alert>
        <ui:text>Why: {summary_result.error.message}</ui:text>
      </q:else>
    </q:if>
  </ui:window>
</q:component>
xml
<q:test name="the page still renders, and says why" page="/">
  <test:visit />
  <test:expect status="200" />
  <test:expect text="Returns are accepted within 30 days" />
  <test:expect text="No summary right now" />
  <test:expect text="127.0.0.1:9" />
  <test:expect no-text="In short:" />
</q:test>
text
tests/failure.test.q
  PASS  the page still renders, and says why
1 passed, 0 failed

onerror 在 q:llm、q:knowledge、q:agent 和 q:query 上的作用相同。

参见 IA-5。

MIT 许可证 · 使用 VitePress 构建