Module: RubyLLM::Protocols::Gemini::Caches

Defined in:
lib/ruby_llm/protocols/gemini/caches.rb

Overview

Explicit content caching via the Gemini cachedContents API.

Class Method Summary collapse

Class Method Details

.cache_model_name(model_id) ⇒ Object



49
50
51
# File 'lib/ruby_llm/protocols/gemini/caches.rb', line 49

def cache_model_name(model_id)
  "models/#{model_id}"
end

.cache_name(name) ⇒ Object



44
45
46
47
# File 'lib/ruby_llm/protocols/gemini/caches.rb', line 44

def cache_name(name)
  name = name.name if name.is_a?(CachedContent)
  name.to_s.include?('/') ? name.to_s : "cachedContents/#{name}"
end

.cache_url(name) ⇒ Object



14
15
16
# File 'lib/ruby_llm/protocols/gemini/caches.rb', line 14

def cache_url(name)
  cache_name(name)
end

.caches_urlObject



10
11
12
# File 'lib/ruby_llm/protocols/gemini/caches.rb', line 10

def caches_url
  'cachedContents'
end

.format_cache_ttl(ttl) ⇒ Object



53
54
55
# File 'lib/ruby_llm/protocols/gemini/caches.rb', line 53

def format_cache_ttl(ttl)
  ttl.is_a?(String) ? ttl : "#{ttl.to_i}s"
end

.parse_cache_response(data) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ruby_llm/protocols/gemini/caches.rb', line 32

def parse_cache_response(data)
  CachedContent.new(
    name: data['name'],
    model: data['model']&.split('/')&.last,
    provider_instance: @provider,
    created_at: (Time.iso8601(data['createTime']) if data['createTime']),
    expires_at: (Time.iso8601(data['expireTime']) if data['expireTime']),
    tokens: data.dig('usageMetadata', 'totalTokenCount'),
    metadata: data
  )
end

.render_cache_payload(content, model:, ttl: nil, instructions: nil, attachments: []) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/ruby_llm/protocols/gemini/caches.rb', line 18

def render_cache_payload(content, model:, ttl: nil, instructions: nil, attachments: [])
  payload = {
    model: cache_model_name(model),
    contents: [{ role: 'user', parts: Media.format_content(content, attachments) }]
  }
  payload[:systemInstruction] = { parts: [{ text: instructions }] } if instructions
  payload[:ttl] = format_cache_ttl(ttl) if ttl
  payload
end

.render_cache_update_payload(ttl:) ⇒ Object



28
29
30
# File 'lib/ruby_llm/protocols/gemini/caches.rb', line 28

def render_cache_update_payload(ttl:)
  { ttl: format_cache_ttl(ttl) }
end