Skip to content

Commit d25d085

Browse files
authored
Merge pull request #373 from alexrudall/hidden-error
Add ability to pass Faraday config block to Client
2 parents 63381af + cd9743a commit d25d085

File tree

6 files changed

+239
-3
lines changed

6 files changed

+239
-3
lines changed

README.md

+10
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 middleware](https://lostisland.github.io/faraday/#/middleware/index) to the client in a block, eg. to enable verbose logging:
114+
115+
```ruby
116+
client = OpenAI::Client.new do |f|
117+
f.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

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ class Client
1111
request_timeout
1212
extra_headers
1313
].freeze
14-
attr_reader *CONFIG_KEYS
14+
attr_reader *CONFIG_KEYS, :faraday_middleware
1515

16-
def initialize(config = {})
16+
def initialize(config = {}, &faraday_middleware)
1717
CONFIG_KEYS.each do |key|
1818
# Set instance variables like api_type & access_token. Fall back to global config
1919
# if not present.
2020
instance_variable_set("@#{key}", config[key] || OpenAI.configuration.send(key))
2121
end
22+
@faraday_middleware = faraday_middleware
2223
end
2324

2425
def chat(parameters: {})

lib/openai/http.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,16 @@ 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+
@faraday_middleware&.call(connection)
82+
83+
connection
8084
end
8185

8286
def uri(path:)

spec/fixtures/cassettes/gpt-3_5-turbo_function_call_chat.yml

+165
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/openai/client/chat_spec.rb

+42
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
parameters: {
99
model: model,
1010
messages: messages,
11+
functions: functions,
1112
stream: stream
1213
}
1314
)
1415
end
16+
let(:functions) { [] }
1517
let(:content) { response.dig("choices", 0, "message", "content") }
1618
let(:cassette) { "#{model} #{'streamed' if stream} chat".downcase }
1719

@@ -24,6 +26,46 @@
2426
end
2527
end
2628

29+
context "with an invalid function call" do
30+
let(:cassette) { "#{model} function call chat".downcase }
31+
let(:messages) do
32+
[
33+
{
34+
"role" => "function",
35+
# "name" => "function",
36+
"content" => "function"
37+
}
38+
]
39+
end
40+
let(:functions) do
41+
[
42+
{
43+
"name" => "function",
44+
"description" => "function",
45+
"parameters" =>
46+
{
47+
"type" => "object",
48+
"properties" => {
49+
"user" => {
50+
"type" => "string",
51+
"description" => "the full name of the user"
52+
}
53+
}
54+
}
55+
}
56+
]
57+
end
58+
59+
it "raises an error containing the reason" do
60+
VCR.use_cassette(cassette) do
61+
response
62+
rescue Faraday::Error => e
63+
expect(e.response.dig(:body, "error",
64+
"message")).to include("Missing parameter 'name'")
65+
end
66+
end
67+
end
68+
2769
describe "streaming" do
2870
let(:chunks) { [] }
2971
let(:stream) do

spec/openai/client/client_spec.rb

+14
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_middleware.call(connection)
133+
expect(connection.builder.handlers).to include Faraday::Response::Logger
134+
end
135+
end
122136
end

0 commit comments

Comments
 (0)