ai21 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5d246c86807d3bcd6b2bafe31b34b6b426f9b4b89c28544cfb5abfce6204a9d0
4
- data.tar.gz: 2006023a575aa77e4172bd8f2fae30384751427833a91ec813aa56102c5c434c
3
+ metadata.gz: 4c3de47196822dfd0be6702f363cf4e662891f5ad9398064cdb2eee45d91756d
4
+ data.tar.gz: bbb4dd92c5ab9f023b3113b9f1b1ecdad55839c2f9ffc26acef1d3eb536fe7f8
5
5
  SHA512:
6
- metadata.gz: cebbc69bcfab0c46090c7e7a443b2c49a9c7d7be74713d7a73e87bad8216a338b182f76f582287ab2715d18b75da1eddf222134600ceca7092022ac6fae965bc
7
- data.tar.gz: 33a0182f3bca6897d4746f84cade3954b1d9638c94389fc6718215f5a85fd24ccf17ef9549ef48e8efaa856d7485ba1ea5f0e65464ed044b06fadb95dcea894c
6
+ metadata.gz: b2218a7c4179ba7e3d9cc2d4c3168c9b83b11cf983d37e1b28bc13108d06a315656e493d9a2f8d1971a54eaa49d32362bf89f9741dcd16e9d8709fa7585b6564
7
+ data.tar.gz: 4854f7b1c1323f7b8155c32cc0e9292411e5bdded311160038bba3b6a6b127ab69aeeff0fc4c162ca77af4ab033f297036556ad80a03c9979a3a58ac89ab8eac
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ai21 (0.1.0)
4
+ ai21 (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Alex Chaplinsky
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md CHANGED
@@ -1,24 +1,82 @@
1
- # Ai21
1
+ # Ruby client for AI21
2
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.
3
+ Use the [Studio AI21](https://www.ai21.com/studio) API with Ruby! Differentiate your product with generative text AI :robot:.
6
4
 
7
5
  ## Installation
8
6
 
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.
7
+ ### Gem install
8
+ Install gem with:
9
+
10
+ ```
11
+ gem install ai21
12
+ ```
13
+
14
+ and require with:
15
+
16
+ ```ruby
17
+ require "ai21"
18
+ ```
19
+
20
+ ### Bundler
21
+ Add gem to your application's `Gemfile`:
22
+
23
+ ```ruby
24
+ gem "ai21"
25
+ ```
10
26
 
11
- Install the gem and add to the application's Gemfile by executing:
27
+ And then execute:
12
28
 
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
29
+ ```
30
+ bundle install
31
+ ```
14
32
 
15
- If bundler is not being used to manage dependencies, install the gem by executing:
33
+ ## Getting Started
16
34
 
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
35
+ - Sign up for AI21 Studio here https://studio.ai21.com/sign-up
36
+ - Get your API key from https://studio.ai21.com/account/api-key
37
+ - Refer to [AI21 API doc](https://docs.ai21.com/reference/) to understand options that can be passed to different tasks
18
38
 
19
39
  ## Usage
20
40
 
21
- TODO: Write usage instructions here
41
+ Instantiate a client
42
+ ```ruby
43
+ client = AI21::Client.new "YOUR_API_TOKEN"
44
+ ```
45
+
46
+ #### Completion
47
+ ```ruby
48
+ client.complete("The capital of France is ")
49
+ ```
50
+
51
+ #### Intstruct
52
+ ```ruby
53
+ client.instruct("Tell me the name of main character in 'The Matrix'")
54
+ ```
55
+
56
+ #### Paraphrase
57
+ ```ruby
58
+ client.paraphrase("there is nothing that can't be fixed")
59
+ ```
60
+
61
+ #### Correction
62
+ ```ruby
63
+ client.correct("can you fix this speling mistake?")
64
+ ```
65
+
66
+ #### Improvements
67
+ ```ruby
68
+ client.improvements("This is a bad decision to let AI control the world")
69
+ ```
70
+
71
+ #### Summarization
72
+ ```ruby
73
+ client.summarize("Long text that requires summarization ...")
74
+ ```
75
+
76
+ #### Question Answering
77
+ ```ruby
78
+ client.answer("what is the capital of France?", "Capital of France is the city called Paris")
79
+ ```
22
80
 
23
81
  ## Development
24
82
 
@@ -28,8 +86,8 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
28
86
 
29
87
  ## Contributing
30
88
 
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).
89
+ Bug reports and pull requests are welcome on GitHub at https://github.com/alchaplinsky/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/alchaplinsky/ai21/blob/main/CODE_OF_CONDUCT.md).
32
90
 
33
91
  ## Code of Conduct
34
92
 
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).
93
+ 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/alchaplinsky/ai21/blob/main/CODE_OF_CONDUCT.md).
data/lib/ai21/client.rb CHANGED
@@ -32,16 +32,16 @@ module AI21
32
32
  AI21::Client.post("/gec", {text: prompt})
33
33
  end
34
34
 
35
- def improvements(prompt, types, options = {})
36
- AI21::Client.post("/improvements", {text: prompt, types: types}.merge(snake_to_camel(options)))
35
+ def improvements(prompt, types = ["fluency"])
36
+ AI21::Client.post("/improvements", {text: prompt, types: types})
37
37
  end
38
38
 
39
- def summarize(prompt, source_type, options = {})
39
+ def summarize(prompt, source_type = "TEXT", options = {})
40
40
  AI21::Client.post("/summarize", {source: prompt, sourceType: source_type}.merge(snake_to_camel(options)))
41
41
  end
42
42
 
43
- def segmentation(prompt, source_type, options = {})
44
- AI21::Client.post("/segmentation", {source: prompt, sourceType: source_type}.merge(snake_to_camel(options)))
43
+ def segmentation(prompt, source_type = "TEXT")
44
+ AI21::Client.post("/segmentation", {source: prompt, sourceType: source_type})
45
45
  end
46
46
 
47
47
  def answer(question, context)
data/lib/ai21/http.rb CHANGED
@@ -4,42 +4,44 @@ require "uri"
4
4
  require "net/http"
5
5
  require "openssl"
6
6
  require "json"
7
- require "pry"
8
7
 
9
8
  module AI21
10
9
  module HTTP
11
10
  include AI21::Helper
12
11
 
13
12
  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)
13
+ fetch(path, :get)
19
14
  end
20
15
 
21
16
  def post(path, body)
22
- url = URI("#{AI21.configuration.uri_base}#{AI21.configuration.api_version}#{path}")
17
+ fetch(path, :post, body)
18
+ end
19
+
20
+ def delete(path)
21
+ fetch(path, :delete)
22
+ end
23
+
24
+ def fetch(path, method, body = nil)
25
+ url = url(path)
23
26
  http = http(url)
24
- request = post_request(url, body)
27
+ request = request(url, method)
28
+
29
+ request.body = body.to_json if body
30
+
25
31
  body = http.request(request).read_body
26
32
  camel_to_snake ::JSON.parse(body)
27
33
  end
28
34
 
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}"
35
+ def request(url, method)
36
+ request = Object.const_get("Net::HTTP::#{method.capitalize}").new(url)
37
+ request["accept"] = content_type
38
+ request["content-type"] = content_type
39
+ request["Authorization"] = authorization
33
40
  request
34
41
  end
35
42
 
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
+ def url(path)
44
+ URI("#{AI21.configuration.uri_base}#{AI21.configuration.api_version}#{path}")
43
45
  end
44
46
 
45
47
  def http(url)
@@ -47,5 +49,13 @@ module AI21
47
49
  http.use_ssl = true
48
50
  http
49
51
  end
52
+
53
+ def authorization
54
+ "Bearer #{AI21.configuration.access_token}"
55
+ end
56
+
57
+ def content_type
58
+ "application/json"
59
+ end
50
60
  end
51
61
  end
data/lib/ai21/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AI21
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ai21
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Chaplinsky
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-05-31 00:00:00.000000000 Z
11
+ date: 2023-06-01 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Ruby client for AI21 Studio API.
14
14
  email:
@@ -22,6 +22,7 @@ files:
22
22
  - CODE_OF_CONDUCT.md
23
23
  - Gemfile
24
24
  - Gemfile.lock
25
+ - LICENSE
25
26
  - README.md
26
27
  - Rakefile
27
28
  - lib/ai21.rb