gemini_client 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +54 -0
- data/Rakefile +4 -0
- data/lib/gemini_client/version.rb +5 -0
- data/lib/gemini_client.rb +83 -0
- data/sig/gemini_client.rbs +4 -0
- metadata +66 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: fa79c9c23c71cae2d43dc94db1c8ca92faec83c7458ca3fa99c4b985a3017ce3
|
4
|
+
data.tar.gz: 04ae660a3b939ae5867ffc1ba8fe217cc587b92acd0c62d86fb79cf9927a802e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bb659e7159c0b9d72cc9a21f6fbd57e36d5b6ae70773f727a5b51a0a96fc45a15bfce5efc41c8a65bcc62271080b5721ceb9c10aa92ecb2f4eeda49b16f3d5ce
|
7
|
+
data.tar.gz: 40e46bd1a1e8b4dcd325542ccd7a5994c76189732a34f8abf60088b93517b3e996461cbaafcdc979d928f9b1135cd27cd956ec80962268bac275e3a2def7724b
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 i2bskn
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# GeminiClient
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/gemini_client`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Install the gem and add to the application's Gemfile by executing:
|
8
|
+
|
9
|
+
$ bundle add gemini_client
|
10
|
+
|
11
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
12
|
+
|
13
|
+
$ gem install gemini_client
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
```
|
18
|
+
client = GeminiClient.new(api_key: GEMINI_API_KEY)
|
19
|
+
payload = {
|
20
|
+
contents: [
|
21
|
+
{
|
22
|
+
"role": "user",
|
23
|
+
"parts": [
|
24
|
+
{
|
25
|
+
"text": "hello"
|
26
|
+
}
|
27
|
+
]
|
28
|
+
}
|
29
|
+
]
|
30
|
+
}
|
31
|
+
res = client.generate_content(payload: payload)
|
32
|
+
data = JSON.parse(res.body)
|
33
|
+
pp data
|
34
|
+
```
|
35
|
+
|
36
|
+
```
|
37
|
+
{"candidates"=>[{"content"=>{"parts"=>[{"text"=>"Hello there! How can I help you today?\n"}], "role"=>"model"}, "finishReason"=>"STOP", "avgLogprobs"=>-0.0006325314752757549}],
|
38
|
+
"usageMetadata"=>{"promptTokenCount"=>2, "candidatesTokenCount"=>11, "totalTokenCount"=>13},
|
39
|
+
"modelVersion"=>"gemini-1.5-flash"}
|
40
|
+
```
|
41
|
+
|
42
|
+
## Development
|
43
|
+
|
44
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
45
|
+
|
46
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
47
|
+
|
48
|
+
## Contributing
|
49
|
+
|
50
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/i2bskn/gemini_client.
|
51
|
+
|
52
|
+
## License
|
53
|
+
|
54
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "net/http"
|
4
|
+
require "json"
|
5
|
+
|
6
|
+
require_relative "gemini_client/version"
|
7
|
+
|
8
|
+
class GeminiClient
|
9
|
+
class Error < StandardError; end
|
10
|
+
|
11
|
+
module Model
|
12
|
+
GEMINI_1_5_FLASH = "gemini-1.5-flash"
|
13
|
+
end
|
14
|
+
|
15
|
+
module Method
|
16
|
+
GENERATE_CONTENT = "generateContent"
|
17
|
+
end
|
18
|
+
|
19
|
+
API_VERSION = "v1beta".freeze
|
20
|
+
|
21
|
+
def initialize(api_key:, version: API_VERSION, model: Model::GEMINI_1_5_FLASH)
|
22
|
+
@api_key = api_key
|
23
|
+
@version = version
|
24
|
+
@model = model
|
25
|
+
end
|
26
|
+
|
27
|
+
def generate_content(payload:, model: nil)
|
28
|
+
url = api_url(api_key: @api_key, version: @version, model: model || @model, method_name: Method::GENERATE_CONTENT)
|
29
|
+
request(:post, url, payload: JSON.dump(payload), headers: { "Content-Type" => "application/json" })
|
30
|
+
end
|
31
|
+
|
32
|
+
def stream_generate_content
|
33
|
+
# TODO
|
34
|
+
end
|
35
|
+
|
36
|
+
def count_tokens
|
37
|
+
# TODO
|
38
|
+
end
|
39
|
+
|
40
|
+
def embed_content
|
41
|
+
# TODO
|
42
|
+
end
|
43
|
+
|
44
|
+
def models
|
45
|
+
# TODO
|
46
|
+
end
|
47
|
+
|
48
|
+
def model_info(model)
|
49
|
+
# TODO
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def api_url(api_key:, version:, model: nil, method_name: nil)
|
55
|
+
model_and_method = [model, method_name].compact.join(":")
|
56
|
+
"https://generativelanguage.googleapis.com/#{version}/models/#{model_and_method}?key=#{api_key}"
|
57
|
+
end
|
58
|
+
|
59
|
+
def request(meth, url, options = {})
|
60
|
+
url = URI.parse(url)
|
61
|
+
# query strings
|
62
|
+
url.query = URI.encode_www_form(options.fetch(:query)) if options.key?(:query)
|
63
|
+
req = Net::HTTP.const_get(meth.to_s.capitalize).new(url.request_uri)
|
64
|
+
# request http headers
|
65
|
+
(options[:headers] || {}).each { |k, v| req[k.to_s] = v }
|
66
|
+
# Authorization
|
67
|
+
req["Authorization"] = options.fetch(:auth) if options.key?(:auth)
|
68
|
+
# Basic Authentication
|
69
|
+
req.basic_auth(options.fetch(:user), options.fetch(:password)) if options.key?(:user) && options.key?(:password)
|
70
|
+
case
|
71
|
+
when options.key?(:form)
|
72
|
+
# form data
|
73
|
+
req.set_form_data(options.fetch(:form))
|
74
|
+
when options.key?(:payload)
|
75
|
+
# payload
|
76
|
+
req.body = options.fetch(:payload)
|
77
|
+
end
|
78
|
+
|
79
|
+
http = Net::HTTP.new(url.host, url.port)
|
80
|
+
http.use_ssl = url.is_a?(URI::HTTPS)
|
81
|
+
http.request(req)
|
82
|
+
end
|
83
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gemini_client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- i2bskn
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-01-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: pry
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.15.0
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.15.0
|
27
|
+
description: Gemini client is a client library for Gemini API.
|
28
|
+
email:
|
29
|
+
- i2bskn@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- LICENSE.txt
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- lib/gemini_client.rb
|
38
|
+
- lib/gemini_client/version.rb
|
39
|
+
- sig/gemini_client.rbs
|
40
|
+
homepage: https://github.com/i2bskn/gemini_client
|
41
|
+
licenses:
|
42
|
+
- MIT
|
43
|
+
metadata:
|
44
|
+
homepage_uri: https://github.com/i2bskn/gemini_client
|
45
|
+
source_code_uri: https://github.com/i2bskn/gemini_client
|
46
|
+
changelog_uri: https://github.com/i2bskn/gemini_client/blob/master/CHANGELOG.md
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 3.0.0
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
requirements: []
|
62
|
+
rubygems_version: 3.5.9
|
63
|
+
signing_key:
|
64
|
+
specification_version: 4
|
65
|
+
summary: Gemini client is a client library for Gemini API.
|
66
|
+
test_files: []
|