Cohere API client for Ruby.
Part of the Langchain.rb stack.
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
require "cohere"
client = Cohere::Client.new(
api_key: ENV['COHERE_API_KEY']
)
client.generate(
prompt: "Once upon a time in a magical land called"
)
client.chat(
message: "Hey! How are you?"
)
chat
supports a streaming option. You can pass a block to the chat
method and it will yield a new chunk as soon as it is received.
client.chat(message: "Hey! How are you?", stream: true) do |chunk, overall_received_bytes|
puts "Received #{overall_received_bytes} bytes: #{chunk.force_encoding(Encoding::UTF_8)}"
end
force_encoding
is preferred to avoid JSON parsing issue when Cohere returns emoticon.
chat
supports Tool use (function calling).
tools = [
{
name: "query_daily_sales_report",
description: "Connects to a database to retrieve overall sales volumes and sales information for a given day.",
parameter_definitions: {
day: {
description: "Retrieves sales data for this day, formatted as YYYY-MM-DD.",
type: "str",
required: true
}
}
}
]
message = "Can you provide a sales summary for 29th September 2023, and also give me some details about the products in the 'Electronics' category, for example their prices and stock levels?"
client.chat(
model: model,
message: message,
tools: tools,
)
client.embed(
texts: ["hello!"]
)
examples = [
{ text: "Dermatologists don't like her!", label: "Spam" },
{ text: "Hello, open to this?", label: "Spam" },
{ text: "I need help please wire me $1000 right now", label: "Spam" },
{ text: "Nice to know you ;)", label: "Spam" },
{ text: "Please help me?", label: "Spam" },
{ text: "Your parcel will be delivered today", label: "Not spam" },
{ text: "Review changes to our Terms and Conditions", label: "Not spam" },
{ text: "Weekly sync notes", label: "Not spam" },
{ text: "Re: Follow up from today's meeting", label: "Not spam" },
{ text: "Pre-read for tomorrow", label: "Not spam" }
]
inputs = [
"Confirm your email address",
"hey i need u to send some $",
]
client.classify(
examples: examples,
inputs: inputs
)
client.tokenize(
text: "hello world!"
)
client.detokenize(
tokens: [33555, 1114 , 34]
)
client.detect_language(
texts: ["Здравствуй, Мир"]
)
client.summarize(
text: "..."
)
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.
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.
Bug reports and pull requests are welcome on GitHub at https://github.com/andreibondarev/cohere.
The gem is available as open source under the terms of the MIT License.