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



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/openai/file_part.rb', line 110

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, _]
    ::File.basename(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)


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

def content
  @content
end

#content_typeString?

Returns:

  • (String, nil)

Raises:

  • (ArgumentError)

    if the content type is not a valid MIME media type



51
52
53
# File 'lib/openai/file_part.rb', line 51

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

#filenameString? (readonly)

Returns:

  • (String, nil)


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

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)


39
40
41
42
43
44
# File 'lib/openai/file_part.rb', line 39

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)


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

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

#to_yaml(*a) ⇒ String

Parameters:

  • a (Object)

Returns:

  • (String)


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

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:



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

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