Skip to content

クイックスタート

このガイドでは、プロジェクトで Koog の使用を開始する方法を説明します。

前提条件

環境およびプロジェクトが以下の要件を満たしていることを確認してください:

  • JDK 17+

  • Kotlin 2.2.0+

  • Gradle 8.0+ または Maven 3.8+

Koog のインストール

Koogパッケージを依存関係として追加します:


dependencies {

    // 安定版

    implementation("ai.koog:koog-agents:1.2.0")

    // ベータ版

    implementation("ai.koog:koog-agents-additions:1.2.0-beta")

}

dependencies {

    // 安定版

    implementation 'ai.koog:koog-agents:1.2.0'

    // ベータ版

    implementation 'ai.koog:koog-agents-additions:1.2.0-beta'

}
xml

<dependency>

    <!-- 安定版 -->

    <dependency>

        <groupId>ai.koog</groupId>

        <artifactId>koog-agents-jvm</artifactId>

        <version>1.2.0</version>

    </dependency>

    <!-- ベータ版 -->

    <dependency>

        <groupId>ai.koog</groupId>

        <artifactId>koog-agents-additions-jvm</artifactId>

        <version>1.2.0-beta</version>

    </dependency>

</dependency>
モジュールのバージョニング

Koog はセマンティックバージョニング(X.Y.Z)に従います。安定版モジュール(例:1.0.0)は API が保証されていますが、ベータ版モジュール(例:1.0.0-beta)は実験的なものであり、リリース間で変更される可能性があります。

詳細はモジュールのバージョニングを参照してください。

ナイトリービルド

develop ブランチからのナイトリービルドは、JetBrains Grazie Maven リポジトリに公開されています。

ナイトリービルドを使用するには、ビルド構成に以下のリポジトリを追加してください: https://packages.jetbrains.team/maven/p/grazi/grazie-platform-public

次に、Koog の依存関係を目的のナイトリーバージョンに更新します。ナイトリーバージョンは以下のパターンに従います: [次期メジャーバージョン]-develop-[日付]-[時刻]

利用可能なナイトリービルドはこちらで確認できます。

API キーの設定

Koog を使用するには、サポートされている LLM プロバイダーの API キー、またはローカルで実行されている LLM のいずれかが必要です。

Warning

ソースコードに API キーをハードコードしないでください。 API キーの保存には環境変数を使用してください。

OpenAI API キーを取得し、それを環境変数 OPENAI_API_KEY に割り当てます。

shell
export OPENAI_API_KEY=your-api-key
cmd
setx OPENAI_API_KEY "your-api-key"

Anthropic API キーを取得し、それを環境変数 ANTHROPIC_API_KEY に割り当てます。

shell
export ANTHROPIC_API_KEY=your-api-key
cmd
setx ANTHROPIC_API_KEY "your-api-key"

Gemini API キーを取得し、それを環境変数 GOOGLE_API_KEY に割り当てます。

shell
export GOOGLE_API_KEY=your-api-key
cmd
setx GOOGLE_API_KEY "your-api-key"

DeepSeek API キーを取得し、それを環境変数 DEEPSEEK_API_KEY に割り当てます。

shell
export DEEPSEEK_API_KEY=your-api-key
cmd
setx DEEPSEEK_API_KEY "your-api-key"

OpenRouter API キーを取得し、それを環境変数 OPENROUTER_API_KEY に割り当てます。

shell
export OPENROUTER_API_KEY=your-api-key
cmd
setx OPENROUTER_API_KEY "your-api-key"

Amazon Bedrock API キーを生成し、それを環境変数 BEDROCK_API_KEY に割り当てます。

shell
export BEDROCK_API_KEY=your-api-key
cmd
setx BEDROCK_API_KEY "your-api-key"

Mistral API キーを取得し、それを環境変数 MISTRAL_API_KEY に割り当てます。

shell
export MISTRAL_API_KEY=your-api-key
cmd
setx MISTRAL_API_KEY "your-api-key"

Ollama のドキュメントに従って、Ollama でローカル LLM を実行します。

初めての Koog エージェントの作成

以下の例では、OpenAI API を介して GPT-4o モデルを使用し、シンプルな Koog エージェントを作成して実行します。

kotlin
fun main() = runBlocking {
    // 環境変数 OPENAI_API_KEY から OpenAI API キーを取得します
    val apiKey = System.getenv("OPENAI_API_KEY")
        ?: error("The API key is not set.")

    // エージェントを作成します
    val agent = AIAgent(
        promptExecutor = MultiLLMPromptExecutor(OpenAILLMClient(apiKey)),
        llmModel = OpenAIModels.Chat.GPT4o
    )

    // エージェントを実行します
    val result = agent.run("Hello! How can you help me?")
    println(result)
}
java
// 環境変数 OPENAI_API_KEY から OpenAI API キーを取得します
String apiKey = System.getenv("OPENAI_API_KEY");
if (apiKey == null) {
    throw new RuntimeException("The API key is not set.");
}

// エージェントを作成します
AIAgent<String, String> agent = AIAgent.builder()
    .promptExecutor(new MultiLLMPromptExecutor(openAIClient(apiKey)))
    .llmModel(OpenAIModels.Chat.GPT4o)
    .build();

// エージェントを実行します
String result = agent.run("Hello! How can you help me?");
System.out.println(result);

この例では、以下のような出力が生成されます:

Hello! I'm here to help you with whatever you need. Here are just a few things I can do:

- Answer questions.
- Explain concepts or topics you're curious about.
- Provide step-by-step instructions for tasks.
- Offer advice, notes, or ideas.
- Help with research or summarize complex material.
- Write or edit text, emails, or other documents.
- Brainstorm creative projects or solutions.
- Solve problems or calculations.

Let me know what you need help with—I’m here for you!

以下の例では、Anthropic API を介して Claude Opus 4.1 モデルを使用し、シンプルな Koog エージェントを作成して実行します。

kotlin
fun main() = runBlocking {
    // 環境変数 ANTHROPIC_API_KEY から Anthropic API キーを取得します
    val apiKey = System.getenv("ANTHROPIC_API_KEY")
        ?: error("The API key is not set.")

    // エージェントを作成します
    val agent = AIAgent(
        promptExecutor = MultiLLMPromptExecutor(AnthropicLLMClient(apiKey)),
        llmModel = AnthropicModels.Opus_4_1
    )

    // エージェントを実行します
    val result = agent.run("Hello! How can you help me?")
    println(result)
}
java
// 環境変数 ANTHROPIC_API_KEY から Anthropic API キーを取得します
String apiKey = System.getenv("ANTHROPIC_API_KEY");
if (apiKey == null) {
    throw new RuntimeException("The API key is not set.");
}

// エージェントを作成します
AIAgent<String, String> agent = AIAgent.builder()
    .promptExecutor(new MultiLLMPromptExecutor(anthropicClient(apiKey)))
    .llmModel(AnthropicModels.Opus_4_1)
    .build();

// エージェントを実行します
String result = agent.run("Hello! How can you help me?");
System.out.println(result);

この例では、以下のような出力が生成されます:

Hello! I can help you with:

- **Answering questions** and explaining topics
- **Writing** - drafting, editing, proofreading
- **Learning** - homework, math, study help
- **Problem-solving** and brainstorming
- **Research** and information finding
- **General tasks** - instructions, planning, recommendations

What do you need help with today?

以下の例では、Gemini API を介して Gemini 2.5 Pro モデルを使用し、シンプルな Koog エージェントを作成して実行します。

kotlin
fun main() = runBlocking {
    // 環境変数 GOOGLE_API_KEY から Gemini API キーを取得します
    val apiKey = System.getenv("GOOGLE_API_KEY")
        ?: error("The API key is not set.")

    // エージェントを作成します
    val agent = AIAgent(
        promptExecutor = MultiLLMPromptExecutor(GoogleLLMClient(apiKey)),
        llmModel = GoogleModels.Gemini2_5Pro
    )

    // エージェントを実行します
    val result = agent.run("Hello! How can you help me?")
    println(result)
}
java
// 環境変数 GOOGLE_API_KEY から Gemini API キーを取得します
String apiKey = System.getenv("GOOGLE_API_KEY");
if (apiKey == null) {
    throw new RuntimeException("The API key is not set.");
}

// エージェントを作成します
AIAgent<String, String> agent = AIAgent.builder()
    .promptExecutor(new MultiLLMPromptExecutor(googleClient(apiKey)))
    .llmModel(GoogleModels.Gemini2_5Pro)
    .build();

// エージェントを実行します
String result = agent.run("Hello! How can you help me?");
System.out.println(result);

この例では、以下のような出力が生成されます:

I'm an AI that can help you with tasks involving language and information. You can ask me to:

*   **Answer questions**
*   **Write or edit text** (emails, stories, code, etc.)
*   **Brainstorm ideas**
*   **Summarize long documents**
*   **Plan things** (like trips or projects)
*   **Be a creative partner**

Just tell me what you need

以下の例では、DeepSeek API を介して deepseek-v4-flash モデルを使用し、シンプルな Koog エージェントを作成して実行します。

kotlin
fun main() = runBlocking {
    // 環境変数 DEEPSEEK_API_KEY から DeepSeek API キーを取得します
    val apiKey = System.getenv("DEEPSEEK_API_KEY")
        ?: error("The API key is not set.")

    // エージェントを作成します
    val agent = AIAgent(
        promptExecutor = MultiLLMPromptExecutor(DeepSeekLLMClient(apiKey)),
        llmModel = DeepSeekModels.DeepSeekV4Flash
    )

    // エージェントを実行します
    val result = agent.run("Hello! How can you help me?")
    println(result)
}
java
// 環境変数 DEEPSEEK_API_KEY から DeepSeek API キーを取得します
String apiKey = System.getenv("DEEPSEEK_API_KEY");
if (apiKey == null) {
    throw new RuntimeException("The API key is not set.");
}

// エージェントを作成します
AIAgent<String, String> agent = AIAgent.builder()
    .promptExecutor(new MultiLLMPromptExecutor(deepSeekClient(apiKey)))
    .llmModel(DeepSeekModels.DeepSeekV4Flash)
    .build();

// エージェントを実行します
String result = agent.run("Hello! How can you help me?");
System.out.println(result);

この例では、以下のような出力が生成されます:

Hello! I'm here to assist you with a wide range of tasks, including answering questions, providing information, helping with problem-solving, offering creative ideas, and even just chatting. Whether you need help with research, writing, learning something new, or simply want to discuss a topic, feel free to ask—I’m happy to help! 😊

以下の例では、OpenRouter API を介して GPT-4o モデルを使用し、シンプルな Koog エージェントを作成して実行します。

kotlin
fun main() = runBlocking {
    // 環境変数 OPENROUTER_API_KEY から OpenRouter API キーを取得します
    val apiKey = System.getenv("OPENROUTER_API_KEY")
        ?: error("The API key is not set.")

    // エージェントを作成します
    val agent = AIAgent(
        promptExecutor = MultiLLMPromptExecutor(OpenRouterLLMClient(apiKey)),
        llmModel = OpenRouterModels.GPT4o
    )

    // エージェントを実行します
    val result = agent.run("Hello! How can you help me?")
    println(result)
}
java
// 環境変数 OPENROUTER_API_KEY から OpenRouter API キーを取得します
String apiKey = System.getenv("OPENROUTER_API_KEY");
if (apiKey == null) {
    throw new RuntimeException("The API key is not set.");
}

// エージェントを作成します
AIAgent<String, String> agent = AIAgent.builder()
    .promptExecutor(new MultiLLMPromptExecutor(openRouterClient(apiKey)))
    .llmModel(OpenRouterModels.GPT4o)
    .build();

// エージェントを実行します
String result = agent.run("Hello! How can you help me?");
System.out.println(result);

この例では、以下のような出力が生成されます:

I can answer questions, help with writing, solve problems, organize tasks, and more—just let me know what you need!

以下の例では、Bedrock API を介して Claude Sonnet 4.5 モデルを使用し、シンプルな Koog エージェントを作成して実行します。

kotlin
fun main() = runBlocking {
    // 環境変数 BEDROCK_API_KEY から Bedrock API キーを取得します
    val apiKey = System.getenv("BEDROCK_API_KEY")
        ?: error("The API key is not set.")

    // エージェントを作成します
    val agent = AIAgent(
        promptExecutor = MultiLLMPromptExecutor(
            BedrockLLMClient(
                StaticBearerTokenProvider(apiKey),
                BedrockClientSettings()
            )
        ),
        llmModel = BedrockModels.AnthropicClaude4_5Sonnet
    )

    // エージェントを実行します
    val result = agent.run("Hello! How can you help me?")
    println(result)
}
java
// 環境変数 BEDROCK_API_KEY から Bedrock API キーを取得します
String apiKey = System.getenv("BEDROCK_API_KEY");
if (apiKey == null) {
    throw new RuntimeException("The API key is not set.");
}

// エージェントを作成します
AIAgent<String, String> agent = AIAgent.builder()
    .promptExecutor(simpleBedrockExecutorWithBearerToken(apiKey, new BedrockClientSettings()))
    .llmModel(BedrockModels.INSTANCE.getAnthropicClaude4_5Sonnet())
    .build();

// エージェントを実行します
String result = agent.run("Hello! How can you help me?");
System.out.println(result);

この例では、以下のような出力が生成されます:

Hello! I'm a helpful assistant and I can assist you in many ways, including:

- **Answering questions** on a wide range of topics (science, history, technology, etc.)
- **Writing help** - drafting emails, essays, creative content, or editing text
- **Problem-solving** - working through math problems, logic puzzles, or troubleshooting issues
- **Learning support** - explaining concepts, providing study notes, or tutoring
- **Planning & organizing** - helping with projects, schedules, or breaking down tasks
- **Coding assistance** - explaining programming concepts or helping debug code
- **Creative brainstorming** - generating ideas for projects, stories, or solutions
- **General conversation** - discussing topics or just chatting

 What would you like help with today?

以下の例では、Mistral AI API を介して Mistral Medium 3.1 モデルを使用し、シンプルな Koog エージェントを作成して実行します。

kotlin
fun main() = runBlocking {
    // 環境変数 MISTRAL_API_KEY から Mistral AI API キーを取得します
    val apiKey = System.getenv("MISTRAL_API_KEY")
        ?: error("The API key is not set.")

    // エージェントを作成します
    val agent = AIAgent(
        promptExecutor = MultiLLMPromptExecutor(MistralAILLMClient(apiKey)),
        llmModel = MistralAIModels.Chat.MistralMedium31
    )

    // エージェントを実行します
    val result = agent.run("Hello! How can you help me?")
    println(result)
}
java
// 環境変数 MISTRAL_API_KEY から Mistral AI API キーを取得します
String apiKey = System.getenv("MISTRAL_API_KEY");
if (apiKey == null) {
    throw new RuntimeException("The API key is not set.");
}

// エージェントを作成します
AIAgent<String, String> agent = AIAgent.builder()
    .promptExecutor(new MultiLLMPromptExecutor(mistralAIClient(apiKey)))
    .llmModel(MistralAIModels.Chat.MistralMedium31)
    .build();

// エージェントを実行します
String result = agent.run("Hello! How can you help me?");
System.out.println(result);

この例では、以下のような出力が生成されます:

I can assist you with a wide range of topics and tasks. Here are some examples:

1. **Answering questions**: I can provide information on various subjects, including history, science, technology, literature, and more.
2. **Providing definitions**: If you're unsure about the meaning of a word or phrase, I can help define it for you.
3. **Generating text**: Whether it's writing an email, creating content for social media, or composing a story, I can help with text generation.
4. **Translation**: I can translate text from one language to another.
5. **Conversation**: We can have a chat about any topic that interests you, and I'll respond accordingly.
6. **Language practice**: If you're learning a new language, I can help with pronunciation, grammar, and vocabulary practice.
7. **Brainstorming**: If you're stuck on a problem or need ideas for a project, I can help brainstorm solutions.
8. **Summarization**: If you have a long piece of text and want a summary, I can condense it for you.

What's on your mind? Is there something specific you'd like help with?

以下の例では、Ollama を介してローカルで実行されている llama3.2 モデルを使用し、シンプルな Koog エージェントを作成して実行します。

kotlin
fun main() = runBlocking {
    // エージェントを作成します
    val agent = AIAgent(
        promptExecutor = MultiLLMPromptExecutor(OllamaClient()),
        llmModel = OllamaModels.Meta.LLAMA_3_2
    )

    // エージェントを実行します
    val result = agent.run("Hello! How can you help me?")
    println(result)
}
java
// エージェントを作成します
AIAgent<String, String> agent = AIAgent.builder()
    .promptExecutor(new MultiLLMPromptExecutor(ollamaClient("http://localhost:11434")))
    .llmModel(OllamaModels.Meta.LLAMA_3_2)
    .build();

// エージェントを実行します
String result = agent.run("Hello! How can you help me?");
System.out.println(result);

この例では、以下のような出力が生成されます:

I can assist with various tasks such as answering questions, providing information, and even helping with language-related tasks like proofreading or writing suggestions. What's on your mind today?

次のステップ