Class: Foobara::Agent::DetermineNextCommandNameAndInputs

Inherits:
LlmBackedCommand show all
Defined in:
foobara-agent-0.0.21/src/foobara/agent/determine_next_command_name_and_inputs.rb

Constant Summary

Constants included from LlmBackedExecuteMethod

LlmBackedExecuteMethod::LLM_INTEGRATION_KEYS

Constants included from TruncatedInspect

TruncatedInspect::MAX_LENGTH

Instance Attribute Summary

Attributes included from LlmBackedExecuteMethod

#answer, #assistant_serializer, #computed_assistant_association_depth, #computed_user_association_depth, #llm_instructions, #messages, #parsed_answer, #user_serializer

Attributes included from CommandPatternImplementation::Concerns::Subcommands

#is_subcommand

Attributes included from CommandPatternImplementation::Concerns::Runtime

#exception, #outcome, #raw_result

Attributes included from CommandPatternImplementation::Concerns::Errors

#error_collection

Attributes included from CommandPatternImplementation::Concerns::Inputs

#inputs, #raw_inputs

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LlmBackedExecuteMethod

#construct_messages, #depth_to_serializer, #determine_assistant_association_depth, #determine_assistant_serializer, #determine_user_association_depth, #determine_user_serializer, #execute, #generate_answer, #parse_answer

Methods included from Concern

foobara_class_methods_module_for, foobara_concern?, included

Methods inherited from Command

install!, reset_all

Methods included from CommandPatternImplementation::Concerns::Reflection

#initialize

Methods included from CommandPatternImplementation::Concerns::DomainMappers

#domain_map, #domain_map!, #run_mapped_subcommand!

Methods included from CommandPatternImplementation::Concerns::Subcommands

#run_subcommand!, #subcommand?

Methods included from CommandPatternImplementation::Concerns::Entities

#load_entities, #load_records

Methods included from CommandPatternImplementation::Concerns::Transactions

#relevant_entity_classes

Methods included from NestedTransactionable

#auto_detect_current_transactions, #commit_transaction, #commit_transaction_if_open, #open_transaction, #opened_transactions, #relevant_entity_classes, relevant_entity_classes_for_type, #relevant_entity_classes_for_type, #rollback_transaction, #transactions, #use_transaction, with_needed_transactions_for_type

Methods included from CommandPatternImplementation::Concerns::StateMachine

#state_machine

Methods included from CommandPatternImplementation::Concerns::Callbacks

#state_machine_callback_registry

Methods included from CommandPatternImplementation::Concerns::Runtime

#execute, #halt!, #run, #run!, #run_execute, #succeed, #success?, #validate, #validate_records

Methods included from CommandPatternImplementation::Concerns::Errors

#initialize

Methods included from CommandPatternImplementation::Concerns::Inputs

#cast_and_validate_inputs, #initialize, #method_missing, #respond_to_missing?, #respond_to_missing_for_inputs?

Methods included from TruncatedInspect

#inspect, truncating

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Foobara::CommandPatternImplementation::Concerns::Inputs

Class Method Details

.build_llm_instructions(assistant_association_depth, goal, previous_goals) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'foobara-agent-0.0.21/src/foobara/agent/determine_next_command_name_and_inputs.rb', line 19

def build_llm_instructions(assistant_association_depth, goal, previous_goals)
  instructions = "You are the implementation of a command called #{scoped_full_name}"

  instructions += if description && !description.empty?
                    " which has the following description:\n\n#{description}\n\n"
                  else
                    # :nocov:
                    ". "
                    # :nocov:
                  end

  result_schema = result_json_schema(assistant_association_depth)

  if previous_goals && !previous_goals.empty?
    instructions += "You have previously accomplished the following goals:\n\n"

    previous_goals.each.with_index do |(previous_goal, state), index|
      instructions += "previous goal #{index + 1}: #{previous_goal}\n"
      instructions += "status of goal #{index + 1}: #{state}\n\n"
    end
  end

  instructions += "You are working towards accomplishing the following goal:\n\n#{goal}\n\n"

  instructions += "Your response of which command to run next should match the following JSON schema:"
  instructions += "\n\n#{result_schema}\n\n"
  instructions += "You can get more details about the inputs and result schemas for a specific command by " \
                  "choosing the DescribeCommand command. " \
                  "You will reply with nothing more than the JSON you've generated so that the calling code " \
                  "can successfully parse your answer."

  instructions
end

.llm_instructions(assistant_association_depth, goal, previous_goals = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'foobara-agent-0.0.21/src/foobara/agent/determine_next_command_name_and_inputs.rb', line 7

def llm_instructions(assistant_association_depth, goal, previous_goals = nil)
  key = [assistant_association_depth, goal, previous_goals]

  @llm_instructions_cache ||= {}

  if @llm_instructions_cache.key?(key)
    @llm_instructions_cache[key]
  else
    @llm_instructions_cache[key] = build_llm_instructions(assistant_association_depth, goal, previous_goals)
  end
end

Instance Method Details

#association_depthObject



82
83
84
85
86
87
88
# File 'foobara-agent-0.0.21/src/foobara/agent/determine_next_command_name_and_inputs.rb', line 82

def association_depth
  if pass_aggregates_to_llm
    Foobara::AssociationDepth::AGGREGATE
  else
    Foobara::AssociationDepth::ATOM
  end
end

#build_messagesObject



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
115
116
117
118
119
120
121
122
# File 'foobara-agent-0.0.21/src/foobara/agent/determine_next_command_name_and_inputs.rb', line 90

def build_messages
  p = [
    {
      content: llm_instructions,
      role: :system
    }
  ]

  context.command_log.each do |command_log_entry|
    agent_entry = {
      command: command_log_entry.command_name
    }

    inputs = command_log_entry.inputs

    if inputs && !inputs.empty?
      agent_entry[:inputs] = inputs
    end

    outcome_entry = command_log_entry.outcome

    p << {
      content: agent_entry,
      role: :assistant
    }
    p << {
      content: outcome_entry,
      role: :user
    }
  end

  p
end

#contextObject



136
137
138
# File 'foobara-agent-0.0.21/src/foobara/agent/determine_next_command_name_and_inputs.rb', line 136

def context
  agent.context
end

#determine_llm_instructionsObject



74
75
76
77
78
79
80
# File 'foobara-agent-0.0.21/src/foobara/agent/determine_next_command_name_and_inputs.rb', line 74

def determine_llm_instructions
  self.llm_instructions = self.class.llm_instructions(
    computed_user_association_depth,
    goal,
    previous_goal_and_status_pairs
  )
end

#goalObject



124
125
126
# File 'foobara-agent-0.0.21/src/foobara/agent/determine_next_command_name_and_inputs.rb', line 124

def goal
  context.current_goal.text
end

#previous_goal_and_status_pairsObject



128
129
130
131
132
133
134
# File 'foobara-agent-0.0.21/src/foobara/agent/determine_next_command_name_and_inputs.rb', line 128

def previous_goal_and_status_pairs
  if context.previous_goals && !context.previous_goals.empty?
    context.previous_goals.map do |previous_goal|
      [previous_goal.text, previous_goal.state]
    end
  end
end