File tree Expand file tree Collapse file tree 5 files changed +35
-7
lines changed Expand file tree Collapse file tree 5 files changed +35
-7
lines changed Original file line number Diff line number Diff line change @@ -108,6 +108,16 @@ OpenAI.configure do |config|
108108end
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
113123To use the [ Azure OpenAI Service] ( https://learn.microsoft.com/en-us/azure/cognitive-services/openai/ ) API, you can configure the gem like this:
Original file line number Diff line number Diff 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 : { } )
Original file line number Diff line number Diff 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 :)
Original file line number Diff line number Diff line change 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 ,
Original file line number Diff line number Diff line change 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
122136end
You can’t perform that action at this time.
0 commit comments