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:) ⇒ RuntimeCommand

Returns a new instance of RuntimeCommand.



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

def initialize(runtime:, server_model:, host:, port:)
  @runtime = self.class.normalize_runtime(runtime)
  @server_model = server_model
  @host = host
  @port = port
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

#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_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)


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

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



37
38
39
# File 'lib/embedding_util/runtime_command.rb', line 37

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



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

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

.resolve(runtime) ⇒ Object



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

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



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

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)


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

def detached_server?
  runtime == :ramalama
end

#labelObject



53
54
55
# File 'lib/embedding_util/runtime_command.rb', line 53

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

#server_nameObject



77
78
79
# File 'lib/embedding_util/runtime_command.rb', line 77

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

#stop_argvObject



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

def stop_argv
  return unless detached_server?

  stop_argvs.first
end

#stop_argvsObject



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

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