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

Returns a new instance of RuntimeCommand.



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

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


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

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



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

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



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

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

.resolve(runtime) ⇒ Object



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

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



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

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)


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

def detached_server?
  runtime == :ramalama
end

#labelObject



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

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

#server_nameObject



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

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

#stop_argvObject



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

def stop_argv
  return unless detached_server?

  stop_argvs.first
end

#stop_argvsObject



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

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