Module: LLM::Utils

Extended by:
Utils
Included in:
Anthropic, Google, Utils
Defined in:
lib/llm/utils.rb

Overview

Shared utility methods used across the runtime.

Instance Method Summary collapse

Instance Method Details

#resolve_option(obj, option, resolve_symbol: true) ⇒ Object

Resolves a configured option against an object instance.

Proc values are evaluated with instance_exec, symbol values are optionally sent to the object as method calls, hashes are duplicated, and all other values are returned as-is.

Parameters:

  • obj (Object)
  • option (Object)
  • resolve_symbol (Boolean) (defaults to: true)

Returns:



20
21
22
23
24
25
26
27
# File 'lib/llm/utils.rb', line 20

def resolve_option(obj, option, resolve_symbol: true)
  case option
  when Proc then obj.instance_exec(&option)
  when Symbol then resolve_symbol ? obj.send(option) : option
  when Hash then option.dup
  else option
  end
end

#normalize_base_path(path) ⇒ String

Normalizes an HTTP API base path.

Blank paths normalize to an empty string. Non-empty paths are prefixed with a leading slash and stripped of trailing slashes.

Parameters:

  • path (String, nil)

Returns:

  • (String)


37
38
39
40
41
42
# File 'lib/llm/utils.rb', line 37

def normalize_base_path(path)
  path = path.to_s.strip
  return "" if path.empty? || path == "/"
  path = "/#{path}" unless path.start_with?("/")
  path.sub(%r{/+\z}, "")
end

#name_of(obj) ⇒ String?

Returns the Ruby module or class name for an object.

This bypasses overridden #name implementations by binding Module#name directly.

Parameters:

  • obj (Module)

Returns:

  • (String, nil)


52
53
54
# File 'lib/llm/utils.rb', line 52

def name_of(obj)
  ::Module.instance_method(:name).bind(obj).call
end

#object_id(obj) ⇒ String

Renders the class-and-object-id portion of an inspect string.

This returns strings like LLM::Tool:0x1234abcd, which can be embedded into custom inspect output.

Parameters:

Returns:

  • (String)


64
65
66
67
68
69
70
71
# File 'lib/llm/utils.rb', line 64

def object_id(obj)
  klass = if Class === obj
    name_of(obj) || name_of(obj.superclass) || obj.class.name
  else
    obj.class.name || obj.class.to_s
  end
  "#{klass}:0x#{obj.object_id.to_s(16)}"
end