Class: Hyrum::Generators::AiGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/hyrum/generators/ai_generator.rb

Constant Summary collapse

API_KEY_ENV_VARS =
{
  openai: 'OPENAI_API_KEY',
  anthropic: 'ANTHROPIC_API_KEY',
  gemini: 'GEMINI_API_KEY',
  ollama: 'OLLAMA_API_BASE',
  vertexai: 'GOOGLE_CLOUD_PROJECT',
  bedrock: 'AWS_ACCESS_KEY_ID',
  deepseek: 'DEEPSEEK_API_KEY',
  mistral: 'MISTRAL_API_KEY',
  perplexity: 'PERPLEXITY_API_KEY',
  openrouter: 'OPENROUTER_API_KEY',
  gpustack: 'GPUSTACK_API_KEY'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ AiGenerator

Returns a new instance of AiGenerator.



24
25
26
# File 'lib/hyrum/generators/ai_generator.rb', line 24

def initialize(options)
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



22
23
24
# File 'lib/hyrum/generators/ai_generator.rb', line 22

def options
  @options
end

Instance Method Details

#generateObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/hyrum/generators/ai_generator.rb', line 28

def generate
  response = chat.ask(prompt)
  puts "AI response: #{response.inspect}" if options[:verbose]

  # Prepend the original message to the generated variations
  # RubyLLM returns string keys, but our options use symbols
  result = response.content.dup
  key_str = options[:key].to_s
  if result[key_str].is_a?(Array)
    result[key_str] = [options[:message]] + result[key_str]
  end

  # Convert string keys to symbols for consistency with the rest of hyrum
  result.transform_keys(&:to_sym)
rescue RubyLLM::ConfigurationError => e
  handle_configuration_error(e)
rescue RubyLLM::Error => e
  handle_general_error(e)
end