gemini_cache 0.0.7 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/gemini_cache/item_extender.rb +46 -0
- data/lib/gemini_cache.rb +20 -70
- metadata +30 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ae4847ce3abf808c2779601462af28a47989301593355a8a39977a5df495f322
|
4
|
+
data.tar.gz: 71e72d0e2750d6b530d407f97a7490c65b6acbf342ad10803916b4b24d418b71
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8ab29a93e9555626f6d1eecb189351b5d62b28805e0e90e0ba9c29cb9e5bcf8698f72dadb0621ffbf196503d186a2c341b45c387bc25f940d055bb110455ff78
|
7
|
+
data.tar.gz: 50a917b60662ecd5bc1cef90c0a633febfcfbe294c297525d397f60863dfaa29977e43c0743c9f717452fa7d45ce5fe2da8f11f3816d0f65fa5e06f6632675ff
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module ItemExtender
|
2
|
+
def delete = GeminiCache.delete(name: self['name'])
|
3
|
+
|
4
|
+
def ttl=(new_ttl)
|
5
|
+
GeminiCache.update(name: self['name'], content: { ttl: "#{new_ttl}s" })
|
6
|
+
GeminiCache.get_by_name(name: self['name'])
|
7
|
+
end
|
8
|
+
|
9
|
+
def generate_content(contents:, generation_config: nil)
|
10
|
+
conn = Faraday.new(
|
11
|
+
url: 'https://generativelanguage.googleapis.com',
|
12
|
+
headers: { 'Content-Type' => 'application/json' }
|
13
|
+
) do |f|
|
14
|
+
f.options.timeout = 300 # timeout em segundos para a requisição completa
|
15
|
+
f.options.open_timeout = 300 # timeout em segundos para abrir a conexão
|
16
|
+
end
|
17
|
+
|
18
|
+
body = {
|
19
|
+
cached_content: self['name'],
|
20
|
+
contents:
|
21
|
+
}
|
22
|
+
|
23
|
+
body[:generation_config] = generation_config if !generation_config.nil?
|
24
|
+
|
25
|
+
response = conn.post("/v1beta/models/#{self['model'].split('/').last}:generateContent") do |req|
|
26
|
+
req.params['key'] = ENV.fetch('GEMINI_API_KEY')
|
27
|
+
req.body = body.to_json
|
28
|
+
end
|
29
|
+
|
30
|
+
if response.status == 200
|
31
|
+
resp = JSON.parse(response.body)
|
32
|
+
def resp.content = dig('candidates', 0, 'content', 'parts', 0, 'text')
|
33
|
+
return resp
|
34
|
+
end
|
35
|
+
|
36
|
+
raise "Erro ao gerar conteúdo: #{response.body}"
|
37
|
+
rescue Faraday::Error => e
|
38
|
+
raise "Erro na requisição: #{e.message}"
|
39
|
+
end
|
40
|
+
|
41
|
+
def single_prompt(prompt:, generation_config: :accurate_mode)
|
42
|
+
# accurate_mode: less creative, more accurate
|
43
|
+
generation_config = { temperature: 0, topP: 0, topK: 1 } if generation_config.eql?(:accurate_mode)
|
44
|
+
generate_content(contents: [{ parts: [{ text: prompt }], role: 'user' }], generation_config:).content
|
45
|
+
end
|
46
|
+
end
|
data/lib/gemini_cache.rb
CHANGED
@@ -2,19 +2,22 @@ require 'faraday'
|
|
2
2
|
require 'open-uri'
|
3
3
|
require 'nokogiri'
|
4
4
|
require 'json'
|
5
|
+
require 'base64'
|
6
|
+
|
7
|
+
require 'gemini_cache/item_extender'
|
5
8
|
|
6
9
|
module GeminiCache
|
7
|
-
def self.read_local_file(
|
8
|
-
def self.read_remote_file(
|
9
|
-
def self.read_html(url
|
10
|
+
def self.read_local_file(path:, mime_type:) = { inline_data: { mime_type:, data: Base64.strict_encode64(File.read(path)) } }
|
11
|
+
def self.read_remote_file(url:, mime_type:) = { inline_data: { mime_type:, data: Base64.strict_encode64(URI.open(url).read) } }
|
12
|
+
def self.read_html(url:, default_remover: true)
|
10
13
|
doc = Nokogiri::HTML(URI.open(url))
|
11
14
|
%w[script style].each { |element| doc.css(element).each(&:remove) } if default_remover
|
12
15
|
|
13
16
|
doc
|
14
17
|
end
|
15
18
|
|
16
|
-
def self.create(parts:, display_name:, model: 'gemini-1.5-flash-8b', ttl:
|
17
|
-
raise "Cache name already exist: '#{display_name}'" if GeminiCache.
|
19
|
+
def self.create(parts:, display_name:, model: 'gemini-1.5-flash-8b', ttl: 300)
|
20
|
+
raise "Cache name already exist: '#{display_name}'" if GeminiCache.get_by_display_name(display_name:)
|
18
21
|
|
19
22
|
content = {
|
20
23
|
model: "models/#{model}",
|
@@ -33,41 +36,32 @@ module GeminiCache
|
|
33
36
|
req.body = content
|
34
37
|
end
|
35
38
|
|
36
|
-
return
|
39
|
+
return get_by_name(name: JSON.parse(response.body)['name']) if response.status == 200
|
37
40
|
|
38
41
|
raise "Erro ao criar cache: #{response.status} - #{response.body}"
|
39
42
|
rescue Faraday::Error => e
|
40
43
|
raise "Erro na requisição: #{e.message}"
|
41
44
|
end
|
42
45
|
|
43
|
-
def self.
|
44
|
-
raise 'Nome do cache ou display name é obrigatório' if name.nil? && display_name.nil?
|
45
|
-
raise 'Nome do cache e display name não podem ser informados juntos' if !name.nil? && !display_name.nil?
|
46
|
-
|
47
|
-
return GeminiCache.list.find { |item| item['name'].eql? name } if !name.nil?
|
48
|
-
return GeminiCache.list.find { |item| item['displayName'].eql? display_name } if !display_name.nil?
|
49
|
-
end
|
50
|
-
|
51
|
-
def self.create_from_text(text:, display_name:, model: 'gemini-1.5-flash-8b', ttl: 600)
|
46
|
+
def self.create_from_text(text:, display_name:, model: 'gemini-1.5-flash-8b', ttl: 300)
|
52
47
|
GeminiCache.create(parts: [{ text: }], display_name:, model:, ttl:)
|
53
48
|
end
|
54
49
|
|
55
|
-
def self.create_from_webpage(url:, display_name:, model: 'gemini-1.5-flash-8b', ttl:
|
56
|
-
|
50
|
+
def self.create_from_webpage(url:, display_name:, model: 'gemini-1.5-flash-8b', ttl: 300)
|
51
|
+
create_from_text(text: GeminiCache.read_html(url:).inner_text, display_name:, model:, ttl:)
|
57
52
|
end
|
58
53
|
|
59
|
-
def self.create_from_local_file(
|
60
|
-
parts
|
54
|
+
def self.create_from_local_file(path:, mime_type:, display_name:, model: 'gemini-1.5-flash-8b', ttl: 300)
|
55
|
+
GeminiCache.create(parts: GeminiCache.read_local_file(path:, mime_type:), display_name:, model:, ttl:)
|
56
|
+
end
|
61
57
|
|
62
|
-
|
58
|
+
def self.create_from_remote_file(url:, mime_type:, display_name:, model: 'gemini-1.5-flash-8b', ttl: 300)
|
59
|
+
GeminiCache.create(parts: GeminiCache.read_remote_file(url:, mime_type:), display_name:, model:, ttl:)
|
63
60
|
end
|
64
61
|
|
65
|
-
def self.
|
66
|
-
|
62
|
+
def self.get_by_name(name: nil) = GeminiCache.list.find { |item| item['name'].eql? name }
|
63
|
+
def self.get_by_display_name(display_name: nil) = GeminiCache.list.find { |item| item['displayName'].eql? display_name }
|
67
64
|
|
68
|
-
GeminiCache.create(parts:, display_name:, model:, ttl:)
|
69
|
-
end
|
70
|
-
|
71
65
|
def self.list
|
72
66
|
conn = Faraday.new(
|
73
67
|
url: 'https://generativelanguage.googleapis.com',
|
@@ -80,51 +74,7 @@ module GeminiCache
|
|
80
74
|
|
81
75
|
return [] if JSON.parse(response.body).empty?
|
82
76
|
|
83
|
-
JSON.parse(response.body)['cachedContents'].map
|
84
|
-
def item.delete = GeminiCache.delete(name: self['name'])
|
85
|
-
def item.set_ttl(ttl = 120) = GeminiCache.update(name: self['name'], content: { ttl: "#{ttl}s" })
|
86
|
-
|
87
|
-
def item.generate_content(contents:, generation_config: nil)
|
88
|
-
conn = Faraday.new(
|
89
|
-
url: 'https://generativelanguage.googleapis.com',
|
90
|
-
headers: { 'Content-Type' => 'application/json' }
|
91
|
-
) do |f|
|
92
|
-
f.options.timeout = 300 # timeout em segundos para a requisição completa
|
93
|
-
f.options.open_timeout = 300 # timeout em segundos para abrir a conexão
|
94
|
-
end
|
95
|
-
|
96
|
-
body = {
|
97
|
-
cached_content: self['name'],
|
98
|
-
contents:
|
99
|
-
}
|
100
|
-
|
101
|
-
body[:generation_config] = generation_config if !generation_config.nil?
|
102
|
-
|
103
|
-
response = conn.post("/v1beta/models/#{self['model'].split('/').last}:generateContent") do |req|
|
104
|
-
req.params['key'] = ENV.fetch('GEMINI_API_KEY')
|
105
|
-
req.body = body.to_json
|
106
|
-
end
|
107
|
-
|
108
|
-
if response.status == 200
|
109
|
-
resp = JSON.parse(response.body)
|
110
|
-
def resp.content = dig('candidates', 0, 'content', 'parts', 0, 'text')
|
111
|
-
return resp
|
112
|
-
end
|
113
|
-
|
114
|
-
raise "Erro ao gerar conteúdo: #{response.body}"
|
115
|
-
rescue Faraday::Error => e
|
116
|
-
raise "Erro na requisição: #{e.message}"
|
117
|
-
end
|
118
|
-
|
119
|
-
def item.single_prompt(prompt:, generation_config: :accurate_mode)
|
120
|
-
# accurate_mode: less creative, more accurate
|
121
|
-
generation_config = { temperature: 0, topP: 0, topK: 1 } if generation_config.eql?(:accurate_mode)
|
122
|
-
|
123
|
-
generate_content(contents: [{ parts: [{ text: prompt }], role: 'user' }], generation_config:).content
|
124
|
-
end
|
125
|
-
|
126
|
-
item
|
127
|
-
end
|
77
|
+
JSON.parse(response.body)['cachedContents'].map { |item| item.extend(ItemExtender) }
|
128
78
|
|
129
79
|
rescue Faraday::Error => e
|
130
80
|
raise "Erro na requisição: #{e.message}"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gemini_cache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gedean Dias
|
@@ -24,6 +24,34 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: base64
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.2.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.2.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: nokogiri
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1'
|
27
55
|
description: Ruby's Gemini Context Caching wrapper
|
28
56
|
email: gedean.dias@gmail.com
|
29
57
|
executables: []
|
@@ -31,6 +59,7 @@ extensions: []
|
|
31
59
|
extra_rdoc_files: []
|
32
60
|
files:
|
33
61
|
- lib/gemini_cache.rb
|
62
|
+
- lib/gemini_cache/item_extender.rb
|
34
63
|
homepage: https://github.com/gedean/gemini_cache
|
35
64
|
licenses:
|
36
65
|
- MIT
|