geminiext 0.0.2 → 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/geminiext/cache.rb +43 -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: 06c950bcdc066e3a29b824226c9ce351a3555aece1cb6169526e7a350124ce98
4
+ data.tar.gz: a60ec80c78df74698c81bfbf5c009be396255ba51cc772d5541947e36c9c8db3
5
5
  SHA512:
6
- metadata.gz: 20fc9f1d721df29f89f419e6ac9e9d76af4220b1e901e548562681f6d612f557f59e1a8415bb1a013575da14f055026a838b995096e2fce2bf566d4831b53731
7
- data.tar.gz: 4d7b0de55589e664125455afc6c5a6a4fac115e610f4faa271a770e692b4ebbdd96e3a100c993fc2aeeadc9b93ce2dcd59152373ce0e6e8ee5eab425611091c3
6
+ metadata.gz: be97a24aa8df7fff3a54d53c0c236460db69910bdeb9b2760b9bb6099610f19870d7ed36c9626fb2ff6ef48d95b7f26207a7ceeb5d4df99c79e43440dbd9409e
7
+ data.tar.gz: 138f4a593288ae8593bf91900101bba8e37c0bbc2cadd48b82122c07d9ec1e67cb47896862d3fc4f77ad8c6cd64b3e2cb511a86017933f290bb52ebeedd890ac
@@ -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,41 @@ 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
+ if response.status == 200
68
+ resp = JSON.parse(response.body)
69
+ def resp.content = dig('candidates', 0, 'content', 'parts', 0, 'text')
70
+ return resp
71
+ end
72
+
73
+ raise "Erro ao gerar conteúdo: #{response.body}"
74
+ rescue Faraday::Error => e
75
+ raise "Erro na requisição: #{e.message}"
76
+ end
77
+
78
+ def item.single_prompt(prompt) = generate_content(contents: [{ parts: [{ text: prompt }], role: 'user' }])
79
+
80
+ item
81
+ end
40
82
 
41
- JSON.parse(response.body)
42
83
  rescue Faraday::Error => e
43
84
  raise "Erro na requisição: #{e.message}"
44
85
  end
@@ -51,7 +92,7 @@ module GeminiExt
51
92
 
52
93
  response = conn.patch("/v1beta/#{name}") do |req|
53
94
  req.params['key'] = ENV.fetch('GEMINI_API_KEY')
54
- req.body = content
95
+ req.body = content.to_json
55
96
  end
56
97
 
57
98
  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.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gedean Dias