-
-
Notifications
You must be signed in to change notification settings - Fork 359
/
Copy pathhttp_spec.rb
346 lines (279 loc) · 9.79 KB
/
http_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
RSpec.describe OpenAI::HTTP do
describe "with an aggressive timeout" do
let(:timeout_errors) { [Faraday::ConnectionFailed, Faraday::TimeoutError] }
let(:timeout) { 0 }
# We disable VCR and WebMock for timeout specs, otherwise VCR will return instant
# responses when using the recorded responses and the specs will fail incorrectly.
# The timeout is set to 0, so these specs will never actually hit the API and
# therefore are still fast and deterministic.
before do
VCR.turn_off!
WebMock.allow_net_connect!
OpenAI.configuration.request_timeout = timeout
end
after do
VCR.turn_on!
WebMock.disable_net_connect!
OpenAI.configuration.request_timeout = OpenAI::Configuration::DEFAULT_REQUEST_TIMEOUT
end
describe ".get" do
let(:response) { OpenAI::Client.new.models.list }
it "times out" do
expect { response }.to raise_error do |error|
expect(timeout_errors).to include(error.class)
end
end
end
describe ".json_post" do
let(:response) do
OpenAI::Client.new.chat(parameters: parameters)
end
let(:parameters) do
{
model: "gpt-3.5-turbo",
messages: [{ role: "user", content: "Hello!" }],
stream: stream
}
end
context "not streaming" do
let(:stream) { false }
it "times out" do
expect { response }.to raise_error do |error|
expect(timeout_errors).to include(error.class)
end
end
end
context "streaming" do
let(:chunks) { [] }
let(:stream) do
proc do |chunk, _bytesize|
chunks << chunk
end
end
it "times out" do
expect { response }.to raise_error do |error|
expect(timeout_errors).to include(error.class)
end
end
it "doesn't change the parameters stream proc" do
expect { response }.to raise_error(Faraday::ConnectionFailed)
expect(parameters[:stream]).to eq(stream)
end
end
end
describe ".multipart_post" do
let(:filename) { "sentiment.jsonl" }
let(:file) { File.join(RSPEC_ROOT, "fixtures/files", filename) }
let(:upload_purpose) { "fine-tune" }
let(:response) do
OpenAI::Client.new.files.upload(
parameters: { file: file, purpose: upload_purpose }
)
end
it "times out" do
expect { response }.to raise_error do |error|
expect(timeout_errors).to include(error.class)
end
end
end
describe ".delete" do
let(:response) do
OpenAI::Client.new.files.delete(id: "1a")
end
it "times out" do
expect { response }.to raise_error do |error|
expect(timeout_errors).to include(error.class)
end
end
end
end
describe ".get" do
context "with an error response" do
let(:cassette) { "http get with error response".downcase }
it "raises an HTTP error" do
VCR.use_cassette(cassette, record: :none) do
OpenAI::Client.new.models.retrieve(id: "text-ada-001")
rescue Faraday::Error => e
expect(e.response).to include(status: 400)
else
raise "Expected to raise Faraday::BadRequestError"
end
end
end
end
describe ".to_json_stream" do
context "with a proc" do
let(:user_proc) { proc { |x| x } }
let(:stream) { OpenAI::Client.new.send(:to_json_stream, user_proc: user_proc) }
it "returns a proc" do
expect(stream).to be_a(Proc)
end
context "when called with a string containing a single JSON object" do
it "calls the user proc with the data parsed as JSON" do
expect(user_proc).to receive(:call).with(JSON.parse('{"foo": "bar"}'))
stream.call(<<~CHUNK)
data: { "foo": "bar" }
#
CHUNK
end
end
context "when called with a string containing more than one JSON object" do
it "calls the user proc for each data parsed as JSON" do
expect(user_proc).to receive(:call).with(JSON.parse('{"foo": "bar"}'))
expect(user_proc).to receive(:call).with(JSON.parse('{"baz": "qud"}'))
stream.call(<<~CHUNK)
data: { "foo": "bar" }
data: { "baz": "qud" }
data: [DONE]
#
CHUNK
end
end
context "when called with string containing invalid JSON" do
let(:chunk) do
<<~CHUNK
data: { "foo": "bar" }
data: NOT JSON
#
CHUNK
end
it "raise an error" do
expect(user_proc).to receive(:call).with(JSON.parse('{"foo": "bar"}'))
expect do
stream.call(chunk)
end.to raise_error(JSON::ParserError)
end
end
context "when called with JSON split across chunks" do
it "calls the user proc with the data parsed as JSON" do
expect(user_proc).to receive(:call).with(JSON.parse('{ "foo": "bar" }'))
expect do
stream.call("data: { \"foo\":")
stream.call(" \"bar\" }\n\n")
end.not_to raise_error
end
end
end
end
describe ".parse_jsonl" do
context "with a jsonl string" do
let(:body) { "{\"prompt\":\":)\"}\n{\"prompt\":\":(\"}\n" }
let(:parsed) { OpenAI::Client.new.send(:parse_jsonl, body) }
it { expect(parsed).to eq([{ "prompt" => ":)" }, { "prompt" => ":(" }]) }
end
end
describe ".uri" do
let(:path) { "/chat" }
let(:uri) { OpenAI::Client.new.send(:uri, path: path) }
it { expect(uri).to eq("https://api.openai.com/v1/chat") }
context "uri_base with version included" do
before do
OpenAI.configuration.uri_base = "https://api.openai.com/v1/"
end
after do
OpenAI.configuration.uri_base = "https://api.openai.com/"
end
it { expect(uri).to eq("https://api.openai.com/v1/chat") }
end
context "uri_base without trailing slash" do
before do
OpenAI.configuration.uri_base = "https://api.openai.com"
end
after do
OpenAI.configuration.uri_base = "https://api.openai.com/"
end
it { expect(uri).to eq("https://api.openai.com/v1/chat") }
end
describe "with Azure" do
before do
OpenAI.configuration.uri_base = uri_base
OpenAI.configuration.api_type = :azure
end
after do
OpenAI.configuration.uri_base = "https://api.openai.com/"
OpenAI.configuration.api_type = nil
end
let(:path) { "/chat" }
let(:uri) { OpenAI::Client.new.send(:uri, path: path) }
context "with a trailing slash" do
let(:uri_base) { "https://custom-domain.openai.azure.com/openai/deployments/gpt-35-turbo/" }
it { expect(uri).to eq("https://custom-domain.openai.azure.com/openai/deployments/gpt-35-turbo/chat?api-version=v1") }
end
context "without a trailing slash" do
let(:uri_base) { "https://custom-domain.openai.azure.com/openai/deployments/gpt-35-turbo" }
it { expect(uri).to eq("https://custom-domain.openai.azure.com/openai/deployments/gpt-35-turbo/chat?api-version=v1") }
end
context "with assistants" do
let(:path) { "/assistants/test_assistant_id" }
let(:uri_base) { "https://custom-domain.openai.azure.com/openai/deployments/gpt-35-turbo" }
it { expect(uri).to eq("https://custom-domain.openai.azure.com/openai/assistants/test_assistant_id?api-version=v1") }
end
end
end
describe ".headers" do
before do
OpenAI.configuration.api_type = :nil
end
let(:headers) { OpenAI::Client.new.send(:headers) }
it {
expect(headers).to eq({ "Authorization" => "Bearer #{OpenAI.configuration.access_token}",
"Content-Type" => "application/json", "OpenAI-Organization" => nil })
}
describe "with Azure" do
before do
OpenAI.configuration.api_type = :azure
end
after do
OpenAI.configuration.api_type = nil
end
let(:headers) { OpenAI::Client.new.send(:headers) }
it {
expect(headers).to eq({ "Content-Type" => "application/json",
"api-key" => OpenAI.configuration.access_token })
}
end
end
describe "logging errors" do
let(:cassette) { "http get with error response".downcase }
before do
@original_stdout = $stdout
$stdout = StringIO.new
end
after do
$stdout = @original_stdout
end
it "is disabled by default" do
VCR.use_cassette(cassette, record: :none) do
expect { OpenAI::Client.new.models.retrieve(id: "text-ada-001") }
.to raise_error Faraday::Error
$stdout.rewind
captured_stdout = $stdout.string
expect(captured_stdout).not_to include("OpenAI HTTP Error")
end
end
describe "when log_errors is set to true" do
let(:log_errors) { true }
it "logs errors" do
VCR.use_cassette(cassette, record: :none) do
expect { OpenAI::Client.new(log_errors: log_errors).models.retrieve(id: "text-ada-001") }
.to raise_error Faraday::Error
$stdout.rewind
captured_stdout = $stdout.string
expect(captured_stdout).to include("OpenAI HTTP Error")
end
end
end
describe "when log_errors is set to false" do
let(:log_errors) { false }
it "does not log errors" do
VCR.use_cassette(cassette, record: :none) do
expect { OpenAI::Client.new(log_errors: log_errors).models.retrieve(id: "text-ada-001") }
.to raise_error Faraday::Error
$stdout.rewind
captured_stdout = $stdout.string
expect(captured_stdout).not_to include("OpenAI HTTP Error")
end
end
end
end
end