Class: Llmshim::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/llmshim/client.rb

Overview

A thin, dependency-free HTTP client for a running llmshim proxy.

client = Llmshim::Client.new(base_url: "http://localhost:3000")
resp = client.chat(model: "claude-sonnet-4-6", messages: "Hello!")
puts resp.content

All request/response shapes follow api/openapi.yaml. Only the Ruby standard library is used (net/http, json, uri).

Constant Summary collapse

DEFAULT_BASE_URL =
"http://localhost:3000"
DEFAULT_TIMEOUT =
120

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url: DEFAULT_BASE_URL, headers: {}, timeout: DEFAULT_TIMEOUT) ⇒ Client

Returns a new instance of Client.

Parameters:

  • base_url (String) (defaults to: DEFAULT_BASE_URL)

    proxy base URL (default http://localhost:3000)

  • headers (Hash) (defaults to: {})

    extra headers merged into every request

  • timeout (Numeric) (defaults to: DEFAULT_TIMEOUT)

    read/open timeout in seconds



33
34
35
36
37
# File 'lib/llmshim/client.rb', line 33

def initialize(base_url: DEFAULT_BASE_URL, headers: {}, timeout: DEFAULT_TIMEOUT)
  @base_url = base_url.to_s.sub(%r{/+\z}, "")
  @headers = headers || {}
  @timeout = timeout
end

Instance Attribute Details

#base_urlObject (readonly)

Base URL of the proxy (without a trailing slash).



24
25
26
# File 'lib/llmshim/client.rb', line 24

def base_url
  @base_url
end

#headersObject (readonly)

Extra headers sent with every request (Hash).



26
27
28
# File 'lib/llmshim/client.rb', line 26

def headers
  @headers
end

#timeoutObject (readonly)

Read/open timeout in seconds.



28
29
30
# File 'lib/llmshim/client.rb', line 28

def timeout
  @timeout
end

Instance Method Details

#chat(model:, messages:, **opts) ⇒ Llmshim::ChatResponse

Send a non-streaming chat completion (POST /v1/chat).

Parameters:

  • model (String)

    "provider/model" or a bare model name

  • messages (String, Array<Hash>)

    a single user string or message hashes

  • opts (Hash)

    see #build_body (max_tokens, temperature, top_p, top_k, stop, reasoning_effort, config, provider_config, tools, tool_choice, fallback)

Returns:



46
47
48
49
50
# File 'lib/llmshim/client.rb', line 46

def chat(model:, messages:, **opts)
  body = build_body(model, messages, opts)
  hash = post_json("/v1/chat", body)
  ChatResponse.from_hash(hash)
end

#healthLlmshim::Health

Health check (GET /health).

Returns:



83
84
85
# File 'lib/llmshim/client.rb', line 83

def health
  Health.from_hash(get_json("/health"))
end

#modelsArray<Llmshim::Model>

List available models (GET /v1/models).

Returns:



75
76
77
78
# File 'lib/llmshim/client.rb', line 75

def models
  hash = get_json("/v1/models")
  (hash["models"] || []).map { |m| Model.from_hash(m) }
end

#stream(model:, messages:, **opts) ⇒ Array<Llmshim::StreamEvent>?

Stream a chat completion (POST /v1/chat/stream).

Yields a Llmshim::StreamEvent per SSE event. Iteration stops after a "done" event or on EOF. When no block is given, returns the collected array of events.

Returns:



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/llmshim/client.rb', line 59

def stream(model:, messages:, **opts)
  body = build_body(model, messages, opts)
  collected = block_given? ? nil : []
  stream_sse("/v1/chat/stream", body) do |event|
    if block_given?
      yield event
    else
      collected << event
    end
  end
  collected
end