Skip to content

LLMベースのプランナー

ベータ

この機能はベータモジュール(1.2.0-beta)の一部です。将来のリリースでAPIが変更される可能性があります。

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

LLMベースのプランナーは、LLMを使用してプランの生成と評価を行います。 これらは文字列ベースの状態(string-based state)に基づいて動作し、LLMリクエストを通じて各ステップを実行します。 文字列ベースの状態とは、エージェントの状態が単一の文字列であることを意味します。 各ステップにおいて、エージェントは初期状態の文字列を受け取り、結果として最終状態の文字列を返します。

前提条件

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

  • JDK 17+

  • Kotlin 2.2.0+

  • Gradle 8.0+ または Maven 3.8+ 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>

LLMプロバイダーからAPIキーを取得するか、Ollamaを介してローカルLLMを実行してください。

詳細については、クイックスタートを参照してください。 このページの例では、環境変数 OPENAI_API_KEY が設定されていることを前提としています。

Koogは2つのシンプルなプランナーを提供しています:

  • SimpleLLMPlanner は、最初に一度だけプランを生成し、完了するまでそのプランに従います。 再プランニング(replanning)を含めるには、SimpleLLMPlanner を拡張して assessPlan メソッドをオーバーライドし、エージェントがいつ再プランニングすべきかを指定します。
  • SimpleLLMWithCriticPlanner は、LLMリクエストを介してプランの妥当性を確認し、エージェントが再プランニングすべきかどうかを評価するためにLLMを使用する assessPlan メソッドを実装しています。

以下の例は、SimpleLLMPlanner を使用してシンプルなプランナーエージェントを作成する方法を示しています:

kotlin
// プランナーの作成
val planner = SimpleLLMPlanner()

// プランナーストラテジーでラップする
val strategy = AIAgentPlannerStrategy(
    name = "simple-planner",
    planner = planner
)

// エージェントの設定
val agentConfig = AIAgentConfig(
    prompt = prompt("planner") {
        system("You are a helpful planning assistant.")
    },
    model = OpenAIModels.Chat.GPT4o,
    maxAgentIterations = 50
)

// プランナーエージェントの作成
val agent = PlannerAIAgent(
    promptExecutor = simpleOpenAIExecutor(System.getenv("OPENAI_API_KEY")),
    strategy = strategy,
    agentConfig = agentConfig
)

suspend fun main() {
    // タスクを指定してエージェントを実行
    val result = agent.run("Create a plan to organize a team meeting")
    println(result)
}
java
// LLMベースのプランナーを使用してプランナーストラテジーを作成する
AIAgentPlannerStrategy<String, String> strategy =
    Planners.llmBased("simple-planner")
        .build();

// OpenAIエグゼキューターの作成
var promptExecutor = PromptExecutor.builder()
    .openAI("OPENAI_API_KEY")
    .build();

// AIAgentビルダーを使用してプランナーエージェントを作成する
AIAgent<String, String> agent = AIAgent.builder()
    .plannerStrategy(strategy)
    .promptExecutor(promptExecutor)
    .llmModel(OpenAIModels.Chat.GPT4o)
    .systemPrompt("You are a helpful planning assistant.")
    .maxIterations(50)
    .build();

// タスクを指定してエージェントを実行
String result = agent.run("Create a plan to organize a team meeting");
System.out.println(result);

次のステップ