Class: Soka::LLM

Inherits:
Object
  • Object
show all
Defined in:
lib/soka/llm.rb

Overview

LLM wrapper class that delegates to specific provider implementations

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider_name = nil, **options) ⇒ LLM

Initialize LLM with specified provider

Parameters:

  • provider_name (Symbol, nil) (defaults to: nil)

    The provider to use (:gemini, :openai, :anthropic)

  • options (Hash)

    Provider-specific options (model, api_key, etc.)



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/soka/llm.rb', line 11

def initialize(provider_name = nil, **options)
  provider_name ||= Soka.configuration.ai.provider

  # Merge configuration options if no explicit options provided
  if options.empty? && provider_name == Soka.configuration.ai.provider
    config = Soka.configuration.ai
    options = {
      api_key: config.api_key,
      model: config.model
    }.compact
  end

  @provider = create_provider(provider_name, **options)
end

Instance Attribute Details

#providerObject (readonly)

Returns the value of attribute provider.



6
7
8
# File 'lib/soka/llm.rb', line 6

def provider
  @provider
end

Class Method Details

.build(config) ⇒ LLM

Build LLM instance from configuration object

Parameters:

  • config (Object)

    Configuration with provider, model, and api_key

Returns:

  • (LLM)

    New LLM instance



57
58
59
60
61
62
63
64
# File 'lib/soka/llm.rb', line 57

def self.build(config)
  options = {
    model: config.model,
    api_key: config.api_key
  }.compact

  new(config.provider, **options)
end

Instance Method Details

#chat(messages, **params) ⇒ LLMs::Result

Chat with the LLM

Parameters:

  • messages (Array<Hash>)

    Array of message hashes with role and content

  • params (Hash)

    Additional parameters for the chat

Returns:



30
31
32
# File 'lib/soka/llm.rb', line 30

def chat(messages, **params)
  @provider.chat(messages, **params)
end

#modelString

Get the model being used

Returns:

  • (String)

    The model name



50
51
52
# File 'lib/soka/llm.rb', line 50

def model
  @provider.model
end

#streaming_chat(messages, **params) {|chunk| ... } ⇒ Object

Stream chat responses

Parameters:

  • messages (Array<Hash>)

    Array of message hashes

  • params (Hash)

    Additional parameters

Yields:

  • (chunk)

    Yields each response chunk



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

def streaming_chat(messages, **params, &)
  @provider.streaming_chat(messages, **params, &)
end

#supports_streaming?Boolean

Check if provider supports streaming

Returns:

  • (Boolean)

    True if streaming is supported



44
45
46
# File 'lib/soka/llm.rb', line 44

def supports_streaming?
  @provider.supports_streaming?
end