Module: ItemExtender
- Defined in:
- lib/gemini_cache/item_extender.rb
Instance Method Summary collapse
- #delete ⇒ Object
- #generate_content(contents:, generation_config: nil) ⇒ Object
- #single_prompt(prompt:, generation_config: :accurate_mode) ⇒ Object
- #ttl=(new_ttl) ⇒ Object
Instance Method Details
#delete ⇒ Object
2 |
# File 'lib/gemini_cache/item_extender.rb', line 2 def delete = GeminiCache.delete(name: self['name']) |
#generate_content(contents:, generation_config: nil) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/gemini_cache/item_extender.rb', line 9 def 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 |
#single_prompt(prompt:, generation_config: :accurate_mode) ⇒ Object
41 42 43 44 45 |
# File 'lib/gemini_cache/item_extender.rb', line 41 def 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 |
#ttl=(new_ttl) ⇒ Object
4 5 6 7 |
# File 'lib/gemini_cache/item_extender.rb', line 4 def ttl=(new_ttl) GeminiCache.update(name: self['name'], content: { ttl: "#{new_ttl}s" }) GeminiCache.get_by_name(name: self['name']) end |