KScript 문서
KO

언어

영어에서 자동 번역되었습니다. 원문은 영어입니다.

언어

언어 모델: 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는 모델에게 도구를 호출하는 방법을 알려줍니다. 도구가 실패하면 모델에게 오류가 전달되고, 모델은 작업을 계속할 수 있습니다.
  • 테스트는 Claude 대신 @fake_llm(replies)를 사용합니다. 언어 모델 흉내 내기를 참고하세요.
  • 오류는 AI 타입을 가지므로, try와 catch e를 함께 사용하면 $e.type를 확인하고 누락된 키, 거부, 또는 max_tokens로 인해 잘린 응답을 처리할 수 있어요. Claude의 안전 검사가 요청을 거부하면 API는 먼저 대체 모델로 요청을 다시 시도해요.

Edit this page on GitHub