Class: LLM::Multipart

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ LLM::Multipart

Parameters:

  • params (Hash)

    Request parameters



18
19
20
21
# File 'lib/llm/multipart.rb', line 18

def initialize(params)
  @boundary = "BOUNDARY__#{SecureRandom.hex(16)}"
  @params = params
end

Instance Attribute Details

#boundaryString (readonly)

Returns:

  • (String)


12
13
14
# File 'lib/llm/multipart.rb', line 12

def boundary
  @boundary
end

Instance Method Details

#bodyString

Returns the multipart request body

Returns:

  • (String)


47
48
49
50
51
# File 'lib/llm/multipart.rb', line 47

def body
  io = StringIO.new("".b)
  [*parts, StringIO.new("--#{@boundary}--\r\n".b)].each { IO.copy_stream(_1.tap(&:rewind), io) }
  io.tap(&:rewind)
end

#content_typeString

Returns the multipart content type

Returns:

  • (String)


26
27
28
# File 'lib/llm/multipart.rb', line 26

def content_type
  "multipart/form-data; boundary=#{@boundary}"
end

#partsArray<String>

Returns the multipart request body parts

Returns:

  • (Array<String>)


33
34
35
36
37
38
39
40
41
42
# File 'lib/llm/multipart.rb', line 33

def parts
  params.map do |key, value|
    locals = {key: key.to_s.b, boundary: boundary.to_s.b}
    if value.respond_to?(:path)
      file_part(key, value, locals)
    else
      data_part(key, value, locals)
    end
  end
end