-
Notifications
You must be signed in to change notification settings - Fork 3
/
ChatGPT-PromptLibrary.txsMacro
102 lines (102 loc) · 6.16 KB
/
ChatGPT-PromptLibrary.txsMacro
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
{
"abbrev": "",
"description": [
"Highlight any text in your document and run the macro using the shortcut Shift+F1 or by clicking on it. Then choose the actions that you want to perform on your selected text (e.g. reduce it, expand it...) and watch as the power of AI enhances your LaTeX documents!"
],
"formatVersion": 1,
"menu": "",
"name": "ChatGPT - Prompt Library",
"shortcut": "Shift+F2",
"tag": [
"%SCRIPT",
"",
"// #########################################################################",
"// Set the path to Python3 and the Python script",
"var python_path = \"/usr/bin/python3\";\t\t\t\t// *** edit this line **",
"var script_path = \"/path/to/openai_python_script.py\"; // *** edit this line **",
"// #########################################################################",
"",
"// #########################################################################",
"// This is the prompt library. The promptOption is the display text in the ",
"// dropdown and the basePrompt is the command sent to ChatGPT.",
"// Just add a new line or change an existing one and the next time you call",
"// this macro your new prompt will be available to you.",
"// #########################################################################",
"var promptOptions = [",
" { promptOption: \"expand it\", basePrompt: \"You are a scientific writer. Expand the following text:\\n\" },",
" { promptOption: \"continue it\", basePrompt: \"You are a scientific writer. Continue the following text:\\n\" },",
" { promptOption: \"proofread it\", basePrompt: \"You are a scientific writer. Proofread the following text. Fix grammar and spelling:\\n\" },",
" { promptOption: \"improve it\", basePrompt: \"You are a scientific writer. Improve the following text. Focus on expression and coherence:\\n\" },",
" { promptOption: \"reduce it\", basePrompt: \"Reduce the following text to its core statements:\\n\" },",
" { promptOption: \"check its logic\", basePrompt: \"Check the following text for logical mistakes:\\n\" },",
"\t{ promptOption: \"review it\", basePrompt: \"I want you to act as a journal reviewer. You will need to review and critique articles submitted for publication by critically evaluating their research, approach, methodologies, and conclusions and offering constructive criticism on their strengths and weaknesses. My first suggestion request is:\\n\"}",
"];",
"// #########################################################################",
"// From here on, no more adjustments are usually necessary:",
"",
"",
"",
"",
"// Function to generate a prompt message based on user's selection",
"function generatePrompt(promptType) {",
" var basePrompt = \"\";",
" promptOptions.forEach(function (option) {",
" if (option.promptOption === promptType) {",
" basePrompt = option.basePrompt;",
" }",
" });",
" return basePrompt;",
"}",
"",
"// Get the selected text from the editor and insert a newline character after it",
"var selectedText = cursor.selectedText();",
"editor.write(selectedText + \"\\n\");",
"",
"// Check if any text is selected",
"if (selectedText === \"\") {",
" alert(\"Please select some text before running the ChatGPT-macro.\");",
"} else {",
"\t// Create a dialog box to prompt the user for what to do with the selected text",
"\tdlg = new UniversalInputDialog();",
"\t// Set the title of the dialog box",
"\tdlg.setWindowTitle( \"ChatGPT\" );",
"\t// Create a selectbox to take a selection out of a list, describe it as \"selectbox\", give the internal ID \"selectbox\".",
"\t dlg.add(promptOptions.map(function(option) { return option.promptOption; }), \"What do you want to do with the selected text?\", \"selectbox\");",
" // Show the newly created and populated dialog box",
"\t// Show the newly created and populated dialog box",
"\tdlg.exec();",
"\tvar promptType = dlg.get( \"selectbox\" ).toString();",
"\t// Generate the prompt message based on the user's selection",
"\tvar prompt = generatePrompt(promptType);",
"\t// Construct the Python command to run the OpenAI script with the selected text and prompt message as arguments",
"\tvar command = python_path + \" \" + script_path + \" \\\"\" + prompt + selectedText + \"\\\"\";",
"\teditor.write(promptType + \"\\n\");\t",
"\t// Execute the Python command and capture the output",
"\texecute(command);",
"}",
"",
"// Function to execute a command in the system shell",
"function execute(command) {",
"\tcmd = system(command + \" > /dev/null\");",
"",
"\tvar menu = app.getManagedMenu(\"main/macros\"); //get macro menu",
"\tvar act = app.newManagedAction(menu, \"PromptLib\", \"Stop ChatGPT PromptLib\"); //add action",
" act.triggered.connect(function(){cmd.terminate();}); //define what should happen when action is triggered\t",
"",
"\t// Connect to the standard output stream of the executed command and write the output to the editor",
"\tcmd.standardOutputRead.connect(function(output) {",
"\t\teditor.write(output);",
" });",
" // Connect to the finished signal of the executed command and perform cleanup",
"\t// this step is required as sometimes unwanted closing brackets are inserted by autocompletion",
" cmd.finished.connect(function(exitCode) {",
" editor.write(\"\\ncleanup...\");",
"\t\t// for some reasons the cursor.eraseline did not work here, therefore we just replace",
"\t\t// the unwanted closing brackets with an empty space",
"\t\teditor.replace(/cleanup\\.\\.\\.([}\\]]*)/g, \"\", \"\", \"\");",
" });",
"}",
""
],
"trigger": ""
}