Skip to content

v2 endpoint #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
COHERE_API_KEY=
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@

# rspec failure tracking
.rspec_status

.env
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## [Unreleased]
- Migrate to v2 APIs

## [0.9.11] - 2024-08-01
- New `rerank()` method
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ gem "rake", "~> 13.0"

gem "rspec", "~> 3.0"
gem "standard", "~> 1.28.0"
gem "dotenv", "~> 2.8.1"
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ GEM
specs:
ast (2.4.2)
diff-lcs (1.5.0)
dotenv (2.8.1)
faraday (2.7.10)
faraday-net_http (>= 2.0, < 3.1)
ruby2_keywords (>= 0.0.4)
Expand Down Expand Up @@ -74,6 +75,7 @@ PLATFORMS

DEPENDENCIES
cohere-ruby!
dotenv (~> 2.8.1)
rake (~> 13.0)
rspec (~> 3.0)
standard (~> 1.28.0)
Expand Down
57 changes: 34 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

Cohere API client for Ruby.

Part of the [Langchain.rb](https://github.com/andreibondarev/langchainrb) stack.
Part of the [Langchain.rb](https://github.com/patterns-ai-core/langchainrb) stack.

![Tests status](https://github.com/andreibondarev/cohere-ruby/actions/workflows/ci.yml/badge.svg)
![Tests status](https://github.com/patterns-ai-core/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)
[![Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://rubydoc.info/gems/cohere-ruby)
[![License](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/andreibondarev/cohere-ruby/blob/main/LICENSE.txt)
[![License](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/patterns-ai-core/cohere-ruby/blob/main/LICENSE.txt)
[![](https://dcbadge.vercel.app/api/server/WDARp7J2n8?compact=true&style=flat)](https://discord.gg/WDARp7J2n8)

## Installation
Expand Down Expand Up @@ -50,14 +50,18 @@ client.generate(

```ruby
client.chat(
message: "Hey! How are you?"
model: "command-r-plus-08-2024",
messages: [{role:"user", content: "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.

```ruby
client.chat(message: "Hey! How are you?", stream: true) do |chunk, overall_received_bytes|
client.chat(
model: "command-r-plus-08-2024",
messages: [{role:"user", content: "Hey! How are you?"}]
) do |chunk, overall_received_bytes|
puts "Received #{overall_received_bytes} bytes: #{chunk.force_encoding(Encoding::UTF_8)}"
end
```
Expand All @@ -68,33 +72,36 @@ end

```ruby
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
}
}
}
{
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,
messages: [{ role:"user", content: message }],
tools: tools
)
```

### Embed

```ruby
client.embed(
texts: ["hello!"]
model: "embed-english-v3.0",
texts: ["hello", "goodbye"],
input_type: "classification",
embedding_types: ["float"]
)
```

Expand All @@ -110,6 +117,7 @@ docs = [
]

client.rerank(
model: "rerank-english-v3.0",
query: "What is the capital of the United States?",
documents: docs
)
Expand Down Expand Up @@ -137,24 +145,27 @@ inputs = [
]

client.classify(
examples: examples,
inputs: inputs
model: "embed-multilingual-v2.0",
inputs: inputs,
examples: examples
)
```

### Tokenize

```ruby
client.tokenize(
text: "hello world!"
model: "command-r-plus-08-2024",
text: "Hello, world!"
)
```

### Detokenize

```ruby
client.detokenize(
tokens: [33555, 1114 , 34]
model: "command-r-plus-08-2024",
tokens: [33555, 1114, 34]
)
```

Expand Down
8 changes: 1 addition & 7 deletions bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@

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
require "dotenv/load"

client = Cohere::Client.new(
api_key: ENV['COHERE_API_KEY']
Expand Down
Loading