Class: LLM::File
- Inherits:
-
Object
- Object
- LLM::File
- Defined in:
- lib/llm/file.rb
Overview
LLM::File represents a local file. It can be used as a prompt with certain providers (eg: Ollama, Gemini), and as an input with certain methods. It is usually not necessary to create an instance of LLM::File directly.
Instance Attribute Summary collapse
-
#path ⇒ String
readonly
Returns the path to the file.
Instance Method Summary collapse
-
#with_io ⇒ File
Yields an IO object suitable to be streamed.
-
#basename ⇒ String
Returns basename of the file.
-
#mime_type ⇒ String
Returns the MIME type of the file.
-
#image? ⇒ Boolean
Returns true if the file is an image.
-
#pdf? ⇒ Boolean
Returns true if the file is a PDF document.
-
#bytesize ⇒ Integer
Returns the size of the file in bytes.
-
#to_b64 ⇒ String
Returns the file contents in base64.
-
#to_data_uri ⇒ String
Returns the file contents in base64 URL format.
-
#initialize(path) ⇒ File
constructor
A new instance of File.
Constructor Details
#initialize(path) ⇒ File
Returns a new instance of File.
14 15 16 |
# File 'lib/llm/file.rb', line 14 def initialize(path) @path = path end |
Instance Attribute Details
#path ⇒ String (readonly)
Returns the path to the file
12 13 14 |
# File 'lib/llm/file.rb', line 12 def path @path end |
Instance Method Details
#with_io ⇒ File
Returns Yields an IO object suitable to be streamed.
70 71 72 73 74 75 |
# File 'lib/llm/file.rb', line 70 def with_io io = File.open(path, "rb") yield(io) ensure io.close end |
#basename ⇒ String
Returns basename of the file
21 22 23 |
# File 'lib/llm/file.rb', line 21 def basename File.basename(path) end |
#mime_type ⇒ String
Returns the MIME type of the file
28 29 30 |
# File 'lib/llm/file.rb', line 28 def mime_type LLM::Mime[path] end |
#image? ⇒ Boolean
Returns true if the file is an image
35 36 37 |
# File 'lib/llm/file.rb', line 35 def image? mime_type.start_with?("image/") end |
#pdf? ⇒ Boolean
Returns true if the file is a PDF document
42 43 44 |
# File 'lib/llm/file.rb', line 42 def pdf? mime_type == "application/pdf" end |
#bytesize ⇒ Integer
Returns the size of the file in bytes
49 50 51 |
# File 'lib/llm/file.rb', line 49 def bytesize File.size(path) end |
#to_b64 ⇒ String
Returns the file contents in base64
56 57 58 |
# File 'lib/llm/file.rb', line 56 def to_b64 [File.binread(path)].pack("m0") end |
#to_data_uri ⇒ String
Returns the file contents in base64 URL format
63 64 65 |
# File 'lib/llm/file.rb', line 63 def to_data_uri "data:#{mime_type};base64,#{to_b64}" end |