-
-
Notifications
You must be signed in to change notification settings - Fork 360
/
Copy pathchat_spec.rb
171 lines (153 loc) · 5.28 KB
/
chat_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
RSpec.describe OpenAI::Client do
describe "#chat" do
context "with messages" do
let(:messages) { [{ role: "user", content: "Hello!" }] }
let(:stream) { false }
let(:response) do
OpenAI::Client.new.chat(
parameters: parameters
)
end
let(:parameters) { { model: model, messages: messages, stream: stream } }
let(:content) { response.dig("choices", 0, "message", "content") }
let(:cassette) { "#{model} #{'streamed' if stream} chat".downcase }
context "with model: gpt-3.5-turbo" do
let(:model) { "gpt-3.5-turbo" }
it "succeeds" do
VCR.use_cassette(cassette) do
expect(content.split.empty?).to eq(false)
end
end
context "with an invalid function call" do
let(:cassette) { "#{model} function call chat".downcase }
let(:messages) do
[
{
"role" => "function",
# "name" => "function",
"content" => "function"
}
]
end
let(:parameters) do
{
model: model,
messages: messages,
stream: stream,
functions: functions
}
end
let(:functions) do
[
{
"name" => "function",
"description" => "function",
"parameters" =>
{
"type" => "object",
"properties" => {
"user" => {
"type" => "string",
"description" => "the full name of the user"
}
}
}
}
]
end
it "raises an error containing the reason" do
VCR.use_cassette(cassette) do
response
rescue Faraday::Error => e
expect(e.response.dig(:body, "error",
"message")).to include("Missing parameter 'name'")
end
end
end
describe "streaming" do
let(:chunks) { [] }
let(:stream) do
proc do |chunk, _bytesize|
chunks << chunk
end
end
it "succeeds" do
VCR.use_cassette(cassette) do
response
expect(chunks.dig(0, "choices", 0, "index")).to eq(0)
end
end
context "with an object with a call method" do
let(:cassette) { "#{model} streamed chat without proc".downcase }
let(:stream) do
Class.new do
attr_reader :chunks
def initialize
@chunks = []
end
def call(chunk)
@chunks << chunk
end
end.new
end
it "succeeds" do
VCR.use_cassette(cassette) do
response
expect(stream.chunks.dig(0, "choices", 0, "index")).to eq(0)
end
end
end
context "with an object without a call method" do
let(:stream) { Object.new }
it "raises an error" do
VCR.use_cassette(cassette) do
expect { response }.to raise_error(ArgumentError)
end
end
end
context "with an error response with a JSON body" do
let(:cassette) { "#{model} streamed chat with json error response".downcase }
it "raises an HTTP error with the parsed body" do
VCR.use_cassette(cassette, record: :none) do
response
rescue Faraday::BadRequestError => e
expect(e.response).to include(status: 400)
expect(e.response[:body]).to eq({
"error" => {
"message" => "Test error",
"type" => "test_error",
"param" => nil,
"code" => "test"
}
})
else
raise "Expected to raise Faraday::BadRequestError"
end
end
end
context "with an error response without a JSON body" do
let(:cassette) { "#{model} streamed chat with error response".downcase }
it "raises an HTTP error" do
VCR.use_cassette(cassette, record: :none) do
response
rescue Faraday::ServerError => e
expect(e.response).to include(status: 500)
expect(e.response[:body]).to eq("")
else
raise "Expected to raise Faraday::ServerError"
end
end
end
end
end
context "with model: gpt-3.5-turbo-0301" do
let(:model) { "gpt-3.5-turbo-0301" }
it "succeeds" do
VCR.use_cassette(cassette) do
expect(content.split.empty?).to eq(false)
end
end
end
end
end
end