Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for user-defined custom prompts #23

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ local CONFIGURATION = {
}
```

### Custom Prompts

You can add predefined prompts for specific tasks in the `configuration.lua` file. This allows you to customize the type of inquiries you can make based on your usual needs.

```lua
local CONFIGURATION = {
api_key = "YOUR_API_KEY",
model = "gpt-4o-mini",
base_url = "https://api.openai.com/v1/chat/completions",
prompts = {
{ name = "Summarize Text", prompt = "Please summarize the following text." },
{ name = "ELI5", prompt = "Explain like I'm five: "},
{ name = "Find Key Points", prompt = "List the key points in the following content." }
}
}
```

## Installation

If you clone this project, you should be able to put the directory, `askgpt.koplugin`, in the `koreader/plugins` directory and it should work. If you want to use the plugin without cloning the project, you can download the zip file from the releases page and extract the `askgpt.koplugin` directory to the `koreader/plugins` directory. If for some reason you extract the files of this repository in another directory, rename it before moving it to the `koreader/plugins` directory.
Expand Down
14 changes: 14 additions & 0 deletions configuration.lua.sample
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ local CONFIGURATION = {
model = "gpt-4o-mini",
base_url = "https://api.openai.com/v1/chat/completions",
additional_parameters = {},
prompts = {
{
name = "Summarize Text",
prompt = "Summarize the following text"
},
{
name = "ELI5",
prompt = "Explain the following as if I'm a five-year-old"
},
{
name = "Find Key Points",
prompt = "List the key points from the following text"
}
}
}

return CONFIGURATION
109 changes: 92 additions & 17 deletions dialogs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,12 @@ else
print("configuration.lua not found, skipping...")
end

local function translateText(text, target_language)
local translation_message = {
role = "user",
content = "Translate the following text to " .. target_language .. ": " .. text
}
local translation_history = {
{
role = "system",
content = "You are a helpful translation assistant. Provide direct translations without additional commentary."
},
translation_message
local function showLoadingDialog()
local loading = InfoMessage:new{
text = _("Loading..."),
timeout = 0.1
}
return queryChatGPT(translation_history)
UIManager:show(loading)
end

local function createResultText(highlightedText, message_history)
Expand All @@ -45,12 +38,83 @@ local function createResultText(highlightedText, message_history)
return result_text
end

local function showLoadingDialog()
local loading = InfoMessage:new{
text = _("Loading..."),
timeout = 0.1
local function showPromptSelectionDialog(callback)
local prompt_buttons = {}
local prompt_dialog

for _i, prompt in ipairs(CONFIGURATION.prompts) do
table.insert(prompt_buttons, {
text = _(prompt.name),
callback = function()
UIManager:close(input_dialog)
showLoadingDialog()

UIManager:scheduleIn(0.1, function()
callback(prompt.prompt)
end)
end
})
end

table.insert(prompt_buttons, {
text = _("Cancel"),
id = "close",
callback = function()
UIManager:close(prompt_dialog)
end
})

prompt_dialog = InputDialog:new{
title = _("Select a custom prompt"),
buttons = {prompt_buttons},
input_type = "none"
}
UIManager:show(loading)
UIManager:show(prompt_dialog)
end

local function executeCustomPrompt(highlightedText, prompt, message_history)
local custom_message = {
role = "user",
content = prompt .. ": " .. highlightedText
}
local custom_history = {
{
role = "system",
content = "You are a helpful assistant. Execute the task as described in the prompt."
},
custom_message
}
local answer = queryChatGPT(custom_history)

table.insert(message_history, custom_message)
table.insert(message_history, {
role = "assistant",
content = answer
})

local result_text = createResultText(highlightedText, message_history)
local chatgpt_viewer = ChatGPTViewer:new {
title = _("Custom Prompt Response"),
text = result_text,
onAskQuestion = handleNewQuestion
}

UIManager:show(chatgpt_viewer)
end

local function translateText(text, target_language)
local translation_message = {
role = "user",
content = "Translate the following text to " .. target_language .. ": " .. text
}
local translation_history = {
{
role = "system",
content = "You are a helpful translation assistant. Provide direct translations without additional commentary."
},
translation_message
}
return queryChatGPT(translation_history)
end

local function showChatGPTDialog(ui, highlightedText, message_history)
Expand Down Expand Up @@ -161,6 +225,17 @@ local function showChatGPTDialog(ui, highlightedText, message_history)
})
end

if CONFIGURATION and CONFIGURATION.prompts then
table.insert(buttons, {
text = _("Custom Prompt"),
callback = function()
showPromptSelectionDialog(function(selected_prompt)
executeCustomPrompt(highlightedText, selected_prompt, message_history)
end)
end
})
end

input_dialog = InputDialog:new{
title = _("Ask a question about the highlighted text"),
input_hint = _("Type your question here..."),
Expand Down