Class: Soka::Agent

Overview

Base class for AI agents that use ReAct pattern

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Soka::Agents::DSLMethods

included

Constructor Details

#initialize(memory: nil, engine: Engines::React, **options) ⇒ Agent

Initialize a new Agent instance

Parameters:

  • memory (Memory, Array, nil) (defaults to: nil)

    The memory instance to use (defaults to new Memory) Can be a Memory instance or an Array of message hashes

  • engine (Class) (defaults to: Engines::React)

    The engine class to use (defaults to Engine::React)

  • options (Hash)

    Configuration options

Options Hash (**options):

  • :max_iterations (Integer)

    Maximum iterations for reasoning

  • :provider (Symbol)

    LLM provider override

  • :model (String)

    LLM model override

  • :api_key (String)

    LLM API key override



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/soka/agent.rb', line 23

def initialize(memory: nil, engine: Engines::React, **options)
  @memory = initialize_memory(memory)
  @thoughts_memory = ThoughtsMemory.new
  @engine = engine

  # Initialize components
  @llm = build_llm(options)
  @tools = build_tools

  # Apply configuration with clear defaults
  apply_configuration(options)
end

Instance Attribute Details

#engineObject (readonly)

Returns the value of attribute engine.



12
13
14
# File 'lib/soka/agent.rb', line 12

def engine
  @engine
end

#instructionsObject (readonly)

Returns the value of attribute instructions.



12
13
14
# File 'lib/soka/agent.rb', line 12

def instructions
  @instructions
end

#llmObject (readonly)

Returns the value of attribute llm.



12
13
14
# File 'lib/soka/agent.rb', line 12

def llm
  @llm
end

#memoryObject (readonly)

Returns the value of attribute memory.



12
13
14
# File 'lib/soka/agent.rb', line 12

def memory
  @memory
end

#think_inObject (readonly)

Returns the value of attribute think_in.



12
13
14
# File 'lib/soka/agent.rb', line 12

def think_in
  @think_in
end

#thoughts_memoryObject (readonly)

Returns the value of attribute thoughts_memory.



12
13
14
# File 'lib/soka/agent.rb', line 12

def thoughts_memory
  @thoughts_memory
end

#toolsObject (readonly)

Returns the value of attribute tools.



12
13
14
# File 'lib/soka/agent.rb', line 12

def tools
  @tools
end

Instance Method Details

#apply_behavior_config(options) ⇒ Object

Apply behavior-related configuration

Parameters:

  • options (Hash)

    Configuration options



53
54
55
56
# File 'lib/soka/agent.rb', line 53

def apply_behavior_config(options)
  @instructions = options.fetch(:instructions) { resolve_instructions }
  @think_in = options.fetch(:think_in) { self.class._think_in } || 'en'
end

#apply_configuration(options) ⇒ Object

Apply configuration options with defaults

Parameters:

  • options (Hash)

    Configuration options



38
39
40
41
# File 'lib/soka/agent.rb', line 38

def apply_configuration(options)
  apply_performance_config(options)
  apply_behavior_config(options)
end

#apply_performance_config(options) ⇒ Object

Apply performance-related configuration

Parameters:

  • options (Hash)

    Configuration options



45
46
47
48
49
# File 'lib/soka/agent.rb', line 45

def apply_performance_config(options)
  @max_iterations = options.fetch(:max_iterations) do
    self.class._max_iterations || Soka.configuration.performance.max_iterations
  end
end

#run(input) {|event| ... } ⇒ Result

Run the agent with the given input

Parameters:

  • input (String)

    The input query or task

Yields:

  • (event)

    Optional block to handle events during execution

Returns:

  • (Result)

    The result of the agent’s reasoning



62
63
64
65
66
67
68
69
# File 'lib/soka/agent.rb', line 62

def run(input, &)
  validate_input(input)
  execute_reasoning(input, &)
rescue ArgumentError
  raise # Re-raise ArgumentError without handling
rescue StandardError => e
  handle_error(e, input)
end