forked from drewbaumann/AskGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialogs.lua
78 lines (67 loc) · 2.67 KB
/
dialogs.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
local InputDialog = require("ui/widget/inputdialog")
local ChatGPTViewer = require("chatgptviewer")
local UIManager = require("ui/uimanager")
local _ = require("gettext")
local queryChatGPT = require("gpt_query")
local function showChatGPTDialog(ui, highlightedText)
local title = ui.document:getProps().title or _("Unknown Title")
local author = ui.document:getProps().authors or _("Unknown Author")
local input_dialog
input_dialog = InputDialog:new {
title = _("Ask a question about the highlighted text"),
input_hint = _("Type your question here..."),
input_type = "text",
buttons = {
{
{
text = _("Cancel"),
callback = function()
UIManager:close(input_dialog)
end,
},
{
text = _("Ask"),
callback = function()
local InfoMessage = require("ui/widget/infomessage")
local loading = InfoMessage:new {
text = _("Loading..."),
timeout = 1,
}
UIManager:show(loading)
-- Construct the prompt
local context = "I'm reading something titled '" .. title .. "' by " .. author ..
". I have a question about the following highlighted text: " .. highlightedText
local question = input_dialog:getInputText()
local prompt = context .. "\n\nUser: " .. question
-- Query ChatGPT
local answer = queryChatGPT(prompt)
UIManager:close(loading)
UIManager:close(input_dialog)
-- Create the result text
local result_text = _("Highlighted text: ") .. "\"" .. highlightedText .. "\"" ..
"\n\n" .. _("User: ") .. question ..
"\n\n" .. _("ChatGPT: ") .. answer
-- Function to handle new questions
local function handleNewQuestion(chatgpt_viewer, new_question)
local new_prompt = context .. "\n\nUser: " .. new_question
local new_answer = queryChatGPT(new_prompt)
result_text = result_text .. "\n\n" .. _("User: ") .. new_question ..
"\n\n" .. _("ChatGPT: ") .. new_answer
chatgpt_viewer:update(result_text)
end
-- Show the viewer with the result
local chatgpt_viewer = ChatGPTViewer:new {
title = _("AskKoboldCpp"),
text = result_text,
onAskQuestion = handleNewQuestion,
}
UIManager:show(chatgpt_viewer)
end,
},
},
},
}
UIManager:show(input_dialog)
input_dialog:onShowKeyboard()
end
return showChatGPTDialog