cohere-ruby 0.9.11 → 1.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5f1c3798500d154b882bd6bb2e0e43904fea4bf69db4d982da4abc6265bc461c
4
- data.tar.gz: d91ed933c24f2dacc5f2604639d307fd389f3774aaed8082df07e03ac2961704
3
+ metadata.gz: e2b7ec81e4c8f12d8f037913efd5ae99c72060eda6ae483c97f1dbe5d64fe153
4
+ data.tar.gz: e5f246ade127deb9802287b73525bc4ecaea9795ee8a9477753413132bf2e9bd
5
5
  SHA512:
6
- metadata.gz: b989c60be670a143848367a7be84f53fc6adf97f31e1f3739113a7a7caec988450e4e889bcbc7f2733783623977dc4f6e1eb7484470259935e30e7dc6d5d1414
7
- data.tar.gz: 00a2e3e1a19bfb1980d06ebc1fd6117262bcdf226bc95bfe0edff8ea8e1397791eaf7ef37b78ad158e0ef5a8363fa809c9215fc325d771e72abf22d7919c54c0
6
+ metadata.gz: 351b92766fe85087c7efeb2e01bcef4972575467b4bfb4c3b9611b5b6ccf3c4f280a9fc0560b3f8ff9fd7ea1219e1ee04fa6c43ba8386cb860b23e6d8677c453
7
+ data.tar.gz: 499e3b50697dfd688fa8416202cad430b42fb044f3d19418a83ff63731e4fad8edac596c774039893d2fbce34120fdd9ac9b95a4f023517d0b32cf1e5e9e9ddd
data/.env.example ADDED
@@ -0,0 +1 @@
1
+ COHERE_API_KEY=
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [1.0.2] - 2026-04-27
4
+ - Add `adapter:` keyword argument to `Cohere::Client.new` for configuring the Faraday HTTP adapter (defaults to `Faraday.default_adapter`). Enables using persistent adapters such as `:net_http_persistent` to amortize TCP/TLS handshakes across requests.
5
+
6
+ ## [1.0.1] - 2024-11-22
7
+
8
+ ## [1.0.0] - 2024-11-22
9
+ - Migrate to v2 APIs
10
+
3
11
  ## [0.9.11] - 2024-08-01
4
12
  - New `rerank()` method
5
13
 
data/Gemfile CHANGED
@@ -9,3 +9,4 @@ gem "rake", "~> 13.0"
9
9
 
10
10
  gem "rspec", "~> 3.0"
11
11
  gem "standard", "~> 1.28.0"
12
+ gem "dotenv", "~> 2.8.1"
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cohere-ruby (0.9.11)
4
+ cohere-ruby (1.0.2)
5
5
  faraday (>= 2.0.1, < 3.0)
6
6
 
7
7
  GEM
@@ -9,6 +9,7 @@ GEM
9
9
  specs:
10
10
  ast (2.4.2)
11
11
  diff-lcs (1.5.0)
12
+ dotenv (2.8.1)
12
13
  faraday (2.7.10)
13
14
  faraday-net_http (>= 2.0, < 3.1)
14
15
  ruby2_keywords (>= 0.0.4)
@@ -68,12 +69,14 @@ GEM
68
69
 
69
70
  PLATFORMS
70
71
  arm64-darwin-23
72
+ arm64-darwin-24
71
73
  x86_64-darwin-19
72
74
  x86_64-darwin-21
73
75
  x86_64-linux
74
76
 
75
77
  DEPENDENCIES
76
78
  cohere-ruby!
79
+ dotenv (~> 2.8.1)
77
80
  rake (~> 13.0)
78
81
  rspec (~> 3.0)
79
82
  standard (~> 1.28.0)
data/README.md CHANGED
@@ -8,12 +8,12 @@
8
8
 
9
9
  Cohere API client for Ruby.
10
10
 
11
- Part of the [Langchain.rb](https://github.com/andreibondarev/langchainrb) stack.
11
+ Part of the [Langchain.rb](https://github.com/patterns-ai-core/langchainrb) stack.
12
12
 
13
- ![Tests status](https://github.com/andreibondarev/cohere-ruby/actions/workflows/ci.yml/badge.svg)
13
+ ![Tests status](https://github.com/patterns-ai-core/cohere-ruby/actions/workflows/ci.yml/badge.svg)
14
14
  [![Gem Version](https://badge.fury.io/rb/cohere-ruby.svg)](https://badge.fury.io/rb/cohere-ruby)
15
15
  [![Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://rubydoc.info/gems/cohere-ruby)
16
- [![License](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/andreibondarev/cohere-ruby/blob/main/LICENSE.txt)
16
+ [![License](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/patterns-ai-core/cohere-ruby/blob/main/LICENSE.txt)
17
17
  [![](https://dcbadge.vercel.app/api/server/WDARp7J2n8?compact=true&style=flat)](https://discord.gg/WDARp7J2n8)
18
18
 
19
19
  ## Installation
@@ -38,6 +38,30 @@ client = Cohere::Client.new(
38
38
  )
39
39
  ```
40
40
 
41
+ #### Custom HTTP adapter
42
+
43
+ By default the client uses `Faraday.default_adapter` (Net::HTTP), which opens a
44
+ new TCP/TLS connection per request. For high-throughput use cases, pass a
45
+ persistent adapter to reuse connections across requests:
46
+
47
+ ```ruby
48
+ require "faraday/net_http_persistent"
49
+
50
+ client = Cohere::Client.new(
51
+ api_key: ENV['COHERE_API_KEY'],
52
+ adapter: :net_http_persistent
53
+ )
54
+ ```
55
+
56
+ You can also pass adapter options:
57
+
58
+ ```ruby
59
+ client = Cohere::Client.new(
60
+ api_key: ENV['COHERE_API_KEY'],
61
+ adapter: [:net_http_persistent, {name: "cohere", pool_size: 10}]
62
+ )
63
+ ```
64
+
41
65
  ### Generate
42
66
 
43
67
  ```ruby
@@ -50,14 +74,18 @@ client.generate(
50
74
 
51
75
  ```ruby
52
76
  client.chat(
53
- message: "Hey! How are you?"
77
+ model: "command-r-plus-08-2024",
78
+ messages: [{role:"user", content: "Hey! How are you?"}]
54
79
  )
55
80
  ```
56
81
 
57
82
  `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.
58
83
 
59
84
  ```ruby
60
- client.chat(message: "Hey! How are you?", stream: true) do |chunk, overall_received_bytes|
85
+ client.chat(
86
+ model: "command-r-plus-08-2024",
87
+ messages: [{role:"user", content: "Hey! How are you?"}]
88
+ ) do |chunk, overall_received_bytes|
61
89
  puts "Received #{overall_received_bytes} bytes: #{chunk.force_encoding(Encoding::UTF_8)}"
62
90
  end
63
91
  ```
@@ -68,25 +96,25 @@ end
68
96
 
69
97
  ```ruby
70
98
  tools = [
71
- {
72
- name: "query_daily_sales_report",
73
- description: "Connects to a database to retrieve overall sales volumes and sales information for a given day.",
74
- parameter_definitions: {
75
- day: {
76
- description: "Retrieves sales data for this day, formatted as YYYY-MM-DD.",
77
- type: "str",
78
- required: true
79
- }
80
- }
81
- }
99
+ {
100
+ name: "query_daily_sales_report",
101
+ description: "Connects to a database to retrieve overall sales volumes and sales information for a given day.",
102
+ parameter_definitions: {
103
+ day: {
104
+ description: "Retrieves sales data for this day, formatted as YYYY-MM-DD.",
105
+ type: "str",
106
+ required: true
107
+ }
108
+ }
109
+ }
82
110
  ]
83
111
 
84
112
  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?"
85
113
 
86
114
  client.chat(
87
115
  model: model,
88
- message: message,
89
- tools: tools,
116
+ messages: [{ role:"user", content: message }],
117
+ tools: tools
90
118
  )
91
119
  ```
92
120
 
@@ -94,7 +122,10 @@ client.chat(
94
122
 
95
123
  ```ruby
96
124
  client.embed(
97
- texts: ["hello!"]
125
+ model: "embed-english-v3.0",
126
+ texts: ["hello", "goodbye"],
127
+ input_type: "classification",
128
+ embedding_types: ["float"]
98
129
  )
99
130
  ```
100
131
 
@@ -110,11 +141,12 @@ docs = [
110
141
  ]
111
142
 
112
143
  client.rerank(
113
- texts: ["hello!"]
144
+ model: "rerank-english-v3.0",
145
+ query: "What is the capital of the United States?",
146
+ documents: docs
114
147
  )
115
148
  ```
116
149
 
117
-
118
150
  ### Classify
119
151
 
120
152
  ```ruby
@@ -137,8 +169,9 @@ inputs = [
137
169
  ]
138
170
 
139
171
  client.classify(
140
- examples: examples,
141
- inputs: inputs
172
+ model: "embed-multilingual-v2.0",
173
+ inputs: inputs,
174
+ examples: examples
142
175
  )
143
176
  ```
144
177
 
@@ -146,7 +179,8 @@ client.classify(
146
179
 
147
180
  ```ruby
148
181
  client.tokenize(
149
- text: "hello world!"
182
+ model: "command-r-plus-08-2024",
183
+ text: "Hello, world!"
150
184
  )
151
185
  ```
152
186
 
@@ -154,7 +188,8 @@ client.tokenize(
154
188
 
155
189
  ```ruby
156
190
  client.detokenize(
157
- tokens: [33555, 1114 , 34]
191
+ model: "command-r-plus-08-2024",
192
+ tokens: [33555, 1114, 34]
158
193
  )
159
194
  ```
160
195
 
data/lib/cohere/client.rb CHANGED
@@ -4,64 +4,59 @@ require "faraday"
4
4
 
5
5
  module Cohere
6
6
  class Client
7
- attr_reader :api_key, :connection
7
+ attr_reader :api_key, :connection, :adapter
8
8
 
9
- ENDPOINT_URL = "https://api.cohere.ai/v1"
10
-
11
- def initialize(api_key:, timeout: nil)
9
+ def initialize(api_key:, timeout: nil, adapter: Faraday.default_adapter)
12
10
  @api_key = api_key
13
11
  @timeout = timeout
12
+ @adapter = adapter
14
13
  end
15
14
 
15
+ # Generates a text response to a user message and streams it down, token by token
16
16
  def chat(
17
- message: nil,
18
- model: nil,
17
+ model:,
18
+ messages:,
19
19
  stream: false,
20
- preamble: nil,
21
- preamble_override: nil,
22
- chat_history: [],
23
- conversation_id: nil,
24
- prompt_truncation: nil,
25
- connectors: [],
26
- search_queries_only: false,
20
+ tools: [],
27
21
  documents: [],
28
- citation_quality: nil,
29
- temperature: nil,
22
+ citation_options: nil,
23
+ response_format: nil,
24
+ safety_mode: nil,
30
25
  max_tokens: nil,
31
- k: nil,
32
- p: nil,
26
+ stop_sequences: nil,
27
+ temperature: nil,
33
28
  seed: nil,
34
29
  frequency_penalty: nil,
35
30
  presence_penalty: nil,
36
- tools: [],
31
+ k: nil,
32
+ p: nil,
33
+ logprops: nil,
37
34
  &block
38
35
  )
39
- response = connection.post("chat") do |req|
36
+ response = v2_connection.post("chat") do |req|
40
37
  req.body = {}
41
38
 
42
- req.body[:message] = message if message
43
- req.body[:model] = model if model
44
- if stream || block
45
- req.body[:stream] = true
46
- req.options.on_data = block if block
47
- end
48
- req.body[:preamble] = preamble if preamble
49
- req.body[:preamble_override] = preamble_override if preamble_override
50
- req.body[:chat_history] = chat_history if chat_history
51
- req.body[:conversation_id] = conversation_id if conversation_id
52
- req.body[:prompt_truncation] = prompt_truncation if prompt_truncation
53
- req.body[:connectors] = connectors if connectors
54
- req.body[:search_queries_only] = search_queries_only if search_queries_only
55
- req.body[:documents] = documents if documents
56
- req.body[:citation_quality] = citation_quality if citation_quality
57
- req.body[:temperature] = temperature if temperature
39
+ req.body[:model] = model
40
+ req.body[:messages] = messages if messages
41
+ req.body[:tools] = tools if tools.any?
42
+ req.body[:documents] = documents if documents.any?
43
+ req.body[:citation_options] = citation_options if citation_options
44
+ req.body[:response_format] = response_format if response_format
45
+ req.body[:safety_mode] = safety_mode if safety_mode
58
46
  req.body[:max_tokens] = max_tokens if max_tokens
59
- req.body[:k] = k if k
60
- req.body[:p] = p if p
47
+ req.body[:stop_sequences] = stop_sequences if stop_sequences
48
+ req.body[:temperature] = temperature if temperature
61
49
  req.body[:seed] = seed if seed
62
50
  req.body[:frequency_penalty] = frequency_penalty if frequency_penalty
63
51
  req.body[:presence_penalty] = presence_penalty if presence_penalty
64
- req.body[:tools] = tools if tools
52
+ req.body[:k] = k if k
53
+ req.body[:p] = p if p
54
+ req.body[:logprops] = logprops if logprops
55
+
56
+ if stream || block
57
+ req.body[:stream] = true
58
+ req.options.on_data = block if block
59
+ end
65
60
  end
66
61
  response.body
67
62
  end
@@ -84,7 +79,7 @@ module Cohere
84
79
  logit_bias: nil,
85
80
  truncate: nil
86
81
  )
87
- response = connection.post("generate") do |req|
82
+ response = v1_connection.post("generate") do |req|
88
83
  req.body = {prompt: prompt}
89
84
  req.body[:model] = model if model
90
85
  req.body[:num_generations] = num_generations if num_generations
@@ -104,36 +99,44 @@ module Cohere
104
99
  response.body
105
100
  end
106
101
 
102
+ # This endpoint returns text embeddings. An embedding is a list of floating point numbers that captures semantic information about the text that it represents.
107
103
  def embed(
108
- texts:,
109
- model: nil,
110
- input_type: nil,
104
+ model:,
105
+ input_type:,
106
+ embedding_types:,
107
+ texts: nil,
108
+ images: nil,
111
109
  truncate: nil
112
110
  )
113
- response = connection.post("embed") do |req|
114
- req.body = {texts: texts}
115
- req.body[:model] = model if model
116
- req.body[:input_type] = input_type if input_type
111
+ response = v2_connection.post("embed") do |req|
112
+ req.body = {
113
+ model: model,
114
+ input_type: input_type,
115
+ embedding_types: embedding_types
116
+ }
117
+ req.body[:texts] = texts if texts
118
+ req.body[:images] = images if images
117
119
  req.body[:truncate] = truncate if truncate
118
120
  end
119
121
  response.body
120
122
  end
121
123
 
124
+ # This endpoint takes in a query and a list of texts and produces an ordered array with each text assigned a relevance score.
122
125
  def rerank(
126
+ model:,
123
127
  query:,
124
128
  documents:,
125
- model: nil,
126
129
  top_n: nil,
127
130
  rank_fields: nil,
128
131
  return_documents: nil,
129
132
  max_chunks_per_doc: nil
130
133
  )
131
- response = connection.post("rerank") do |req|
134
+ response = v2_connection.post("rerank") do |req|
132
135
  req.body = {
136
+ model: model,
133
137
  query: query,
134
138
  documents: documents
135
139
  }
136
- req.body[:model] = model if model
137
140
  req.body[:top_n] = top_n if top_n
138
141
  req.body[:rank_fields] = rank_fields if rank_fields
139
142
  req.body[:return_documents] = return_documents if return_documents
@@ -142,41 +145,44 @@ module Cohere
142
145
  response.body
143
146
  end
144
147
 
148
+ # This endpoint makes a prediction about which label fits the specified text inputs best.
145
149
  def classify(
150
+ model:,
146
151
  inputs:,
147
- examples:,
148
- model: nil,
149
- present: nil,
152
+ examples: nil,
153
+ preset: nil,
150
154
  truncate: nil
151
155
  )
152
- response = connection.post("classify") do |req|
156
+ response = v1_connection.post("classify") do |req|
153
157
  req.body = {
154
- inputs: inputs,
155
- examples: examples
158
+ model: model,
159
+ inputs: inputs
156
160
  }
157
- req.body[:model] = model if model
158
- req.body[:present] = present if present
161
+ req.body[:examples] = examples if examples
162
+ req.body[:preset] = preset if preset
159
163
  req.body[:truncate] = truncate if truncate
160
164
  end
161
165
  response.body
162
166
  end
163
167
 
164
- def tokenize(text:, model: nil)
165
- response = connection.post("tokenize") do |req|
166
- req.body = model.nil? ? {text: text} : {text: text, model: model}
168
+ # This endpoint splits input text into smaller units called tokens using byte-pair encoding (BPE).
169
+ def tokenize(text:, model:)
170
+ response = v1_connection.post("tokenize") do |req|
171
+ req.body = {text: text, model: model}
167
172
  end
168
173
  response.body
169
174
  end
170
175
 
171
- def detokenize(tokens:, model: nil)
172
- response = connection.post("detokenize") do |req|
173
- req.body = model.nil? ? {tokens: tokens} : {tokens: tokens, model: model}
176
+ # This endpoint takes tokens using byte-pair encoding and returns their text representation.
177
+ def detokenize(tokens:, model:)
178
+ response = v1_connection.post("detokenize") do |req|
179
+ req.body = {tokens: tokens, model: model}
174
180
  end
175
181
  response.body
176
182
  end
177
183
 
178
184
  def detect_language(texts:)
179
- response = connection.post("detect-language") do |req|
185
+ response = v1_connection.post("detect-language") do |req|
180
186
  req.body = {texts: texts}
181
187
  end
182
188
  response.body
@@ -191,7 +197,7 @@ module Cohere
191
197
  temperature: nil,
192
198
  additional_command: nil
193
199
  )
194
- response = connection.post("summarize") do |req|
200
+ response = v1_connection.post("summarize") do |req|
195
201
  req.body = {text: text}
196
202
  req.body[:length] = length if length
197
203
  req.body[:format] = format if format
@@ -205,17 +211,22 @@ module Cohere
205
211
 
206
212
  private
207
213
 
208
- # standard:disable Lint/DuplicateMethods
209
- def connection
210
- @connection ||= Faraday.new(url: ENDPOINT_URL, request: {timeout: @timeout}) do |faraday|
211
- if api_key
212
- faraday.request :authorization, :Bearer, api_key
213
- end
214
+ def v1_connection
215
+ @connection ||= Faraday.new(url: "https://api.cohere.ai/v1", request: {timeout: @timeout}) do |faraday|
216
+ faraday.request :authorization, :Bearer, api_key
217
+ faraday.request :json
218
+ faraday.response :json, content_type: /\bjson$/
219
+ faraday.adapter(*Array(@adapter))
220
+ end
221
+ end
222
+
223
+ def v2_connection
224
+ @connection ||= Faraday.new(url: "https://api.cohere.com/v2", request: {timeout: @timeout}) do |faraday|
225
+ faraday.request :authorization, :Bearer, api_key
214
226
  faraday.request :json
215
227
  faraday.response :json, content_type: /\bjson$/
216
- faraday.adapter Faraday.default_adapter
228
+ faraday.adapter(*Array(@adapter))
217
229
  end
218
230
  end
219
- # standard:enable Lint/DuplicateMethods
220
231
  end
221
232
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Cohere
4
- VERSION = "0.9.11"
4
+ VERSION = "1.0.2"
5
5
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cohere-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.11
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrei Bondarev
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2024-08-01 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: faraday
@@ -37,6 +36,7 @@ executables: []
37
36
  extensions: []
38
37
  extra_rdoc_files: []
39
38
  files:
39
+ - ".env.example"
40
40
  - ".rspec"
41
41
  - CHANGELOG.md
42
42
  - Gemfile
@@ -55,7 +55,6 @@ metadata:
55
55
  homepage_uri: https://github.com/andreibondarev/cohere-ruby
56
56
  source_code_uri: https://github.com/andreibondarev/cohere-ruby
57
57
  changelog_uri: https://github.com/andreibondarev/cohere-ruby/CHANGELOG.md
58
- post_install_message:
59
58
  rdoc_options: []
60
59
  require_paths:
61
60
  - lib
@@ -70,8 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
69
  - !ruby/object:Gem::Version
71
70
  version: '0'
72
71
  requirements: []
73
- rubygems_version: 3.5.11
74
- signing_key:
72
+ rubygems_version: 3.6.9
75
73
  specification_version: 4
76
74
  summary: Cohere API client for Ruby.
77
75
  test_files: []