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:)
10
10
11
11
def json_post ( path :, parameters :)
12
12
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 )
22
14
end &.body )
23
15
end
24
16
@@ -123,5 +115,19 @@ def multipart_parameters(parameters)
123
115
Faraday ::UploadIO . new ( value , "" , value . path )
124
116
end
125
117
end
118
+
119
+ def configure_json_post_request ( req , parameters )
120
+ req_parameters = parameters . dup
121
+
122
+ if parameters [ :stream ] . respond_to? ( :call )
123
+ req . options . on_data = to_json_stream ( user_proc : parameters [ :stream ] )
124
+ req_parameters [ :stream ] = true # Necessary to tell OpenAI to stream.
125
+ elsif parameters [ :stream ]
126
+ raise ArgumentError , "The stream parameter must be a Proc or have a #call method"
127
+ end
128
+
129
+ req . headers = headers
130
+ req . body = req_parameters . to_json
131
+ end
126
132
end
127
133
end
Original file line number Diff line number Diff line change 31
31
32
32
describe ".json_post" do
33
33
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
+ }
41
43
end
42
44
43
45
context "not streaming" do
63
65
expect ( timeout_errors ) . to include ( error . class )
64
66
end
65
67
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
66
74
end
67
75
end
68
76
You can’t perform that action at this time.
0 commit comments