Class: Llmshim::Client
- Inherits:
-
Object
- Object
- Llmshim::Client
- 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
-
#base_url ⇒ Object
readonly
Base URL of the proxy (without a trailing slash).
-
#headers ⇒ Object
readonly
Extra headers sent with every request (Hash).
-
#timeout ⇒ Object
readonly
Read/open timeout in seconds.
Instance Method Summary collapse
-
#chat(model:, messages:, **opts) ⇒ Llmshim::ChatResponse
Send a non-streaming chat completion (POST /v1/chat).
-
#health ⇒ Llmshim::Health
Health check (GET /health).
-
#initialize(base_url: DEFAULT_BASE_URL, headers: {}, timeout: DEFAULT_TIMEOUT) ⇒ Client
constructor
A new instance of Client.
-
#models ⇒ Array<Llmshim::Model>
List available models (GET /v1/models).
-
#stream(model:, messages:, **opts) ⇒ Array<Llmshim::StreamEvent>?
Stream a chat completion (POST /v1/chat/stream).
Constructor Details
#initialize(base_url: DEFAULT_BASE_URL, headers: {}, timeout: DEFAULT_TIMEOUT) ⇒ Client
Returns a new instance of Client.
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_url ⇒ Object (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 |
#headers ⇒ Object (readonly)
Extra headers sent with every request (Hash).
26 27 28 |
# File 'lib/llmshim/client.rb', line 26 def headers @headers end |
#timeout ⇒ Object (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).
46 47 48 49 50 |
# File 'lib/llmshim/client.rb', line 46 def chat(model:, messages:, **opts) body = build_body(model, , opts) hash = post_json("/v1/chat", body) ChatResponse.from_hash(hash) end |
#health ⇒ Llmshim::Health
Health check (GET /health).
83 84 85 |
# File 'lib/llmshim/client.rb', line 83 def health Health.from_hash(get_json("/health")) end |
#models ⇒ Array<Llmshim::Model>
List available models (GET /v1/models).
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.
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, , 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 |