Class: LLM::Transport::Curb Private

Inherits:
LLM::Transport show all
Defined in:
lib/llm/transport/curb.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

The LLM::Transport::Curb transport is an optional adapter for libcurl via the [curb](github.com/taf2/curb) gem.

Curb is a C extension around libcurl. It releases the GVL during I/O so other Ruby threads can run while requests are in flight. Its timeout handling is built into libcurl itself — no thread-based timeout library required. It supports HTTP/2, connection reuse, and a wider range of network protocols out of the box.

Unlike the built-in Net::HTTP transports, this transport does not require any Ruby standard library HTTP client and can be used on platforms where Net::HTTP is not available or desired.

Examples:

LLM.openai(key: ENV["KEY"], transport: :curb)

Defined Under Namespace

Classes: ActiveRequest

Constant Summary collapse

INTERRUPT_ERRORS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

[::IOError, ::EOFError, Errno::EBADF].freeze

Instance Method Summary collapse

Methods inherited from LLM::Transport

curb, net_http, net_http_persistent, #set_body_stream

Constructor Details

#initialize(host:, port:, timeout:, ssl:) ⇒ LLM::Transport::Curb

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • host (String)
  • port (Integer)
  • timeout (Integer)
  • ssl (Boolean)


33
34
35
36
37
38
39
40
# File 'lib/llm/transport/curb.rb', line 33

def initialize(host:, port:, timeout:, ssl:)
  @host = host
  @port = port
  @timeout = timeout
  @ssl = ssl
  @base_uri = URI("#{ssl ? "https" : "http"}://#{host}:#{port}/")
  @monitor = Monitor.new
end

Instance Method Details

#inspectString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (String)


109
110
111
# File 'lib/llm/transport/curb.rb', line 109

def inspect
  "#<#{LLM::Utils.object_id(self)}>"
end

#interrupt!(owner) ⇒ nil

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Interrupt an active request, if any.

Sets the interrupt flag so the on_body callback can raise LLM::Interrupt on the next chunk.

Parameters:

  • owner (Fiber)

Returns:

  • (nil)


64
65
66
67
68
69
# File 'lib/llm/transport/curb.rb', line 64

def interrupt!(owner)
  request_for(owner) or return
  lock { (@interrupts ||= {})[owner] = true }
rescue *interrupt_errors
  nil
end

#interrupt_errorsArray<Class<Exception>>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Array<Class<Exception>>)


52
53
54
# File 'lib/llm/transport/curb.rb', line 52

def interrupt_errors
  [*INTERRUPT_ERRORS, *optional_interrupt_errors]
end

#interrupted?(owner) ⇒ Boolean?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns whether an execution owner was interrupted.

Parameters:

  • owner (Fiber)

Returns:

  • (Boolean, nil)


75
76
77
# File 'lib/llm/transport/curb.rb', line 75

def interrupted?(owner)
  lock { @interrupts&.delete(owner) }
end

#request(request, owner:, stream: nil) {|response| ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Performs a request through curb and returns a transport response wrapper so the provider layer can stay transport-agnostic.

Parameters:

Yield Parameters:

Returns:



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/llm/transport/curb.rb', line 88

def request(request, owner:, stream: nil, &b)
  easy = build_easy(request)
  set_request(ActiveRequest.new(easy:), owner)
  if stream
    perform_streaming(easy, owner, stream)
  elsif b
    res = perform_blocking(easy, owner)
    if LLM::Transport::Response === res
      res.success? ? b.call(res) : res
    else
      res
    end
  else
    perform_blocking(easy, owner)
  end
ensure
  clear_request(owner)
end

#request_ownerObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the current request owner.

Returns:



45
46
47
48
# File 'lib/llm/transport/curb.rb', line 45

def request_owner
  return Fiber.current unless defined?(::Async)
  Async::Task.current? ? Async::Task.current : Fiber.current
end