Modelos de linguagem: KScript.AI
KScript.AI permite que um script consulte o Claude. A chave de API vem da variável de ambiente ANTHROPIC_API_KEY (ou de um token em ANTHROPIC_AUTH_TOKEN, ou do perfil que ant auth login salvou, quando o comando ant está instalado), nunca do script. ANTHROPIC_BASE_URL envia as requisições para outro host, como um proxy. Os scripts usam claude-opus-5-5, a menos que peçam outro modelo, ou que KSCRIPT_AI_MODEL informe um.
<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, ...)fazem a mesma pergunta com as mesmas opções:system,schema,tools(uma lista como[@order_status]),modelemax_tokens(16.000 por padrão). A única diferença é de onde vem o prompt e para onde vai a resposta: a instruçãollmrecebe o prompt como seu texto e armazena a resposta emto:, enquanto@llm()recebe o prompt como um valor e retorna a resposta, de modo que pode ser usado dentro de uma expressão.@llm()recebe suas opções pelo nome:@llm($prompt, system: 'Be brief.', max_tokens: 500).- Esquemas: cada campo é obrigatório.
'string','number','int','bool'e'date'nomeiam um tipo,'int 1-5'define um intervalo,'a|b|c'lista as opções, um mapa é um objeto aninhado,['string']é uma lista, e qualquer outro texto descreve um campo de string. - Ferramentas são funções
fn. Suaparams(com tipos comoid: int) edescriptioninformam ao modelo como chamá-las. Se uma ferramenta falhar, o modelo recebe o erro e pode continuar. - Testes usam
@fake_llm(replies)em vez do Claude: veja Simulando um modelo de linguagem. - Erros têm o tipo
AI, então umtrycomcatch epode verificar$e.typee tratar uma chave ausente, uma recusa ou uma resposta cortada pormax_tokens. Quando as verificações de segurança do Claude recusam uma solicitação, a API primeiro tenta novamente com um modelo de fallback.