Module: GeminiCache
- Defined in:
- lib/gemini_cache.rb,
lib/gemini_cache/api_client.rb,
lib/gemini_cache/configuration.rb
Overview
Module for interacting with Google’s Gemini API cached contents
Defined Under Namespace
Classes: ApiClient, Configuration, Error
Class Method Summary collapse
-
.configuration ⇒ Configuration
Current configuration.
-
.configure {|Configuration| ... } ⇒ Object
Configures GeminiCache.
-
.create(parts:, display_name:, on_conflict: :raise_error, model: nil, ttl: nil) ⇒ Hash
Creates a new cache in the Gemini API.
-
.create_from_local_file(path:, mime_type:, **options) ⇒ Hash
Creates a cache from a local file.
-
.create_from_remote_file(url:, mime_type:, **options) ⇒ Hash
Creates a cache from a remote file.
-
.create_from_text(text:, **options) ⇒ Hash
Creates a cache from plain text.
-
.create_from_webpage(url:, **options) ⇒ Hash
Creates a cache from a webpage’s content.
-
.delete(name:) ⇒ Boolean
Deletes a specific cache.
-
.delete_all ⇒ void
(also: clear)
Deletes all caches.
-
.find_by_display_name(display_name:) ⇒ Hash?
Finds a cache by its display name.
-
.find_by_name(name:) ⇒ Hash?
Finds a cache by its internal name.
-
.list ⇒ Array<Hash>
Lists all available caches.
-
.read_html(url:, default_remover: true) ⇒ Nokogiri::HTML::Document
Reads and parses HTML content from a URL.
-
.read_local_file(path:, mime_type:) ⇒ Hash
Reads a local file and prepares it for the Gemini API.
-
.read_remote_file(url:, mime_type:) ⇒ Hash
Reads a remote file and prepares it for the Gemini API.
-
.update(name:, content:) ⇒ Hash
Updates an existing cache.
Class Method Details
.configuration ⇒ Configuration
Returns current configuration.
20 21 22 |
# File 'lib/gemini_cache/configuration.rb', line 20 def configuration @configuration ||= Configuration.new end |
.configure {|Configuration| ... } ⇒ Object
Configures GeminiCache
31 32 33 |
# File 'lib/gemini_cache/configuration.rb', line 31 def configure yield(configuration) end |
.create(parts:, display_name:, on_conflict: :raise_error, model: nil, ttl: nil) ⇒ Hash
Creates a new cache in the Gemini API
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/gemini_cache.rb', line 61 def create(parts:, display_name:, on_conflict: :raise_error, model: nil, ttl: nil) existing_cache = find_by_display_name(display_name:) if existing_cache case on_conflict when :raise_error raise Error, "Cache with display name '#{display_name}' already exists" when :get_existing return existing_cache end end content = { model: "models/#{model || configuration.default_model}", display_name: display_name, contents: [{ parts:, role: 'user' }], ttl: "#{ttl || configuration.default_ttl}s" } response = api_client.create_cache(content.to_json) find_by_name(name: response['name']) end |
.create_from_local_file(path:, mime_type:, **options) ⇒ Hash
Creates a cache from a local file
105 106 107 108 |
# File 'lib/gemini_cache.rb', line 105 def create_from_local_file(path:, mime_type:, **) file_data = read_local_file(path: path, mime_type: mime_type) create(parts: [file_data], **) end |
.create_from_remote_file(url:, mime_type:, **options) ⇒ Hash
Creates a cache from a remote file
115 116 117 118 |
# File 'lib/gemini_cache.rb', line 115 def create_from_remote_file(url:, mime_type:, **) file_data = read_remote_file(url: url, mime_type: mime_type) create(parts: [file_data], **) end |
.create_from_text(text:, **options) ⇒ Hash
Creates a cache from plain text
88 89 90 |
# File 'lib/gemini_cache.rb', line 88 def create_from_text(text:, **) create(parts: [{ text: }], **) end |
.create_from_webpage(url:, **options) ⇒ Hash
Creates a cache from a webpage’s content
96 97 98 |
# File 'lib/gemini_cache.rb', line 96 def create_from_webpage(url:, **) create_from_text(text: read_html(url:).inner_text, **) end |
.delete(name:) ⇒ Boolean
Deletes a specific cache
154 155 156 157 |
# File 'lib/gemini_cache.rb', line 154 def delete(name:) api_client.delete_cache(name) true end |
.delete_all ⇒ void Also known as: clear
This method returns an undefined value.
Deletes all caches
161 162 163 |
# File 'lib/gemini_cache.rb', line 161 def delete_all list.each { |item| item.delete } end |
.find_by_display_name(display_name:) ⇒ Hash?
Finds a cache by its display name
139 140 141 |
# File 'lib/gemini_cache.rb', line 139 def find_by_display_name(display_name:) list.find { |item| item['displayName'].eql?(display_name) } end |
.find_by_name(name:) ⇒ Hash?
Finds a cache by its internal name
132 133 134 |
# File 'lib/gemini_cache.rb', line 132 def find_by_name(name:) list.find { |item| item['name'].eql?(name) } end |
.list ⇒ Array<Hash>
Lists all available caches
122 123 124 125 126 127 |
# File 'lib/gemini_cache.rb', line 122 def list response = api_client.list_caches return [] if response.empty? response['cachedContents'].map { |item| item.extend(ItemExtender) } end |
.read_html(url:, default_remover: true) ⇒ Nokogiri::HTML::Document
Reads and parses HTML content from a URL
47 48 49 50 51 |
# File 'lib/gemini_cache.rb', line 47 def read_html(url:, default_remover: true) doc = Nokogiri::HTML(URI.open(url)) %w[script style].each { |element| doc.css(element).each(&:remove) } if default_remover doc end |
.read_local_file(path:, mime_type:) ⇒ Hash
Reads a local file and prepares it for the Gemini API
31 32 33 |
# File 'lib/gemini_cache.rb', line 31 def read_local_file(path:, mime_type:) { inline_data: { mime_type:, data: Base64.strict_encode64(File.read(path)) } } end |
.read_remote_file(url:, mime_type:) ⇒ Hash
Reads a remote file and prepares it for the Gemini API
39 40 41 |
# File 'lib/gemini_cache.rb', line 39 def read_remote_file(url:, mime_type:) { inline_data: { mime_type:, data: Base64.strict_encode64(URI.open(url).read) } } end |
.update(name:, content:) ⇒ Hash
Updates an existing cache
147 148 149 |
# File 'lib/gemini_cache.rb', line 147 def update(name:, content:) api_client.update_cache(name, content) end |