Class: LanguageOperator::CLI::Commands::Agent::Helpers::ClusterLLMClient

Inherits:
Object
  • Object
show all
Defined in:
lib/language_operator/cli/commands/agent/helpers/cluster_llm_client.rb

Overview

LLM client that uses port-forwarding to cluster model deployments (LiteLLM proxy)

Instance Method Summary collapse

Constructor Details

#initialize(ctx:, model_name:, model_id:, agent_command:) ⇒ ClusterLLMClient

Returns a new instance of ClusterLLMClient.



14
15
16
17
18
19
# File 'lib/language_operator/cli/commands/agent/helpers/cluster_llm_client.rb', line 14

def initialize(ctx:, model_name:, model_id:, agent_command:)
  @ctx = ctx
  @model_name = model_name
  @model_id = model_id
  @agent_command = agent_command
end

Instance Method Details

#chat(prompt) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/language_operator/cli/commands/agent/helpers/cluster_llm_client.rb', line 21

def chat(prompt)
  pod = get_model_pod
  pod_name = pod.dig('metadata', 'name')

  local_port = find_available_port
  port_forward_pid = nil

  begin
    port_forward_pid = start_port_forward(pod_name, local_port, 4000)
    wait_for_port(local_port)

    conn = Faraday.new(url: "http://localhost:#{local_port}") do |f|
      f.request :json
      f.response :json
      f.adapter Faraday.default_adapter
      f.options.timeout = 120
      f.options.open_timeout = 10
    end

    payload = {
      model: @model_id,
      messages: [{ role: 'user', content: prompt }],
      max_tokens: 4000,
      temperature: 0.3
    }

    response = conn.post('/v1/chat/completions', payload)
    result = response.body

    raise "LLM error: #{result['error']['message'] || result['error']}" if result['error']

    result.dig('choices', 0, 'message', 'content')
  ensure
    cleanup_port_forward(port_forward_pid) if port_forward_pid
  end
end