Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
andreibondarev committed Apr 23, 2023
0 parents commit 7ada4b1
Show file tree
Hide file tree
Showing 18 changed files with 392 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Tests

on:
pull_request:
branches:
- "*"
push:
branches:
- master
jobs:
tests:
runs-on: ubuntu-latest
strategy:
matrix:
ruby: ["2.7", "3.0", "3.1", "3.2"]

steps:
- uses: actions/checkout@master

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler: default
bundler-cache: true

- name: StandardRb check
run: bundle exec standardrb

- name: Run tests
run: |
bundle exec rspec
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/

# rspec failure tracking
.rspec_status
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--format documentation
--color
--require spec_helper
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## [Unreleased]

## [0.1.0] - 2023-04-23

- Initial release
10 changes: 10 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

source "https://rubygems.org"

# Specify your gem's dependencies in cohere.gemspec
gemspec

gem "rake", "~> 13.0"

gem "rspec", "~> 3.0"
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2023 Andrei Bondarev

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Cohere

<p>
<img alt='Weaviate logo' src='https://static.wikia.nocookie.net/logopedia/images/d/d4/Cohere_2023.svg/revision/latest?cb=20230419182227' height='50' />
+&nbsp;&nbsp;
<img alt='Ruby logo' src='https://user-images.githubusercontent.com/541665/230231593-43861278-4550-421d-a543-fd3553aac4f6.png' height='40' />
</p>

Cohere API client for Ruby.

![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)

## Installation

Install the gem and add to the application's Gemfile by executing:

$ bundle add cohere-ruby

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install cohere-ruby

## Usage

### Instantiating API client
```ruby
client = Cohere::Client.new(
api_key: ENV['COHERE_API_KEY']
)
```

## Development

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.

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).

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/andreibondarev/cohere.

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
8 changes: 8 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

require "bundler/gem_tasks"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)

task default: :spec
19 changes: 19 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require "bundler/setup"
require "cohere"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start

client = Cohere::Client.new(
api_key: ENV['COHERE_API_KEY']
)

require "irb"
IRB.start(__FILE__)
8 changes: 8 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here
33 changes: 33 additions & 0 deletions cohere.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

require_relative "lib/cohere/version"

Gem::Specification.new do |spec|
spec.name = "cohere-ruby"
spec.version = Cohere::VERSION
spec.authors = ["Andrei Bondarev"]
spec.email = ["[email protected]"]

spec.summary = "Cohere API client for Ruby."
spec.description = "Cohere API client for Ruby."
spec.homepage = "https://github.com/andreibondarev/cohere-ruby"
spec.license = "MIT"
spec.required_ruby_version = ">= 2.6.0"

spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = "https://github.com/andreibondarev/cohere-ruby"
spec.metadata["changelog_uri"] = "https://github.com/andreibondarev/cohere-ruby/CHANGELOG.md"

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
spec.files = Dir.chdir(__dir__) do
`git ls-files -z`.split("\x0").reject do |f|
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|circleci)|appveyor)})
end
end
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency "faraday", "~> 2.7.0"
end
11 changes: 11 additions & 0 deletions lib/cohere.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

require_relative "cohere/version"

module Cohere
class Error < StandardError; end
# Your code goes here...

autoload :Base, "cohere/base"
autoload :Client, "cohere/client"
end
11 changes: 11 additions & 0 deletions lib/cohere/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module Cohere
class Base
attr_reader :client

def initialize(client:)
@client = client
end
end
end
141 changes: 141 additions & 0 deletions lib/cohere/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# frozen_string_literal: true

require "faraday"
require "graphlient"

module Weaviate
class Client
attr_reader :api_key

ENDPOINT_URL = "https://api.cohere.ai/v1"

def initialize(api_key:)
@api_key = api_key
end

# This endpoint generates realistic text conditioned on a given input.
def generate(
prompt:,
model: nil,
num_generations: nil,
max_tokens: nil,
preset: nil,
temperature: nil,
k: nil,
p: nil,
frequency_penalty: nil,
presence_penalty: nil,
end_sequences: nil,
stop_sequence: nil,
return_likelihoods: nil,
logit_bias: nil,
truncate: nil
)
response = connection.post('generate') do |req|
req.body = { prompt: prompt }
req.body[:model] = model if model
req.body[:num_generations] = num_generations if num_generations
req.body[:max_tokens] = max_tokens if max_tokens
req.body[:preset] = preset if preset
req.body[:temperature] = temperature if temperature
req.body[:k] = k if k
req.body[:p] = p if p
req.body[:frequency_penalty] = frequency_penalty if frequency_penalty
req.body[:presence_penalty] = presence_penalty if presence_penalty
req.body[:end_sequences] = end_sequences if end_sequences
req.body[:stop_sequence] = stop_sequence if stop_sequence
req.body[:return_likelihoods] = return_likelihoods if return_likelihoods
req.body[:logit_bias] = logit_bias if logit_bias
req.body[:truncate] = truncate if truncate
end
response.body
end

def embed(
texts:,
model: nil,
truncate: nil
)
response = connection.post('embed') do |req|
req.body = { texts: texts }
req.body[:model] = model if model
req.body[:truncate] = truncate if truncate
end
response.body
end

def classify(
inputs:,
examples:,
model: nil,
present: nil,
truncate: nil
)
response = connection.post('classify') do |req|
req.body = {
inputs: inputs,
examples: examples
}
req.body[:model] = model if model
req.body[:present] = present if present
req.body[:truncate] = truncate if truncate
end
response.body
end

def tokenize(text:)
response = connection.post('tokenize') do |req|
req.body = { text: text }
end
response.body
end

def detokenize(tokens:)
response = connection.post('detokenize') do |req|
req.body = { tokens: tokens }
end
response.body
end

def detect_language(texts:)
response = connection.post('detect-language') do |req|
req.body = { texts: texts }
end
response.body
end
end

def summarize(
text:,
length: nil,
format: nil,
model: nil,
extractiveness: nil,
temperature: nil,
additional_command: nil
)
response = connection.post('summarize') do |req|
req.body = { text: text }
req.body[:length] = length if length
req.body[:format] = format if format
req.body[:model] = model if model
req.body[:extractiveness] = extractiveness if extractiveness
req.body[:temperature] = temperature if temperature
req.body[:additional_command] = additional_command if additional_command
end
end

private

def connection
@connection ||= Faraday.new(url: ENDPOINT_URL) do |faraday|
if api_key
faraday.request :authorization, :Bearer, api_key
end
faraday.request :json
faraday.response :json, content_type: /\bjson$/
faraday.adapter Faraday.default_adapter
end
end
end
end
5 changes: 5 additions & 0 deletions lib/cohere/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

module Cohere
VERSION = "0.1.0"
end
4 changes: 4 additions & 0 deletions sig/cohere.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Cohere
VERSION: String
# See the writing guide of rbs: https://github.com/ruby/rbs#guides
end
11 changes: 11 additions & 0 deletions spec/cohere_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

RSpec.describe Cohere do
it "has a version number" do
expect(Cohere::VERSION).not_to be nil
end

it "does something useful" do
expect(false).to eq(true)
end
end
Loading

0 comments on commit 7ada4b1

Please sign in to comment.