forked from gsuuon/model.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
huggingface.lua
71 lines (60 loc) · 1.9 KB
/
huggingface.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
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
local util = require('model.util')
local sse = require('model.util.sse')
local M = {}
---@param handlers StreamHandlers
---@param params? any Additional params for request. Note the parameters detailed at https://huggingface.co/docs/api-inference/detailed_parameters need to go in the `params.parameters` field.
---@param options? { model?: string }
function M.request_completion(handlers, params, options)
local model = (options or {}).model or 'bigscience/bloom'
-- TODO handle non-streaming calls
return sse.curl_client(
{
url = 'https://api-inference.huggingface.co/models/' .. model,
method = 'POST',
body = vim.tbl_extend('force', { stream = true }, params),
headers = {
Authorization = 'Bearer ' .. util.env_memo('HUGGINGFACE_API_KEY'),
['Content-Type'] = 'application/json'
}
},
{
on_message = function (msg)
local item = util.json.decode(msg.data)
if item == nil then
handlers.on_error(msg.data, 'json parse error')
return
end
if item.token == nil then
if item[1] ~= nil and item[1].generated_text ~= nil then
-- non-streaming
handlers.on_finish(item[1].generated_text, 'stop')
return
end
handlers.on_error(item, 'missing token')
return
end
local partial = item.token.text
handlers.on_partial(partial)
-- We get the completed text including input unless parameters.return_full_text is set to false
if item.generated_text ~= nil and #item.generated_text > 0 then
handlers.on_finish(item.generated_text, 'stop')
end
end
}
)
end
M.default_prompt = {
provider = M,
options = {
model = 'bigscience/bloom'
},
params = {
parameters = {
return_full_text = false
}
},
builder = function(input)
return { inputs = input }
end
}
return M