Module: GeminiCache
- Defined in:
- lib/gemini_cache.rb
Class Method Summary collapse
- .create(parts:, display_name:, model: 'gemini-1.5-flash-8b', ttl: 600) ⇒ Object
- .delete(name:) ⇒ Object
- .delete_all ⇒ Object (also: clear)
- .get(name: nil, display_name: nil) ⇒ Object
- .list ⇒ Object
- .read_local_file(file_path) ⇒ Object
- .read_nokogiri_html(url, default_remover: true) ⇒ Object
- .read_remote_file(file_url) ⇒ Object
- .update(name:, content:) ⇒ Object
Class Method Details
.create(parts:, display_name:, model: 'gemini-1.5-flash-8b', ttl: 600) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/gemini_cache.rb', line 7 def self.create(parts:, display_name:, model: 'gemini-1.5-flash-8b', ttl: 600) raise "Cache name already exist: '#{display_name}'" if GeminiCache.get(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 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
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/gemini_cache.rb', line 122 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_all ⇒ Object Also known as: clear
139 140 141 |
# File 'lib/gemini_cache.rb', line 139 def self.delete_all GeminiCache.list.each { |item| item.delete } end |
.get(name: nil, display_name: nil) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/gemini_cache.rb', line 34 def self.get(name: nil, display_name: nil) raise 'Nome do cache ou display name é obrigatório' if name.nil? && display_name.nil? raise 'Nome do cache e display name não podem ser informados juntos' if !name.nil? && !display_name.nil? return GeminiCache.list.find { |item| item['name'].eql? name } if !name.nil? return GeminiCache.list.find { |item| item['displayName'].eql? display_name } if !display_name.nil? end |
.list ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/gemini_cache.rb', line 42 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 do |item| def item.delete = GeminiCache.delete(name: self['name']) def item.set_ttl(ttl = 120) = GeminiCache.update(name: self['name'], content: { ttl: "#{ttl}s" }) def item.generate_content(contents:, generation_config: nil) conn = Faraday.new( url: 'https://generativelanguage.googleapis.com', headers: { 'Content-Type' => 'application/json' } ) do |f| f..timeout = 300 # timeout em segundos para a requisição completa f..open_timeout = 300 # timeout em segundos para abrir a conexão end body = { cached_content: self['name'], contents: } body[:generation_config] = generation_config if !generation_config.nil? response = conn.post("/v1beta/models/#{self['model'].split('/').last}:generateContent") do |req| req.params['key'] = ENV.fetch('GEMINI_API_KEY') req.body = body.to_json end if response.status == 200 resp = JSON.parse(response.body) def resp.content = dig('candidates', 0, 'content', 'parts', 0, 'text') return resp end raise "Erro ao gerar conteúdo: #{response.body}" rescue Faraday::Error => e raise "Erro na requisição: #{e.message}" end def item.single_prompt(prompt:, generation_config: :accurate_mode) # accurate_mode: less creative, more accurate generation_config = { temperature: 0, topP: 0, topK: 1 } if generation_config.eql?(:accurate_mode) generate_content(contents: [{ parts: [{ text: prompt }], role: 'user' }], generation_config:).content end item end rescue Faraday::Error => e raise "Erro na requisição: #{e.message}" end |
.read_local_file(file_path) ⇒ Object
147 |
# File 'lib/gemini_cache.rb', line 147 def self.read_local_file(file_path) = Base64.strict_encode64(File.read(file_path)) |
.read_nokogiri_html(url, default_remover: true) ⇒ Object
150 151 152 153 154 |
# File 'lib/gemini_cache.rb', line 150 def self.read_nokogiri_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_remote_file(file_url) ⇒ Object
148 |
# File 'lib/gemini_cache.rb', line 148 def self.read_remote_file(file_url) = Base64.strict_encode64(URI.open(file_url).read) |
.update(name:, content:) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/gemini_cache.rb', line 104 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 |