geminiext 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0217cac9967438f0bbd4fe0dc38b6c0aba1d21475b937c6b4d1b4d41d51a12b6
4
- data.tar.gz: df412fa82b43d42dcaca12b993cfa76f6c9e49dea76e55dfa9cbcddc79a9bee6
3
+ metadata.gz: 226e4ae21c091df3b4f0a02141097e2f55d475e0b34336fe7415e3517c8d7c73
4
+ data.tar.gz: 99d3eba31272bd7a04095e9fa1b9b56a897ecf058fd5c3ed67bc1e3530e489b1
5
5
  SHA512:
6
- metadata.gz: 884cdc51610f1d2beefd9884a4b779560adfff38c14deafc3bad84b3dc2ce84079cabf29eae88d96e6688d73dc25824d23eaea7fc2b379fac327de87974e29ea
7
- data.tar.gz: e9f69ffe68c4434738612884e54f89d5e471016ee1425846d374f9efa80b13e3fb4210f3f21a3b088e292861717fe0fa3f45473414d28307ec5b63781fe84b3b
6
+ metadata.gz: cdd1c32369e78fd98d74a432b85035f55bc2e2104bfd5d98ecfc5e3dd8c5066313767e7ad6d307cda80a41a37aa57723ff78214dae319f3e97cc4fb8a3d9a86c
7
+ data.tar.gz: 344c36bd43bddd92d7867e7d14789a0bd2919de308f86c2ed7dc1f8b7be36c8b62a4707a71d832e5375580802eb7c5fd776838c40f92ffa6cc5736c529433074
@@ -0,0 +1,116 @@
1
+ require 'faraday'
2
+ require 'json'
3
+
4
+ module GeminiExt
5
+ module Cache
6
+ def self.create(contents:, display_name:, model: 'gemini-1.5-flash-001', ttl: 600)
7
+ content = {
8
+ model: "models/#{model}",
9
+ display_name:,
10
+ contents:,
11
+ ttl: "#{ttl}s"
12
+ }.to_json
13
+
14
+ conn = Faraday.new(
15
+ url: 'https://generativelanguage.googleapis.com',
16
+ headers: { 'Content-Type' => 'application/json' }
17
+ )
18
+
19
+ response = conn.post('/v1beta/cachedContents') do |req|
20
+ req.params['key'] = ENV.fetch('GEMINI_API_KEY')
21
+ req.body = content
22
+ end
23
+
24
+ return JSON.parse(response.body) if response.status == 200
25
+
26
+ raise "Erro ao criar cache: #{response.status} - #{response.body}"
27
+ rescue Faraday::Error => e
28
+ raise "Erro na requisição: #{e.message}"
29
+ end
30
+
31
+ def self.get(name: nil, display_name: nil)
32
+ raise 'Nome do cache ou display name é obrigatório' if name.nil? && display_name.nil?
33
+ raise 'Nome do cache e display name não podem ser informados juntos' if !name.nil? && !display_name.nil?
34
+
35
+ return GeminiExt::Cache.list.find { |item| item['name'].eql? name } if !name.nil?
36
+ return GeminiExt::Cache.list.find { |item| item['displayName'].eql? display_name } if !display_name.nil?
37
+ end
38
+
39
+ def self.list
40
+ conn = Faraday.new(
41
+ url: 'https://generativelanguage.googleapis.com',
42
+ headers: { 'Content-Type' => 'application/json' }
43
+ )
44
+
45
+ response = conn.get("/v1beta/cachedContents") do |req|
46
+ req.params['key'] = ENV.fetch('GEMINI_API_KEY')
47
+ end
48
+
49
+ JSON.parse(response.body)['cachedContents'].map do |item|
50
+ def item.delete = GeminiExt::Cache.delete(name: self['name'])
51
+ def item.set_ttl(ttl = 120) = GeminiExt::Cache.update(name: self['name'], content: { ttl: "#{ttl}s" })
52
+
53
+ def item.generate_content(contents:)
54
+ conn = Faraday.new(
55
+ url: 'https://generativelanguage.googleapis.com',
56
+ headers: { 'Content-Type' => 'application/json' }
57
+ )
58
+
59
+ response = conn.post("/v1beta/models/#{self['model'].split('/').last}:generateContent") do |req|
60
+ req.params['key'] = ENV.fetch('GEMINI_API_KEY')
61
+ req.body = {
62
+ contents:,
63
+ cached_content: self['name']
64
+ }.to_json
65
+ end
66
+
67
+ return JSON.parse(response.body) if response.status == 200
68
+
69
+ raise "Erro ao gerar conteúdo: #{response.body}"
70
+ rescue Faraday::Error => e
71
+ raise "Erro na requisição: #{e.message}"
72
+ end
73
+
74
+ item
75
+ end
76
+
77
+ rescue Faraday::Error => e
78
+ raise "Erro na requisição: #{e.message}"
79
+ end
80
+
81
+ def self.update(name:, content:)
82
+ conn = Faraday.new(
83
+ url: 'https://generativelanguage.googleapis.com',
84
+ headers: { 'Content-Type' => 'application/json' }
85
+ )
86
+
87
+ response = conn.patch("/v1beta/#{name}") do |req|
88
+ req.params['key'] = ENV.fetch('GEMINI_API_KEY')
89
+ req.body = content.to_json
90
+ end
91
+
92
+ return JSON.parse(response.body) if response.status == 200
93
+
94
+ raise "Erro ao atualizar cache: #{response.body}"
95
+ rescue Faraday::Error => e
96
+ raise "Erro na requisição: #{e.message}"
97
+ end
98
+
99
+ def self.delete(name:)
100
+ conn = Faraday.new(
101
+ url: 'https://generativelanguage.googleapis.com',
102
+ headers: { 'Content-Type' => 'application/json' }
103
+ )
104
+
105
+ response = conn.delete("/v1beta/#{name}") do |req|
106
+ req.params['key'] = ENV.fetch('GEMINI_API_KEY')
107
+ end
108
+
109
+ return true if response.status == 200
110
+
111
+ raise "Erro ao deletar cache: #{response.body}"
112
+ rescue Faraday::Error => e
113
+ raise "Erro na requisição: #{e.message}"
114
+ end
115
+ end
116
+ end
data/lib/geminiext.rb CHANGED
@@ -1,15 +1,17 @@
1
1
  require 'gemini-ai'
2
+ #require 'faraday'
2
3
 
3
- # require 'geminiext/model'
4
+ require 'geminiext/cache'
4
5
  # require 'geminiext/messages'
5
6
  # require 'geminiext/response_extender'
6
7
 
7
8
  module GeminiExt
8
9
  MAX_TOKENS = ENV.fetch('GEMINI_MAX_TOKENS', 8_192).to_i
9
-
10
+
10
11
  def self.new(model: 'gemini-1.5-flash-001')
11
12
  Gemini.new(load_config(model: model))
12
13
  end
14
+
13
15
 
14
16
  def self.load_config(model: 'gemini-1.5-flash-001')
15
17
  {
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geminiext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gedean Dias
@@ -46,9 +46,10 @@ extra_rdoc_files: []
46
46
  files:
47
47
  - README.md
48
48
  - lib/geminiext.rb
49
- - lib/openaiext/messages.rb.disabled
50
- - lib/openaiext/model.rb.disabled
51
- - lib/openaiext/response_extender.rb.disabled
49
+ - lib/geminiext/cache.rb
50
+ - lib/geminiext/messages.rb.disabled
51
+ - lib/geminiext/model.rb.disabled
52
+ - lib/geminiext/response_extender.rb.disabled
52
53
  homepage: https://github.com/gedean/geminiext
53
54
  licenses:
54
55
  - MIT
File without changes
File without changes