Class: Ace::LLM::Organisms::BaseClient

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/llm/organisms/base_client.rb

Overview

BaseClient provides common functionality for all LLM provider clients

Constant Summary collapse

DEFAULT_GENERATION_CONFIG =

Default generation configuration

{
  temperature: 0.7,
  max_tokens: nil,
  top_p: nil,
  top_k: nil
}.freeze
DEFAULT_SYSTEM_PROMPT_SEPARATOR =

Default separator for concatenating system prompts Can be overridden by subclasses if needed

"\n\n---\n\n"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, model: nil, **options) ⇒ BaseClient

Initialize base client with common configuration

Parameters:

  • api_key (String, nil) (defaults to: nil)

    API key (uses env if nil)

  • model (String, nil) (defaults to: nil)

    Model to use (uses default if nil)

  • options (Hash)

    Additional options



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ace/llm/organisms/base_client.rb', line 29

def initialize(api_key: nil, model: nil, **options)
  # Prevent direct instantiation of abstract base class
  if instance_of?(BaseClient)
    raise NotImplementedError, "BaseClient is abstract and cannot be instantiated directly"
  end

  @model = model || default_model
  @base_url = options.fetch(:base_url, self.class::API_BASE_URL)
  @generation_config = self.class::DEFAULT_GENERATION_CONFIG.merge(
    options.fetch(:generation_config, {})
  )

  # Setup API key
  @api_key = api_key || fetch_api_key_from_env

  # Setup HTTP client
  @http_client = Atoms::HTTPClient.new(
    timeout: options.fetch(:timeout, 30),
    max_retries: options.fetch(:max_retries, 3)
  )

  @options = options
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



23
24
25
# File 'lib/ace/llm/organisms/base_client.rb', line 23

def api_key
  @api_key
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



23
24
25
# File 'lib/ace/llm/organisms/base_client.rb', line 23

def base_url
  @base_url
end

#generation_configObject (readonly)

Returns the value of attribute generation_config.



23
24
25
# File 'lib/ace/llm/organisms/base_client.rb', line 23

def generation_config
  @generation_config
end

#http_clientObject (readonly)

Returns the value of attribute http_client.



23
24
25
# File 'lib/ace/llm/organisms/base_client.rb', line 23

def http_client
  @http_client
end

#modelObject (readonly)

Returns the value of attribute model.



23
24
25
# File 'lib/ace/llm/organisms/base_client.rb', line 23

def model
  @model
end

Class Method Details

.provider_nameString

Get the provider name (class method)

Returns:

  • (String)

    Provider name

Raises:

  • (NotImplementedError)


69
70
71
# File 'lib/ace/llm/organisms/base_client.rb', line 69

def self.provider_name
  raise NotImplementedError, "#{name} must implement .provider_name"
end

Instance Method Details

#generate(messages, **options) ⇒ Hash

Generate a response from the LLM

Parameters:

  • messages (Array<Hash>)

    Conversation messages

  • options (Hash)

    Generation options

Returns:

  • (Hash)

    Response with text and metadata

Raises:

  • (NotImplementedError)


57
58
59
# File 'lib/ace/llm/organisms/base_client.rb', line 57

def generate(messages, **options)
  raise NotImplementedError, "Subclasses must implement #generate"
end

#needs_credentials?Boolean

Check if this client needs API credentials

Returns:

  • (Boolean)

    True if credentials are required



75
76
77
# File 'lib/ace/llm/organisms/base_client.rb', line 75

def needs_credentials?
  true
end

#provider_nameString

Get the provider name for this client

Returns:

  • (String)

    Provider name



63
64
65
# File 'lib/ace/llm/organisms/base_client.rb', line 63

def provider_name
  self.class.provider_name
end