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

Instance Method Details

#deletevoid

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

Parameters:

  • contents (Array<Hash>)

    array of content parts

  • generation_config (Hash, nil) (defaults to: nil)

    optional generation configuration

Returns:

  • (Hash)

    response with added #content method

Raises:



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

Parameters:

  • prompt (String)

    the input prompt

  • generation_config (Hash, Symbol) (defaults to: :accurate_mode)

    generation configuration or :accurate_mode

Returns:

  • (String)

    generated content



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

Parameters:

  • new_ttl (Integer)

    new TTL value in seconds



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