Skip to content

Commit 5e88e7b

Browse files
committed
add test cases for simple exchange and error responses. Don't always pass functions object, don't return status on malformed 200 status output
1 parent ef0b28d commit 5e88e7b

File tree

3 files changed

+128
-6
lines changed

3 files changed

+128
-6
lines changed

openai/init.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ do
165165
end
166166
local out, err = parse_chat_response(response)
167167
if not (out) then
168-
return nil, err, status, response
168+
err = "Failed to parse response from server: " .. tostring(err)
169+
return nil, err, response
169170
end
170171
if append_response then
171172
self:append_message(out.message)
@@ -181,11 +182,11 @@ do
181182
end
182183
self.client, self.opts = client, opts
183184
self.messages = { }
184-
self.functions = { }
185185
if type(self.opts.messages) == "table" then
186186
self:append_message(unpack(self.opts.messages))
187187
end
188188
if type(self.opts.functions) == "table" then
189+
self.functions = { }
189190
local _list_0 = self.opts.functions
190191
for _index_0 = 1, #_list_0 do
191192
local func = _list_0[_index_0]

openai/init.moon

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,12 @@ parse_error_message = types.partial {
123123
class ChatSession
124124
new: (@client, @opts={}) =>
125125
@messages = {}
126-
@functions = {}
127126

128127
if type(@opts.messages) == "table"
129128
@append_message unpack @opts.messages
130129

131130
if type(@opts.functions) == "table"
131+
@functions = {}
132132
for func in *@opts.functions
133133
assert test_function func
134134
table.insert @functions, func
@@ -201,7 +201,8 @@ class ChatSession
201201
out, err = parse_chat_response response
202202

203203
unless out
204-
return nil, err, status, response
204+
err = "Failed to parse response from server: #{err}"
205+
return nil, err, response
205206

206207
if append_response
207208
@append_message out.message

spec/openai_spec.moon

Lines changed: 122 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ describe "OpenAI API Client", ->
6363
assert.same 200, status
6464
assert.same "This is a completion response.", response.choices[1].text
6565

66-
describe "chat session #ddd", ->
66+
describe "chat session", ->
6767
local snapshot
6868
before_each = ->
6969
snapshot = assert\snapshot!
@@ -72,8 +72,128 @@ describe "OpenAI API Client", ->
7272
snapshot\revert!
7373

7474
it "simple exchange", ->
75+
stub(OpenAI.__base, "chat").invokes (c, messages, params) ->
76+
assert.same {
77+
{
78+
role: "user"
79+
content: "Who are you?"
80+
}
81+
}, messages
82+
assert.same {
83+
temperature: 0.75
84+
}, params
85+
86+
200, {
87+
usage: {}
88+
choices: {
89+
{
90+
message: {
91+
content: "I am you"
92+
role: "assistant"
93+
}
94+
}
95+
}
96+
}
97+
98+
client = OpenAI "test-api-key"
99+
chat = client\new_chat_session { temperature: .75 }
100+
res = assert chat\send "Who are you?"
101+
assert.same "I am you", res
102+
103+
-- verify that all the messages are stored
104+
assert.same {
105+
{
106+
role: "user"
107+
content: "Who are you?"
108+
}
109+
{
110+
role: "assistant"
111+
content: "I am you"
112+
}
113+
}, chat.messages
75114

76-
it "handles error", ->
115+
stub(OpenAI.__base, "chat").invokes (c, messages, params) ->
116+
assert.same {
117+
{
118+
role: "user"
119+
content: "Who are you?"
120+
}
121+
{
122+
role: "assistant"
123+
content: "I am you"
124+
}
125+
{
126+
role: "user"
127+
content: "Thank you"
128+
}
129+
}, messages
130+
assert.same {
131+
temperature: 0.75
132+
}, params
133+
134+
200, {
135+
usage: {}
136+
choices: {
137+
{
138+
message: {
139+
content: "You're welcome"
140+
role: "assistant"
141+
}
142+
}
143+
}
144+
}
145+
146+
assert.same "You're welcome", chat\send "Thank you"
147+
148+
it "handles error responses", ->
149+
client = OpenAI "test-api-key"
150+
chat = client\new_chat_session { model: "gpt-4" }
151+
152+
-- bad status
153+
stub(OpenAI.__base, "chat").invokes (c, messages, params) ->
154+
400, {}
155+
156+
assert.same {nil, "Bad status: 400", {}}, {chat\send "Hello"}
157+
158+
-- bad status with error
159+
stub(OpenAI.__base, "chat").invokes (c, messages, params) ->
160+
400, {
161+
error: {
162+
message: "Not valid thing"
163+
}
164+
}
165+
166+
assert.same {nil, "Bad status: 400: Not valid thing", {
167+
error: {
168+
message: "Not valid thing"
169+
}
170+
}}, {chat\send "Hello"}
171+
172+
-- bad status with error message and code
173+
stub(OpenAI.__base, "chat").invokes (c, messages, params) ->
174+
400, {
175+
error: {
176+
message: "Not valid thing"
177+
code: "99"
178+
}
179+
}
180+
181+
assert.same {nil, "Bad status: 400: Not valid thing (99)", {
182+
error: {
183+
message: "Not valid thing"
184+
code: "99"
185+
}
186+
}}, {chat\send "Hello"}
187+
188+
-- malformed output
189+
stub(OpenAI.__base, "chat").invokes (c, messages, params) ->
190+
200, { usage: {} }
191+
192+
assert.same {
193+
nil
194+
[[Failed to parse response from server: field "choices": expected type "table", got "nil"]]
195+
{ usage: {}}
196+
}, {chat\send "Hello"}
77197

78198
it "with functions", ->
79199
stub(OpenAI.__base, "chat").invokes (c, args, params) ->

0 commit comments

Comments
 (0)