KScript 文档
ZH

测试

伪造语言模型

可以在没有密钥或网络的情况下测试通过 @llm、llm 或 $chat->ask() 向 Claude 提问的代码。@fake_llm(replies) 会在本次运行的其余部分回答每个问题,并保存所提的问题。可以传入按顺序使用的答案列表,也可以传入一个为每个提示词调用的函数。对于带有 schema 的提问,则使用映射来作答:

<import package="KScript.AI" />

<fn name="triage" params="report: string" value="@llm(&quot;Triage this report: { $report }&quot;, schema: { category: 'bug|feature|question' })" />

<test name="a crash report is a bug">
  <let name="fake" value="@fake_llm([{ category: 'bug' }])" />
  <expect value="@triage('The app crashes on start')" has="{ category: 'bug' }" />
  <expect value="$fake->prompts()" equals="['Triage this report: The app crashes on start']" />
</test>

<test name="the reply can depend on the prompt">
  <do>@fake_llm(fn(prompt) => $prompt->contains('French') ? 'Bonjour' : 'Hello')</do>
  <expect value="@llm('Say hello in French')" equals="'Bonjour'" />
</test>
import KScript.AI

fn triage(report: string) => @llm("Triage this report: { $report }", schema: { category: 'bug|feature|question' })

test(name: 'a crash report is a bug') {
  $fake = @fake_llm([{ category: 'bug' }])
  expect(@triage('The app crashes on start'), has: { category: 'bug' })
  expect($fake->prompts(), equals: ['Triage this report: The app crashes on start'])
}

test(name: 'the reply can depend on the prompt') {
  @fake_llm(fn(prompt) => $prompt->contains('French') ? 'Bonjour' : 'Hello')
  expect(@llm('Say hello in French'), equals: 'Bonjour')
}

$fake->count() 表示模型被询问的次数。当列表用完时,下一次询问会以没有答案的提示失败。

Edit this page on GitHub