geminiext 0.0.2 → 0.0.3

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/geminiext/cache.rb +37 -2
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8584393659f6094698f8efc8f19d5bb2c1a1608641614cc2bbbf69734853328a
4
- data.tar.gz: 586b5fb85e7b7074ead525b376379d79e01a471facf54c009b7575eb2dbabe10
3
+ metadata.gz: 226e4ae21c091df3b4f0a02141097e2f55d475e0b34336fe7415e3517c8d7c73
4
+ data.tar.gz: 99d3eba31272bd7a04095e9fa1b9b56a897ecf058fd5c3ed67bc1e3530e489b1
5
5
  SHA512:
6
- metadata.gz: 20fc9f1d721df29f89f419e6ac9e9d76af4220b1e901e548562681f6d612f557f59e1a8415bb1a013575da14f055026a838b995096e2fce2bf566d4831b53731
7
- data.tar.gz: 4d7b0de55589e664125455afc6c5a6a4fac115e610f4faa271a770e692b4ebbdd96e3a100c993fc2aeeadc9b93ce2dcd59152373ce0e6e8ee5eab425611091c3
6
+ metadata.gz: cdd1c32369e78fd98d74a432b85035f55bc2e2104bfd5d98ecfc5e3dd8c5066313767e7ad6d307cda80a41a37aa57723ff78214dae319f3e97cc4fb8a3d9a86c
7
+ data.tar.gz: 344c36bd43bddd92d7867e7d14789a0bd2919de308f86c2ed7dc1f8b7be36c8b62a4707a71d832e5375580802eb7c5fd776838c40f92ffa6cc5736c529433074
@@ -28,6 +28,14 @@ module GeminiExt
28
28
  raise "Erro na requisição: #{e.message}"
29
29
  end
30
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
+
31
39
  def self.list
32
40
  conn = Faraday.new(
33
41
  url: 'https://generativelanguage.googleapis.com',
@@ -37,8 +45,35 @@ module GeminiExt
37
45
  response = conn.get("/v1beta/cachedContents") do |req|
38
46
  req.params['key'] = ENV.fetch('GEMINI_API_KEY')
39
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
40
76
 
41
- JSON.parse(response.body)
42
77
  rescue Faraday::Error => e
43
78
  raise "Erro na requisição: #{e.message}"
44
79
  end
@@ -51,7 +86,7 @@ module GeminiExt
51
86
 
52
87
  response = conn.patch("/v1beta/#{name}") do |req|
53
88
  req.params['key'] = ENV.fetch('GEMINI_API_KEY')
54
- req.body = content
89
+ req.body = content.to_json
55
90
  end
56
91
 
57
92
  return JSON.parse(response.body) if response.status == 200
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.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gedean Dias