forked from gsuuon/model.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkobold.lua
40 lines (34 loc) · 993 Bytes
/
kobold.lua
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
local curl = require('model.util.curl')
local util = require('model.util')
local provider_util = require('model.providers.util')
local M = {}
---@param handlers StreamHandlers
---@param params any Parameters for endpoint
---@param options? { url?: string } Url defaults to 'http://localhost:5001/api/extra/generate/stream'
function M.request_completion(handlers, params, options)
options = options or {}
local url_ = options.url or 'http://localhost:5001/api/extra/generate/stream'
return curl.stream(
{
url = url_,
method = 'POST',
body = params,
headers = {
['Content-Type'] = 'application/json'
}
},
provider_util.iter_sse_data(
function(data)
local item, err = util.json.decode(data)
if item == nil then
util.eshow(data, 'failed to parse server-sent event')
error(err)
else
handlers.on_partial(item.token)
end
end
),
util.eshow
)
end
return M