Class: EmbeddingUtil::RuntimeCommand

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

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.



7
8
9
10
11
12
13
14
# File 'lib/embedding_util/runtime_command.rb', line 7

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.



5
6
7
# File 'lib/embedding_util/runtime_command.rb', line 5

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



5
6
7
# File 'lib/embedding_util/runtime_command.rb', line 5

def port
  @port
end

#ramalama_deviceObject (readonly)

Returns the value of attribute ramalama_device.



5
6
7
# File 'lib/embedding_util/runtime_command.rb', line 5

def ramalama_device
  @ramalama_device
end

#runtimeObject (readonly)

Returns the value of attribute runtime.



5
6
7
# File 'lib/embedding_util/runtime_command.rb', line 5

def runtime
  @runtime
end

#server_flagsObject (readonly)

Returns the value of attribute server_flags.



5
6
7
# File 'lib/embedding_util/runtime_command.rb', line 5

def server_flags
  @server_flags
end

#server_modelObject (readonly)

Returns the value of attribute server_model.



5
6
7
# File 'lib/embedding_util/runtime_command.rb', line 5

def server_model
  @server_model
end

Class Method Details

.available?(runtime) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/embedding_util/runtime_command.rb', line 16

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



39
40
41
# File 'lib/embedding_util/runtime_command.rb', line 39

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



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

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

.resolve(runtime) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/embedding_util/runtime_command.rb', line 29

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



47
48
49
50
51
52
53
# File 'lib/embedding_util/runtime_command.rb', line 47

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)


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

def detached_server?
  runtime == :ramalama
end

#labelObject



55
56
57
# File 'lib/embedding_util/runtime_command.rb', line 55

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

#server_nameObject



79
80
81
# File 'lib/embedding_util/runtime_command.rb', line 79

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

#stop_argvObject



63
64
65
66
67
# File 'lib/embedding_util/runtime_command.rb', line 63

def stop_argv
  return unless detached_server?

  stop_argvs.first
end

#stop_argvsObject



69
70
71
72
73
74
75
76
77
# File 'lib/embedding_util/runtime_command.rb', line 69

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