cohere 1.0.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: 80d602bfb7febea1a69c2c38b1aec66eb930d2af18ade1861fe6af124497f951
4
+ data.tar.gz: 757535497512e7727ba6ce3306c13db05623b7054a33d471a13844a254b59002
5
+ SHA512:
6
+ metadata.gz: 394627e843268f8dd61d245543c131d98512043fdf1abed1ccfcff45932dd48571586004393865f2ca23615be6407a5ae5eb3d73d8f34c92a6cf8f998e6cfa78
7
+ data.tar.gz: 30371a994420e47df5284b31e04b2bcc77ea01ce4b192a0799e32336f1057a1c774f48fe2041fec87ce0bc517cc7b720d93f4b593539aed50e2884340e9e0b0f
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-03-12
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 knajjars@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,14 @@
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
+
12
+ gem "rubocop", "~> 1.21"
13
+
14
+ gem "dotenv", "~> 2.7"
data/Gemfile.lock ADDED
@@ -0,0 +1,65 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cohere (1.0.0)
5
+ httparty (= 0.20.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ diff-lcs (1.5.0)
12
+ dotenv (2.7.6)
13
+ httparty (0.20.0)
14
+ mime-types (~> 3.0)
15
+ multi_xml (>= 0.5.2)
16
+ mime-types (3.4.1)
17
+ mime-types-data (~> 3.2015)
18
+ mime-types-data (3.2022.0105)
19
+ multi_xml (0.6.0)
20
+ parallel (1.22.1)
21
+ parser (3.1.2.0)
22
+ ast (~> 2.4.1)
23
+ rainbow (3.1.1)
24
+ rake (13.0.6)
25
+ regexp_parser (2.3.1)
26
+ rexml (3.2.5)
27
+ rspec (3.11.0)
28
+ rspec-core (~> 3.11.0)
29
+ rspec-expectations (~> 3.11.0)
30
+ rspec-mocks (~> 3.11.0)
31
+ rspec-core (3.11.0)
32
+ rspec-support (~> 3.11.0)
33
+ rspec-expectations (3.11.0)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.11.0)
36
+ rspec-mocks (3.11.1)
37
+ diff-lcs (>= 1.2.0, < 2.0)
38
+ rspec-support (~> 3.11.0)
39
+ rspec-support (3.11.0)
40
+ rubocop (1.28.2)
41
+ parallel (~> 1.10)
42
+ parser (>= 3.1.0.0)
43
+ rainbow (>= 2.2.2, < 4.0)
44
+ regexp_parser (>= 1.8, < 3.0)
45
+ rexml
46
+ rubocop-ast (>= 1.17.0, < 2.0)
47
+ ruby-progressbar (~> 1.7)
48
+ unicode-display_width (>= 1.4.0, < 3.0)
49
+ rubocop-ast (1.17.0)
50
+ parser (>= 3.1.1.0)
51
+ ruby-progressbar (1.11.0)
52
+ unicode-display_width (2.1.0)
53
+
54
+ PLATFORMS
55
+ ruby
56
+
57
+ DEPENDENCIES
58
+ cohere!
59
+ dotenv (~> 2.7)
60
+ rake (~> 13.0)
61
+ rspec (~> 3.0)
62
+ rubocop (~> 1.21)
63
+
64
+ BUNDLED WITH
65
+ 2.3.7
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Khalil Najjar
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,99 @@
1
+ # Cohere::Ruby
2
+
3
+ The official SDK for the [Cohere](https://cohere.ai/) API.
4
+
5
+ Harness the power of language understanding. Join the developers and businesses who are using Cohere to generate, categorize and organize text at a scale that was previously unimaginable.
6
+
7
+ ## Installation
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add cohere
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install cohere
16
+
17
+ ## Usage
18
+
19
+ ```ruby
20
+ client = Cohere::Client.new access_token: "<COHERE_ACCESS_TOKEN>"
21
+ ```
22
+
23
+ ### Embed
24
+
25
+ See full docs [here](https://docs.cohere.ai/embed-reference).
26
+
27
+ - **texts**: `array of strings` - *required*
28
+ - **truncate**: `Accepts "NONE", "LEFT" or "RIGHT`, default `NONE` - *optional*
29
+
30
+ ```ruby
31
+ client.embed(model: "medium",
32
+ options: { texts: ["When are you open?", "When do you close?", "What are the hours?",
33
+ "Are you open on weekends?", "Are you available on holidays?"] })
34
+ ```
35
+
36
+ ### Classify
37
+
38
+ See full docs [here](https://docs.cohere.ai/classify-reference).
39
+
40
+ - inputs: `array of strings` - *required*
41
+ - examples: `array of {label:{},text:{}}` - *required*
42
+ - outputIndicator: `string` - *optional*
43
+ - taskDescription: `string` - *optional*
44
+
45
+ ```ruby
46
+ client.classify(model: "medium",
47
+ options: { inputs: [ "This item was broken when it arrived", "This item broke after 3 weeks" ],
48
+ examples: [
49
+ { text: "The order came 5 days early", label: "positive" },
50
+ { text: "The item exceeded my expectations", label: "positive" }
51
+ ]})
52
+ ```
53
+
54
+ ### Generate
55
+
56
+ See full docs [here](https://docs.cohere.ai/generate-reference).
57
+
58
+ - prompt: `string` - *required*
59
+ - temperature: `number`. Default `0.75` - *optional*
60
+ - p: `number`. Default `0.75` - *optional*
61
+ - k: `number`. Default `0` - *optional*
62
+ - frequency_penalty: `number`. Default `0` - *optional*
63
+ - presence_penalty: `number`. Default `0` - *optional*
64
+ - max_tokens: `number`. Default `50` - *optional*
65
+ - stop_sequences: `array of strings` - *optional*
66
+ - return_likelihoods: `GENERATION|ALL|NONE`. Default `NONE` - *optional*
67
+ - num_generations: `number`. Default `1` - *optional*
68
+
69
+
70
+ ```ruby
71
+ client.generate(model: "medium", options: { prompt: "What is your name?" })
72
+ ```
73
+
74
+ ## Response
75
+
76
+ You can read more about the responses from the endpoints in our official API [docs site](https://docs.cohere.ai/api-reference/).
77
+
78
+ A struct containing:
79
+
80
+ - code: `http status code of the request`
81
+ - body: `the body contains a hash with the response body or a message if something went wrong`
82
+
83
+ ## Development
84
+
85
+ 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.
86
+
87
+ 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).
88
+
89
+ ## Contributing
90
+
91
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/cohere-ruby. 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]/cohere-ruby/blob/master/CODE_OF_CONDUCT.md).
92
+
93
+ ## License
94
+
95
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
96
+
97
+ ## Code of Conduct
98
+
99
+ Everyone interacting in the Cohere::Ruby project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/cohere-ruby/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
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
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/cohere.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/cohere/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "cohere"
7
+ spec.version = Cohere::VERSION
8
+ spec.authors = ["Khalil Najjar"]
9
+ spec.email = ["khalil@cohere.ai"]
10
+
11
+ spec.summary = "Ruby Gem for Cohere-AI API"
12
+ spec.description = "Official SDK for Cohere AI"
13
+ spec.homepage = "https://cohere.ai"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/cohere-ai/cohere-ruby"
19
+ spec.metadata["changelog_uri"] = "https://github.com/cohere-ai/cohere-ruby/blob/main/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
26
+ end
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_dependency "httparty", "0.20.0"
33
+ end
@@ -0,0 +1,80 @@
1
+ module Cohere
2
+ class Client
3
+ include HTTParty
4
+ base_uri "https://api.cohere.ai"
5
+
6
+ def initialize(access_token: nil, version: nil)
7
+ @access_token = access_token
8
+ @version = version || "2021-11-08"; # currently latest, update when we version better
9
+ end
10
+
11
+ def classify(model:, options:)
12
+ serialize post(endpoint: "classify", model: model, body: DEFAULT_CLASSIFY_PARAMS.merge(options))
13
+ end
14
+
15
+ def embed(model:, options:)
16
+ threads = []
17
+ default_options = DEFAULT_EMBED_PARAMS.merge(options)
18
+ return OpenStruct.new(code: 400, body: { embeddings: [], ids: [] }) if default_options[:texts].empty?
19
+
20
+ default_options[:texts].each_slice(1) do |batch|
21
+ threads << threded_post(model: model, endpoint: "embed", body: default_options.merge(texts: batch))
22
+ end
23
+
24
+ results = threads.each(&:join).map(&:value)
25
+ serialize_embed_results(results)
26
+ end
27
+
28
+ def generate(model:, options:)
29
+ serialize post(endpoint: "generate", model: model, body: DEFAULT_GENERATE_PARAMS.merge(options))
30
+ end
31
+
32
+ private
33
+
34
+ def post(model:, endpoint:, body:)
35
+ self.class.post(
36
+ "/#{model}/#{endpoint}",
37
+ headers: {
38
+ "Authorization": "Bearer #{@access_token}",
39
+ "Content-Type": "application/json",
40
+ "Cohere-Version": @version,
41
+ "Request-Source": "ruby-sdk"
42
+ },
43
+ body: body.to_json
44
+ )
45
+ end
46
+
47
+ def serialize_embed_results(results)
48
+ result = { body: { embeddings: [], ids: [] }, code: nil }
49
+ results.each do |res|
50
+ response = JSON.parse(res.response.body)
51
+ result[:body][:embeddings].concat(response["embeddings"])
52
+ result[:body][:ids] << response["id"]
53
+ result[:code] = res.code
54
+ end
55
+
56
+ to_hash result
57
+ end
58
+
59
+ def threded_post(endpoint:, model:, body:)
60
+ Thread.new do
61
+ post(endpoint: endpoint, model: model,
62
+ body: body)
63
+ end
64
+ end
65
+
66
+ def serialize(response)
67
+ serialized_response = { body: {}, code: {} }
68
+ serialized_response[:body] = JSON.parse(response.response.body)
69
+ serialized_response[:code] = response.code
70
+ to_hash serialized_response
71
+ end
72
+
73
+ def to_hash(hash)
74
+ result = hash.each_with_object({}) do |(key, val), memo|
75
+ memo[key] = val.is_a?(Hash) ? to_hash(val) : val
76
+ end
77
+ OpenStruct.new result
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,8 @@
1
+ module Cohere
2
+ DEFAULT_GENERATE_PARAMS = { temperature: 0.75, p: 0.75, k: 0, frequency_penalty: 0, presence_penalty: 0,
3
+ max_tokens: 50, stop_sequences: [], return_likelihoods: "NONE", prompt: nil, num_generations: 1 }.freeze
4
+
5
+ DEFAULT_EMBED_PARAMS = { texts: [], truncate: "NONE" }.freeze
6
+
7
+ DEFAULT_CLASSIFY_PARAMS = { inputs: [], examples: [], outputIndicator: "", taskDescription: "" }.freeze
8
+ end
@@ -0,0 +1,3 @@
1
+ module Cohere
2
+ VERSION = "1.0.0".freeze
3
+ end
data/lib/cohere.rb ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "httparty"
4
+ require_relative "cohere/version"
5
+ require_relative "cohere/params"
6
+ require_relative "cohere/client"
7
+
8
+ module Cohere
9
+ class Error < StandardError; end
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,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cohere
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Khalil Najjar
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-05-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.20.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.20.0
27
+ description: Official SDK for Cohere AI
28
+ email:
29
+ - khalil@cohere.ai
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".rspec"
35
+ - ".rubocop.yml"
36
+ - CHANGELOG.md
37
+ - CODE_OF_CONDUCT.md
38
+ - Gemfile
39
+ - Gemfile.lock
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - cohere.gemspec
44
+ - lib/cohere.rb
45
+ - lib/cohere/client.rb
46
+ - lib/cohere/params.rb
47
+ - lib/cohere/version.rb
48
+ - sig/cohere.rbs
49
+ homepage: https://cohere.ai
50
+ licenses:
51
+ - MIT
52
+ metadata:
53
+ homepage_uri: https://cohere.ai
54
+ source_code_uri: https://github.com/cohere-ai/cohere-ruby
55
+ changelog_uri: https://github.com/cohere-ai/cohere-ruby/blob/main/CHANGELOG.md
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 2.6.0
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubygems_version: 3.3.7
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: Ruby Gem for Cohere-AI API
75
+ test_files: []