-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
83 lines (81 loc) · 2.98 KB
/
service-worker.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
chrome.commands.onCommand.addListener(function (command) {
if (command === "mingle") {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, { action: "triggerMingle" });
});
}
});
const apiKey = key;
const apiTextUrl = 'https://api.openai.com/v1/chat/completions';
const apiImageUrl = 'https://api.openai.com/v1/images/generations';
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
console.log({ request, sender, sendResponse });
if (request.contentScriptQuery == 'chatCompletion') {
fetch(apiTextUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify({
messages: [{
role: "system",
content: "Answer in a consistent style, with 'definition' and 'sentence' always in English." +
"The actual definition and sentence will be in the same language of the word I'm asking about. Make the example sentence funny but short."
}, {
role: "user",
content: "noxious"
}, {
role: "assistant",
content: "Definition: harmful, poisonous, or very unpleasant.\n" +
"Sentence: The noxious odor from Uncle Ned's surprise casserole was so potent that even the dog demanded an apology."
}, {
role: "user",
content: "未練"
}, {
role: "assistant",
content: "Definition: 執心が残って思い切れないこと。あきらめきれないこと。また、そのさま。\n" +
"Sentence: 古いぬいぐるみを処分すると、未練たるや!まるでそれが家を抜け出し、新しいオーナーに「ずっと一緒だったのに!」と泣きついているかのようだった。"
}, {
role: "user",
content: request.prompt
}],
max_tokens: 150,
model: "gpt-3.5-turbo",
})
})
.then(response => response.json()).then(data => {
console.log({ data })
sendResponse(data);
}).catch(error => {
console.log({ error });
sendResponse(error);
})
return true
}
if (request.contentScriptQuery == 'imageCompletion') {
fetch(apiImageUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify({
prompt: `A funny and satirical image about the word ${request.prompt}`,
n: 1,
size: "256x256",
model: "dall-e-2"
})
})
.then(response => response.json()).then(data => {
console.log({ data })
sendResponse(data);
}).catch(error => {
console.log({ error });
sendResponse(error);
})
return true
}
}
);