Skip to content

Commit fb545c0

Browse files
committed
add models method to fetch models, make _request work with GET requests
1 parent 3406810 commit fb545c0

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

openai/init.lua

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,17 +260,23 @@ do
260260
end
261261
return self:_request("POST", "/moderations", payload)
262262
end,
263+
models = function(self)
264+
return self:_request("GET", "/models")
265+
end,
263266
_request = function(self, method, path, payload, more_headers, stream_fn)
264267
assert(path, "missing path")
265268
assert(method, "missing method")
266269
assert(self.api_key, "missing api_key")
267270
local url = self.api_base .. path
268-
local body = cjson.encode(payload)
271+
local body
272+
if payload then
273+
body = cjson.encode(payload)
274+
end
269275
local headers = {
270276
["Host"] = parse_url(self.api_base).host,
271277
["Accept"] = "application/json",
272278
["Content-Type"] = "application/json",
273-
["Content-Length"] = #body,
279+
["Content-Length"] = body and #body or nil,
274280
["Authorization"] = "Bearer " .. tostring(self.api_key)
275281
}
276282
if more_headers then
@@ -279,7 +285,10 @@ do
279285
end
280286
end
281287
local out = { }
282-
local source = ltn12.source.string(body)
288+
local source
289+
if body then
290+
source = ltn12.source.string(body)
291+
end
283292
local sink = ltn12.sink.table(out)
284293
if stream_fn then
285294
sink = ltn12.sink.chain(stream_fn, sink)

openai/init.moon

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,9 @@ class OpenAI
275275

276276
@_request "POST", "/moderations", payload
277277

278+
models: =>
279+
@_request "GET", "/models"
280+
278281
_request: (method, path, payload, more_headers, stream_fn) =>
279282
assert path, "missing path"
280283
assert method, "missing method"
@@ -283,13 +286,14 @@ class OpenAI
283286

284287
url = @api_base .. path
285288

286-
body = cjson.encode payload
289+
body = if payload
290+
cjson.encode payload
287291

288292
headers = {
289293
"Host": parse_url(@api_base).host
290294
"Accept": "application/json"
291295
"Content-Type": "application/json"
292-
"Content-Length": #body
296+
"Content-Length": body and #body or nil
293297
"Authorization": "Bearer #{@api_key}"
294298
}
295299

@@ -299,7 +303,9 @@ class OpenAI
299303

300304
out = {}
301305

302-
source = ltn12.source.string body
306+
source = if body
307+
ltn12.source.string body
308+
303309
sink = ltn12.sink.table out
304310

305311
if stream_fn

0 commit comments

Comments
 (0)