프롬프트 만들기
원문: Koog Documentation — prompts/prompt-creation/index.md 이 글은 Koog 공식 문서의 prompts/prompt-creation/index.md 페이지를 한국어로 옮긴 번역본입니다. 문서 구조와 링크 의미를 유지하되, MkDocs 전용 UI 문법은 블로그에서 읽기 좋도록 정리했습니다.
프롬프트 만들기
Koog는 메시지 유형, 순서, 내용을 제어하여 프롬프트를 생성하는 구조화된 방법을 제공합니다.
- Kotlin 사용자의 경우 유형이 안전한 Kotlin DSL을 통해.
- Java 사용자의 경우 Fluent Builder API를 통해.
기본 구조
Kotlin의 prompt() 함수 또는 Java의 Prompt.builder() 함수는 고유 ID와 목록이 있는 Prompt 객체를 생성합니다.
메시지 수:
코틀린
1val prompt = prompt("unique_prompt_id") {2 // List of messages3}자바
1Prompt prompt = Prompt.builder("unique_prompt_id")2 // List of messages3 .build();메시지 유형
Kotlin DSL 및 Java 빌더 API는 다음과 같은 유형의 메시지를 지원하며, 각 메시지는 특정 메시지에 해당합니다. 대화에서의 역할:
- 시스템 메시지: LLM에 대한 컨텍스트, 지침 및 제약 조건을 제공하여 해당 동작을 정의합니다.
- 사용자 메시지: 사용자 입력을 나타냅니다.
- 어시스턴트 메시지: Few-Shot 학습이나 대화를 계속하는 데 사용되는 LLM 응답을 나타냅니다.
- 도구 메시지: 도구 호출 및 결과를 나타냅니다.
코틀린
1val prompt = prompt("unique_prompt_id") {2 // Add a system message to set the context3 system("You are a helpful assistant with access to tools.")4 // Add a user message5 user("What is 5 + 3 ?")6 // Add an assistant message7 assistant("The result is 8.")8}자바
1Prompt prompt = Prompt.builder("unique_prompt_id")2 // Add a system message to set the context3 .system("You are a helpful assistant with access to tools.")4 // Add a user message5 .user("What is 5 + 3 ?")6 // Add an assistant message7 .assistant("The result is 8.")8 .build();시스템 메시지
시스템 메시지는 LLM 동작을 정의하고 전체 대화의 컨텍스트를 설정합니다. 모델의 역할과 톤을 지정하고, 응답에 대한 지침과 제약 사항을 제공하고, 응답 예시를 제공할 수 있습니다.
시스템 메시지를 생성하려면 system() Kotlin 함수 또는 Java 메소드에 대한 인수로 문자열을 제공하십시오.
코틀린
1val prompt = prompt("system_message") {2 system("You are a helpful assistant that explains technical concepts.")3}자바
1Prompt prompt = Prompt.builder("system_message")2 .system("You are a helpful assistant that explains technical concepts.")3 .build();사용자 메시지
사용자 메시지는 사용자의 입력을 나타냅니다.
사용자 메시지를 생성하려면 user() Kotlin 함수 또는 Java 메소드에 대한 인수로 문자열을 제공하십시오.
코틀린
1val prompt = prompt("user_message") {2 system("You are a helpful assistant.")3 user("What is Koog?")4}자바
1Prompt prompt = Prompt.builder("user_message")2 .system("You are a helpful assistant.")3 .user("What is Koog?")4 .build();대부분의 사용자 메시지에는 일반 텍스트가 포함되어 있지만 이미지, 오디오, 비디오 등의 다중 모드 콘텐츠도 포함될 수 있습니다. 문서. 자세한 내용과 예시는 Multimodal content을 참조하세요.
어시스턴트 메시지
보조 메시지는 LLM 응답을 나타내며, 이는 향후 유사한 상호 작용에서 소수 학습에 사용될 수 있습니다. 대화를 계속하거나 예상되는 출력 구조를 보여주기 위해.
보조 메시지를 생성하려면 assistant() Kotlin 함수 또는 Java 메소드에 대한 인수로 문자열을 제공하십시오.
코틀린
1val prompt = prompt("article_review") {2 system("Evaluate the article.")34 // Example 15 user("The article is clear and easy to understand.")6 assistant("positive")78 // Example 29 user("The article is hard to read but it's clear and useful.")10 assistant("neutral")1112 // Example 313 user("The article is confusing and misleading.")14 assistant("negative")1516 // New input to classify17 user("The article is interesting and helpful.")18}자바
1Prompt prompt = Prompt.builder("article_review")2 .system("Evaluate the article.")34 // Example 15 .user("The article is clear and easy to understand.")6 .assistant("positive")78 // Example 29 .user("The article is hard to read but it's clear and useful.")10 .assistant("neutral")1112 // Example 313 .user("The article is confusing and misleading.")14 .assistant("negative")1516 // New input to classify17 .user("The article is interesting and helpful.")18 .build();도구 메시지
도구 메시지는 도구 호출과 그 결과를 나타내며, 도구 호출 기록을 미리 채우는 데 사용할 수 있습니다.
참고 LLM은 실행 중에 도구 호출을 생성합니다. 이를 미리 채우면 몇 번의 학습이나 도구 사용 방법을 시연하는 데 도움이 됩니다.
도구 메시지를 생성하려면 Kotlin에서 tool() 함수를 호출하거나 Java에서 toolCall() 및 toolResult() 메서드를 호출하세요.
코틀린
1val prompt = prompt("calculator_example") {2 system("You are a helpful assistant with access to tools.")3 user("What is 5 + 3?")4 tool {5 // Tool call6 call(7 id = "calculator_tool_id",8 tool = "calculator",9 content = """{"operation": "add", "a": 5, "b": 3}"""10 )1112 // Tool result13 result(14 id = "calculator_tool_id",15 tool = "calculator",16 content = "8"17 )18 }1920 // LLM response based on tool result21 assistant("The result of 5 + 3 is 8.")22 user("What is 4 + 5?")23}자바
1Prompt prompt = Prompt.builder("calculator_example")2 .system("You are a helpful assistant with access to tools.")3 .user("What is 5 + 3?")4 // Tool call5 .toolCall("calculator_tool_id", "calculator", "{\"operation\": \"add\", \"a\": 5, \"b\": 3}")6 // Tool result7 .toolResult("calculator_tool_id", "calculator", "8")8 // LLM response based on tool result 9 .assistant("The result of 5 + 3 is 8.")10 .user("What is 4 + 5?")11 .build();문자 메시지 빌더
참고 텍스트 메시지 빌더는 Kotlin에서만 사용할 수 있습니다.
system(), user() 또는 assistant() 메시지를 작성할 때 도우미 text-building functions를 사용할 수 있습니다.
서식 있는 텍스트 형식을 위한 것입니다.
코틀린
1val prompt = prompt("text_example") {2 user {3 +"Review the following code snippet:"4 +"fun greet(name: String) = println(\"Hello, \$name!\")"56 // Paragraph break7 br()8 text("Please include in your explanation:")910 // Indent content11 padding(" ") {12 +"1. What the function does."13 +"2. How string interpolation works."14 }15 }16}Markdown 및 XML 빌더를 사용하여 해당 형식으로 컨텐츠를 추가할 수도 있습니다.
코틀린
1val prompt = prompt("markdown_xml_example") {2 // A user message in Markdown format3 user {4 markdown {5 h2("Evaluate the article using the following criteria:")6 bulleted {7 item { +"Clarity and readability" }8 item { +"Accuracy of information" }9 item { +"Usefulness to the reader" }10 }11 }12 }13 // An assistant message in XML format14 assistant {15 xml {16 xmlDeclaration()17 tag("review") {18 tag("clarity") { text("positive") }19 tag("accuracy") { text("neutral") }20 tag("usefulness") { text("positive") }21 }22 }23 }24}참고 텍스트 작성 기능을 XML 및 마크다운 빌더와 혼합할 수 있습니다.
프롬프트 매개변수
LLM의 동작을 제어하는 매개변수를 구성하여 프롬프트를 사용자 정의할 수 있습니다.
코틀린
1val prompt = prompt(2 id = "custom_params",3 params = LLMParams(4 temperature = 0.7,5 numberOfChoices = 1,6 toolChoice = LLMParams.ToolChoice.Auto7 )8) {9 system("You are a creative writing assistant.")10 user("Write a song about winter.")11}자바
1// Create params first2LLMParams params = new LLMParams(3 0.7, // temperature4 null, // maxTokens5 1, // numberOfChoices6 null, // speculation7 null, // schema8 LLMParams.ToolChoice.Auto.INSTANCE, // toolChoice9 null, // user10 null // additionalProperties11);1213Prompt prompt = Prompt.builder("custom_params")14 .system("You are a creative writing assistant.")15 .user("Write a song about winter.")16 .build();17 18// Apply params to the built prompt19prompt = prompt.withParams(params);다음 매개변수가 지원됩니다.
temperature: 모델 응답의 무작위성을 제어합니다.toolChoice: 모델의 도구 호출 동작을 제어합니다.numberOfChoices: 여러 대체 응답을 요청합니다.schema: 모델의 응답 형식에 대한 구조를 정의합니다.maxTokens: 응답의 토큰 수를 제한합니다.speculation: 예상 응답 형식에 대한 힌트를 제공합니다(특정 모델에서만 지원됨).
자세한 내용은 LLM parameters을 참조하세요.
기존 프롬프트 확장
Kotlin에서 prompt() 함수를 호출하거나 Java에서 Prompt.builder()을 호출하여 기존 프롬프트를 확장할 수 있습니다.
기존 프롬프트를 인수로 사용:
코틀린
1val basePrompt = prompt("base") {2 system("You are a helpful assistant.")3 user("Hello!")4 assistant("Hi! How can I help you?")5}67val extendedPrompt = prompt(basePrompt) {8 user("What's the weather like?")9}자바
1Prompt basePrompt = Prompt.builder("base")2 .system("You are a helpful assistant.")3 .user("Hello!")4 .assistant("Hi! How can I help you?")5 .build();67Prompt extendedPrompt = Prompt.builder(String.valueOf(basePrompt))8 .user("What's the weather like?")9 .build();이렇게 하면 basePrompt의 모든 메시지와 새 사용자 메시지를 포함하는 새 프롬프트가 생성됩니다.
다음 단계
- multimodal content 작업 방법을 알아보세요.
- 단일 LLM 제공업체와 협력하는 경우 LLM clients으로 프롬프트를 실행하세요.
- 여러 LLM 제공업체와 협력하는 경우 prompt executors으로 프롬프트를 실행하세요.
- cache control에서 llm 캐시를 사용하는 방법을 알아보세요.