Module: LlmGateway::Adapters::AnthropicOptionMapper

Defined in:
lib/llm_gateway/adapters/anthropic_option_mapper.rb

Constant Summary collapse

DEFAULT_MAX_TOKENS =
20_480
REASONING_EFFORT_BUDGET_TOKENS =
{
  "low" => 1024,
  "medium" => 5 * 1024,
  "high" => 10 * 1024,
  "xhigh" => 20 * 1024
}.freeze

Class Method Summary collapse

Class Method Details

.map(options) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/llm_gateway/adapters/anthropic_option_mapper.rb', line 16

def map(options)
  mapped_options = options.reject { |key, _| %i[reasoning max_completion_tokens response_format prompt_cache_retention cache_key prompt_cache_key].include?(key) }
  mapped_options[:max_tokens] = options[:max_completion_tokens] || DEFAULT_MAX_TOKENS

  retention = options[:cache_retention]
  mapped_options[:cache_retention] = retention unless retention.nil?

  response_format = options[:response_format]
  mapped_options[:output_config] = normalize_output_config(response_format) unless response_format.nil?

  reasoning = options[:reasoning]
  return mapped_options if reasoning.nil? || reasoning.to_s == "none"

  mapped_options[:thinking] = normalize_reasoning(reasoning)
  mapped_options
end

.normalize_output_config(response_format) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/llm_gateway/adapters/anthropic_option_mapper.rb', line 33

def normalize_output_config(response_format)
  format_type = response_format.is_a?(Hash) ? response_format[:type] || response_format["type"] : response_format

  case format_type.to_s
  when "json_object", "json_schema"
    { format: "json_schema" }
  else
    { format: "text" }
  end
end

.normalize_reasoning(reasoning) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/llm_gateway/adapters/anthropic_option_mapper.rb', line 44

def normalize_reasoning(reasoning)
  budget_tokens = REASONING_EFFORT_BUDGET_TOKENS[reasoning.to_s] ||
    raise(ArgumentError,
          "Invalid reasoning '#{reasoning}'. Use 'none', 'low', 'medium', 'high', or 'xhigh'.")

  { type: "enabled", budget_tokens: budget_tokens }
end