KScript ドキュメント
JA

言語

英語から自動翻訳されています。原文は英語です。

テスト

言語モデルを偽装する

@llm、llm、$chat->ask() を通じて Claude に問い合わせるコードは、キーやネットワークがなくてもテストできます。@fake_llm(replies) は、その実行の残りの間すべての質問に応答し、尋ねられた内容を保持します。回答のリストを渡すと先頭から順に使われ、各プロンプトを受け取る関数を渡すこともできます。スキーマ付きで尋ねられた質問には、マップで回答できます。

<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() はモデルに質問された回数を示します。リストが尽きると、次の質問は回答のないプロンプトで失敗します。