Class: Agentic::TaskPlanner

Inherits:
Object
  • Object
show all
Defined in:
lib/agentic/task_planner.rb

Overview

Handles the task planning process for Agentic using LLM

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(goal, llm_config = LlmConfig.new) ⇒ TaskPlanner

Initializes a new TaskPlanner

Parameters:

  • goal (String)

    The goal to be accomplished

  • llm_config (LlmConfig) (defaults to: LlmConfig.new)

    The configuration for the LLM



21
22
23
24
25
26
# File 'lib/agentic/task_planner.rb', line 21

def initialize(goal, llm_config = LlmConfig.new)
  @goal = goal
  @tasks = []
  @expected_answer = {}
  @llm_config = llm_config
end

Instance Attribute Details

#expected_answerHash (readonly)

Returns The expected answer format.

Returns:

  • (Hash)

    The expected answer format



13
14
15
# File 'lib/agentic/task_planner.rb', line 13

def expected_answer
  @expected_answer
end

#goalString (readonly)

Returns The goal to be accomplished.

Returns:

  • (String)

    The goal to be accomplished



7
8
9
# File 'lib/agentic/task_planner.rb', line 7

def goal
  @goal
end

#llm_configLlmConfig (readonly)

Returns The configuration for the LLM.

Returns:

  • (LlmConfig)

    The configuration for the LLM



16
17
18
# File 'lib/agentic/task_planner.rb', line 16

def llm_config
  @llm_config
end

#tasksArray<Hash> (readonly)

Returns The list of tasks to accomplish the goal.

Returns:

  • (Array<Hash>)

    The list of tasks to accomplish the goal



10
11
12
# File 'lib/agentic/task_planner.rb', line 10

def tasks
  @tasks
end

Instance Method Details

#analyze_goalvoid

This method returns an undefined value.

Analyzes the goal and breaks it down into tasks using LLM



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/agentic/task_planner.rb', line 30

def analyze_goal
  system_message = "You are an expert project planner. Your task is to break down complex goals into actionable tasks."
  user_message = "Goal: #{@goal}\n\nBreak this goal down into a series of tasks. For each task:\n1. Specify the type of agent best suited to complete it.\n2. Include a brief description of the agent\n3. Include a set of instructions that the agent can follow to perform this task."

  schema = StructuredOutputs::Schema.new("tasks") do |s|
    s.array :tasks, items: {
      type: "object",
      properties: {
        description: {type: "string"},
        agent: {
          type: "object",
          properties: {
            name: {type: "string"},
            description: {type: "string"},
            instructions: {type: "string"}
          },
          required: %w[name description instructions]
        }
      },
      required: %w[description agent]
    }
  end

  response = llm_request(system_message, user_message, schema)
  @tasks = response[:content]["tasks"]
end

#determine_expected_answervoid

This method returns an undefined value.

Determines the expected answer format using LLM



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/agentic/task_planner.rb', line 59

def determine_expected_answer
  system_message = "You are an expert in report structuring and formatting. Your task is to determine the best format for a given report goal."
  user_message = "Goal: #{@goal}\n\nDetermine the optimal format, sections, and length for a report addressing this goal."

  schema = StructuredOutputs::Schema.new("answer_format") do |s|
    s.string :format
    s.array :sections, items: {type: "string"}
    s.string :length
  end

  response = llm_request(system_message, user_message, schema)
  @expected_answer = response[:content]
end

#display_planString

Displays the execution plan

Returns:

  • (String)

    The formatted execution plan



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/agentic/task_planner.rb', line 75

def display_plan
  plan = "Execution Plan:\n\n"
  @tasks.each_with_index do |task, index|
    plan += "#{index + 1}. #{task["description"]} (Agent: #{task["agent"].inspect})\n"
  end
  plan += "\nExpected Answer:\n"
  plan += "Format: #{@expected_answer["format"]}\n"
  plan += "Sections: #{@expected_answer["sections"].join(", ")}\n"
  plan += "Length: #{@expected_answer["length"]}\n"
  plan
end

#planString

Executes the entire planning process

Returns:

  • (String)

    The formatted execution plan



89
90
91
92
93
# File 'lib/agentic/task_planner.rb', line 89

def plan
  analyze_goal
  determine_expected_answer
  display_plan
end