Module: ItemExtender
- Defined in:
- lib/gemini_cache/item_extender.rb
Constant Summary collapse
- GEMINI_API_BASE_URL =
'https://generativelanguage.googleapis.com'- DEFAULT_TIMEOUT =
seconds
300- ACCURATE_MODE_CONFIG =
{ temperature: 0, topP: 0, topK: 1 }.freeze
Instance Method Summary collapse
-
#delete ⇒ void
Deletes the cached item.
-
#generate_content(contents:, generation_config: nil) ⇒ Hash
Generates content using the Gemini API.
-
#single_prompt(prompt:, generation_config: :accurate_mode) ⇒ String
Generates content from a single prompt.
-
#ttl=(new_ttl) ⇒ void
Updates the TTL of the cached item.
Instance Method Details
#delete ⇒ void
This method returns an undefined value.
Deletes the cached item
10 11 12 |
# File 'lib/gemini_cache/item_extender.rb', line 10 def delete GeminiCache.delete(name: self['name']) end |
#generate_content(contents:, generation_config: nil) ⇒ Hash
Generates content using the Gemini API
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/gemini_cache/item_extender.rb', line 26 def generate_content(contents:, generation_config: nil) response = api_client.post(generate_content_endpoint) do |req| req.params['key'] = ENV.fetch('GEMINI_API_KEY') req.body = build_request_body(contents, generation_config) end handle_response(response) rescue Faraday::Error => e raise GeminiAPIError, "Request failed: #{e.message}" end |
#single_prompt(prompt:, generation_config: :accurate_mode) ⇒ String
Generates content from a single prompt
41 42 43 44 45 46 47 48 |
# File 'lib/gemini_cache/item_extender.rb', line 41 def single_prompt(prompt:, generation_config: :accurate_mode) config = generation_config.eql?(:accurate_mode) ? ACCURATE_MODE_CONFIG : generation_config generate_content( contents: [{ parts: [{ text: prompt }], role: 'user' }], generation_config: config ).content end |
#ttl=(new_ttl) ⇒ void
This method returns an undefined value.
Updates the TTL of the cached item
17 18 19 |
# File 'lib/gemini_cache/item_extender.rb', line 17 def ttl=(new_ttl) GeminiCache.update(name: self['name'], content: { ttl: "#{new_ttl}s" }.to_json) end |