gemini_api_ruby 1.0.3 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/gemini_api_ruby/client.rb +37 -12
- data/lib/gemini_api_ruby/version.rb +1 -1
- 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: 547f60b4912b4a21c4f2faf3a204f437806a6250975994e3baad4d9df9734125
|
4
|
+
data.tar.gz: 8e239a07bc4c20a7aeca22afddd3ee2ec2f01bdc6145a437b16cbc7a48cd96e8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1b3c3521cbb1b066478f4dcf64a9b023bdf190540f923ccae11cc1390a966f1aab60578251c1b3bb04925755cc65da7de90ee0af1b40e2618a6520e3f1553294
|
7
|
+
data.tar.gz: 3b5639015fb507862962f3e508606f75df26adb0ed3addd04088370d7d005ae9260a7cdff5840b52cdcc5b7f5ab7549bbd1c35a9ce7a19b044955722a9deaabe
|
@@ -1,25 +1,50 @@
|
|
1
|
+
# lib/gemini_api_ruby/client.rb
|
2
|
+
|
1
3
|
module GeminiApiRuby
|
2
4
|
class Client
|
5
|
+
BASE_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash".freeze
|
6
|
+
|
3
7
|
def initialize(api_key:)
|
4
8
|
@api_key = api_key
|
5
9
|
end
|
6
10
|
|
7
|
-
def
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
def generate_content(prompt)
|
12
|
+
endpoint = "#{BASE_URL}:generateContent?key=#{@api_key}"
|
13
|
+
payload = {
|
14
|
+
contents: [
|
15
|
+
{
|
16
|
+
parts: [
|
17
|
+
{ text: prompt }
|
18
|
+
]
|
19
|
+
}
|
20
|
+
]
|
21
|
+
}
|
14
22
|
|
15
|
-
|
16
|
-
|
23
|
+
response = request(:post, endpoint, payload)
|
24
|
+
parse_response(response)
|
17
25
|
end
|
18
26
|
|
19
27
|
private
|
20
28
|
|
21
|
-
def request(method, endpoint,
|
22
|
-
|
29
|
+
def request(method, endpoint, payload)
|
30
|
+
connection = Faraday.new(url: endpoint) do |faraday|
|
31
|
+
faraday.request :json
|
32
|
+
faraday.response :json, content_type: /\bjson$/
|
33
|
+
faraday.adapter Faraday.default_adapter
|
34
|
+
end
|
35
|
+
|
36
|
+
connection.post do |req|
|
37
|
+
req.headers["Content-Type"] = "application/json"
|
38
|
+
req.body = payload.to_json
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def parse_response(response)
|
43
|
+
if response.success?
|
44
|
+
response.body.dig("candidates", 0, "content", "parts", 0, "text") || "No text generated"
|
45
|
+
else
|
46
|
+
raise GeminiApiRuby::Error, "API request failed: #{response.body['error']['message']}"
|
47
|
+
end
|
23
48
|
end
|
24
49
|
end
|
25
|
-
end
|
50
|
+
end
|