Module: GeminiExt::Cache

Defined in:
lib/geminiext/cache.rb

Class Method Summary collapse

Class Method Details

.create(contents:, display_name:, model: 'gemini-1.5-flash-001', ttl: 600) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/geminiext/cache.rb', line 6

def self.create(contents:, display_name:, model: 'gemini-1.5-flash-001', ttl: 600)
  content = {
    model: "models/#{model}",
    display_name:,
    contents:,
    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 JSON.parse(response.body) 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

.delete(name:) ⇒ Object



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

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

.listObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/geminiext/cache.rb', line 31

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

  JSON.parse(response.body)
rescue Faraday::Error => e
  raise "Erro na requisição: #{e.message}"
end

.update(name:, content:) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/geminiext/cache.rb', line 46

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
  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