-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
51 lines (45 loc) · 1.45 KB
/
background.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
chrome.action.onClicked.addListener((tab) => {
chrome.tabs.sendMessage(tab.id, { action: "toggleSummarize" });
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "summarize") {
summarizeParagraph(request.text)
.then((summary) => sendResponse({ summary: summary }))
.catch((error) => sendResponse({ error: error.message }));
return true; // Indicates we will send a response asynchronously
}
});
async function summarizeParagraph(text) {
const apiKey = await new Promise((resolve) => {
chrome.storage.sync.get("apiKey", (data) => resolve(data.apiKey));
});
if (!apiKey) {
throw new Error("Please set your OpenAI API key in the extension options.");
}
const response = await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
},
body: JSON.stringify({
model: "gpt-3.5-turbo",
messages: [
{
role: "system",
content: "You are a helpful assistant that summarizes text.",
},
{
role: "user",
content: `Summarize this paragraph in one sentence: ${text}`,
},
],
max_tokens: 40,
}),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data.choices[0].message.content.trim();
}