Class: LLM::Google::Files
- Inherits:
-
Object
- Object
- LLM::Google::Files
- Defined in:
- lib/llm/providers/google/files.rb
Overview
The LLM::Google::Files class provides a files object for interacting with Gemini's Files API. 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
-
#initialize(provider)
⇒ LLM::Google::Files constructor
Returns a new Files object.
-
#all(**params) ⇒
LLM::Response
List all files.
-
#create(file:,
**params) ⇒ LLM::Response
Create a file.
-
#get(file:, **params) ⇒
LLM::Response
Get a file.
-
#delete(file:,
**params) ⇒ LLM::Response
Delete a file.
- #download ⇒ Object
Constructor Details
#initialize(provider) ⇒ LLM::Google::Files
Returns a new Files object
30 31 32 |
# File 'lib/llm/providers/google/files.rb', line 30 def initialize(provider) @provider = provider end |
Instance Method Details
#all(**params) ⇒ LLM::Response
List all files
46 47 48 49 50 51 52 53 |
# File 'lib/llm/providers/google/files.rb', line 46 def all(**params) query = URI.encode_www_form(params.merge!(key: key)) req = Net::HTTP::Get.new("/v1beta/files?#{query}", headers) res, span, tracer = execute(request: req, operation: "request") res = ResponseAdapter.adapt(res, type: :files) tracer.on_request_finish(operation: "request", res:, span:) res end |
#create(file:, **params) ⇒ LLM::Response
Create a file
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/llm/providers/google/files.rb', line 65 def create(file:, **params) file = LLM.File(file) 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" file.with_io do |io| set_body_stream(req, io) res, span, tracer = execute(request: req, operation: "request") res = ResponseAdapter.adapt(res, type: :file) tracer.on_request_finish(operation: "request", res:, span:) res end end |
#get(file:, **params) ⇒ LLM::Response
Get a file
91 92 93 94 95 96 97 98 99 |
# File 'lib/llm/providers/google/files.rb', line 91 def get(file:, **params) file_id = file.respond_to?(:name) ? file.name : file.to_s query = URI.encode_www_form(params.merge!(key: key)) req = Net::HTTP::Get.new("/v1beta/#{file_id}?#{query}", headers) res, span, tracer = execute(request: req, operation: "request") res = ResponseAdapter.adapt(res, type: :file) tracer.on_request_finish(operation: "request", res:, span:) res end |
#delete(file:, **params) ⇒ LLM::Response
Delete a file
111 112 113 114 115 116 117 118 119 |
# File 'lib/llm/providers/google/files.rb', line 111 def delete(file:, **params) file_id = file.respond_to?(:name) ? file.name : file.to_s query = URI.encode_www_form(params.merge!(key: key)) req = Net::HTTP::Delete.new("/v1beta/#{file_id}?#{query}", headers) res, span, tracer = execute(request: req, operation: "request") res = LLM::Response.new(res) tracer.on_request_finish(operation: "request", res:, span:) res end |
#download ⇒ Object
124 125 126 |
# File 'lib/llm/providers/google/files.rb', line 124 def download raise NotImplementedError end |