ruby-gemini 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8d1bbdc9362a037041cb330506159090fea6bf598b4f60e890a0c90cccf04147
4
+ data.tar.gz: 06c0cf0384ad2abfea9cc6854f1caa68a3886425e53abd950d275a6b6a4220d2
5
+ SHA512:
6
+ metadata.gz: 9cb44a155719e4cf71f9cbfd7bf00360c43edd43caaf0368f044ba04e6524cd6fd014ec35f51ee7ff306eb1eb78cdccff4df3aaad3ad1a161e819aec11101c6d
7
+ data.tar.gz: 9d2bcf878b14f27997148726d7ed654556dc989f3f5f2748be5aaeddafd123ab2d26135901f0829aa779664b2317ee96518d26317576878664a4b5e2985ecc9f
@@ -0,0 +1,45 @@
1
+ version: 2.1 # Use 2.1 to enable using orbs and other features.
2
+
3
+ # Declare the orbs that we'll use in our config.
4
+ orbs:
5
+ ruby: circleci/ruby@1.0
6
+
7
+ jobs:
8
+ rubocop:
9
+ parallelism: 1
10
+ docker:
11
+ - image: cimg/ruby:3.1-node
12
+ steps:
13
+ - checkout
14
+ - ruby/install-deps
15
+ - run:
16
+ name: Run Rubocop
17
+ command: bundle exec rubocop
18
+ test:
19
+ parameters:
20
+ ruby-image:
21
+ type: string
22
+ parallelism: 1
23
+ docker:
24
+ - image: << parameters.ruby-image >>
25
+ steps:
26
+ - checkout
27
+ - ruby/install-deps
28
+ - run:
29
+ name: Run tests
30
+ command: bundle exec rspec -fd
31
+
32
+ workflows:
33
+ version: 2
34
+ checks:
35
+ jobs:
36
+ - rubocop
37
+ - test:
38
+ matrix:
39
+ parameters:
40
+ ruby-image:
41
+ - cimg/ruby:2.6-node
42
+ - cimg/ruby:2.7-node
43
+ - cimg/ruby:3.0-node
44
+ - cimg/ruby:3.1-node
45
+ - cimg/ruby:3.2-node
@@ -0,0 +1,38 @@
1
+ ---
2
+ name: Bug report
3
+ about: Create a report to help us improve
4
+ title: ''
5
+ labels: ''
6
+ assignees: ''
7
+
8
+ ---
9
+
10
+ **Describe the bug**
11
+ A clear and concise description of what the bug is.
12
+
13
+ **To Reproduce**
14
+ Steps to reproduce the behavior:
15
+ 1. Go to '...'
16
+ 2. Click on '....'
17
+ 3. Scroll down to '....'
18
+ 4. See error
19
+
20
+ **Expected behavior**
21
+ A clear and concise description of what you expected to happen.
22
+
23
+ **Screenshots**
24
+ If applicable, add screenshots to help explain your problem.
25
+
26
+ **Desktop (please complete the following information):**
27
+ - OS: [e.g. iOS]
28
+ - Browser [e.g. chrome, safari]
29
+ - Version [e.g. 22]
30
+
31
+ **Smartphone (please complete the following information):**
32
+ - Device: [e.g. iPhone6]
33
+ - OS: [e.g. iOS8.1]
34
+ - Browser [e.g. stock browser, safari]
35
+ - Version [e.g. 22]
36
+
37
+ **Additional context**
38
+ Add any other context about the problem here.
@@ -0,0 +1,20 @@
1
+ ---
2
+ name: Feature request
3
+ about: Suggest an idea for this project
4
+ title: ''
5
+ labels: ''
6
+ assignees: ''
7
+
8
+ ---
9
+
10
+ **Is your feature request related to a problem? Please describe.**
11
+ A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12
+
13
+ **Describe the solution you'd like**
14
+ A clear and concise description of what you want to happen.
15
+
16
+ **Describe alternatives you've considered**
17
+ A clear and concise description of any alternative solutions or features you've considered.
18
+
19
+ **Additional context**
20
+ Add any other context or screenshots about the feature request here.
@@ -0,0 +1,15 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: bundler
4
+ directory: "/"
5
+ schedule:
6
+ interval: daily
7
+ open-pull-requests-limit: 10
8
+ ignore:
9
+ - dependency-name: webmock
10
+ versions:
11
+ - 3.11.1
12
+ - 3.11.3
13
+ - dependency-name: rspec
14
+ versions:
15
+ - 3.10.0
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ .byebug_history
14
+ .env
15
+
16
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,26 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+ NewCops: enable
4
+ SuggestExtensions: false
5
+
6
+ Style/Documentation:
7
+ # Skips checking to make sure top level modules / classes have a comment.
8
+ Enabled: false
9
+
10
+ Layout/LineLength:
11
+ Max: 100
12
+ Exclude:
13
+ - "**/*.gemspec"
14
+
15
+ Metrics/AbcSize:
16
+ Max: 20
17
+
18
+ Metrics/BlockLength:
19
+ Exclude:
20
+ - "spec/**/*"
21
+
22
+ Style/StringLiterals:
23
+ EnforcedStyle: double_quotes
24
+
25
+ Style/FrozenStringLiteralComment:
26
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,18 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [0.0.0] - 2023-07-12
9
+
10
+ ### Added
11
+
12
+ - Initialise repository.
13
+
14
+ ## [0.1.0] - 2023-07-18
15
+
16
+ ### Changed
17
+
18
+ - Got the gem working with the API. MVP
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at thechrisoshow@users.noreply.github.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,3 @@
1
+ ## Contributing
2
+
3
+ Bug reports and pull requests are welcome on GitHub at https://github.com/thechrisoshow/gemini. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/thechrisoshow/gemini/blob/main/CODE_OF_CONDUCT.md).
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Include gem dependencies from gemini.gemspec
4
+ gemspec
5
+
6
+ gem "byebug", "~> 11.1.3"
7
+ gem "dotenv", "~> 2.8.1"
8
+ gem "rake", "~> 13.0"
9
+ gem "rspec", "~> 3.12"
10
+ gem "rubocop", "~> 1.50.2"
11
+ gem "vcr", "~> 6.1.0"
12
+ gem "webmock", "~> 3.18.1"
data/Gemfile.lock ADDED
@@ -0,0 +1,84 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ gemini (0.1.0)
5
+ faraday (>= 1)
6
+ faraday-multipart (>= 1)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ addressable (2.8.1)
12
+ public_suffix (>= 2.0.2, < 6.0)
13
+ ast (2.4.2)
14
+ byebug (11.1.3)
15
+ crack (0.4.5)
16
+ rexml
17
+ diff-lcs (1.5.0)
18
+ dotenv (2.8.1)
19
+ faraday (2.7.10)
20
+ faraday-net_http (>= 2.0, < 3.1)
21
+ ruby2_keywords (>= 0.0.4)
22
+ faraday-multipart (1.0.4)
23
+ multipart-post (~> 2)
24
+ faraday-net_http (3.0.2)
25
+ hashdiff (1.0.1)
26
+ json (2.6.3)
27
+ multipart-post (2.3.0)
28
+ parallel (1.22.1)
29
+ parser (3.2.2.0)
30
+ ast (~> 2.4.1)
31
+ public_suffix (5.0.1)
32
+ rainbow (3.1.1)
33
+ rake (13.0.6)
34
+ regexp_parser (2.8.0)
35
+ rexml (3.2.5)
36
+ rspec (3.12.0)
37
+ rspec-core (~> 3.12.0)
38
+ rspec-expectations (~> 3.12.0)
39
+ rspec-mocks (~> 3.12.0)
40
+ rspec-core (3.12.0)
41
+ rspec-support (~> 3.12.0)
42
+ rspec-expectations (3.12.2)
43
+ diff-lcs (>= 1.2.0, < 2.0)
44
+ rspec-support (~> 3.12.0)
45
+ rspec-mocks (3.12.3)
46
+ diff-lcs (>= 1.2.0, < 2.0)
47
+ rspec-support (~> 3.12.0)
48
+ rspec-support (3.12.0)
49
+ rubocop (1.50.2)
50
+ json (~> 2.3)
51
+ parallel (~> 1.10)
52
+ parser (>= 3.2.0.0)
53
+ rainbow (>= 2.2.2, < 4.0)
54
+ regexp_parser (>= 1.8, < 3.0)
55
+ rexml (>= 3.2.5, < 4.0)
56
+ rubocop-ast (>= 1.28.0, < 2.0)
57
+ ruby-progressbar (~> 1.7)
58
+ unicode-display_width (>= 2.4.0, < 3.0)
59
+ rubocop-ast (1.28.0)
60
+ parser (>= 3.2.1.0)
61
+ ruby-progressbar (1.13.0)
62
+ ruby2_keywords (0.0.5)
63
+ unicode-display_width (2.4.2)
64
+ vcr (6.1.0)
65
+ webmock (3.18.1)
66
+ addressable (>= 2.8.0)
67
+ crack (>= 0.3.2)
68
+ hashdiff (>= 0.4.0, < 2.0.0)
69
+
70
+ PLATFORMS
71
+ ruby
72
+
73
+ DEPENDENCIES
74
+ byebug (~> 11.1.3)
75
+ dotenv (~> 2.8.1)
76
+ gemini!
77
+ rake (~> 13.0)
78
+ rspec (~> 3.12)
79
+ rubocop (~> 1.50.2)
80
+ vcr (~> 6.1.0)
81
+ webmock (~> 3.18.1)
82
+
83
+ BUNDLED WITH
84
+ 2.4.5
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Alex
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,115 @@
1
+ # Gemini
2
+
3
+ Use the [Gemini API](https://ai.google.dev/docs) with Ruby!
4
+
5
+ You can apply for access to the API [here](https://ai.google.dev/tutorials/setup).
6
+
7
+
8
+ ### Massive thanks
9
+ This gem is built off the hard work of [Alex Rudall](https://github.com/alexrudall) and his work on the [ruby-openai](https://github.com/alexrudall/ruby-openai) and [anthropic](https://github.com/alexrudall/anthropic) gems. Thanks Alex!
10
+
11
+ ### Bundler
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem "ruby-gemini"
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle install
22
+
23
+ ### Gem install
24
+
25
+ Or install with:
26
+
27
+ $ gem install gemini
28
+
29
+ and require with:
30
+
31
+ ```ruby
32
+ require "gemini"
33
+ ```
34
+
35
+ ## Usage
36
+
37
+ - Get your API key from [https://makersuite.google.com/](https://ai.google.dev/tutorials/setup)
38
+
39
+ ### Quickstart
40
+
41
+ For a quick test you can pass your token directly to a new client:
42
+
43
+ ```ruby
44
+ client = Gemini::Client.new(access_token: "access_token_goes_here")
45
+ ```
46
+
47
+ ### With Config
48
+
49
+ For a more robust setup, you can configure the gem with your API keys, for example in an `gemini.rb` initializer file. Never hardcode secrets into your codebase - instead use something like [dotenv](https://github.com/motdotla/dotenv) to pass the keys safely into your environments.
50
+
51
+ ```ruby
52
+ Gemini.configure do |config|
53
+ config.access_token = ENV.fetch("GEMINI_API_KEY")
54
+ end
55
+ ```
56
+
57
+ Then you can create a client like this:
58
+
59
+ ```ruby
60
+ client = Gemini::Client.new
61
+ ```
62
+
63
+ ### Text content
64
+
65
+ Hit the Gemini API for text content:
66
+
67
+ ```ruby
68
+ response = client.generate_content(
69
+ parameters: {
70
+ model: "gemini-pro",
71
+ prompt: "How high is the sky?"
72
+ })
73
+ puts response
74
+ ```
75
+ {
76
+ "parts"=> [
77
+ {
78
+ "text"=>"The sky does not have a definite height..."
79
+ }
80
+ ]
81
+ }
82
+ ```
83
+
84
+
85
+ ## Development
86
+
87
+ After checking out the repo, run `bin/setup` to install dependencies. You can run `bin/console` for an interactive prompt that will allow you to experiment.
88
+
89
+ To install this gem onto your local machine, run `bundle exec rake install`.
90
+
91
+ ### Warning
92
+
93
+ If you have an `GEMINI_API_KEY` in your `ENV`, running the specs will use this to run the specs against the actual API, which will be slow and cost you money - 2 cents or more! Remove it from your environment with `unset` or similar if you just want to run the specs against the stored VCR responses.
94
+
95
+ ## Release
96
+
97
+ First run the specs without VCR so they actually hit the API. This will cost 2 cents or more. Set GEMINI_API_KEY in your environment or pass it in like this:
98
+
99
+ ```
100
+ GEMINI_API_KEY=123abc bundle exec rspec
101
+ ```
102
+
103
+ Then update the version number in `version.rb`, update `CHANGELOG.md`, run `bundle install` to update Gemfile.lock, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
104
+
105
+ ## Contributing
106
+
107
+ Bug reports and pull requests are welcome on GitHub at <https://github.com/thechrisoshow/gemini>. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/thechrisoshow/gemini/blob/main/CODE_OF_CONDUCT.md).
108
+
109
+ ## License
110
+
111
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
112
+
113
+ ## Code of Conduct
114
+
115
+ Everyone interacting in the Ruby Gemini project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/thechrisoshow/gemini/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "gemini"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,34 @@
1
+ module Gemini
2
+ class Client
3
+ extend Gemini::HTTP
4
+
5
+ def initialize(access_token: nil, organization_id: nil, uri_base: nil, request_timeout: nil,
6
+ extra_headers: {})
7
+ Gemini.configuration.access_token = access_token if access_token
8
+ Gemini.configuration.organization_id = organization_id if organization_id
9
+ Gemini.configuration.uri_base = uri_base if uri_base
10
+ Gemini.configuration.request_timeout = request_timeout if request_timeout
11
+ Gemini.configuration.extra_headers = extra_headers
12
+ end
13
+
14
+ def generate_content(parameters: {})
15
+ Gemini::Client.json_post(path: "generateContent", parameters: adjusted_parameters(parameters))
16
+ end
17
+
18
+ private
19
+
20
+ def adjusted_parameters(params)
21
+ return params unless params[:prompt]
22
+ params[:contents] = [
23
+ {
24
+ parts: [
25
+ {
26
+ text: params.delete(:prompt)
27
+ }
28
+ ]
29
+ }
30
+ ]
31
+ params
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,9 @@
1
+ module Ruby
2
+ module Gemini
3
+ VERSION = ::Gemini::VERSION
4
+
5
+ Error = ::Gemini::Error
6
+ ConfigurationError = ::Gemini::ConfigurationError
7
+ Configuration = ::Gemini::Configuration
8
+ end
9
+ end
@@ -0,0 +1,104 @@
1
+ module Gemini
2
+ module HTTP
3
+ def get(path:)
4
+ to_json(conn.get(uri(path: path)) do |req|
5
+ req.headers = headers
6
+ end&.body)
7
+ end
8
+
9
+ def json_post(path:, parameters:)
10
+ to_json(conn.post(uri(path: adjusted_path(path: path, parameters: parameters))) do |req|
11
+ if parameters[:stream].is_a?(Proc)
12
+ req.options.on_data = to_json_stream(user_proc: parameters[:stream])
13
+ end
14
+
15
+ req.headers = headers
16
+ req.body = parameters.to_json
17
+ end&.body)
18
+ end
19
+
20
+ def multipart_post(path:, parameters: nil)
21
+ adjusted_path = adjusted_path(path: path, parameters: parameters)
22
+ to_json(conn(multipart: true).post(uri(path: adjusted_path)) do |req|
23
+ req.headers = headers.merge({ "Content-Type" => "multipart/form-data" })
24
+ req.body = multipart_parameters(parameters)
25
+ end&.body)
26
+ end
27
+
28
+ def delete(path:)
29
+ to_json(conn.delete(uri(path: path)) do |req|
30
+ req.headers = headers
31
+ end&.body)
32
+ end
33
+
34
+ private
35
+
36
+ def adjusted_path(path:, parameters:)
37
+ prefix = "#{Gemini.configuration.api_version}beta/models/#{parameters[:model] || Gemini.configuration.default_model}:"
38
+ api_suffix = URI.encode_www_form({
39
+ key: Gemini.configuration.access_token
40
+ })
41
+
42
+ if parameters[:stream]
43
+ path = "stream" + text.sub(/\A./) { |match| match.upcase }
44
+ end
45
+
46
+ "#{prefix}#{path}?#{api_suffix}"
47
+ end
48
+
49
+ def to_json(string)
50
+ return unless string
51
+
52
+ JSON.parse(string)
53
+ rescue JSON::ParserError
54
+ # Convert a multiline string of JSON objects to a JSON array.
55
+ JSON.parse(string.gsub("}\n{", "},{").prepend("[").concat("]"))
56
+ end
57
+
58
+ # Given a proc, returns an outer proc that can be used to iterate over a JSON stream of chunks.
59
+ # For each chunk, the inner user_proc is called giving it the JSON object. The JSON object could
60
+ # be a data object or an error object as described in the Gemini API documentation.
61
+ #
62
+ # If the JSON object for a given data or error message is invalid, it is ignored.
63
+ #
64
+ # @param user_proc [Proc] The inner proc to call for each JSON object in the chunk.
65
+ # @return [Proc] An outer proc that iterates over a raw stream, converting it to JSON.
66
+ def to_json_stream(user_proc:)
67
+ proc do |chunk, _|
68
+ chunk.scan(/(?:data|error): (\{.*\})/i).flatten.each do |data|
69
+ user_proc.call(JSON.parse(data))
70
+ rescue JSON::ParserError
71
+ # Ignore invalid JSON.
72
+ end
73
+ end
74
+ end
75
+
76
+ def conn(multipart: false)
77
+ Faraday.new do |f|
78
+ f.options[:timeout] = Gemini.configuration.request_timeout
79
+ f.request(:multipart) if multipart
80
+ end
81
+ end
82
+
83
+ def uri(path:)
84
+ Gemini.configuration.uri_base + path
85
+ end
86
+
87
+ def headers
88
+ {
89
+ "Content-Type" => "application/json",
90
+ }.merge(Gemini.configuration.extra_headers)
91
+ end
92
+
93
+ def multipart_parameters(parameters)
94
+ parameters&.transform_values do |value|
95
+ next value unless value.is_a?(File)
96
+
97
+ # Doesn't seem like Gemini needs mime_type yet, so not worth
98
+ # the library to figure this out. Hence the empty string
99
+ # as the second argument.
100
+ Faraday::UploadIO.new(value, "", value.path)
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,3 @@
1
+ module Gemini
2
+ VERSION = "0.1.0".freeze
3
+ end
data/lib/gemini.rb ADDED
@@ -0,0 +1,50 @@
1
+ require "faraday"
2
+ require "faraday/multipart"
3
+
4
+ require_relative "gemini/http"
5
+ require_relative "gemini/client"
6
+ require_relative "gemini/version"
7
+
8
+ module Gemini
9
+ class Error < StandardError; end
10
+ class ConfigurationError < Error; end
11
+
12
+ class Configuration
13
+ attr_writer :access_token
14
+ attr_accessor :api_version, :extra_headers, :organization_id,
15
+ :request_timeout, :uri_base, :default_model
16
+
17
+ DEFAULT_API_VERSION = "v1".freeze
18
+ DEFAULT_URI_BASE = "https://generativelanguage.googleapis.com/".freeze
19
+ DEFAULT_REQUEST_TIMEOUT = 120
20
+ DEFAULT_MODEL = "gemini-pro".freeze
21
+
22
+ def initialize
23
+ @access_token = nil
24
+ @api_version = DEFAULT_API_VERSION
25
+ @organization_id = nil
26
+ @uri_base = DEFAULT_URI_BASE
27
+ @request_timeout = DEFAULT_REQUEST_TIMEOUT
28
+ @default_model = DEFAULT_MODEL
29
+ end
30
+
31
+ def access_token
32
+ return @access_token if @access_token
33
+
34
+ error_text = "Gemini access token missing! See https://github.com/thechrisoshow/gemini#usage"
35
+ raise ConfigurationError, error_text
36
+ end
37
+ end
38
+
39
+ class << self
40
+ attr_writer :configuration
41
+ end
42
+
43
+ def self.configuration
44
+ @configuration ||= Gemini::Configuration.new
45
+ end
46
+
47
+ def self.configure
48
+ yield(configuration)
49
+ end
50
+ end
@@ -0,0 +1,2 @@
1
+ require_relative "../gemini"
2
+ require_relative "../gemini/compatibility"
@@ -0,0 +1,5 @@
1
+ ## All Submissions:
2
+
3
+ * [ ] Have you followed the guidelines in our [Contributing document](../blob/main/CONTRIBUTING.md)?
4
+ * [ ] Have you checked to ensure there aren't other open [Pull Requests](../pulls) for the same update/change?
5
+ * [ ] Have you added an explanation of what your changes do and why you'd like us to include them?
@@ -0,0 +1,30 @@
1
+ require_relative "lib/gemini/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "ruby-gemini"
5
+ spec.version = Gemini::VERSION
6
+ spec.authors = ["Chris"]
7
+ spec.email = ["thechrisoshow@users.noreply.github.com"]
8
+
9
+ spec.summary = "Gemini API + Ruby! 🤖🌌"
10
+ spec.homepage = "https://github.com/thechrisoshow/ruby-gemini"
11
+ spec.license = "MIT"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
13
+
14
+ spec.metadata["homepage_uri"] = spec.homepage
15
+ spec.metadata["source_code_uri"] = "https://github.com/thechrisoshow/ruby-gemini"
16
+ spec.metadata["changelog_uri"] = "https://github.com/thechrisoshow/ruby-gemini/blob/main/CHANGELOG.md"
17
+ spec.metadata["rubygems_mfa_required"] = "true"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_dependency "faraday", ">= 1"
29
+ spec.add_dependency "faraday-multipart", ">= 1"
30
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-gemini
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Chris
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-12-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday-multipart
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '1'
41
+ description:
42
+ email:
43
+ - thechrisoshow@users.noreply.github.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".circleci/config.yml"
49
+ - ".github/ISSUE_TEMPLATE/bug_report.md"
50
+ - ".github/ISSUE_TEMPLATE/feature_request.md"
51
+ - ".github/dependabot.yml"
52
+ - ".gitignore"
53
+ - ".rspec"
54
+ - ".rubocop.yml"
55
+ - CHANGELOG.md
56
+ - CODE_OF_CONDUCT.md
57
+ - CONTRIBUTING.md
58
+ - Gemfile
59
+ - Gemfile.lock
60
+ - LICENSE.txt
61
+ - README.md
62
+ - Rakefile
63
+ - bin/console
64
+ - bin/setup
65
+ - lib/gemini.rb
66
+ - lib/gemini/client.rb
67
+ - lib/gemini/compatibility.rb
68
+ - lib/gemini/http.rb
69
+ - lib/gemini/version.rb
70
+ - lib/ruby/gemini.rb
71
+ - pull_request_template.md
72
+ - ruby-gemini.gemspec
73
+ homepage: https://github.com/thechrisoshow/ruby-gemini
74
+ licenses:
75
+ - MIT
76
+ metadata:
77
+ homepage_uri: https://github.com/thechrisoshow/ruby-gemini
78
+ source_code_uri: https://github.com/thechrisoshow/ruby-gemini
79
+ changelog_uri: https://github.com/thechrisoshow/ruby-gemini/blob/main/CHANGELOG.md
80
+ rubygems_mfa_required: 'true'
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 2.6.0
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubygems_version: 3.4.1
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: "Gemini API + Ruby! \U0001F916\U0001F30C"
100
+ test_files: []