File tree Expand file tree Collapse file tree 2 files changed +30
-16
lines changed Expand file tree Collapse file tree 2 files changed +30
-16
lines changed Original file line number Diff line number Diff line change @@ -10,15 +10,7 @@ def get(path:)
1010
1111 def json_post ( path :, parameters :)
1212 to_json ( conn . post ( uri ( path : path ) ) do |req |
13- if parameters [ :stream ] . respond_to? ( :call )
14- req . options . on_data = to_json_stream ( user_proc : parameters [ :stream ] )
15- parameters [ :stream ] = true # Necessary to tell OpenAI to stream.
16- elsif parameters [ :stream ]
17- raise ArgumentError , "The stream parameter must be a Proc or have a #call method"
18- end
19-
20- req . headers = headers
21- req . body = parameters . to_json
13+ configure_json_post_request ( req , parameters )
2214 end &.body )
2315 end
2416
@@ -117,5 +109,19 @@ def multipart_parameters(parameters)
117109 Faraday ::UploadIO . new ( value , "" , value . path )
118110 end
119111 end
112+
113+ def configure_json_post_request ( req , parameters )
114+ req_parameters = parameters . dup
115+
116+ if parameters [ :stream ] . respond_to? ( :call )
117+ req . options . on_data = to_json_stream ( user_proc : parameters [ :stream ] )
118+ req_parameters [ :stream ] = true # Necessary to tell OpenAI to stream.
119+ elsif parameters [ :stream ]
120+ raise ArgumentError , "The stream parameter must be a Proc or have a #call method"
121+ end
122+
123+ req . headers = headers
124+ req . body = req_parameters . to_json
125+ end
120126 end
121127end
Original file line number Diff line number Diff line change 3131
3232 describe ".json_post" do
3333 let ( :response ) do
34- OpenAI ::Client . new . chat (
35- parameters : {
36- model : "gpt-3.5-turbo" ,
37- messages : [ { role : "user" , content : "Hello!" } ] ,
38- stream : stream
39- }
40- )
34+ OpenAI ::Client . new . chat ( parameters : parameters )
35+ end
36+
37+ let ( :parameters ) do
38+ {
39+ model : "gpt-3.5-turbo" ,
40+ messages : [ { role : "user" , content : "Hello!" } ] ,
41+ stream : stream
42+ }
4143 end
4244
4345 context "not streaming" do
6365 expect ( timeout_errors ) . to include ( error . class )
6466 end
6567 end
68+
69+ it "doesn't change the parameters stream proc" do
70+ expect { response } . to raise_error ( Faraday ::ConnectionFailed )
71+
72+ expect ( parameters [ :stream ] ) . to eq ( stream )
73+ end
6674 end
6775 end
6876
You can’t perform that action at this time.
0 commit comments