Class: Broadlistening::Provider

Inherits:
Object
  • Object
show all
Defined in:
lib/broadlistening/provider.rb

Constant Summary collapse

PROVIDERS =
{
  openai: {
    api_key_env: "OPENAI_API_KEY",
    model: "gpt-4o-mini",
    embedding_model: "text-embedding-3-small"
  },
  azure: {
    api_key_env: "AZURE_OPENAI_API_KEY",
    base_url_env: "AZURE_OPENAI_URI",
    model: "gpt-4o-mini",
    embedding_model: "text-embedding-3-small"
  },
  gemini: {
    api_key_env: "GEMINI_API_KEY",
    base_url: "https://generativelanguage.googleapis.com/v1beta/openai/",
    model: "gemini-2.0-flash",
    embedding_model: "text-embedding-004"
  },
  openrouter: {
    api_key_env: "OPENROUTER_API_KEY",
    base_url: "https://openrouter.ai/api/v1",
    model: "gpt-4o-mini",
    embedding_model: "text-embedding-3-small"
  },
  local: {
    api_key: "not-needed",
    model: "gpt-4o-mini",
    embedding_model: "text-embedding-3-small"
  }
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, local_llm_address: nil) ⇒ Provider



46
47
48
49
50
# File 'lib/broadlistening/provider.rb', line 46

def initialize(name, local_llm_address: nil)
  @name = name
  @local_llm_address = local_llm_address || "localhost:11434"
  @config = PROVIDERS.fetch(name) { raise ConfigurationError, "Unknown provider: #{name}" }
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



36
37
38
# File 'lib/broadlistening/provider.rb', line 36

def name
  @name
end

Class Method Details

.supported?(name) ⇒ Boolean



38
39
40
# File 'lib/broadlistening/provider.rb', line 38

def self.supported?(name)
  PROVIDERS.key?(name)
end

.supported_namesObject



42
43
44
# File 'lib/broadlistening/provider.rb', line 42

def self.supported_names
  PROVIDERS.keys
end

Instance Method Details

#api_keyObject



52
53
54
# File 'lib/broadlistening/provider.rb', line 52

def api_key
  @config[:api_key] || ENV.fetch(@config[:api_key_env], nil)
end

#azure?Boolean



78
79
80
# File 'lib/broadlistening/provider.rb', line 78

def azure?
  @name == :azure
end

#base_urlObject



56
57
58
59
60
# File 'lib/broadlistening/provider.rb', line 56

def base_url
  return "http://#{@local_llm_address}/v1" if @name == :local

  @config[:base_url] || (@config[:base_url_env] && ENV.fetch(@config[:base_url_env], nil))
end

#build_openai_client(api_key:, base_url:, azure_api_version: nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/broadlistening/provider.rb', line 82

def build_openai_client(api_key:, base_url:, azure_api_version: nil)
  if azure?
    OpenAI::Client.new(
      access_token: api_key,
      uri_base: base_url,
      api_type: :azure,
      api_version: azure_api_version
    )
  elsif base_url
    OpenAI::Client.new(access_token: api_key, uri_base: base_url)
  else
    OpenAI::Client.new(access_token: api_key)
  end
end

#default_embedding_modelObject



66
67
68
# File 'lib/broadlistening/provider.rb', line 66

def default_embedding_model
  @config[:embedding_model]
end

#default_modelObject



62
63
64
# File 'lib/broadlistening/provider.rb', line 62

def default_model
  @config[:model]
end

#requires_api_key?Boolean



70
71
72
# File 'lib/broadlistening/provider.rb', line 70

def requires_api_key?
  @name != :local
end

#requires_base_url?Boolean



74
75
76
# File 'lib/broadlistening/provider.rb', line 74

def requires_base_url?
  @name == :azure
end