google_palm_api 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: 07dac69e13dd3173be4f180d964976dd23fa6fc77b36af0742507d853cf17638
4
+ data.tar.gz: a495c0b2f59377c57c3d90f4d8bbedbc4835a75b194e745c7d99d0322e0fc8f4
5
+ SHA512:
6
+ metadata.gz: 7732a91b0c60d747cb48c1f7c5f9d939d07708a518745ca2c0112b91b52343ef3c1e0b7359a39bcb33b4b5fb68e12b08858c5e5c607f8a9e8e26105111781da1
7
+ data.tar.gz: 428afa1b2b46347acec9826fb8ca9f92eaa68418ba69761170b5e82809f120a4b86b09b47210e41fb405473a94c36bc0e9eba41cf5db4a8cdda9006ac9432608
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-05-23
4
+
5
+ - Initial release
6
+ - Added `GooglePalmApi::Client` class that supports `embed()`, `generate_text()`, `generate_chat_message()`, `get_model()`, `list_models()` and `count_message_tokens()` methods
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in google_palm_api.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "standard", "~> 1.28.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,113 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ google_palm_api (0.1.0)
5
+ faraday (>= 1.0.0)
6
+ faraday_middleware (>= 1.0.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.2)
12
+ byebug (11.1.3)
13
+ coderay (1.1.3)
14
+ diff-lcs (1.5.0)
15
+ faraday (1.10.3)
16
+ faraday-em_http (~> 1.0)
17
+ faraday-em_synchrony (~> 1.0)
18
+ faraday-excon (~> 1.1)
19
+ faraday-httpclient (~> 1.0)
20
+ faraday-multipart (~> 1.0)
21
+ faraday-net_http (~> 1.0)
22
+ faraday-net_http_persistent (~> 1.0)
23
+ faraday-patron (~> 1.0)
24
+ faraday-rack (~> 1.0)
25
+ faraday-retry (~> 1.0)
26
+ ruby2_keywords (>= 0.0.4)
27
+ faraday-em_http (1.0.0)
28
+ faraday-em_synchrony (1.0.0)
29
+ faraday-excon (1.1.0)
30
+ faraday-httpclient (1.0.1)
31
+ faraday-multipart (1.0.4)
32
+ multipart-post (~> 2)
33
+ faraday-net_http (1.0.1)
34
+ faraday-net_http_persistent (1.2.0)
35
+ faraday-patron (1.0.0)
36
+ faraday-rack (1.0.0)
37
+ faraday-retry (1.0.3)
38
+ faraday_middleware (1.2.0)
39
+ faraday (~> 1.0)
40
+ json (2.6.3)
41
+ language_server-protocol (3.17.0.3)
42
+ lint_roller (1.0.0)
43
+ method_source (1.0.0)
44
+ multipart-post (2.3.0)
45
+ parallel (1.23.0)
46
+ parser (3.2.2.1)
47
+ ast (~> 2.4.1)
48
+ pry (0.14.2)
49
+ coderay (~> 1.1)
50
+ method_source (~> 1.0)
51
+ pry-byebug (3.10.1)
52
+ byebug (~> 11.0)
53
+ pry (>= 0.13, < 0.15)
54
+ rainbow (3.1.1)
55
+ rake (13.0.6)
56
+ regexp_parser (2.8.0)
57
+ rexml (3.2.5)
58
+ rspec (3.12.0)
59
+ rspec-core (~> 3.12.0)
60
+ rspec-expectations (~> 3.12.0)
61
+ rspec-mocks (~> 3.12.0)
62
+ rspec-core (3.12.2)
63
+ rspec-support (~> 3.12.0)
64
+ rspec-expectations (3.12.3)
65
+ diff-lcs (>= 1.2.0, < 2.0)
66
+ rspec-support (~> 3.12.0)
67
+ rspec-mocks (3.12.5)
68
+ diff-lcs (>= 1.2.0, < 2.0)
69
+ rspec-support (~> 3.12.0)
70
+ rspec-support (3.12.0)
71
+ rubocop (1.50.2)
72
+ json (~> 2.3)
73
+ parallel (~> 1.10)
74
+ parser (>= 3.2.0.0)
75
+ rainbow (>= 2.2.2, < 4.0)
76
+ regexp_parser (>= 1.8, < 3.0)
77
+ rexml (>= 3.2.5, < 4.0)
78
+ rubocop-ast (>= 1.28.0, < 2.0)
79
+ ruby-progressbar (~> 1.7)
80
+ unicode-display_width (>= 2.4.0, < 3.0)
81
+ rubocop-ast (1.28.1)
82
+ parser (>= 3.2.1.0)
83
+ rubocop-performance (1.16.0)
84
+ rubocop (>= 1.7.0, < 2.0)
85
+ rubocop-ast (>= 0.4.0)
86
+ ruby-progressbar (1.13.0)
87
+ ruby2_keywords (0.0.5)
88
+ standard (1.28.2)
89
+ language_server-protocol (~> 3.17.0.2)
90
+ lint_roller (~> 1.0)
91
+ rubocop (~> 1.50.2)
92
+ standard-custom (~> 1.0.0)
93
+ standard-performance (~> 1.0.1)
94
+ standard-custom (1.0.0)
95
+ lint_roller (~> 1.0)
96
+ standard-performance (1.0.1)
97
+ lint_roller (~> 1.0)
98
+ rubocop-performance (~> 1.16.0)
99
+ unicode-display_width (2.4.2)
100
+
101
+ PLATFORMS
102
+ x86_64-darwin-19
103
+ x86_64-linux
104
+
105
+ DEPENDENCIES
106
+ google_palm_api!
107
+ pry-byebug (~> 3.9)
108
+ rake (~> 13.0)
109
+ rspec (~> 3.0)
110
+ standard (~> 1.28.0)
111
+
112
+ BUNDLED WITH
113
+ 2.4.0
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Andrei Bondarev
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,52 @@
1
+ # GooglePalmApi
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ 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/google_palm_api`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add google_palm_api
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install google_palm_api
16
+
17
+ ## Usage
18
+
19
+ ```ruby
20
+ require 'google_palm_api'
21
+ ```
22
+ ```ruby
23
+ client.generate_text(prompt:)
24
+ ```
25
+ ```ruby
26
+ client.generate_chat_message(prompt:)
27
+ ```
28
+ ```ruby
29
+ client.embed(text:)
30
+ ```
31
+ ```ruby
32
+ client.get_model(model:)
33
+ ```
34
+ ```ruby
35
+ client.list_models()
36
+ ```
37
+ ```ruby
38
+ client.count_message_tokens(message:)
39
+ ```
40
+ ## Development
41
+
42
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
43
+
44
+ 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).
45
+
46
+ ## Contributing
47
+
48
+ Bug reports and pull requests are welcome on GitHub at https://github.com/andreibondarev/google_palm_api.
49
+
50
+ ## License
51
+
52
+ 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,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,200 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "faraday"
4
+ require "faraday_middleware"
5
+
6
+ module GooglePalmApi
7
+ class Client
8
+ attr_reader :api_key, :connection
9
+
10
+ ENDPOINT_URL = "https://generativelanguage.googleapis.com/"
11
+
12
+ DEFAULTS = {
13
+ temperature: 0.0,
14
+ completion_model_name: "text-bison-001",
15
+ chat_completion_model_name: "chat-bison-001",
16
+ embeddings_model_name: "embedding-gecko-001"
17
+ }
18
+
19
+ def initialize(api_key:)
20
+ @api_key = api_key
21
+ end
22
+
23
+ #
24
+ # The text service is designed for single turn interactions.
25
+ # It's ideal for tasks that can be completed within one response from the API, without the need for a continuous conversation.
26
+ # The text service allows you to obtain text completions, generate summaries, or perform other NLP tasks that don't require back-and-forth interactions.
27
+ # Method signature: https://developers.generativeai.google/api/python/google/generativeai/generate_text
28
+ #
29
+ # @param [String] prompt
30
+ # @param [String] model
31
+ # @param [Float] temperature
32
+ # @param [Integer] candidate_count
33
+ # @param [Integer] max_output_tokens
34
+ # @param [Float] top_p
35
+ # @param [Integer] top_k
36
+ # @param [String] safety_settings
37
+ # @param [Array] stop_sequences
38
+ # @param [String] client
39
+ # @return [Hash]
40
+ #
41
+ def generate_text(
42
+ prompt:,
43
+ temperature: nil,
44
+ candidate_count: nil,
45
+ max_output_tokens: nil,
46
+ top_p: nil,
47
+ top_k: nil,
48
+ safety_settings: nil,
49
+ stop_sequences: nil,
50
+ client: nil
51
+ )
52
+ response = connection.post("/v1beta2/models/#{DEFAULTS[:completion_model_name]}:generateText") do |req|
53
+ req.params = {key: api_key}
54
+
55
+ req.body = {prompt: {text: prompt}}
56
+ req.body[:temperature] = temperature || DEFAULTS[:temperature]
57
+ req.body[:candidate_count] = candidate_count if candidate_count
58
+ req.body[:max_output_tokens] = max_output_tokens if max_output_tokens
59
+ req.body[:top_p] = top_p if top_p
60
+ req.body[:top_k] = top_k if top_k
61
+ req.body[:safety_settings] = safety_settings if safety_settings
62
+ req.body[:stop_sequences] = stop_sequences if stop_sequences
63
+ req.body[:client] = client if client
64
+ end
65
+ response.body
66
+ end
67
+
68
+ #
69
+ # The chat service is designed for interactive, multi-turn conversations.
70
+ # The service enables you to create applications that engage users in dynamic and context-aware conversations.
71
+ # You can provide a context for the conversation as well as examples of conversation turns for the model to follow.
72
+ # It's ideal for applications that require ongoing communication, such as chatbots, interactive tutors, or customer support assistants.
73
+ # Method signature: https://developers.generativeai.google/api/python/google/generativeai/chat
74
+ #
75
+ # @param [String] prompt
76
+ # @param [String] model
77
+ # @param [String] context
78
+ # @param [Array] examples
79
+ # @param [Array] messages
80
+ # @param [Float] temperature
81
+ # @param [Integer] candidate_count
82
+ # @param [Float] top_p
83
+ # @param [Integer] top_k
84
+ # @param [String] client
85
+ # @return [Hash]
86
+ #
87
+ def generate_chat_message(
88
+ prompt:,
89
+ context: nil,
90
+ examples: nil,
91
+ messages: nil,
92
+ temperature: nil,
93
+ candidate_count: nil,
94
+ top_p: nil,
95
+ top_k: nil,
96
+ client: nil
97
+ )
98
+ # Overwrite the default ENDPOINT_URL for this method.
99
+ response = connection.post("/v1beta2/models/#{DEFAULTS[:chat_completion_model_name]}:generateMessage") do |req|
100
+ req.params = {key: api_key}
101
+
102
+ req.body = {prompt: {messages: [{content: prompt}]}}
103
+ req.body[:context] = context if context
104
+ req.body[:examples] = examples if examples
105
+ req.body[:messages] = messages if messages
106
+ req.body[:temperature] = temperature || DEFAULTS[:temperature]
107
+ req.body[:candidate_count] = candidate_count if candidate_count
108
+ req.body[:top_p] = top_p if top_p
109
+ req.body[:top_k] = top_k if top_k
110
+ req.body[:client] = client if client
111
+ end
112
+ response.body
113
+ end
114
+
115
+ #
116
+ # The embedding service in the PaLM API generates state-of-the-art embeddings for words, phrases, and sentences.
117
+ # The resulting embeddings can then be used for NLP tasks, such as semantic search, text classification and clustering among many others.
118
+ # This section describes what embeddings are and highlights some key use cases for the embedding service to help you get started.
119
+ # When you're ready to start developing, you can find complete runnable code in the embeddings quickstart.
120
+ # Method signature: https://developers.generativeai.google/api/python/google/generativeai/generate_embeddings
121
+ #
122
+ # @param [String] text
123
+ # @param [String] model
124
+ # @param [String] client
125
+ # @return [Hash]
126
+ #
127
+ def embed(
128
+ text:,
129
+ model: nil,
130
+ client: nil
131
+ )
132
+ response = connection.post("/v1beta2/models/#{model || DEFAULTS[:embeddings_model_name]}:embedText") do |req|
133
+ req.params = {key: api_key}
134
+
135
+ req.body = {text: text}
136
+ req.body[:model] = model if model
137
+ req.body[:client] = client if client
138
+ end
139
+ response.body
140
+ end
141
+
142
+ #
143
+ # Lists models available through the API.
144
+ #
145
+ # @param [Integer] page_size
146
+ # @param [String] page_token
147
+ # @return [Hash]
148
+ #
149
+ def list_models(page_size: nil, page_token: nil)
150
+ response = connection.get("/v1beta2/models") do |req|
151
+ req.params = {key: api_key}
152
+
153
+ req.params[:pageSize] = page_size if page_size
154
+ req.params[:pageToken] = page_token if page_token
155
+ end
156
+ response.body
157
+ end
158
+
159
+ #
160
+ # Runs a model's tokenizer on a string and returns the token count.
161
+ #
162
+ # @param [String] model
163
+ # @param [String] prompt
164
+ # @return [Hash]
165
+ #
166
+ def count_message_tokens(model:, prompt:)
167
+ response = connection.post("/v1beta2/models/#{model}:countMessageTokens") do |req|
168
+ req.params = {key: api_key}
169
+
170
+ req.body = {prompt: {messages: [{content: prompt}]}}
171
+ end
172
+ response.body
173
+ end
174
+
175
+ #
176
+ # Gets information about a specific Model.
177
+ #
178
+ # @param [String] name
179
+ # @return [Hash]
180
+ #
181
+ def get_model(model:)
182
+ response = connection.get("/v1beta2/models/#{model}") do |req|
183
+ req.params = {key: api_key}
184
+ end
185
+ response.body
186
+ end
187
+
188
+ private
189
+
190
+ # standard:disable Lint/DuplicateMethods
191
+ def connection
192
+ Faraday.new(url: ENDPOINT_URL) do |faraday|
193
+ faraday.request :json
194
+ faraday.response :json, content_type: /\bjson$/
195
+ faraday.adapter Faraday.default_adapter
196
+ end
197
+ end
198
+ # standard:enable Lint/DuplicateMethods
199
+ end
200
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GooglePalmApi
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "google_palm_api/version"
4
+
5
+ module GooglePalmApi
6
+ class Error < StandardError; end
7
+
8
+ autoload :Client, "google_palm_api/client"
9
+ end
@@ -0,0 +1,4 @@
1
+ module GooglePalmApi
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google_palm_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrei Bondarev
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-05-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry-byebug
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday_middleware
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 1.0.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.0
55
+ description: Ruby client for Google PaLM API
56
+ email:
57
+ - andrei.bondarev13@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".rspec"
63
+ - CHANGELOG.md
64
+ - Gemfile
65
+ - Gemfile.lock
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/google_palm_api.rb
70
+ - lib/google_palm_api/client.rb
71
+ - lib/google_palm_api/version.rb
72
+ - sig/google_palm_api.rbs
73
+ homepage: https://github.com/andreibondarev/google_palm_api
74
+ licenses:
75
+ - MIT
76
+ metadata:
77
+ homepage_uri: https://github.com/andreibondarev/google_palm_api
78
+ source_code_uri: https://github.com/andreibondarev/google_palm_api
79
+ changelog_uri: https://github.com/andreibondarev/google_palm_api/CHANGELOG.md
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: 2.6.0
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubygems_version: 3.2.3
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Ruby client for Google PaLM API
99
+ test_files: []