KScript 文档
ZH

语言

语言模型:KScript.AI

KScript.AI 让脚本可以向 Claude 发起请求。API 密钥来自 ANTHROPIC_API_KEY 环境变量(或来自 ANTHROPIC_AUTH_TOKEN 中的令牌,或在安装了 ant 命令时由 ant auth login 保存的配置文件),而不是来自脚本本身。ANTHROPIC_BASE_URL 会把请求发送到其他主机,例如代理服务器。除非脚本指定了其他模型,或 KSCRIPT_AI_MODEL 指定了某个模型,否则脚本使用 claude-opus-5-5。

<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?') }"
  • llm 和 @llm(prompt, ...) 询问相同的问题,选项也相同:system、schema、tools(如 [@order_status] 这样的列表)、model 和 max_tokens(默认为 16,000)。唯一的区别在于提示从哪里来、答案放到哪里:llm 语句以提示作为其文本,并将答案存入 to:;而 @llm() 以提示作为一个值,并返回答案,因此可以放在表达式中使用。@llm() 则按名称接收其选项:@llm($prompt, system: 'Be brief.', max_tokens: 500)。
  • 模式:每个字段都是必填的。'string'、'number'、'int'、'bool' 和 'date' 用于指定类型,'int 1-5' 给出取值范围,'a|b|c' 列出可选值,映射是嵌套对象,['string'] 是列表,其他任何文本都表示字符串字段。
  • 工具是 fn 函数。它们的 params(包括 id: int 等类型)和 description 会告诉模型如何调用它们。如果某个工具失败,模型会收到错误信息,并可以继续执行。
  • 测试使用 @fake_llm(replies) 而不是 Claude:参见 模拟语言模型。
  • 错误的类型为 AI,因此可以使用 try 配合 catch e 来检查 $e.type,并处理缺失的密钥、请求被拒绝,或回答因 max_tokens 被截断的情况。当 Claude 的安全检查拒绝某个请求时,API 会先用备用模型重试该请求。