gemini_cache 0.0.6 → 0.0.7

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 +30 -10
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 276c3ca26a6767aad15542bae697d58b6f6c546827c668bc922632d391ea0598
4
- data.tar.gz: 672b16527768229e7d84819b7d2b2844c41fe57c57e1bf1171414c182e8cc101
3
+ metadata.gz: ba751abef4a7f8f6f6c856d782c9e43dfcc27cfb64e6d69a6b6b095522fba338
4
+ data.tar.gz: 5dbef856507a550b7b739a5d972991ab822f36ca0aafef4bd5576df3b920c9c6
5
5
  SHA512:
6
- metadata.gz: 69700db12b8f6c8e78e3a3577843bc01f6fda112aec173653419c0674dd91baa59aee0a03be37eda62d9f1cf07e8a3892c47f28ec29d7a539d29b0d984988e3e
7
- data.tar.gz: 6992ec6e1537b00dc69de4afe0f13457a75758d6327f8c2bafecd9fb868e833d26a499363649e78e08ba9ac680641453fd572aaa52e4075ebac16885897e57a8
6
+ metadata.gz: 032fc3e2f78e791b55d82a63c15094eb325f5778ac4232dfb088e38b99bbd2402f9381ed79c12e03051768a2d488719f6be786cc815dd927927320942f2e8800
7
+ data.tar.gz: 31944aa6d6cbe592fd98909b465a1521b2049772d9436d09ad1663c9088473e7aae954ad7e224e4393670ca7d4cb7791295adff9be40a0b1536ecb5ab3b3f3cb
data/lib/gemini_cache.rb CHANGED
@@ -4,6 +4,15 @@ require 'nokogiri'
4
4
  require 'json'
5
5
 
6
6
  module GeminiCache
7
+ def self.read_local_file(file_path) = Base64.strict_encode64(File.read(file_path))
8
+ def self.read_remote_file(file_url) = Base64.strict_encode64(URI.open(file_url).read)
9
+ def self.read_html(url, default_remover: true)
10
+ doc = Nokogiri::HTML(URI.open(url))
11
+ %w[script style].each { |element| doc.css(element).each(&:remove) } if default_remover
12
+
13
+ doc
14
+ end
15
+
7
16
  def self.create(parts:, display_name:, model: 'gemini-1.5-flash-8b', ttl: 600)
8
17
  raise "Cache name already exist: '#{display_name}'" if GeminiCache.get(display_name:)
9
18
 
@@ -24,7 +33,7 @@ module GeminiCache
24
33
  req.body = content
25
34
  end
26
35
 
27
- return JSON.parse(response.body) if response.status == 200
36
+ return get(name: JSON.parse(response.body)['name']) if response.status == 200
28
37
 
29
38
  raise "Erro ao criar cache: #{response.status} - #{response.body}"
30
39
  rescue Faraday::Error => e
@@ -39,6 +48,26 @@ module GeminiCache
39
48
  return GeminiCache.list.find { |item| item['displayName'].eql? display_name } if !display_name.nil?
40
49
  end
41
50
 
51
+ def self.create_from_text(text:, display_name:, model: 'gemini-1.5-flash-8b', ttl: 600)
52
+ GeminiCache.create(parts: [{ text: }], display_name:, model:, ttl:)
53
+ end
54
+
55
+ def self.create_from_webpage(url:, display_name:, model: 'gemini-1.5-flash-8b', ttl: 600)
56
+ GeminiCache.create(parts: [{ text: GeminiCache.read_html(url).inner_text }], display_name:, model:, ttl:)
57
+ end
58
+
59
+ def self.create_from_local_file(file_path:, mime_type:, display_name:, model: 'gemini-1.5-flash-8b', ttl: 600)
60
+ parts = [{ inline_data: { mime_type:, data: GeminiCache.read_local_file(file_path) } }]
61
+
62
+ GeminiCache.create(parts:, display_name:, model:, ttl:)
63
+ end
64
+
65
+ def self.create_from_remote_file(file_url:, mime_type:, display_name:, model: 'gemini-1.5-flash-8b', ttl: 600)
66
+ parts = [{ inline_data: { mime_type:, data: GeminiCache.read_remote_file(file_url) } }]
67
+
68
+ GeminiCache.create(parts:, display_name:, model:, ttl:)
69
+ end
70
+
42
71
  def self.list
43
72
  conn = Faraday.new(
44
73
  url: 'https://generativelanguage.googleapis.com',
@@ -143,13 +172,4 @@ module GeminiCache
143
172
  class << self
144
173
  alias clear delete_all
145
174
  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
-
150
- def self.read_nokogiri_html(url, default_remover: true)
151
- doc = Nokogiri::HTML(URI.open(url))
152
- %w[script style].each { |element| doc.css(element).each(&:remove) } if default_remover
153
- doc
154
- end
155
175
  end
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.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gedean Dias