Simular un modelo de lenguaje
El código que consulta a Claude mediante @llm, llm o $chat->ask() puede probarse sin una clave ni conexión de red. @fake_llm(replies) responde a todas las preguntas durante el resto de la ejecución y guarda lo que se preguntó. Dale una lista de respuestas, que se usan en orden, o una función a la que se le pasa cada prompt. Un mapa responde a una pregunta formulada con un esquema:
<import package="KScript.AI" />
<fn name="triage" params="report: string" value="@llm("Triage this report: { $report }", 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 cuántas veces se consultó al modelo. Cuando una lista se queda sin elementos, la siguiente pregunta falla con el prompt que no tenía respuesta.