Class: OpenAI::FilePart

Inherits:
Object
  • Object
show all
Defined in:
lib/openai/file_part.rb,
sig/openai/file_part.rbs

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, filename: nil, content_type: nil) ⇒ FilePart

Returns a new instance of FilePart.

Parameters:

  • content (Pathname, StringIO, IO, String)
  • filename (Pathname, String, nil) (defaults to: nil)
  • content_type (String, nil) (defaults to: nil)

Raises:

  • (ArgumentError)

    if the content type is not a valid MIME media type



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/openai/file_part.rb', line 118

def initialize(content, filename: nil, content_type: nil)
  OpenAI::FilePart.validate_content_type(content_type)
  @content_type = content_type
  @filename = case [filename, (@content = content)]
  in [String | Pathname, _]
    strip_directories(filename)
  in [nil, Pathname]
    content.basename.to_path
  in [nil, IO]
    content.to_path&.then { ::File.basename(_1) }
  else
    filename
  end
end

Instance Attribute Details

#contentPathname, ...

Returns:

  • (Pathname, StringIO, IO, String)


55
56
57
# File 'lib/openai/file_part.rb', line 55

def content
  @content
end

#content_typeString?

Returns:

  • (String, nil)

Raises:

  • (ArgumentError)

    if the content type is not a valid MIME media type



59
60
61
# File 'lib/openai/file_part.rb', line 59

def content_type
  OpenAI::FilePart.validate_content_type(@content_type)
end

#filenameString? (readonly)

Returns:

  • (String, nil)


64
65
66
# File 'lib/openai/file_part.rb', line 64

def filename
  @filename
end

Class Method Details

.validate_content_type(content_type) ⇒ String?

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:

  • content_type (String, nil)

Returns:

  • (String, nil)


47
48
49
50
51
52
# File 'lib/openai/file_part.rb', line 47

def self.validate_content_type(content_type)
  return if content_type.nil?
  return content_type if content_type.is_a?(String) && MEDIA_TYPE.match?(content_type.b)

  raise ArgumentError, "`content_type` must be a valid MIME media type"
end

Instance Method Details

#to_json(*a) ⇒ String

Parameters:

  • a (Object)

Returns:

  • (String)


107
# File 'lib/openai/file_part.rb', line 107

def to_json(*a) = read.to_json(*a)

#to_yaml(*a) ⇒ String

Parameters:

  • a (Object)

Returns:

  • (String)


112
# File 'lib/openai/file_part.rb', line 112

def to_yaml(*a) = read.to_yaml(*a)

#with_content(content) ⇒ OpenAI::FilePart

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.

Return a copy with different content while preserving multipart behavior.

Parameters:

  • content (Pathname, StringIO, IO, String)

Returns:



72
73
74
75
76
77
# File 'lib/openai/file_part.rb', line 72

def with_content(content)
  dup.tap do |copy|
    copy.content = content
    copy.content_type = content_type || default_content_type
  end
end