Module: WritersRoom::LLMSetup

Defined in:
lib/writers_room/llm_setup.rb

Overview

Shared LLM configuration to eliminate duplication between Writer, ChatSession, and other components that need to configure RubyLLM and build RobotLab RunConfigs.

Class Method Summary collapse

Class Method Details

.build_run_config(config = WritersRoom.config, **overrides) ⇒ RobotLab::RunConfig

Build a RobotLab::RunConfig from WritersRoom config.

Parameters:

  • config (WritersRoom::Config) (defaults to: WritersRoom.config)

    project configuration

  • overrides (Hash)

    additional RunConfig fields

Returns:

  • (RobotLab::RunConfig)


60
61
62
63
64
65
66
67
# File 'lib/writers_room/llm_setup.rb', line 60

def build_run_config(config = WritersRoom.config, **overrides)
  configure_ruby_llm(config)

  fields = { model: config.model_name }
  fields.merge!(overrides)

  RobotLab::RunConfig.new(**fields)
end

.configure_ruby_llm(config = WritersRoom.config) ⇒ Object

Configure RubyLLM for the given provider.

Parameters:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/writers_room/llm_setup.rb', line 13

def configure_ruby_llm(config = WritersRoom.config)
  provider = config.provider

  RubyLLM.configure do |c|
    case provider
    when "ollama"
      c.ollama_api_base = config.ollama_url
    when "openai"
      c.openai_api_key = ENV["OPENAI_API_KEY"]
    when "anthropic"
      c.anthropic_api_key = ENV["ANTHROPIC_API_KEY"]
    end
  end

  register_local_model(config.model_name, provider)
end

.register_local_model(model_name, provider) ⇒ Object

Pre-register a model in the RubyLLM registry for local providers.

RobotLab's RunConfig doesn't carry a provider field, so when RubyLLM::Chat resolves the model it searches the static registry without knowing which provider to use. Local providers (Ollama, GPUStack) aren't in the static registry, causing "Unknown model" errors. This method registers the model so resolution succeeds.

Parameters:

  • model_name (String)

    the model identifier

  • provider (String)

    the provider name



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/writers_room/llm_setup.rb', line 40

def register_local_model(model_name, provider)
  provider_sym = provider.to_sym
  provider_class = RubyLLM::Provider.providers[provider_sym]
  return unless provider_class

  provider_instance = provider_class.new(RubyLLM.config)
  return unless provider_instance.local?

  # Skip if already registered
  return if RubyLLM::Models.all.any? { |m| m.id == model_name && m.provider == provider }

  default_info = RubyLLM::Model::Info.default(model_name, provider_instance.slug)
  RubyLLM::Models.instance.all << default_info
end