Skip to content

Commit 674d1f4

Browse files
committed
Allow custom Faraday adapter
Add an `adapter:` keyword argument to `Cohere::Client.new`, defaulting to `Faraday.default_adapter`. This lets callers opt into persistent adapters (e.g., `:net_http_persistent`) to amortize TCP/TLS handshakes across requests and reduce tail latency under load.
1 parent 43b9e8f commit 674d1f4

4 files changed

Lines changed: 48 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
## [Unreleased]
2+
- 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.
23

34
## [1.0.1] - 2024-11-22
45

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,30 @@ client = Cohere::Client.new(
3838
)
3939
```
4040

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+
4165
### Generate
4266

4367
```ruby

lib/cohere/client.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
module Cohere
66
class Client
7-
attr_reader :api_key, :connection
7+
attr_reader :api_key, :connection, :adapter
88

9-
def initialize(api_key:, timeout: nil)
9+
def initialize(api_key:, timeout: nil, adapter: Faraday.default_adapter)
1010
@api_key = api_key
1111
@timeout = timeout
12+
@adapter = adapter
1213
end
1314

1415
# Generates a text response to a user message and streams it down, token by token
@@ -215,7 +216,7 @@ def v1_connection
215216
faraday.request :authorization, :Bearer, api_key
216217
faraday.request :json
217218
faraday.response :json, content_type: /\bjson$/
218-
faraday.adapter Faraday.default_adapter
219+
faraday.adapter(*Array(@adapter))
219220
end
220221
end
221222

@@ -224,7 +225,7 @@ def v2_connection
224225
faraday.request :authorization, :Bearer, api_key
225226
faraday.request :json
226227
faraday.response :json, content_type: /\bjson$/
227-
faraday.adapter Faraday.default_adapter
228+
faraday.adapter(*Array(@adapter))
228229
end
229230
end
230231
end

spec/cohere/client_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,24 @@
55
RSpec.describe Cohere::Client do
66
subject { described_class.new(api_key: "123") }
77

8+
describe "#initialize" do
9+
it "defaults to Faraday.default_adapter" do
10+
client = described_class.new(api_key: "x")
11+
expect(client.adapter).to eq(Faraday.default_adapter)
12+
end
13+
14+
it "accepts a custom adapter" do
15+
client = described_class.new(api_key: "x", adapter: :test)
16+
expect(client.adapter).to eq(:test)
17+
end
18+
19+
it "accepts an adapter with options" do
20+
adapter = [:net_http_persistent, {name: "cohere", pool_size: 10}]
21+
client = described_class.new(api_key: "x", adapter: adapter)
22+
expect(client.adapter).to eq(adapter)
23+
end
24+
end
25+
826
describe "#generate" do
927
let(:generate_result) { JSON.parse(File.read("spec/fixtures/generate.json")) }
1028
let(:response) { OpenStruct.new(body: generate_result) }

0 commit comments

Comments
 (0)