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

This class follows separation of concerns by:

  1. Focusing on core planning logic and data generation

  2. Returning structured data (ExecutionPlan) instead of formatted strings

  3. Delegating presentation concerns to the ExecutionPlan class

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



31
32
33
34
35
36
37
38
39
40
# File 'lib/agentic/task_planner.rb', line 31

def initialize(goal, llm_config = LlmConfig.new)
  @goal = goal
  @tasks = []
  @expected_answer = ExpectedAnswerFormat.new(
    format: "Undetermined",
    sections: [],
    length: "Undetermined"
  )
  @llm_config = llm_config
end

Instance Attribute Details

#expected_answerExpectedAnswerFormat (readonly)

Returns The expected answer format.

Returns:



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

def expected_answer
  @expected_answer
end

#goalString (readonly)

Returns The goal to be accomplished.

Returns:

  • (String)

    The goal to be accomplished



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

def goal
  @goal
end

#llm_configLlmConfig (readonly)

Returns The configuration for the LLM.

Returns:

  • (LlmConfig)

    The configuration for the LLM



26
27
28
# File 'lib/agentic/task_planner.rb', line 26

def llm_config
  @llm_config
end

#tasksArray<TaskDefinition> (readonly)

Returns The list of tasks to accomplish the goal.

Returns:

  • (Array<TaskDefinition>)

    The list of tasks to accomplish the goal



20
21
22
# File 'lib/agentic/task_planner.rb', line 20

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/agentic/task_planner.rb', line 44

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)

  if response.successful?
    @tasks = response.content["tasks"].map do |task_data|
      TaskDefinition.new(
        description: task_data["description"],
        agent: AgentSpecification.new(
          name: task_data["agent"]["name"],
          description: task_data["agent"]["description"],
          instructions: task_data["agent"]["instructions"]
        )
      )
    end
  else
    Agentic.logger.error("Failed to analyze goal: #{response.error&.message || response.refusal}")
    @tasks = []
  end
end

#determine_expected_answervoid

This method returns an undefined value.

Determines the expected answer format using LLM



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/agentic/task_planner.rb', line 88

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)

  if response.successful?
    @expected_answer = ExpectedAnswerFormat.new(
      format: response.content["format"],
      sections: response.content["sections"],
      length: response.content["length"]
    )
  else
    Agentic.logger.error("Failed to determine expected answer format: #{response.error&.message || response.refusal}")
    @expected_answer = ExpectedAnswerFormat.new(
      format: "Undetermined",
      sections: [],
      length: "Undetermined"
    )
  end
end

#execution_planExecutionPlan

Returns an ExecutionPlan object representing the execution plan

Returns:



118
119
120
# File 'lib/agentic/task_planner.rb', line 118

def execution_plan
  ExecutionPlan.new(@tasks, @expected_answer)
end

#planExecutionPlan

Executes the entire planning process

Returns:



124
125
126
127
128
# File 'lib/agentic/task_planner.rb', line 124

def plan
  analyze_goal
  determine_expected_answer
  execution_plan
end