Skip to content

Commit 8c10996

Browse files
committed
Add ability to pass Faraday config block to client
1 parent 832c81b commit 8c10996

File tree

5 files changed

+35
-7
lines changed

5 files changed

+35
-7
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ OpenAI.configure do |config|
108108
end
109109
```
110110

111+
#### Verbose Logging
112+
113+
You can pass Faraday connection options to the client in a block, eg. to enable verbose logging:
114+
115+
```ruby
116+
client = OpenAI::Client.new do |client|
117+
client.response :logger, ::Logger.new(STDOUT), bodies: true
118+
end
119+
```
120+
111121
#### Azure
112122

113123
To use the [Azure OpenAI Service](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/) API, you can configure the gem like this:

lib/openai/client.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ class Client
1212
extra_headers
1313
].freeze
1414
attr_reader *CONFIG_KEYS
15+
attr_reader :faraday_config
1516

16-
def initialize(config = {})
17+
def initialize(config = {}, &faraday_config)
1718
CONFIG_KEYS.each do |key|
1819
# Set instance variables like api_type & access_token. Fall back to global config
1920
# if not present.
2021
instance_variable_set("@#{key}", config[key] || OpenAI.configuration.send(key))
2122
end
23+
@faraday_config = faraday_config
2224
end
2325

2426
def chat(parameters: {})

lib/openai/http.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,18 @@ def to_json_stream(user_proc:)
7171
end
7272

7373
def conn(multipart: false)
74-
Faraday.new do |f|
74+
connection = Faraday.new do |f|
7575
f.options[:timeout] = @request_timeout
7676
f.request(:multipart) if multipart
7777
f.response :raise_error
7878
f.response :json
7979
end
80+
81+
if @faraday_config
82+
@faraday_config.call(connection)
83+
end
84+
85+
connection
8086
end
8187

8288
def uri(path:)

spec/openai/client/chat_spec.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@
44
let(:messages) { [{ role: "user", content: "Hello!" }] }
55
let(:stream) { false }
66
let(:response) do
7-
client = OpenAI::Client.new do |client|
8-
client.response :logger, ::Logger.new(STDOUT), bodies: true
9-
end
10-
11-
client.chat(
7+
OpenAI::Client.new.chat(
128
parameters: {
139
model: model,
1410
messages: messages,

spec/openai/client/client_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,18 @@
119119
expect(client.send(:headers)["OpenAI-Beta"]).to eq "assistants=v1"
120120
end
121121
end
122+
123+
context "with a block" do
124+
let(:client) do
125+
OpenAI::Client.new do |client|
126+
client.response :logger, ::Logger.new(STDOUT), bodies: true
127+
end
128+
end
129+
130+
it "sets the logger" do
131+
connection = Faraday.new
132+
client.faraday_config.call(connection)
133+
expect(connection.builder.handlers).to include Faraday::Response::Logger
134+
end
135+
end
122136
end

0 commit comments

Comments
 (0)