-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNexra.lua.cfg
104 lines (95 loc) · 2.98 KB
/
Nexra.lua.cfg
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
name = "Nexra"
url = "https://aryahcr.cc/chat/en"
local U = ...
if not U.restapi then return end
exe = true
historyName = name
predefined = {
model={
"gpt-4o",
"chatgpt",
"gemini-pro",
"llama-3.1",
"Bing",
"blackbox",
"qwen",
},
role={"", "You are a friendly AI assistant"},
conversation_style={
"", "Balanced", "Creative", "Precise",
},
--markdown="true",
websearch={"","true"},
}
local function getMsgText (data)
return data.message or data.detail or data.error
end
--https://nexra.aryahcr.cc/documentation/en
return function (session, stream, context, user, secret, model, role, websearch, conversation_style)
local boolean = U.boolean
local client = U.restapi.LinesStream(nil, "https://nexra.aryahcr.cc/api", {
["x-nexra-user"]=user, -- user-xxxxxxxx
["x-nexra-secret"]=secret,-- nx-xxxxxxx-xxxxx-xxxxx
})
client.defaultEndpoint = "/chat/complements"
client.separatorPattern = "^(.-)\30(.-)$" -- 1E - RS
client.consumeBufferRest = true
local messages = session and U.getHistory(historyName) or {}
local data = {
stream=boolean(stream),
messages=messages,
model=model,
--markdown=boolean(markdown),
websearch=boolean(websearch), --blackbox, qwen
conversation_style=conversation_style, --Bing
}
if role then
if messages[1] and messages[1].role=="system" then
messages[1].content = role
else
table.insert(messages, 1, {role="system", content=role})
end
end
table.insert(messages, {role="user", content=context})
return function (cb)
local errStream, streamed
local lastResponse = ""
local function ln() return streamed and "\n\n" or "" end
local response,statusline,errResponse = client:generate(data, function (chunk,err,extra)
if err then
cb(ln()); streamed = true
errStream = err
if type(extra)=="string" then
err = err.." "..extra
elseif type(extra)=="table" then
return cb(U.formatErrMsg(err,extra,"\n\n",getMsgText).."\n")
end
return cb("Error: "..err)
elseif chunk.error then
error("Error:\n"..U.formatJson(chunk))
elseif chunk.finish then
--cb("\nSTOP")
elseif chunk.message and chunk.message~=U.json.null then
local message = chunk.original~=U.json.null and chunk.original or chunk.message
cb(string.sub(message, #lastResponse+1))
lastResponse = message
streamed = true
else
error("Unexpected response:\n"..U.formatJson(chunk))
end
if U.check"Esc" then error("interrupted", 0) end
end)
if response==U.restapi.STREAMED then
table.insert(messages, {role="assistant", content=lastResponse})
elseif response then
table.insert(messages, {role="assistant", content=response.message})
cb(response.message)
else
table.remove(messages)
if statusline~=errStream then
cb(ln()..U.formatErrMsg(statusline,errResponse,"\n\n",getMsgText))
end
end
cb()
end
end