Module: GeminiCache

Defined in:
lib/gemini_cache.rb

Class Method Summary collapse

Class Method Details

.create(parts:, display_name:, model: 'gemini-1.5-flash-8b', ttl: 300) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gemini_cache.rb', line 19

def self.create(parts:, display_name:, model: 'gemini-1.5-flash-8b', ttl: 300)
  raise "Cache name already exist: '#{display_name}'" if GeminiCache.get_by_display_name(display_name:)

  content = {
    model: "models/#{model}",
    display_name:,
    contents: [parts:, role: 'user'],
    ttl: "#{ttl}s"
  }.to_json

  conn = Faraday.new(
    url: 'https://generativelanguage.googleapis.com',
    headers: { 'Content-Type' => 'application/json' }
  )

  response = conn.post('/v1beta/cachedContents') do |req|
    req.params['key'] = ENV.fetch('GEMINI_API_KEY')
    req.body = content
  end

  return get_by_name(name: JSON.parse(response.body)['name']) if response.status == 200

  raise "Erro ao criar cache: #{response.status} - #{response.body}"
rescue Faraday::Error => e
  raise "Erro na requisição: #{e.message}"
end

.create_from_local_file(path:, mime_type:, display_name:, model: 'gemini-1.5-flash-8b', ttl: 300) ⇒ Object



54
55
56
# File 'lib/gemini_cache.rb', line 54

def self.create_from_local_file(path:, mime_type:, display_name:, model: 'gemini-1.5-flash-8b', ttl: 300)
  GeminiCache.create(parts: GeminiCache.read_local_file(path:, mime_type:), display_name:, model:, ttl:)
end

.create_from_remote_file(url:, mime_type:, display_name:, model: 'gemini-1.5-flash-8b', ttl: 300) ⇒ Object



58
59
60
# File 'lib/gemini_cache.rb', line 58

def self.create_from_remote_file(url:, mime_type:, display_name:, model: 'gemini-1.5-flash-8b', ttl: 300)
  GeminiCache.create(parts: GeminiCache.read_remote_file(url:, mime_type:), display_name:, model:, ttl:)
end

.create_from_text(text:, display_name:, model: 'gemini-1.5-flash-8b', ttl: 300) ⇒ Object



46
47
48
# File 'lib/gemini_cache.rb', line 46

def self.create_from_text(text:, display_name:, model: 'gemini-1.5-flash-8b', ttl: 300)
  GeminiCache.create(parts: [{ text: }], display_name:, model:, ttl:)
end

.create_from_webpage(url:, display_name:, model: 'gemini-1.5-flash-8b', ttl: 300) ⇒ Object



50
51
52
# File 'lib/gemini_cache.rb', line 50

def self.create_from_webpage(url:, display_name:, model: 'gemini-1.5-flash-8b', ttl: 300)
  create_from_text(text: GeminiCache.read_html(url:).inner_text, display_name:, model:, ttl:)
end

.delete(name:) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/gemini_cache.rb', line 101

def self.delete(name:)
  conn = Faraday.new(
    url: 'https://generativelanguage.googleapis.com',
    headers: { 'Content-Type' => 'application/json' }
  )

  response = conn.delete("/v1beta/#{name}") do |req|
    req.params['key'] = ENV.fetch('GEMINI_API_KEY')
  end

  return true if response.status == 200
  
  raise "Erro ao deletar cache: #{response.body}"
rescue Faraday::Error => e
  raise "Erro na requisição: #{e.message}"
end

.delete_allObject Also known as: clear



118
119
120
# File 'lib/gemini_cache.rb', line 118

def self.delete_all
  GeminiCache.list.each { |item| item.delete }
end

.get_by_display_name(display_name: nil) ⇒ Object



63
# File 'lib/gemini_cache.rb', line 63

def self.get_by_display_name(display_name: nil) = GeminiCache.list.find { |item| item['displayName'].eql? display_name }

.get_by_name(name: nil) ⇒ Object



62
# File 'lib/gemini_cache.rb', line 62

def self.get_by_name(name: nil) = GeminiCache.list.find { |item| item['name'].eql? name }

.listObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/gemini_cache.rb', line 65

def self.list
  conn = Faraday.new(
    url: 'https://generativelanguage.googleapis.com',
    headers: { 'Content-Type' => 'application/json' }
  )

  response = conn.get("/v1beta/cachedContents") do |req|
    req.params['key'] = ENV.fetch('GEMINI_API_KEY')
  end
  
  return [] if JSON.parse(response.body).empty?

  JSON.parse(response.body)['cachedContents'].map { |item| item.extend(ItemExtender) }

rescue Faraday::Error => e
  raise "Erro na requisição: #{e.message}"
end

.read_html(url:, default_remover: true) ⇒ Object



12
13
14
15
16
17
# File 'lib/gemini_cache.rb', line 12

def self.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:) ⇒ Object



10
# File 'lib/gemini_cache.rb', line 10

def self.read_local_file(path:, mime_type:) = { inline_data: { mime_type:, data: Base64.strict_encode64(File.read(path)) } }

.read_remote_file(url:, mime_type:) ⇒ Object



11
# File 'lib/gemini_cache.rb', line 11

def self.read_remote_file(url:, mime_type:) = { inline_data: { mime_type:, data: Base64.strict_encode64(URI.open(url).read) } }

.update(name:, content:) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/gemini_cache.rb', line 83

def self.update(name:, content:)
  conn = Faraday.new(
    url: 'https://generativelanguage.googleapis.com',
    headers: { 'Content-Type' => 'application/json' }
  )

  response = conn.patch("/v1beta/#{name}") do |req|
    req.params['key'] = ENV.fetch('GEMINI_API_KEY')
    req.body = content.to_json
  end

  return JSON.parse(response.body) if response.status == 200
  
  raise "Erro ao atualizar cache: #{response.body}"
rescue Faraday::Error => e
  raise "Erro na requisição: #{e.message}"
end