cohere-ruby 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 02ed5a633d1adf0626506a7f1d6023b268b54c3f7c3320086ef5a63240d8e024
4
+ data.tar.gz: c6105abcfaefd5996eeba2fa8cac19e64debcf0a0e4d68f73cc12b340b521305
5
+ SHA512:
6
+ metadata.gz: 27d9ac0456381e1b1734b88432832a9623fc66c87a437747a3e6b64103dce806920fcb208531a8eb7c19befbcc595cd35a653fb2ab4e8d8cd167c2c7c194214c
7
+ data.tar.gz: be9555d9ac14e534c70afe1df520666ff893abc60a54161435bcce45b0973974722cbd81565f74793699b49d23e3e6871957ee5d10010da63eeb8c352a665a7c
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-04-23
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in cohere.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+ gem "standard", "~> 1.28.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,80 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cohere-ruby (0.9.0)
5
+ faraday (~> 2.7.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ diff-lcs (1.5.0)
12
+ faraday (2.7.4)
13
+ faraday-net_http (>= 2.0, < 3.1)
14
+ ruby2_keywords (>= 0.0.4)
15
+ faraday-net_http (3.0.2)
16
+ json (2.6.3)
17
+ language_server-protocol (3.17.0.3)
18
+ lint_roller (1.0.0)
19
+ parallel (1.23.0)
20
+ parser (3.2.2.1)
21
+ ast (~> 2.4.1)
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.1)
31
+ rspec-support (~> 3.12.0)
32
+ rspec-expectations (3.12.2)
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.0)
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
+ ruby2_keywords (0.0.5)
56
+ standard (1.28.0)
57
+ language_server-protocol (~> 3.17.0.2)
58
+ lint_roller (~> 1.0)
59
+ rubocop (~> 1.50.2)
60
+ standard-custom (~> 1.0.0)
61
+ standard-performance (~> 1.0.1)
62
+ standard-custom (1.0.0)
63
+ lint_roller (~> 1.0)
64
+ standard-performance (1.0.1)
65
+ lint_roller (~> 1.0)
66
+ rubocop-performance (~> 1.16.0)
67
+ unicode-display_width (2.4.2)
68
+
69
+ PLATFORMS
70
+ x86_64-darwin-19
71
+ x86_64-linux
72
+
73
+ DEPENDENCIES
74
+ cohere-ruby!
75
+ rake (~> 13.0)
76
+ rspec (~> 3.0)
77
+ standard (~> 1.28.0)
78
+
79
+ BUNDLED WITH
80
+ 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,111 @@
1
+ # Cohere
2
+
3
+ <p>
4
+ <img alt='Weaviate logo' src='https://static.wikia.nocookie.net/logopedia/images/d/d4/Cohere_2023.svg/revision/latest?cb=20230419182227' height='50' />
5
+ +&nbsp;&nbsp;
6
+ <img alt='Ruby logo' src='https://user-images.githubusercontent.com/541665/230231593-43861278-4550-421d-a543-fd3553aac4f6.png' height='40' />
7
+ </p>
8
+
9
+ Cohere API client for Ruby.
10
+
11
+ ![Tests status](https://github.com/andreibondarev/cohere-ruby/actions/workflows/ci.yml/badge.svg) [![Gem Version](https://badge.fury.io/rb/cohere-ruby.svg)](https://badge.fury.io/rb/cohere-ruby)
12
+
13
+ ## Installation
14
+
15
+ Install the gem and add to the application's Gemfile by executing:
16
+
17
+ $ bundle add cohere-ruby
18
+
19
+ If bundler is not being used to manage dependencies, install the gem by executing:
20
+
21
+ $ gem install cohere-ruby
22
+
23
+ ## Usage
24
+
25
+ ### Instantiating API client
26
+ ```ruby
27
+ client = Cohere::Client.new(
28
+ api_key: ENV['COHERE_API_KEY']
29
+ )
30
+ ```
31
+ ### Generate
32
+ ```ruby
33
+ client.generate(
34
+ prompt: "Once upon a time in a magical land called"
35
+ )
36
+ ```
37
+
38
+ ### Embed
39
+ ```ruby
40
+ client.embed(
41
+ texts: ["hello!"]
42
+ )
43
+ ```
44
+
45
+ ### Classify
46
+ ```ruby
47
+ examples = [
48
+ { text: "Dermatologists don't like her!", label: "Spam" },
49
+ { text: "Hello, open to this?", label: "Spam" },
50
+ { text: "I need help please wire me $1000 right now", label: "Spam" },
51
+ { text: "Nice to know you ;)", label: "Spam" },
52
+ { text: "Please help me?", label: "Spam" },
53
+ { text: "Your parcel will be delivered today", label: "Not spam" },
54
+ { text: "Review changes to our Terms and Conditions", label: "Not spam" },
55
+ { text: "Weekly sync notes", label: "Not spam" },
56
+ { text: "Re: Follow up from today's meeting", label: "Not spam" },
57
+ { text: "Pre-read for tomorrow", label: "Not spam" }
58
+ ]
59
+
60
+ inputs = [
61
+ "Confirm your email address",
62
+ "hey i need u to send some $",
63
+ ]
64
+
65
+ client.classify(
66
+ examples: examples,
67
+ inputs: inputs
68
+ )
69
+ ```
70
+
71
+ ### Tokenize
72
+ ```ruby
73
+ client.tokenize(
74
+ text: "hello world!"
75
+ )
76
+ ```
77
+
78
+ ### Detokenize
79
+ ```ruby
80
+ client.detokenize(
81
+ tokens: [33555, 1114 , 34]
82
+ )
83
+ ```
84
+
85
+ ### Detect language
86
+ ```ruby
87
+ client.detect_language(
88
+ texts: ["Здравствуй, Мир"]
89
+ )
90
+ ```
91
+
92
+ ### Summarize
93
+ ```ruby
94
+ client.summarize(
95
+ text: "..."
96
+ )
97
+ ```
98
+
99
+ ## Development
100
+
101
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rspec spec/` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
102
+
103
+ 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).
104
+
105
+ ## Contributing
106
+
107
+ Bug reports and pull requests are welcome on GitHub at https://github.com/andreibondarev/cohere.
108
+
109
+ ## License
110
+
111
+ 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,140 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "faraday"
4
+
5
+ module Cohere
6
+ class Client
7
+ attr_reader :api_key, :connection
8
+
9
+ ENDPOINT_URL = "https://api.cohere.ai/v1"
10
+
11
+ def initialize(api_key:)
12
+ @api_key = api_key
13
+ end
14
+
15
+ # This endpoint generates realistic text conditioned on a given input.
16
+ def generate(
17
+ prompt:,
18
+ model: nil,
19
+ num_generations: nil,
20
+ max_tokens: nil,
21
+ preset: nil,
22
+ temperature: nil,
23
+ k: nil,
24
+ p: nil,
25
+ frequency_penalty: nil,
26
+ presence_penalty: nil,
27
+ end_sequences: nil,
28
+ stop_sequence: nil,
29
+ return_likelihoods: nil,
30
+ logit_bias: nil,
31
+ truncate: nil
32
+ )
33
+ response = connection.post("generate") do |req|
34
+ req.body = {prompt: prompt}
35
+ req.body[:model] = model if model
36
+ req.body[:num_generations] = num_generations if num_generations
37
+ req.body[:max_tokens] = max_tokens if max_tokens
38
+ req.body[:preset] = preset if preset
39
+ req.body[:temperature] = temperature if temperature
40
+ req.body[:k] = k if k
41
+ req.body[:p] = p if p
42
+ req.body[:frequency_penalty] = frequency_penalty if frequency_penalty
43
+ req.body[:presence_penalty] = presence_penalty if presence_penalty
44
+ req.body[:end_sequences] = end_sequences if end_sequences
45
+ req.body[:stop_sequence] = stop_sequence if stop_sequence
46
+ req.body[:return_likelihoods] = return_likelihoods if return_likelihoods
47
+ req.body[:logit_bias] = logit_bias if logit_bias
48
+ req.body[:truncate] = truncate if truncate
49
+ end
50
+ response.body
51
+ end
52
+
53
+ def embed(
54
+ texts:,
55
+ model: nil,
56
+ truncate: nil
57
+ )
58
+ response = connection.post("embed") do |req|
59
+ req.body = {texts: texts}
60
+ req.body[:model] = model if model
61
+ req.body[:truncate] = truncate if truncate
62
+ end
63
+ response.body
64
+ end
65
+
66
+ def classify(
67
+ inputs:,
68
+ examples:,
69
+ model: nil,
70
+ present: nil,
71
+ truncate: nil
72
+ )
73
+ response = connection.post("classify") do |req|
74
+ req.body = {
75
+ inputs: inputs,
76
+ examples: examples
77
+ }
78
+ req.body[:model] = model if model
79
+ req.body[:present] = present if present
80
+ req.body[:truncate] = truncate if truncate
81
+ end
82
+ response.body
83
+ end
84
+
85
+ def tokenize(text:)
86
+ response = connection.post("tokenize") do |req|
87
+ req.body = {text: text}
88
+ end
89
+ response.body
90
+ end
91
+
92
+ def detokenize(tokens:)
93
+ response = connection.post("detokenize") do |req|
94
+ req.body = {tokens: tokens}
95
+ end
96
+ response.body
97
+ end
98
+
99
+ def detect_language(texts:)
100
+ response = connection.post("detect-language") do |req|
101
+ req.body = {texts: texts}
102
+ end
103
+ response.body
104
+ end
105
+
106
+ def summarize(
107
+ text:,
108
+ length: nil,
109
+ format: nil,
110
+ model: nil,
111
+ extractiveness: nil,
112
+ temperature: nil,
113
+ additional_command: nil
114
+ )
115
+ response = connection.post("summarize") do |req|
116
+ req.body = {text: text}
117
+ req.body[:length] = length if length
118
+ req.body[:format] = format if format
119
+ req.body[:model] = model if model
120
+ req.body[:extractiveness] = extractiveness if extractiveness
121
+ req.body[:temperature] = temperature if temperature
122
+ req.body[:additional_command] = additional_command if additional_command
123
+ end
124
+ response.body
125
+ end
126
+
127
+ private
128
+
129
+ def connection
130
+ @connection ||= Faraday.new(url: ENDPOINT_URL) do |faraday|
131
+ if api_key
132
+ faraday.request :authorization, :Bearer, api_key
133
+ end
134
+ faraday.request :json
135
+ faraday.response :json, content_type: /\bjson$/
136
+ faraday.adapter Faraday.default_adapter
137
+ end
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cohere
4
+ VERSION = "0.9.0"
5
+ end
data/lib/cohere.rb ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "cohere/version"
4
+
5
+ module Cohere
6
+ class Error < StandardError; end
7
+ # Your code goes here...
8
+
9
+ autoload :Client, "cohere/client"
10
+ end
data/sig/cohere.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Cohere
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cohere-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrei Bondarev
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-04-24 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: 2.7.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.7.0
27
+ description: Cohere API client for Ruby.
28
+ email:
29
+ - andrei.bondarev13@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".rspec"
35
+ - CHANGELOG.md
36
+ - Gemfile
37
+ - Gemfile.lock
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - lib/cohere.rb
42
+ - lib/cohere/client.rb
43
+ - lib/cohere/version.rb
44
+ - sig/cohere.rbs
45
+ homepage: https://github.com/andreibondarev/cohere-ruby
46
+ licenses:
47
+ - MIT
48
+ metadata:
49
+ homepage_uri: https://github.com/andreibondarev/cohere-ruby
50
+ source_code_uri: https://github.com/andreibondarev/cohere-ruby
51
+ changelog_uri: https://github.com/andreibondarev/cohere-ruby/CHANGELOG.md
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 2.6.0
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubygems_version: 3.2.3
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: Cohere API client for Ruby.
71
+ test_files: []