Skip to content

Commit ef0b28d

Browse files
committed
add test suite for functions chat session flow
1 parent f1e276f commit ef0b28d

File tree

1 file changed

+160
-5
lines changed

1 file changed

+160
-5
lines changed

spec/openai_spec.moon

Lines changed: 160 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import OpenAI from require "openai"
22

3+
cjson = require "cjson"
4+
35
describe "OpenAI API Client", ->
46
before_each ->
57
package.loaded["ssl.https"] = {
@@ -35,7 +37,6 @@ describe "OpenAI API Client", ->
3537
}
3638

3739
status = 200
38-
cjson = require "cjson"
3940
opts.sink cjson.encode(response)
4041
true, status, {}
4142
}
@@ -59,10 +60,166 @@ describe "OpenAI API Client", ->
5960
client = OpenAI "test-api-key"
6061
status, response = assert client\completion "Tell me a joke."
6162

62-
6363
assert.same 200, status
6464
assert.same "This is a completion response.", response.choices[1].text
6565

66+
describe "chat session #ddd", ->
67+
local snapshot
68+
before_each = ->
69+
snapshot = assert\snapshot!
70+
71+
after_each = ->
72+
snapshot\revert!
73+
74+
it "simple exchange", ->
75+
76+
it "handles error", ->
77+
78+
it "with functions", ->
79+
stub(OpenAI.__base, "chat").invokes (c, args, params) ->
80+
assert.same {
81+
{
82+
role: "system"
83+
content: "You are a calculator with access to specified set of functions. All computation should be done with the functions"
84+
}
85+
{
86+
role: "user"
87+
content: "Calculate the square root of 23892391"
88+
}
89+
}, args
90+
91+
assert.same {
92+
model: "gpt-4-0613"
93+
functions: {
94+
{
95+
name: "sqrt"
96+
description: "Calculate square root of a number"
97+
parameters: {
98+
type: "object"
99+
properties: {
100+
a: { type: "number" }
101+
}
102+
}
103+
}
104+
}
105+
}, params
106+
107+
200, {
108+
usage: {}
109+
choices: {
110+
{
111+
message: {
112+
role: "assistant"
113+
function_call: {
114+
name: "sqrt"
115+
arguments: [[{ "a": 23892391 }]]
116+
}
117+
content: cjson.null
118+
}
119+
}
120+
}
121+
}
122+
123+
client = OpenAI "test-api-key"
124+
chat = client\new_chat_session {
125+
model: "gpt-4-0613"
126+
messages: {
127+
{
128+
role: "system"
129+
content: "You are a calculator with access to specified set of functions. All computation should be done with the functions"
130+
}
131+
}
132+
functions: {
133+
{
134+
name: "sqrt"
135+
description: "Calculate square root of a number"
136+
parameters: {
137+
type: "object"
138+
properties: {
139+
a: { type: "number" }
140+
}
141+
}
142+
}
143+
}
144+
}
145+
146+
res = assert chat\send "Calculate the square root of 23892391"
147+
148+
-- returns message object instead of string result due to
149+
-- function call
150+
assert.same {
151+
role: "assistant"
152+
function_call: {
153+
name: "sqrt"
154+
arguments: [[{ "a": 23892391 }]]
155+
}
156+
content: cjson.null
157+
}, res
158+
159+
160+
stub(OpenAI.__base, "chat").invokes (c, args, params) ->
161+
assert.same {
162+
{
163+
role: "system"
164+
content: "You are a calculator with access to specified set of functions. All computation should be done with the functions"
165+
}
166+
{
167+
role: "user"
168+
content: "Calculate the square root of 23892391"
169+
}
170+
{
171+
role: "assistant"
172+
function_call: {
173+
name: "sqrt"
174+
arguments: [[{ "a": 23892391 }]]
175+
}
176+
content: cjson.null -- it must preserve null values
177+
}
178+
{
179+
role: "function"
180+
name: "sqrt"
181+
content: "99"
182+
}
183+
}, args
184+
185+
assert.same {
186+
model: "gpt-4-0613"
187+
functions: {
188+
{
189+
name: "sqrt"
190+
description: "Calculate square root of a number"
191+
parameters: {
192+
type: "object"
193+
properties: {
194+
a: { type: "number" }
195+
}
196+
}
197+
}
198+
}
199+
}, params
200+
201+
200, {
202+
usage: {}
203+
choices: {
204+
{
205+
message: {
206+
role: "assistant"
207+
content: "Good work!"
208+
}
209+
}
210+
}
211+
}
212+
213+
-- send the response
214+
res = assert chat\send {
215+
role: "function"
216+
name: "sqrt"
217+
content: "99"
218+
}
219+
220+
assert.same "Good work!", res
221+
222+
66223
describe "streaming", ->
67224
before_each ->
68225
package.loaded["ssl.https"] = {
@@ -83,9 +240,7 @@ describe "OpenAI API Client", ->
83240
}
84241

85242
it "processes streaming chunks", ->
86-
client = OpenAI "test-api-key", {
87-
http_provider: http_stream_stub
88-
}
243+
client = OpenAI "test-api-key"
89244
chat = client\new_chat_session {
90245
messages: {
91246
{role: "user", content: "tell me a joke"}

0 commit comments

Comments
 (0)