Class: Boxcars::Engines

Inherits:
Object
  • Object
show all
Defined in:
lib/boxcars/engines.rb

Overview

Factory class for creating engine instances based on model names Provides convenient shortcuts and aliases for different AI models

Constant Summary collapse

VALID_ANSWER_REMOVE_IN =
"3.0"
DEFAULT_MODEL =
"gemini-2.5-flash"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.emit_deprecation_warnings ⇒ Object

Returns the value of attribute emit_deprecation_warnings.



14
15
16
# File 'lib/boxcars/engines.rb', line 14

def emit_deprecation_warnings
  @emit_deprecation_warnings
end

Class Method Details

.deprecation_warnings_enabled? ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/boxcars/engines.rb', line 91

def self.deprecation_warnings_enabled?
  emit_deprecation_warnings && Boxcars.configuration.emit_deprecation_warnings
end

.engine(model: nil, **kw_args) ⇒ Boxcars::Engine

Create an engine instance based on the model name

Parameters:

  • model (String) (defaults to: nil) —

    The model name or alias

  • kw_args (Hash) —

    Additional arguments to pass to the engine

Returns:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/boxcars/engines.rb', line 21

def self.engine(model: nil, **kw_args)
  kw_args = Boxcars.configuration.default_model_options.merge(kw_args) if model.nil?
  model ||= Boxcars.configuration.default_model || DEFAULT_MODEL
  Boxcars.logger&.info { "running api with #{model}" }

  case model.to_s
  when "gpt-oss-120b"
    Boxcars::Cerebras.new(model:, **kw_args)
  when /^(gpt-|o\d($|-))/
    Boxcars::Openai.new(model:, **kw_args)
  when "sonnet"
    Boxcars::Anthropic.new(model: "claude-sonnet-5", **kw_args)
  when "opus"
    Boxcars::Anthropic.new(model: "claude-opus-5", **kw_args)
  when /claude-/
    Boxcars::Anthropic.new(model:, **kw_args)
  when "llama-3.3-70b-versatile"
    Boxcars::Groq.new(model: "llama-3.3-70b-versatile", **kw_args)
  when /^mistral-/, %r{^meta-llama/}, /^deepseek-/
    Boxcars::Groq.new(model:, **kw_args)
  when "sonar"
    Boxcars::Perplexityai.new(model: "sonar", **kw_args)
  when "sonar-pro"
    Boxcars::Perplexityai.new(model: "sonar-pro", **kw_args)
  when /^gemini-(?!flash$|pro$)/
    Boxcars::GeminiAi.new(model:, **kw_args)
  when /-sonar-/
    Boxcars::Perplexityai.new(model:, **kw_args)
  when /^together-/
    Boxcars::Together.new(model: model[9..-1], **kw_args)
  when "Qwen/Qwen2.5-VL-72B-Instruct"
    Boxcars::Together.new(model:, **kw_args)
  else
    raise Boxcars::ArgumentError, "Unknown model: #{model}"
  end
end

.json_engine(model: nil, **kw_args) ⇒ Boxcars::Engine

Create an engine instance optimized for JSON responses

Parameters:

  • model (String) (defaults to: nil) —

    The model name or alias

  • kw_args (Hash) —

    Additional arguments to pass to the engine

Returns:



62
63
64
65
66
67
68
69
70
# File 'lib/boxcars/engines.rb', line 62

def self.json_engine(model: nil, **kw_args)
  default_options = { temperature: 0.1 }
  effective_model = model || Boxcars.configuration.default_model || DEFAULT_MODEL
  name = effective_model.to_s
  blocked = name.start_with?("gpt-5", "llama", "claude-") || name.match?(/sonnet|opus|haiku|sonar/)
  default_options[:response_format] = { type: "json_object" } unless blocked
  options = default_options.merge(kw_args)
  engine(model:, **options)
end

.reset_deprecation_warnings! ⇒ Object



87
88
89
# File 'lib/boxcars/engines.rb', line 87

def self.reset_deprecation_warnings!
  @warned_valid_answer = false
end

.valid_answer?(answer) ⇒ Boolean

Deprecated.

Use Boxcars::Result.valid_conduct_payload? or Boxcars::Result.extract.

Deprecated. Validate the shape returned by Boxcar#conduct.

Parameters:

  • answer (Object) —

    Conduct return value.

Returns:

  • (Boolean) —

    True when answer is a hash containing a Boxcars::Result under :answer.



76
77
78
79
80
81
82
83
84
85
# File 'lib/boxcars/engines.rb', line 76

def self.valid_answer?(answer)
  unless @warned_valid_answer || !deprecation_warnings_enabled?
    Boxcars.warn(
      "Boxcars::Engines.valid_answer? is deprecated; use Boxcars::Result.valid_conduct_payload? " \
      "or Boxcars::Result.extract (planned removal in v#{VALID_ANSWER_REMOVE_IN})"
    )
    @warned_valid_answer = true
  end
  Boxcars::Result.valid_conduct_payload?(answer)
end