-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathonlysqapi.lua
More file actions
134 lines (116 loc) · 3.98 KB
/
onlysqapi.lua
File metadata and controls
134 lines (116 loc) · 3.98 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
--[[
OnlySq API (v1.1)
Использование ИИ используя OnlySq API(https://onlysq.ru)
Доступные модели: gpt-4o-mini, gemini
by https://t.me/ImSkaiden
--]]
require "menu"
require "http"
require "json"
require "utils"
local apiUrl = "https://api.onlysq.ru/ai/"
local prefs = inline:getDefaultSharedPreferences()
local availableModels = { "gpt-4o-mini", "gemini" }
local TimeUnit = luajava.bindClass "java.util.concurrent.TimeUnit"
local client = http(
http.newBuilder()
:readTimeout(60, TimeUnit.SECONDS)
:writeTimeout(60, TimeUnit.SECONDS)
:callTimeout(60, TimeUnit.SECONDS
) :build()
)
local function getPreferences(builder)
return {
builder.checkBox("v1_use", "Использовать v1"):setDefault(true),
--builder.text("Текущая модель: "..prefs:getString("model")),
builder.spacer(8),
builder.text("Выберите модель"),
builder.spinner("model", availableModels),
builder.spacer(8),
builder.button("Закрыть", function()
builder:cancel()
end),
}
end
local function contains(table, element)
for _, value in ipairs(table) do
if value == element then
return true
end
end
return false
end
local sendRequest = function(question, query)
local currentModel = prefs:getString("model")
local v1_use = prefs:getBoolean("v1_use")
--local data
local jsonData
if v1_use then
jsonData = json.dump({
{
role = "user",
content = tostring(question)
}
})
else
jsonData = json.dump({
model = currentModel,
request = {
messages = {
{
role = "user",
content = question
}
}
}
})
end
local url = apiUrl .. (v1_use and "v1" or "v2")
local headers = http.buildHeaders({
["Content-Type"] = "application/json",
["Accept"] = "application/json" }
)
local request = http.Request.Builder.new()
:url(url)
:headers(headers)
:post(http.buildBody(jsonData, "application/json"))
:build()
client.call(request, function(_, response, string)
if response:isSuccessful() then
local jsonResponse = json.load(string)
query:answer(tostring(jsonResponse.answer))
else
query:answer("Request failed: " .. response:code())
return
end
end, function(_, exception)
query:answer("Request error: " .. exception)
end)
end
return function(module)
module:setCategory("OnlySq API")
module:registerPreferences(getPreferences)
module:registerCommand("setmodel", function(_, query)
local model = query:getArgs()
if prefs:getBoolean("v1_use") then
query:answer("Используется v1, вы не можете сменить модель.")
return
end
if not contains(availableModels, model) then
query:answer("Модель " .. model " не найдена!\nДоступные модели: " .. availableModels)
return
end
prefs:edit("model"):putString("model", model):apply()
query:answer("Установлена модель " .. colorama.bold(tostring(prefs:getString("model"))))
end, "Установка модели для использования")
module:registerCommand("gpt", function(_, query)
query:answer("Thinking...")
local question = query:getArgs()
-- query:answer(question)
if not question then
query:answer("Вы не задали вопрос!\nDebug: " .. question)
return
end
sendRequest(question, query)
end, "Задать вопрос ИИ")
end