-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLLMClient.lua
80 lines (70 loc) · 2.26 KB
/
LLMClient.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
74
75
76
77
78
79
80
-- Load required modules
local http = require("socket.http")
local ltn12 = require("ltn12")
local json = require("dkjson")
-- Define a class for handling HTTP requests and JSON parsing
local LLMClient = {
temp = 0.8,
stream = false,
timeout = 30, -- Default timeout in seconds
}
LLMClient.__index = LLMClient
--- Create new model
---@param model string LLM model
---@return self instance
function LLMClient:new(model)
local instance = setmetatable({}, self)
instance.model = model
return instance
end
--- Parse JSON stream response body
--- @param responseBody string raw JSON data
--- @return string concatenated text response
function LLMClient:parseResponse(responseBody)
local concatenated = ""
for json_obj in responseBody:gmatch("{.-}") do
local parsed_obj, _, err = json.decode(json_obj)
if err then
print("Error parsing JSON:", err)
else
-- Concatenate the 'response' field
concatenated = concatenated .. (parsed_obj and parsed_obj.response or "")
end
end
return concatenated
end
--- Make HTTP request to LLM API
---@param prompt string your LLM prompt
---@return string responseBody raw JSON data
function LLMClient:makeRequest(prompt)
-- Prepare the POST data
-- https://github.com/ollama/ollama/blob/main/docs/api.md
local data = {
["model"] = self.model,
["prompt"] = prompt,
["stream"] = self.stream,
["options"] = {
["temperature"] = self.temp,
},
}
data = json.encode(data)
-- Prepare the response table
local response = {}
-- Configure HTTP request with a custom timeout
http.TIMEOUT = self.timeout -- Set the timeout globally for this request
-- Make the POST request
local result, statusCode, headers = http.request {
url = "http://cvar1984.my.id:11434/api/generate",
method = "POST",
headers = {
["Content-Type"] = "application/json",
["Content-Length"] = tostring(#data),
},
source = ltn12.source.string(data),
sink = ltn12.sink.table(response),
}
assert(statusCode == 200, "Error response " .. statusCode)
local responseBody = table.concat(response)
return responseBody
end
return LLMClient