forked from gsuuon/model.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
huggingface.lua
73 lines (62 loc) · 1.87 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
72
73
local curl = require('llm.curl')
local util = require('llm.util')
local provider_util = require('llm.providers.util')
local M = {}
---@param handlers StreamHandlers
---@param params? any Additional params for request
---@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 curl.stream(
{
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'
}
},
function(raw)
provider_util.iter_sse_items(raw, function(item)
local data = util.json.decode(item)
if data == nil then
handlers.on_error(item, 'json parse error')
return
end
if data.token == nil then
if data[1] ~= nil and data[1].generated_text ~= nil then
-- non-streaming
handlers.on_finish(data[1].generated_text, 'stop')
return
end
handlers.on_error(data, 'missing token')
return
end
local partial = data.token.text
handlers.on_partial(partial)
-- We get the completed text including input unless parameters.return_full_text is set to false
if data.generated_text ~= nil and #data.generated_text > 0 then
handlers.on_finish(data.generated_text, 'stop')
end
end)
end,
function(error)
handlers.on_error(error)
end
)
end
M.default_prompt = {
provider = M,
options = {
model = 'bigscience/bloom'
},
params = {
return_full_text = false
},
builder = function(input)
return { inputs = input }
end
}
return M