-
Notifications
You must be signed in to change notification settings - Fork 6
/
generate.js
142 lines (128 loc) · 5.03 KB
/
generate.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
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
function: function() {
var selectedText = window.getSelection().toString();
console.log(selectedText);
chrome.runtime.sendMessage({textselected: selectedText});
}
});
});
var custom1Value = "Act as a tranlator.";
var user1Value = 'Rewrite the text in authentic English:';
var custom2Value = "Act as a proofreader.";
var user2Value = 'Proofread the following content:';
var custom3Value = "Act as a summerizer.";
var user3Value = 'summerize following content in less than 100 words:';
var apiKey = 'YOUR_API_KEY';
var temperature = 0.5;
var maxToken = 512;
var topP = 1;
var models = 'gpt-3.5-turbo-0301';
var defaultAssistant = 'custom1user1';
// Load the saved options from storage
chrome.storage.sync.get(['options'], (result) => {
// Set the saved options as the default values
const options = result.options;
document.getElementById('response').innerHTML = options;
if (options) {
document.getElementById('response').innerHTML = options.savetranslator;
if(options.savedefaultAssistant)
defaultAssistant = options.savedefaultAssistant;
if(options.savecustom1)
custom1Value = options.savecustom1;
if(options.savecustom2)
custom2Value = options.savecustom2;
if(options.savecustom3)
custom3Value = options.savecustom3;
if(options.saveuser1)
user1Value = options.saveuser1;
if(options.saveuser2)
user2Value = options.saveuser2;
if(options.saveuser3)
user3Value = options.saveuser3;
if(options.saveapiKey)
apiKey = options.saveapiKey;
if(options.saveapiKey)
temperature = options.savetemperature;
if(options.saveapiKey)
maxToken = options.savemaxToken;
if(options.savetopP)
topP = options.savetopP;
if(options.savemodels)
models = options.savemodels;
}
// Get references to the input fields and the option elements
document.querySelector('#defaultselect option[value="custom1user1"]').textContent = `${custom1Value} / ${user1Value}`;
document.querySelector('#defaultselect option[value="custom2user2"]').textContent = `${custom2Value} / ${user2Value}`;
document.querySelector('#defaultselect option[value="custom3user3"]').textContent = `${custom3Value} / ${user3Value}`;;
document.getElementById('defaultselect').value = defaultAssistant;
});
function makeApiCall(selectedText) {
const API_KEY = apiKey;
const API_URL = "https://api.openai.com/v1/chat/completions";
const xhr = new XMLHttpRequest();
xhr.open("POST", API_URL, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Authorization", `Bearer ${API_KEY}`);
xhr.onreadystatechange = function() {
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
const assistant = JSON.parse(this.responseText);
// console.log(assistant);
const responsemessage = assistant.choices[0].message.content;
// console.log(responsemessage);
document.getElementById('response').innerHTML = responsemessage;
}
else{
document.getElementById('response').innerHTML = "Request fail, Please check the options. Maker sure you have input the correct openAI api key and correct model.";
}
};
var systemstring="";
var userstring=selectedText;
defaultAssistant=document.getElementById('defaultselect').value;
switch (defaultAssistant) {
case 'custom1user1':
systemstring+= custom1Value+user1Value;
userstring = user1Value+selectedText;
break;
case 'custom2user2':
systemstring+= custom2Value+user2Value;
userstring = user2Value+selectedText;
break;
case 'custom3user3':
systemstring+= custom3Value+user3Value;
userstring = user3Value+selectedText;
break;
}
document.getElementById('response').innerHTML = "loading...";
const data = {
messages: [
{"role": "system", "content": "You answer questions factually based on the context provided."},
{"role": "system","name":"context","content": systemstring},
{"role": "user", "content": selectedText}],
temperature: temperature,
max_tokens: maxToken,
model: models,
top_p: topP
};
xhr.send(JSON.stringify(data));
}
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
document.getElementById('edit').value=request.textselected;
makeApiCall(request.textselected);
});
document.getElementById('copyButton').addEventListener('click', () => {
navigator.clipboard.writeText(document.getElementById('response').innerText)
.then(() => {
console.log('Text copied to clipboard');
})
.catch((err) => {
console.error('Failed to copy text: ', err);
});
});
document.getElementById('optionsButton').addEventListener('click', () => {
chrome.runtime.openOptionsPage();
});
document.getElementById('submitButton').addEventListener('click', () => {
makeApiCall(document.getElementById('edit').value);
});