Class: LLM::Transport
- Inherits:
-
Object
- Object
- LLM::Transport
- Defined in:
- lib/llm/transport.rb,
lib/llm/transport/http.rb,
lib/llm/transport/response.rb,
lib/llm/transport/execution.rb,
lib/llm/transport/stream_decoder.rb,
lib/llm/transport/persistent_http.rb
Overview
The LLM::Transport class defines the execution interface used by Provider.
Custom transports can subclass this class and override #request to execute provider requests without changing request adapters or response adapters.
Providers currently construct Net::HTTPRequest objects before delegating to a transport. Custom transports are therefore expected to execute those requests directly, or transform them into backend-specific request objects before execution.
Only #request is required. The remaining methods are optional hooks for features such as interruption, request ownership, or persistence, and only need to be implemented when the underlying adapter can support them.
Returned responses should implement the LLM::Transport::Response interface. In practice this can mean adapting another client’s response object so existing provider execution, response adapters, and error handlers can rely on one normalized response contract instead of transport-specific classes.
Direct Known Subclasses
Defined Under Namespace
Modules: Execution Classes: HTTP, PersistentHTTP, Response, StreamDecoder
Class Method Summary collapse
-
.net_http ⇒ Class
Returns the built-in Net::HTTP transport class.
-
.net_http_persistent ⇒ Class
Returns the built-in Net::HTTP::Persistent transport class.
Instance Method Summary collapse
-
#interrupt!(owner) ⇒ nil
Interrupt an active request, if any.
-
#interrupt_errors ⇒ Array<Class<Exception>>
Returns the exception classes that indicate an interrupted request.
-
#interrupted?(owner) ⇒ Boolean?
Returns whether an execution owner was interrupted.
-
#request(request, owner:, stream: nil) {|response| ... } ⇒ Object
Performs a request through the transport.
-
#request_owner ⇒ Object
Returns the current request owner.
- #set_body_stream(request, io) ⇒ void
Class Method Details
.net_http ⇒ Class
Returns the built-in Net::HTTP transport class.
38 39 40 |
# File 'lib/llm/transport.rb', line 38 def self.net_http HTTP end |
.net_http_persistent ⇒ Class
Returns the built-in Net::HTTP::Persistent transport class.
45 46 47 |
# File 'lib/llm/transport.rb', line 45 def self.net_http_persistent PersistentHTTP end |
Instance Method Details
#interrupt!(owner) ⇒ nil
Interrupt an active request, if any.
79 80 81 |
# File 'lib/llm/transport.rb', line 79 def interrupt!(owner) raise NotImplementedError end |
#interrupt_errors ⇒ Array<Class<Exception>>
Returns the exception classes that indicate an interrupted request.
71 72 73 |
# File 'lib/llm/transport.rb', line 71 def interrupt_errors [] end |
#interrupted?(owner) ⇒ Boolean?
Returns whether an execution owner was interrupted.
87 88 89 |
# File 'lib/llm/transport.rb', line 87 def interrupted?(owner) nil end |
#request(request, owner:, stream: nil) {|response| ... } ⇒ Object
Performs a request through the transport.
56 57 58 |
# File 'lib/llm/transport.rb', line 56 def request(request, owner:, stream: nil, &) raise NotImplementedError end |
#request_owner ⇒ Object
Returns the current request owner.
63 64 65 66 |
# File 'lib/llm/transport.rb', line 63 def request_owner return Fiber.current unless defined?(::Async) Async::Task.current? ? Async::Task.current : Fiber.current end |
#set_body_stream(request, io) ⇒ void
Custom transports may be able to reuse this helper when they operate on Net::HTTPRequest objects, or implement their own request body preparation path instead.
This method returns an undefined value.
99 100 101 102 |
# File 'lib/llm/transport.rb', line 99 def set_body_stream(request, io) request.body_stream = io request["transfer-encoding"] = "chunked" unless request["content-length"] end |