Class: EmbeddingUtil::RuntimeCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/embedding_util/runtime_command.rb

Constant Summary collapse

RAMALAMA_CONTEXT_SIZE =
"4096"
RAMALAMA_RUNTIME_FLAGS =
["--cache-ram", "0"].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runtime:, server_model:, host:, port:, **options) ⇒ RuntimeCommand

Returns a new instance of RuntimeCommand.



10
11
12
13
14
15
16
17
# File 'lib/embedding_util/runtime_command.rb', line 10

def initialize(runtime:, server_model:, host:, port:, **options)
  @runtime = self.class.normalize_runtime(runtime)
  @server_model = server_model
  @host = host
  @port = port
  @server_flags = options[:server_flags] || server_model.settings.fetch(:server_flags)
  @ramalama_device = options[:ramalama_device]
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



8
9
10
# File 'lib/embedding_util/runtime_command.rb', line 8

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



8
9
10
# File 'lib/embedding_util/runtime_command.rb', line 8

def port
  @port
end

#ramalama_deviceObject (readonly)

Returns the value of attribute ramalama_device.



8
9
10
# File 'lib/embedding_util/runtime_command.rb', line 8

def ramalama_device
  @ramalama_device
end

#runtimeObject (readonly)

Returns the value of attribute runtime.



8
9
10
# File 'lib/embedding_util/runtime_command.rb', line 8

def runtime
  @runtime
end

#server_flagsObject (readonly)

Returns the value of attribute server_flags.



8
9
10
# File 'lib/embedding_util/runtime_command.rb', line 8

def server_flags
  @server_flags
end

#server_modelObject (readonly)

Returns the value of attribute server_model.



8
9
10
# File 'lib/embedding_util/runtime_command.rb', line 8

def server_model
  @server_model
end

Class Method Details

.available?(runtime) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/embedding_util/runtime_command.rb', line 19

def self.available?(runtime)
  case normalize_runtime(runtime)
  when :auto
    available?(:ramalama) || available?(:llama_server)
  when :ramalama
    !!command_path("ramalama")
  when :llama_server
    !!command_path("llama-server")
  else
    false
  end
end

.command_path(command) ⇒ Object



42
43
44
# File 'lib/embedding_util/runtime_command.rb', line 42

def self.command_path(command)
  ENV.fetch("PATH", "").split(File::PATH_SEPARATOR).map { |dir| File.join(dir, command) }.find { |path| File.executable?(path) && !File.directory?(path) }
end

.normalize_runtime(runtime) ⇒ Object



46
47
48
# File 'lib/embedding_util/runtime_command.rb', line 46

def self.normalize_runtime(runtime)
  runtime.to_s.tr("-", "_").to_sym
end

.resolve(runtime) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/embedding_util/runtime_command.rb', line 32

def self.resolve(runtime)
  requested = normalize_runtime(runtime)
  return requested unless requested == :auto

  return :ramalama if available?(:ramalama)
  return :llama_server if available?(:llama_server)

  :auto
end

Instance Method Details

#argvObject



50
51
52
53
54
55
56
# File 'lib/embedding_util/runtime_command.rb', line 50

def argv
  case runtime
  when :ramalama then ramalama_argv
  when :llama_server then llama_server_argv
  else raise UnsupportedProviderError, "no supported local runtime found; install ramalama or llama-server"
  end
end

#detached_server?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/embedding_util/runtime_command.rb', line 62

def detached_server?
  runtime == :ramalama
end

#labelObject



58
59
60
# File 'lib/embedding_util/runtime_command.rb', line 58

def label
  runtime == :llama_server ? "llama-server" : runtime.to_s
end

#server_nameObject



82
83
84
# File 'lib/embedding_util/runtime_command.rb', line 82

def server_name
  "embedding-util-#{server_model.name}".tr("_", "-")
end

#stop_argvObject



66
67
68
69
70
# File 'lib/embedding_util/runtime_command.rb', line 66

def stop_argv
  return unless detached_server?

  stop_argvs.first
end

#stop_argvsObject



72
73
74
75
76
77
78
79
80
# File 'lib/embedding_util/runtime_command.rb', line 72

def stop_argvs
  return [] unless detached_server?

  [
    ["ramalama", "stop", server_name],
    ["podman", "stop", "--time", "0", server_name],
    ["docker", "stop", server_name]
  ].select { |argv| self.class.command_path(argv.first) }
end