Class: LLM::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/response.rb

Overview

LLM::Response encapsulates a response from an LLM provider. It is returned by all methods that make requests to a provider, and sometimes extended with provider-specific functionality.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(res) ⇒ LLM::Response

Returns an instance of LLM::Response

Parameters:

  • res (Net::HTTPResponse)

    HTTP response



22
23
24
# File 'lib/llm/response.rb', line 22

def initialize(res)
  @res = res
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, **kwargs, &b) ⇒ Object (private)



59
60
61
# File 'lib/llm/response.rb', line 59

def method_missing(m, *args, **kwargs, &b)
  body.respond_to?(m) ? body[m.to_s] : super
end

Instance Attribute Details

#resNet::HTTPResponse (readonly)

Returns the HTTP response

Returns:

  • (Net::HTTPResponse)


15
16
17
# File 'lib/llm/response.rb', line 15

def res
  @res
end

Instance Method Details

#bodyHash, String

Returns the response body

Returns:

  • (Hash, String)


29
30
31
32
33
34
# File 'lib/llm/response.rb', line 29

def body
  @body ||= case @res["content-type"]
  when %r|\Aapplication/json\s*| then LLM::Object.from_hash(JSON.parse(@res.body))
  else @res.body
  end
end

#inspectString

Returns an inspection of the response object

Returns:

  • (String)


39
40
41
# File 'lib/llm/response.rb', line 39

def inspect
  "#<#{self.class.name}:0x#{object_id.to_s(16)} @body=#{body.inspect} @res=#{@res.inspect}>"
end

#ok?Boolean

Returns true if the response is successful

Returns:

  • (Boolean)


46
47
48
# File 'lib/llm/response.rb', line 46

def ok?
  Net::HTTPSuccess === @res
end

#file?Boolean

Returns true if the response is from the Files API

Returns:

  • (Boolean)


53
54
55
# File 'lib/llm/response.rb', line 53

def file?
  false
end