KScript 문서
KO

언어

영어에서 자동 번역되었습니다. 원문은 영어입니다.

테스트

언어 모델을 흉내 내기

@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()는 모델에게 질문한 횟수를 나타내요. 목록이 다 떨어지면, 답이 없는 프롬프트와 함께 다음 질문이 실패해요.