Module: LanguageOperator::CLI::Helpers::ProviderHelper

Included in:
Wizards::ModelWizard
Defined in:
lib/language_operator/cli/helpers/provider_helper.rb

Overview

LLM provider operations for CLI wizards

Provides helpers for testing provider connections and fetching available models.

Examples:

class ModelWizard
  include Helpers::ProviderHelper

  def test_anthropic_connection(api_key)
    test_provider_connection(:anthropic, api_key: api_key)
  end
end

Instance Method Summary collapse

Instance Method Details

#fetch_provider_models(provider, api_key: nil, endpoint: nil) ⇒ Array<String>?

Fetch available models from a provider

Parameters:

  • provider (Symbol)

    Provider type

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

    API key for authentication

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

    Custom endpoint URL

Returns:

  • (Array<String>, nil)

    List of model IDs or nil if unavailable



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/language_operator/cli/helpers/provider_helper.rb', line 50

def fetch_provider_models(provider, api_key: nil, endpoint: nil)
  case provider
  when :anthropic
    # Anthropic doesn't have a public /v1/models endpoint
    [
      'claude-3-5-sonnet-20241022',
      'claude-3-opus-20240229',
      'claude-3-sonnet-20240229',
      'claude-3-haiku-20240307'
    ]
  when :openai
    fetch_openai_models(api_key)
  when :openai_compatible
    fetch_openai_compatible_models(endpoint, api_key)
  end
rescue StandardError => e
  Formatters::ProgressFormatter.warn("Could not fetch models: #{e.message}")
  nil
end

#test_provider_connection(provider, api_key: nil, endpoint: nil) ⇒ Hash

Test connection to an LLM provider

Parameters:

  • provider (Symbol)

    Provider type (:anthropic, :openai, :openai_compatible)

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

    API key for authentication

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

    Custom endpoint URL (for openai_compatible)

Returns:

  • (Hash)

    Result with :success and optional :error keys



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/language_operator/cli/helpers/provider_helper.rb', line 29

def test_provider_connection(provider, api_key: nil, endpoint: nil)
  require 'ruby_llm'

  case provider
  when :anthropic
    test_anthropic(api_key)
  when :openai
    test_openai(api_key)
  when :openai_compatible
    test_openai_compatible(endpoint, api_key)
  else
    { success: false, error: "Unknown provider: #{provider}" }
  end
end