geminiext 0.0.2 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/geminiext/cache.rb +43 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 06c950bcdc066e3a29b824226c9ce351a3555aece1cb6169526e7a350124ce98
|
4
|
+
data.tar.gz: a60ec80c78df74698c81bfbf5c009be396255ba51cc772d5541947e36c9c8db3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be97a24aa8df7fff3a54d53c0c236460db69910bdeb9b2760b9bb6099610f19870d7ed36c9626fb2ff6ef48d95b7f26207a7ceeb5d4df99c79e43440dbd9409e
|
7
|
+
data.tar.gz: 138f4a593288ae8593bf91900101bba8e37c0bbc2cadd48b82122c07d9ec1e67cb47896862d3fc4f77ad8c6cd64b3e2cb511a86017933f290bb52ebeedd890ac
|
data/lib/geminiext/cache.rb
CHANGED
@@ -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
|