KScript documentazione
IT

Lingua

Tradotto automaticamente dall’inglese, che è la versione originale.

Test

Simulare un modello linguistico

Il codice che chiede a Claude tramite @llm, llm o $chat->ask() può essere testato senza una chiave o la rete. @fake_llm(replies) risponde a ogni domanda per il resto dell'esecuzione e conserva ciò che è stato chiesto. Passagli un elenco di risposte, usate in ordine, oppure una funzione che riceve ogni prompt. Una mappa risponde a una domanda posta con uno 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() indica quante volte è stato chiesto al modello. Quando una lista si esaurisce, la domanda successiva fallisce con il prompt che non aveva risposta.