ai21 0.1.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/LICENSE +21 -0
- data/README.md +75 -12
- data/lib/ai21/client.rb +9 -5
- data/lib/ai21/http.rb +29 -19
- data/lib/ai21/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5215852a66432ed4fd3f8f1577e01f92aaccc2afd141728ef1b26a694c4c018b
|
4
|
+
data.tar.gz: 5adfec11c0ab7504bce0d31816228678f4f32573be47c167acdf622f0521adf4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6ff6db10c500f5f770ad45f6dbc05b03afce008b7ce0f0c062077d3e500295a57a818547223067adc2475165d7e86b39ff3b915cb65c7379f24c5acc1fd4ee44
|
7
|
+
data.tar.gz: 9f0eda1cd97e621d7237d1fef349cda23b4047a0a7bf4c0706f3482df2bd32e44669bf6aeb39f131f0ad5b4063bf074d3b521f1ec0980330f39a5a43bfbdefd7
|
data/Gemfile.lock
CHANGED
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,87 @@
|
|
1
|
-
#
|
1
|
+
# Ruby client for AI21
|
2
2
|
|
3
|
-
|
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
|
-
|
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
|
-
|
27
|
+
And then execute:
|
12
28
|
|
13
|
-
|
29
|
+
```
|
30
|
+
bundle install
|
31
|
+
```
|
14
32
|
|
15
|
-
|
33
|
+
## Getting Started
|
16
34
|
|
17
|
-
|
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
|
-
|
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
|
+
```
|
80
|
+
|
81
|
+
#### Tokenize
|
82
|
+
```ruby
|
83
|
+
client.tokenize("Lorem ipsum long long tokens!")
|
84
|
+
```
|
22
85
|
|
23
86
|
## Development
|
24
87
|
|
@@ -28,8 +91,8 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
28
91
|
|
29
92
|
## Contributing
|
30
93
|
|
31
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
94
|
+
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
95
|
|
33
96
|
## Code of Conduct
|
34
97
|
|
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/
|
98
|
+
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,22 +32,26 @@ module AI21
|
|
32
32
|
AI21::Client.post("/gec", {text: prompt})
|
33
33
|
end
|
34
34
|
|
35
|
-
def improvements(prompt, types
|
36
|
-
AI21::Client.post("/improvements", {text: prompt, types: types}
|
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
|
44
|
-
AI21::Client.post("/segmentation", {source: prompt, sourceType: source_type}
|
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)
|
48
48
|
AI21::Client.post("/experimental/answer", {question: question, context: context})
|
49
49
|
end
|
50
50
|
|
51
|
+
def tokenize(text)
|
52
|
+
AI21::Client.post("/tokenize", {text: text})
|
53
|
+
end
|
54
|
+
|
51
55
|
def dataset
|
52
56
|
@dataset ||= AI21::Dataset.new
|
53
57
|
end
|
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
|
-
|
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
|
-
|
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 =
|
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
|
30
|
-
request = Net::HTTP
|
31
|
-
request["accept"] =
|
32
|
-
request["
|
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
|
37
|
-
|
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
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
|
4
|
+
version: 0.2.1
|
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-
|
11
|
+
date: 2023-06-22 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
|