Skip to content

Commit 3c8eaaa

Browse files
authored
Merge pull request #406 from lalunamel/main
Add log_errors option to enable/disable logging of errors.
2 parents fbaf16a + d0ec994 commit 3c8eaaa

File tree

5 files changed

+87
-6
lines changed

5 files changed

+87
-6
lines changed

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ Stream text with GPT-4, transcribe and translate audio with Whisper, or create i
2222
- [With Config](#with-config)
2323
- [Custom timeout or base URI](#custom-timeout-or-base-uri)
2424
- [Extra Headers per Client](#extra-headers-per-client)
25-
- [Verbose Logging](#verbose-logging)
25+
- [Logging](#logging)
26+
- [Errors](#errors)
27+
- [Faraday middleware](#faraday-middleware)
2628
- [Azure](#azure)
2729
- [Ollama](#ollama)
2830
- [Counting Tokens](#counting-tokens)
@@ -148,6 +150,7 @@ or when configuring the gem:
148150
```ruby
149151
OpenAI.configure do |config|
150152
config.access_token = ENV.fetch("OPENAI_ACCESS_TOKEN")
153+
config.log_errors = true # Optional
151154
config.organization_id = ENV.fetch("OPENAI_ORGANIZATION_ID") # Optional
152155
config.uri_base = "https://oai.hconeai.com/" # Optional
153156
config.request_timeout = 240 # Optional
@@ -168,7 +171,19 @@ client = OpenAI::Client.new(access_token: "access_token_goes_here")
168171
client.add_headers("X-Proxy-TTL" => "43200")
169172
```
170173

171-
#### Verbose Logging
174+
#### Logging
175+
176+
##### Errors
177+
178+
By default, `ruby-openai` does not log any `Faraday::Error`s encountered while executing a network request to avoid leaking data (e.g. 400s, 500s, SSL errors and more - see [here](https://www.rubydoc.info/github/lostisland/faraday/Faraday/Error) for a complete list of subclasses of `Faraday::Error` and what can cause them).
179+
180+
If you would like to enable this functionality, you can set `log_errors` to `true` when configuring the client:
181+
182+
```ruby
183+
client = OpenAI::Client.new(log_errors: true)
184+
```
185+
186+
##### Faraday middleware
172187

173188
You can pass [Faraday middleware](https://lostisland.github.io/faraday/#/middleware/index) to the client in a block, eg. to enable verbose logging with Ruby's [Logger](https://ruby-doc.org/3.2.2/stdlibs/logger/Logger.html):
174189

lib/openai.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,25 @@ def call(env)
3737
end
3838

3939
class Configuration
40-
attr_accessor :access_token, :api_type, :api_version, :organization_id,
41-
:uri_base, :request_timeout, :extra_headers
40+
attr_accessor :access_token,
41+
:api_type,
42+
:api_version,
43+
:log_errors,
44+
:organization_id,
45+
:uri_base,
46+
:request_timeout,
47+
:extra_headers
4248

4349
DEFAULT_API_VERSION = "v1".freeze
4450
DEFAULT_URI_BASE = "https://api.openai.com/".freeze
4551
DEFAULT_REQUEST_TIMEOUT = 120
52+
DEFAULT_LOG_ERRORS = false
4653

4754
def initialize
4855
@access_token = nil
4956
@api_type = nil
5057
@api_version = DEFAULT_API_VERSION
58+
@log_errors = DEFAULT_LOG_ERRORS
5159
@organization_id = nil
5260
@uri_base = DEFAULT_URI_BASE
5361
@request_timeout = DEFAULT_REQUEST_TIMEOUT

lib/openai/client.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Client
66
api_type
77
api_version
88
access_token
9+
log_errors
910
organization_id
1011
uri_base
1112
request_timeout
@@ -17,7 +18,10 @@ def initialize(config = {}, &faraday_middleware)
1718
CONFIG_KEYS.each do |key|
1819
# Set instance variables like api_type & access_token. Fall back to global config
1920
# if not present.
20-
instance_variable_set("@#{key}", config[key] || OpenAI.configuration.send(key))
21+
instance_variable_set(
22+
"@#{key}",
23+
config[key].nil? ? OpenAI.configuration.send(key) : config[key]
24+
)
2125
end
2226
@faraday_middleware = faraday_middleware
2327
end

lib/openai/http.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def conn(multipart: false)
7474
connection = Faraday.new do |f|
7575
f.options[:timeout] = @request_timeout
7676
f.request(:multipart) if multipart
77-
f.use MiddlewareErrors
77+
f.use MiddlewareErrors if @log_errors
7878
f.response :raise_error
7979
f.response :json
8080
end

spec/openai/client/http_spec.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,58 @@
271271
}
272272
end
273273
end
274+
275+
describe "logging errors" do
276+
let(:cassette) { "http get with error response".downcase }
277+
278+
before do
279+
@original_stdout = $stdout
280+
$stdout = StringIO.new
281+
end
282+
283+
after do
284+
$stdout = @original_stdout
285+
end
286+
287+
it "is disabled by default" do
288+
VCR.use_cassette(cassette, record: :none) do
289+
expect { OpenAI::Client.new.models.retrieve(id: "text-ada-001") }
290+
.to raise_error Faraday::Error
291+
292+
$stdout.rewind
293+
captured_stdout = $stdout.string
294+
expect(captured_stdout).not_to include("OpenAI HTTP Error")
295+
end
296+
end
297+
298+
describe "when log_errors is set to true" do
299+
let(:log_errors) { true }
300+
301+
it "logs errors" do
302+
VCR.use_cassette(cassette, record: :none) do
303+
expect { OpenAI::Client.new(log_errors: log_errors).models.retrieve(id: "text-ada-001") }
304+
.to raise_error Faraday::Error
305+
306+
$stdout.rewind
307+
captured_stdout = $stdout.string
308+
expect(captured_stdout).to include("OpenAI HTTP Error")
309+
end
310+
end
311+
end
312+
313+
describe "when log_errors is set to false" do
314+
let(:log_errors) { false }
315+
316+
it "does not log errors" do
317+
VCR.use_cassette(cassette, record: :none) do
318+
expect { OpenAI::Client.new(log_errors: log_errors).models.retrieve(id: "text-ada-001") }
319+
.to raise_error Faraday::Error
320+
321+
$stdout.rewind
322+
captured_stdout = $stdout.string
323+
expect(captured_stdout).not_to include("OpenAI HTTP Error")
324+
end
325+
end
326+
end
327+
end
274328
end

0 commit comments

Comments
 (0)