Modelli linguistici: KScript.AI
KScript.AI permette a uno script di interrogare Claude. La chiave API viene dalla variabile d'ambiente ANTHROPIC_API_KEY (oppure da un token in ANTHROPIC_AUTH_TOKEN, o dal profilo salvato con ant auth login, quando è installato il comando ant), mai dallo script. ANTHROPIC_BASE_URL invia le richieste a un altro host, come un proxy. Gli script usano claude-opus-5-5 a meno che non chiedano un altro modello, o KSCRIPT_AI_MODEL ne indichi 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?') }"llme@llm(prompt, ...)pongono la stessa domanda con le stesse opzioni:system,schema,tools(una lista come[@order_status]),modelemax_tokens(16.000 per impostazione predefinita). L'unica differenza sta in dove viene il prompt e dove va la risposta: l'istruzionellmprende il prompt come testo e salva la risposta into:, mentre@llm()prende il prompt come valore e restituisce la risposta, quindi può stare dentro un'espressione.@llm()prende le sue opzioni per nome:@llm($prompt, system: 'Be brief.', max_tokens: 500).- Schemi: ogni campo è obbligatorio.
'string','number','int','bool'e'date'indicano un tipo,'int 1-5'dà un intervallo,'a|b|c'elenca le scelte, una mappa è un oggetto annidato,['string']è una lista, e ogni altro testo descrive un campo stringa. - Gli strumenti sono funzioni
fn. La loroparams(con tipi comeid: int) edescriptiondicono al modello come chiamarle. Se uno strumento fallisce, il modello riceve l'errore e può continuare. - I test usano
@fake_llm(replies)al posto di Claude: vedi Simulare un modello linguistico. - Gli errori hanno il tipo
AI, quindi untryconcatch epuò controllare$e.typee gestire una chiave mancante, un rifiuto o una risposta troncata damax_tokens. Quando i controlli di sicurezza di Claude respingono una richiesta, l'API la ripete prima su un modello di ripiego.