-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathllm-superset.js
164 lines (142 loc) · 5.55 KB
/
llm-superset.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const axios = require("axios");
function prepareResponse(data) {
// if holding shift, copy just the response. else, paste the last input and response.
if (popclip.modifiers.shift) {
popclip.copyText(data);
} else {
popclip.pasteText(data);
}
}
async function callLLMapi(prompt, options) {
if (options.model.startsWith("gpt") || options.model.startsWith("o1")) {
return await callOpenAPI(prompt, options);
} else if (options.model.startsWith("claude")) {
return await callClaudeAPI(prompt, options);
} else throw new Error("Unknown model selection:" + options.model);
}
// --- CLAUDE
async function callClaudeAPI(prompt, options) {
const key = options.claudeapikey.trim();
if (!key || key.length === 0)
throw new Error("Settings error: missing ClaudeAPI key");
const requestConfig = {
headers: {
"x-api-key": key,
"anthropic-version": "2023-06-01",
"Content-Type": "application/json",
Accept: "application/json",
},
};
const messages = [{ role: "user", content: prompt }];
const { data } = await axios.post(
"https://api.anthropic.com/v1/messages",
{
model: options.model,
max_tokens: 2048,
messages,
},
requestConfig
);
return data.content[0].text.trim();
}
// --- OPENAI
async function callOpenAPI(prompt, options) {
const key = options.apikey.trim();
if (!key || key.length === 0)
throw new Error("Settings error: missing OpenAPI key");
const requestConfig = {
headers: {
Authorization: `Bearer ${key}`,
},
};
const messages = [{ role: "user", content: prompt }];
const { data } = await axios.post(
"https://api.openai.com/v1/chat/completions",
{
model: options.model,
messages,
},
requestConfig
);
return data.choices[0].message.content.trim();
}
async function improveWritting(input, options) {
const prompt =
"I want you to act as an improver. Keep the meaning the same, use a " +
options.tone +
" tone, avoid complex words and verbs. I want you to only reply the improvements and nothing else, do not write explanations. keep the input language the same, rewrite and improve the following: \n\n" +
input.text.trim();
const data = await callLLMapi(prompt, options);
prepareResponse(data);
}
async function spellingAndGrammar(input, options) {
const prompt =
"I want you to act as an spelling corrector, only reply the correction and nothing else, do not write explanations. keep the input language the same, correct the following using a " +
options.tone +
" tone: \n\n" +
input.text.trim();
const data = await callLLMapi(prompt, options);
prepareResponse(data);
}
async function summarize(input, options) {
const prompt =
"You are a highly skilled AI trained in language comprehension and summarization. I would like you to read the following text and summarize it into a concise abstract paragraph. Aim to retain the most important points, providing a coherent and readable summary that could help a person understand the main points of the discussion without needing to read the entire text. keep the input language the same, avoid unnecessary details or tangential points: \n\n" +
input.text.trim();
const data = await callLLMapi(prompt, options);
prepareResponse(data);
}
async function makeLonger(input, options) {
const prompt =
"You are a highly advanced AI with exceptional language comprehension skills. I will provide you with a piece of text, and your task will be to rewrite it into a longer, more detailed form. Ensure the original meaning is preserved and maintain the same language variety or dialect as the input. Your response should consist solely of the rewritten text, with no additional commentary or explanation: \n\n" +
input.text.trim();
const data = await callLLMapi(prompt, options);
prepareResponse(data);
}
async function makeShorter(input, options) {
const prompt =
"You are a highly advanced AI with exceptional language comprehension skills. I will provide you with a piece of text, and your task will be to rewrite it into a short, less detailed form. Ensure the original meaning is preserved and maintain the same language variety or dialect as the input. Your response should consist solely of the rewritten text, with no additional commentary or explanation: \n\n" +
input.text.trim();
const data = await callLLMapi(prompt, options);
prepareResponse(data);
}
async function translate(input, options) {
const prompt = `I will provide you with text content, and your task is to translate it into ${
options.tolang
} while preserving its original meaning. Maintain the exact structure and formatting of the original text without any alterations. Your response should consist solely of the translated content, with no additional commentary or explanation: ${input.text.trim()} `;
const data = await callLLMapi(prompt, options);
prepareResponse(data);
}
exports.actions = [
{
title: "Improve Writing",
code: improveWritting,
icon: "iconify:tabler:file-text-ai",
},
{
title: "Correct Spelling&Grammar",
// after: "copy-result",
code: spellingAndGrammar,
icon: "iconify:ic:round-fact-check",
},
{
title: "Make Longer",
code: makeLonger,
icon: "iconify:mdi:file-plus",
},
{
title: "Make Shorter",
code: makeShorter,
icon: "iconify:mdi:file-minus",
},
{
title: "Summarize the text",
after: "show-result",
code: summarize,
icon: "symbol:arrow.down.right.and.arrow.up.left",
},
{
title: "Translate",
code: translate,
icon: "iconify:bi:translate",
},
];