Modelos de lenguaje: KScript.AI
KScript.AI permite que un script consulte a Claude. La clave de API se obtiene de la variable de entorno ANTHROPIC_API_KEY (o de un token en ANTHROPIC_AUTH_TOKEN, o del perfil que ant auth login guarda, cuando está instalado el comando ant), nunca desde el script. ANTHROPIC_BASE_URL envía las solicitudes a otro host, como un proxy. Los scripts usan claude-opus-5-5 salvo que pidan otro modelo o que KSCRIPT_AI_MODEL indique uno.
<import package="KScript.AI" />
<!-- One question, one answer. -->
<echo>{ @llm('Write a haiku about XML.') }</echo>
<!-- A prompt written as a template, with a system prompt. -->
<let name="customer" value="{ name: 'Ann' }" />
<let name="message" value="'My order is late arrived.'" />
<llm into="reply" system="You answer customer emails politely and briefly.">{$customer.name} wrote: {$message}</llm>
<!-- Structured output: the answer is a map with these fields. -->
<let name="report" value="'The login page spins forever on Safari.'" />
<llm into="ticket" schema="{ category: 'bug|feature|question', urgency: 'int 1-5', summary: 'one line' }">Triage this report: {$report}</llm>
<echo>{$ticket.category} ({$ticket.urgency}): {$ticket.summary}</echo>
<!-- Tools: the model may call your fn functions, and sees what they return. -->
<fn name="order_status" params="id: int" description="The shipping status of an order." value="$id == 42 ? 'shipped' : 'unknown'" />
<llm into="answer" tools="order_status">Where is order 42?</llm>
<!-- A conversation that remembers what was said. -->
<let name="chat" value="@chat('You are a patient maths tutor.')" />
<echo>{ $chat->ask('What is a prime number?') }</echo>
<echo>{ $chat->ask('Is 91 one?') }</echo>import KScript.AI
// One question, one answer.
echo "{ @llm('Write a haiku about XML.') }"
// A prompt written as a template, with a system prompt.
$customer = { name: 'Ann' }
$message = 'My order is late arrived.'
llm(into: reply, system: 'You answer customer emails politely and briefly.') "{$customer.name} wrote: {$message}"
// Structured output: the answer is a map with these fields.
$report = 'The login page spins forever on Safari.'
llm(into: ticket, schema: { category: 'bug|feature|question', urgency: 'int 1-5', summary: 'one line' }) "Triage this report: {$report}"
echo "{$ticket.category} ({$ticket.urgency}): {$ticket.summary}"
// Tools: the model may call your fn functions, and sees what they return.
/// The shipping status of an order.
fn order_status(id: int) => $id == 42 ? 'shipped' : 'unknown'
llm(into: answer, tools: [@order_status]) "Where is order 42?"
// A conversation that remembers what was said.
$chat = @chat('You are a patient maths tutor.')
echo "{ $chat->ask('What is a prime number?') }"
echo "{ $chat->ask('Is 91 one?') }"llmy@llm(prompt, ...)hacen la misma pregunta con las mismas opciones:system,schema,tools(una lista como[@order_status]),modelymax_tokens(16.000 por defecto). La única diferencia está en de dónde viene el mensaje y adónde va la respuesta: la instrucciónllmtoma el mensaje como su texto y guarda la respuesta ento:, mientras que@llm()toma el mensaje como un valor y devuelve la respuesta, por lo que puedes usarlo dentro de una expresión.@llm()recibe sus opciones por nombre:@llm($prompt, system: 'Be brief.', max_tokens: 500).- Esquemas: cada campo es obligatorio.
'string','number','int','bool'y'date'indican un tipo,'int 1-5'indica un rango,'a|b|c'enumera las opciones, un mapa es un objeto anidado,['string']es una lista, y cualquier otro texto describe un campo de cadena. - Las herramientas son funciones
fn. Suparams(con tipos comoid: int) ydescriptionindican al modelo cómo llamarlas. Si una herramienta falla, se le informa del error y puede seguir adelante. - Las pruebas usan
@fake_llm(replies)en lugar de Claude: consulta Simular un modelo de lenguaje. - Los errores tienen el tipo
AI, así que untryconcatch epuede comprobar$e.typey gestionar una clave que falta, una negativa o una respuesta cortada pormax_tokens. Cuando los controles de seguridad de Claude rechazan una solicitud, la API primero la reintenta con un modelo de respaldo.