gemini_cache 0.0.3 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/gemini_cache.rb +25 -8
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '04685d49a72d97ba748888a2c1a90ffae49dd52d5bdea582e5cc93c5278ee05d'
4
- data.tar.gz: 1fbf73ef5d37c9eeffee36e2667faf3846a326a217c4ae3dcc9bd6f781bec0ed
3
+ metadata.gz: db225c3f8c93563f2bd4f1c8bd4fec3df644609ca9268bb7eeec996f42eaf48b
4
+ data.tar.gz: bb4f2198799740b227c16621396475f171b89806aae3f9644631651c22dfc049
5
5
  SHA512:
6
- metadata.gz: 9397ae965759c13a8b4f23cd26e17fbd66807cf6ef09ec299b5ff6695fdbc32f74c9858574f727da489d7039d761be2d900220c39d8d3d9a6cfb22360eb6bcdb
7
- data.tar.gz: efa123d59d17f71e2193b82de461b0648d5a887377175331993b9bc48ef17872b0987f9a9622311c40062a6639f3c72e792d69b5a2345568f4985601eb0de624
6
+ metadata.gz: d5f925f85d3c1bbe71b1b2c44febdce091ca508db753c26bc543613c81fffdefc2a4636f7bdb7ac06152f25d759bb805d6279e4a74d1bcf9452d18e7d0889f5c
7
+ data.tar.gz: 228a2728324e5d10210ec3c9691c1ce505cd3b13fa4914ba889bb6348dc24c43ddcf35c6c8821af6c583a3f0893a7d1467ce521bbf8bbec33454cd631e6fc420
data/lib/gemini_cache.rb CHANGED
@@ -1,12 +1,16 @@
1
1
  require 'faraday'
2
+ require 'open-uri'
3
+ require 'nokogiri'
2
4
  require 'json'
3
5
 
4
6
  module GeminiCache
5
- def self.create(contents:, display_name:, model: 'gemini-1.5-flash-8b', ttl: 600)
7
+ def self.create(parts:, display_name:, model: 'gemini-1.5-flash-8b', ttl: 600)
8
+ raise "Cache name already exist: '#{display_name}'" if GeminiCache.get(display_name:)
9
+
6
10
  content = {
7
11
  model: "models/#{model}",
8
12
  display_name:,
9
- contents:,
13
+ contents: [parts:, role: 'user'],
10
14
  ttl: "#{ttl}s"
11
15
  }.to_json
12
16
 
@@ -51,7 +55,7 @@ module GeminiCache
51
55
  def item.delete = GeminiCache.delete(name: self['name'])
52
56
  def item.set_ttl(ttl = 120) = GeminiCache.update(name: self['name'], content: { ttl: "#{ttl}s" })
53
57
 
54
- def item.generate_content(contents:)
58
+ def item.generate_content(contents:, generation_config: nil)
55
59
  conn = Faraday.new(
56
60
  url: 'https://generativelanguage.googleapis.com',
57
61
  headers: { 'Content-Type' => 'application/json' }
@@ -60,12 +64,16 @@ module GeminiCache
60
64
  f.options.open_timeout = 300 # timeout em segundos para abrir a conexão
61
65
  end
62
66
 
67
+ body = {
68
+ cached_content: self['name'],
69
+ contents:
70
+ }
71
+
72
+ body[:generation_config] = generation_config if !generation_config.nil?
73
+
63
74
  response = conn.post("/v1beta/models/#{self['model'].split('/').last}:generateContent") do |req|
64
75
  req.params['key'] = ENV.fetch('GEMINI_API_KEY')
65
- req.body = {
66
- cached_content: self['name'],
67
- contents:
68
- }.to_json
76
+ req.body = body.to_json
69
77
  end
70
78
 
71
79
  if response.status == 200
@@ -79,7 +87,12 @@ module GeminiCache
79
87
  raise "Erro na requisição: #{e.message}"
80
88
  end
81
89
 
82
- def item.single_prompt(prompt:) = generate_content(contents: [{ parts: [{ text: prompt }], role: 'user' }]).content
90
+ def item.single_prompt(prompt:, generation_config: :accurate_mode)
91
+ # accurate_mode: less creative, more accurate
92
+ generation_config = { temperature: 0, topP: 0, topK: 1 } if generation_config.eql?(:accurate_mode)
93
+
94
+ generate_content(contents: [{ parts: [{ text: prompt }], role: 'user' }], generation_config:).content
95
+ end
83
96
 
84
97
  item
85
98
  end
@@ -130,4 +143,8 @@ module GeminiCache
130
143
  class << self
131
144
  alias clear delete_all
132
145
  end
146
+
147
+ def self.read_local_file(file_path) = Base64.strict_encode64(File.read(file_path))
148
+ def self.read_remote_file(file_url) = Base64.strict_encode64(URI.open(file_url).read)
149
+ def self.read_nokogiri_html(url) = Nokogiri::HTML(URI.open(url))
133
150
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gemini_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gedean Dias
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-11-10 00:00:00.000000000 Z
11
+ date: 2024-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday