KScript ドキュメント
JA

言語

英語から自動翻訳されています。原文は英語です。

言語

言語モデル: KScript.AI

KScript.AI を使うと、スクリプトから Claude に問い合わせられます。API キーは ANTHROPIC_API_KEY 環境変数から取得します(ant コマンドがインストールされている場合は、ANTHROPIC_AUTH_TOKEN のトークン、または保存された ant auth login プロファイルから取得します)。スクリプトから直接指定することはありません。ANTHROPIC_BASE_URL を使うと、プロキシなど別のホストにリクエストを送信できます。スクリプトは、別のモデルを指定しない限り claude-opus-5-5 を使用します。KSCRIPT_AI_MODEL でモデルを指定することもできます。

<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によって、モデルに呼び出し方が伝えられます。ツールが失敗した場合は、そのエラーがモデルに伝えられ、処理を続行できます。
  • テストでは、Claudeの代わりに@fake_llm(replies)を使用します。詳しくは言語モデルのフェイク化を参照してください。
  • エラーはAI型を持つので、tryのcatch eで$e.typeを確認し、キーの欠落、拒否、max_tokensによる応答の打ち切りを処理できます。Claudeの安全性チェックがリクエストを拒否した場合、APIはまず代替モデルで再試行します。