Class: LLM::Gemini::Files
- Inherits:
-
Object
- Object
- LLM::Gemini::Files
- Includes:
- Utils
- Defined in:
- lib/llm/providers/gemini/files.rb
Overview
The LLM::Gemini::Files class provides a files object for interacting with [Gemini’s Files API](ai.google.dev/gemini-api/docs/files). The files API allows a client to reference media files in prompts where they can be referenced by their URL.
The files API is intended to preserve bandwidth and latency, especially for large files but it can be helpful for smaller files as well because it does not require the client to include a file in the prompt over and over again (which could be the case in a multi-turn conversation).
Instance Method Summary collapse
-
#all(**params) ⇒ LLM::Response::FileList
List all files.
-
#create(file:, **params) ⇒ LLM::Response::File
Create a file.
-
#delete(file:, **params) ⇒ LLM::Response::File
Delete a file.
- #download ⇒ Object
-
#get(file:, **params) ⇒ LLM::Response::File
Get a file.
-
#initialize(provider) ⇒ LLM::Gemini::Files
constructor
Returns a new Files object.
Methods included from Utils
Constructor Details
#initialize(provider) ⇒ LLM::Gemini::Files
Returns a new Files object
41 42 43 |
# File 'lib/llm/providers/gemini/files.rb', line 41 def initialize(provider) @provider = provider end |
Instance Method Details
#all(**params) ⇒ LLM::Response::FileList
List all files
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/llm/providers/gemini/files.rb', line 57 def all(**params) query = URI.encode_www_form(params.merge!(key: secret)) req = Net::HTTP::Get.new("/v1beta/files?#{query}", headers) res = request(http, req) LLM::Response::FileList.new(res).tap { |filelist| files = filelist.body["files"]&.map do |file| file = file.transform_keys { snakecase(_1) } OpenStruct.from_hash(file) end || [] filelist.files = files } end |
#create(file:, **params) ⇒ LLM::Response::File
Create a file
80 81 82 83 84 85 86 87 88 |
# File 'lib/llm/providers/gemini/files.rb', line 80 def create(file:, **params) req = Net::HTTP::Post.new(request_upload_url(file:), {}) req["content-length"] = file.bytesize req["X-Goog-Upload-Offset"] = 0 req["X-Goog-Upload-Command"] = "upload, finalize" req.body = File.binread(file.path) res = request(http, req) LLM::Response::File.new(res) end |
#delete(file:, **params) ⇒ LLM::Response::File
Delete a file
119 120 121 122 123 124 |
# File 'lib/llm/providers/gemini/files.rb', line 119 def delete(file:, **params) file_id = file.respond_to?(:name) ? file.name : file.to_s query = URI.encode_www_form(params.merge!(key: secret)) req = Net::HTTP::Delete.new("/v1beta/#{file_id}?#{query}", headers) request(http, req) end |
#download ⇒ Object
129 130 131 |
# File 'lib/llm/providers/gemini/files.rb', line 129 def download raise NotImplementedError end |
#get(file:, **params) ⇒ LLM::Response::File
Get a file
101 102 103 104 105 106 107 |
# File 'lib/llm/providers/gemini/files.rb', line 101 def get(file:, **params) file_id = file.respond_to?(:name) ? file.name : file.to_s query = URI.encode_www_form(params.merge!(key: secret)) req = Net::HTTP::Get.new("/v1beta/#{file_id}?#{query}", headers) res = request(http, req) LLM::Response::File.new(res) end |