ai21 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: 5d246c86807d3bcd6b2bafe31b34b6b426f9b4b89c28544cfb5abfce6204a9d0
4
+ data.tar.gz: 2006023a575aa77e4172bd8f2fae30384751427833a91ec813aa56102c5c434c
5
+ SHA512:
6
+ metadata.gz: cebbc69bcfab0c46090c7e7a443b2c49a9c7d7be74713d7a73e87bad8216a338b182f76f582287ab2715d18b75da1eddf222134600ceca7092022ac6fae965bc
7
+ data.tar.gz: 33a0182f3bca6897d4746f84cade3954b1d9638c94389fc6718215f5a85fd24ccf17ef9549ef48e8efaa856d7485ba1ea5f0e65464ed044b06fadb95dcea894c
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-05-27
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at alchaplinsky@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
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 ai21.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+ gem "pry"
12
+ gem "standardrb"
data/Gemfile.lock ADDED
@@ -0,0 +1,82 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ai21 (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ coderay (1.1.3)
11
+ diff-lcs (1.5.0)
12
+ json (2.6.3)
13
+ language_server-protocol (3.17.0.3)
14
+ lint_roller (1.0.0)
15
+ method_source (1.0.0)
16
+ parallel (1.23.0)
17
+ parser (3.2.2.1)
18
+ ast (~> 2.4.1)
19
+ pry (0.14.2)
20
+ coderay (~> 1.1)
21
+ method_source (~> 1.0)
22
+ rainbow (3.1.1)
23
+ rake (13.0.6)
24
+ regexp_parser (2.8.0)
25
+ rexml (3.2.5)
26
+ rspec (3.12.0)
27
+ rspec-core (~> 3.12.0)
28
+ rspec-expectations (~> 3.12.0)
29
+ rspec-mocks (~> 3.12.0)
30
+ rspec-core (3.12.2)
31
+ rspec-support (~> 3.12.0)
32
+ rspec-expectations (3.12.3)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.12.0)
35
+ rspec-mocks (3.12.5)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.12.0)
38
+ rspec-support (3.12.0)
39
+ rubocop (1.50.2)
40
+ json (~> 2.3)
41
+ parallel (~> 1.10)
42
+ parser (>= 3.2.0.0)
43
+ rainbow (>= 2.2.2, < 4.0)
44
+ regexp_parser (>= 1.8, < 3.0)
45
+ rexml (>= 3.2.5, < 4.0)
46
+ rubocop-ast (>= 1.28.0, < 2.0)
47
+ ruby-progressbar (~> 1.7)
48
+ unicode-display_width (>= 2.4.0, < 3.0)
49
+ rubocop-ast (1.28.1)
50
+ parser (>= 3.2.1.0)
51
+ rubocop-performance (1.16.0)
52
+ rubocop (>= 1.7.0, < 2.0)
53
+ rubocop-ast (>= 0.4.0)
54
+ ruby-progressbar (1.13.0)
55
+ standard (1.28.2)
56
+ language_server-protocol (~> 3.17.0.2)
57
+ lint_roller (~> 1.0)
58
+ rubocop (~> 1.50.2)
59
+ standard-custom (~> 1.0.0)
60
+ standard-performance (~> 1.0.1)
61
+ standard-custom (1.0.0)
62
+ lint_roller (~> 1.0)
63
+ standard-performance (1.0.1)
64
+ lint_roller (~> 1.0)
65
+ rubocop-performance (~> 1.16.0)
66
+ standardrb (1.0.1)
67
+ standard
68
+ unicode-display_width (2.4.2)
69
+
70
+ PLATFORMS
71
+ arm64-darwin-22
72
+ x86_64-linux
73
+
74
+ DEPENDENCIES
75
+ ai21!
76
+ pry
77
+ rake (~> 13.0)
78
+ rspec (~> 3.0)
79
+ standardrb
80
+
81
+ BUNDLED WITH
82
+ 2.4.0
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Ai21
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/ai21`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ 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.
26
+
27
+ 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).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ai21. 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/[USERNAME]/ai21/blob/main/CODE_OF_CONDUCT.md).
32
+
33
+ ## Code of Conduct
34
+
35
+ Everyone interacting in the Ai21 project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/ai21/blob/main/CODE_OF_CONDUCT.md).
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,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AI21
4
+ class Client
5
+ include AI21::Helper
6
+ extend AI21::HTTP
7
+
8
+ DEFALUT_COMPLETE_MODEL = "j2-grande"
9
+ DEFAULT_INSTRUCT_MODEL = "j2-grande-instruct"
10
+
11
+ def initialize(access_token)
12
+ AI21.configuration.access_token = access_token if access_token
13
+ end
14
+
15
+ def complete(prompt, options = {})
16
+ model = options.delete(:model) || DEFALUT_COMPLETE_MODEL
17
+
18
+ AI21::Client.post("/#{model}/complete", {prompt: prompt}.merge(snake_to_camel(options)))
19
+ end
20
+
21
+ def instruct(prompt, options = {})
22
+ model = options.delete(:model) || DEFAULT_INSTRUCT_MODEL
23
+
24
+ AI21::Client.post("/#{model}/complete", {prompt: prompt}.merge(snake_to_camel(options)))
25
+ end
26
+
27
+ def paraphrase(prompt, options = {})
28
+ AI21::Client.post("/paraphrase", {text: prompt}.merge(snake_to_camel(options)))
29
+ end
30
+
31
+ def correct(prompt)
32
+ AI21::Client.post("/gec", {text: prompt})
33
+ end
34
+
35
+ def improvements(prompt, types, options = {})
36
+ AI21::Client.post("/improvements", {text: prompt, types: types}.merge(snake_to_camel(options)))
37
+ end
38
+
39
+ def summarize(prompt, source_type, options = {})
40
+ AI21::Client.post("/summarize", {source: prompt, sourceType: source_type}.merge(snake_to_camel(options)))
41
+ end
42
+
43
+ def segmentation(prompt, source_type, options = {})
44
+ AI21::Client.post("/segmentation", {source: prompt, sourceType: source_type}.merge(snake_to_camel(options)))
45
+ end
46
+
47
+ def answer(question, context)
48
+ AI21::Client.post("/experimental/answer", {question: question, context: context})
49
+ end
50
+
51
+ def dataset
52
+ @dataset ||= AI21::Dataset.new
53
+ end
54
+
55
+ def custom_model
56
+ @custom_model ||= AI21::CustomModel.new
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AI21
4
+ class CustomModel
5
+ def list
6
+ end
7
+
8
+ def get
9
+ end
10
+
11
+ def complete
12
+ end
13
+
14
+ def update
15
+ end
16
+
17
+ def train
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AI21
4
+ class Dataset
5
+ def list
6
+ AI21::Client.get("/dataset")
7
+ end
8
+
9
+ def get(id)
10
+ AI21::Client.get("/dataset/#{id}")
11
+ end
12
+
13
+ def create(name, file)
14
+ # TODO: create dataset endpoint
15
+ end
16
+
17
+ def delete(id)
18
+ AI21::Client.delete("/dataset/#{id}")
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AI21
4
+ module Helper
5
+ def deep_transform_keys!(object, &block)
6
+ case object
7
+ when Hash
8
+ object.keys.each do |key|
9
+ value = object.delete(key)
10
+ object[yield(key)] = deep_transform_keys!(value, &block)
11
+ end
12
+ object
13
+ when Array
14
+ object.map! { |item| deep_transform_keys!(item, &block) }
15
+ else
16
+ object
17
+ end
18
+ end
19
+
20
+ def camel_to_snake(hash)
21
+ deep_transform_keys!(hash) do |key|
22
+ key.to_s.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase.to_sym
23
+ end
24
+ end
25
+
26
+ def snake_to_camel(hash)
27
+ deep_transform_keys!(hash) do |key|
28
+ key.to_s.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }.to_sym
29
+ end
30
+ end
31
+ end
32
+ end
data/lib/ai21/http.rb ADDED
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "uri"
4
+ require "net/http"
5
+ require "openssl"
6
+ require "json"
7
+ require "pry"
8
+
9
+ module AI21
10
+ module HTTP
11
+ include AI21::Helper
12
+
13
+ def get(path)
14
+ url = URI("#{AI21.configuration.uri_base}#{AI21.configuration.api_version}#{path}")
15
+ http = http(url)
16
+ request = get_request(url)
17
+ body = http.request(request).read_body
18
+ camel_to_snake ::JSON.parse(body)
19
+ end
20
+
21
+ def post(path, body)
22
+ url = URI("#{AI21.configuration.uri_base}#{AI21.configuration.api_version}#{path}")
23
+ http = http(url)
24
+ request = post_request(url, body)
25
+ body = http.request(request).read_body
26
+ camel_to_snake ::JSON.parse(body)
27
+ end
28
+
29
+ def get_request(url)
30
+ request = Net::HTTP::Get.new(url)
31
+ request["accept"] = "application/json"
32
+ request["Authorization"] = "Bearer #{AI21.configuration.access_token}"
33
+ request
34
+ end
35
+
36
+ def post_request(url, body)
37
+ request = Net::HTTP::Post.new(url)
38
+ request["accept"] = "application/json"
39
+ request["content-type"] = "application/json"
40
+ request["Authorization"] = "Bearer #{AI21.configuration.access_token}"
41
+ request.body = body.to_json
42
+ request
43
+ end
44
+
45
+ def http(url)
46
+ http = Net::HTTP.new(url.host, url.port)
47
+ http.use_ssl = true
48
+ http
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AI21
4
+ VERSION = "0.1.0"
5
+ end
data/lib/ai21.rb ADDED
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "ai21/version"
4
+ require_relative "ai21/helper"
5
+ require_relative "ai21/http"
6
+ require_relative "ai21/client"
7
+ require_relative "ai21/dataset"
8
+ require_relative "ai21/custom_model"
9
+
10
+ module AI21
11
+ class Error < StandardError; end
12
+
13
+ class ConfigurationError < Error; end
14
+
15
+ class Configuration
16
+ attr_writer :access_token
17
+ attr_accessor :api_version, :uri_base
18
+
19
+ def initialize
20
+ @access_token = nil
21
+ @uri_base = "https://api.ai21.com/studio/"
22
+ @api_version = "v1"
23
+ end
24
+
25
+ def access_token
26
+ return @access_token if @access_token
27
+
28
+ error_text = "AI21 access token is missing. Visit https://studio.ai21.com/account"
29
+ raise ConfigurationError, error_text
30
+ end
31
+ end
32
+
33
+ class << self
34
+ attr_writer :configuration
35
+
36
+ def configuration
37
+ @configuration ||= Configuration.new
38
+ end
39
+
40
+ def configure
41
+ yield(configuration)
42
+ end
43
+ end
44
+ end
data/sig/ai21.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Ai21
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ai21
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alex Chaplinsky
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-05-31 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Ruby client for AI21 Studio API.
14
+ email:
15
+ - alchaplinsky@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - CHANGELOG.md
22
+ - CODE_OF_CONDUCT.md
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - README.md
26
+ - Rakefile
27
+ - lib/ai21.rb
28
+ - lib/ai21/client.rb
29
+ - lib/ai21/custom_model.rb
30
+ - lib/ai21/dataset.rb
31
+ - lib/ai21/helper.rb
32
+ - lib/ai21/http.rb
33
+ - lib/ai21/version.rb
34
+ - sig/ai21.rbs
35
+ homepage: https://rubygems.org/gems/ai21
36
+ licenses: []
37
+ metadata:
38
+ homepage_uri: https://rubygems.org/gems/ai21
39
+ source_code_uri: https://github.com/alchaplinsky/ai21
40
+ changelog_uri: https://github.com/alchaplinsky/ai21/blob/master/CHANGELOG.md
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 2.6.0
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubygems_version: 3.2.33
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: Ruby client for AI21 Studio API.
60
+ test_files: []