Class: LLM::File
- Inherits:
-
Object
- Object
- LLM::File
- Defined in:
- lib/llm/file.rb
Overview
The LLM::File class represents a local file. It can be used as a prompt with certain providers (eg: Ollama, Gemini), and as an input with certain methods
Instance Attribute Summary collapse
-
#path ⇒ String
readonly
Returns the path to a file.
Instance Method Summary collapse
-
#initialize(path) ⇒ File
constructor
A new instance of File.
-
#mime_type ⇒ String
Returns the MIME type of the file.
-
#image? ⇒ String
Returns true if the file is an image.
-
#bytesize ⇒ Integer
Returns the size of the file in bytes.
-
#to_b64 ⇒ String
Returns the file contents in base64.
-
#with_io ⇒ File
Yields an IO object suitable to be streamed.
Constructor Details
#initialize(path) ⇒ File
Returns a new instance of File.
13 14 15 |
# File 'lib/llm/file.rb', line 13 def initialize(path) @path = path end |
Instance Attribute Details
#path ⇒ String (readonly)
Returns the path to a file
11 12 13 |
# File 'lib/llm/file.rb', line 11 def path @path end |
Instance Method Details
#mime_type ⇒ String
Returns the MIME type of the file
20 21 22 |
# File 'lib/llm/file.rb', line 20 def mime_type LLM::Mime[File.extname(path)] end |
#image? ⇒ String
Returns true if the file is an image
27 28 29 |
# File 'lib/llm/file.rb', line 27 def image? mime_type.start_with?("image/") end |
#bytesize ⇒ Integer
Returns the size of the file in bytes
34 35 36 |
# File 'lib/llm/file.rb', line 34 def bytesize File.size(path) end |
#to_b64 ⇒ String
Returns the file contents in base64
41 42 43 |
# File 'lib/llm/file.rb', line 41 def to_b64 [File.binread(path)].pack("m0") end |