From 6f1d3b3469c2b4b828bfbc5dc2e767f9895cf17d Mon Sep 17 00:00:00 2001 From: Gabriel Sroka Date: Wed, 25 Sep 2024 11:52:25 -0700 Subject: [PATCH 1/4] factor out post/postJson --- llm.js | 84 +++++++++++++++++++++++++--------------------------------- 1 file changed, 36 insertions(+), 48 deletions(-) diff --git a/llm.js b/llm.js index 49d7e9b..25ef1aa 100644 --- a/llm.js +++ b/llm.js @@ -7,6 +7,20 @@ class LLMProvider { async summarizeStream(prompt, onChunk) { throw new Error("summarizeStream method must be implemented."); } + + async post(url, body) { + const headers = { + 'Content-Type': 'application/json' + }; + if (this.apiKey) headers.authorization = `Bearer ${this.apiKey}`; + const init = {method: 'POST', headers, body: JSON.stringify(body)}; + return fetch(url, init); + } + + async postJson(url, body) { + const r = await post(url, body); + return r.json(); + } } // OpenAIProvider.js @@ -19,41 +33,25 @@ export class OpenAIProvider extends LLMProvider { } async summarize(prompt) { - const response = await fetch(this.apiUrl, { - method: "POST", - headers: { - Authorization: `Bearer ${this.apiKey}`, - "Content-Type": "application/json", - }, - body: JSON.stringify({ - model: this.model, - messages: [ - { role: "system", content: "You are a helpful assistant." }, - { role: "user", content: prompt }, - ], - stream: false, - }), + const data = await this.postJson(this.apiUrl, { + model: this.model, + messages: [ + { role: "system", content: "You are a helpful assistant." }, + { role: "user", content: prompt }, + ], + stream: false, }); - - const data = await response.json(); return data.choices[0].message.content; } async summarizeStream(prompt, onChunk) { - const response = await fetch(this.apiUrl, { - method: "POST", - headers: { - Authorization: `Bearer ${this.apiKey}`, - "Content-Type": "application/json", - }, - body: JSON.stringify({ - model: this.model, - messages: [ - { role: "system", content: "You are a helpful assistant." }, - { role: "user", content: prompt }, - ], - stream: true, - }), + const response = await this.post(this.apiUrl, { + model: this.model, + messages: [ + { role: "system", content: "You are a helpful assistant." }, + { role: "user", content: prompt }, + ], + stream: true, }); const reader = response.body.getReader(); @@ -91,29 +89,19 @@ export class OllamaProvider extends LLMProvider { } async summarize(prompt) { - const response = await fetch(this.apiUrl, { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ - model: this.model, - prompt: prompt, - stream: false, - }), + const data = await this.postJson(this.apiUrl, { + model: this.model, + prompt: prompt, + stream: false, }); - - const data = await response.json(); return data.response; } async summarizeStream(prompt, onChunk) { - const response = await fetch(this.apiUrl, { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ - model: this.model, - prompt: prompt, - stream: true, - }), + const response = await this.post(this.apiUrl, { + model: this.model, + prompt: prompt, + stream: true, }); const reader = response.body.getReader(); From ff34573438c5f74f389e52d95d591a880168a107 Mon Sep 17 00:00:00 2001 From: Gabriel Sroka Date: Wed, 25 Sep 2024 11:58:53 -0700 Subject: [PATCH 2/4] shorten code --- llm.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/llm.js b/llm.js index 25ef1aa..d8819b1 100644 --- a/llm.js +++ b/llm.js @@ -9,12 +9,9 @@ class LLMProvider { } async post(url, body) { - const headers = { - 'Content-Type': 'application/json' - }; + const headers = {'Content-Type': 'application/json'}; if (this.apiKey) headers.authorization = `Bearer ${this.apiKey}`; - const init = {method: 'POST', headers, body: JSON.stringify(body)}; - return fetch(url, init); + return fetch(url, {method: 'POST', headers, body: JSON.stringify(body)}); } async postJson(url, body) { @@ -91,7 +88,7 @@ export class OllamaProvider extends LLMProvider { async summarize(prompt) { const data = await this.postJson(this.apiUrl, { model: this.model, - prompt: prompt, + prompt, stream: false, }); return data.response; @@ -100,7 +97,7 @@ export class OllamaProvider extends LLMProvider { async summarizeStream(prompt, onChunk) { const response = await this.post(this.apiUrl, { model: this.model, - prompt: prompt, + prompt, stream: true, }); From fe0aea54f3b19da5513f86b8c71aecfdd0968fb8 Mon Sep 17 00:00:00 2001 From: Gabriel Sroka Date: Wed, 25 Sep 2024 15:10:42 -0700 Subject: [PATCH 3/4] shorten code --- README.md | 2 +- content.js | 6 +++--- manifest.json | 2 +- background.js => service_worker.js | 2 +- sidepanel.js | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) rename background.js => service_worker.js (99%) diff --git a/README.md b/README.md index 83cbb0e..5dc880e 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ FastDigest is a Chrome extension designed to summarize Hacker News comments usin 2. **Configure LLM Provider** - - Go to background.js. At the top, you will see a variable to set your OpenAI API Key. + - Go to service_worker.js. At the top, you will see a variable to set your OpenAI API Key. - FastDigest also supports Ollama(llama3.1) for using local LLMs. To use Ollama, you will need to do the following: - shut down Ollama if it is currently running - run `launchctl setenv OLLAMA_ORIGINS "*"` diff --git a/content.js b/content.js index 04c913e..8c09d4f 100644 --- a/content.js +++ b/content.js @@ -147,7 +147,7 @@ async function summarizeComment(commentElement) { commentsArray.push([parentComment, ...currentChunk]); } - // Send the array of chunks to the background script in one message + // Send the array of chunks to the service_worker script in one message try { await chrome.runtime.sendMessage({ action: "summarize_comments", @@ -261,7 +261,7 @@ async function summarizeAllComments() { commentsArray.push([...currentChunk]); } - // Send the array of chunks to the background script + // Send the array of chunks to the service_worker script try { await chrome.runtime.sendMessage({ action: "summarize_comments", @@ -296,7 +296,7 @@ async function sendSummarizationRequest(commentsText, accumulatedSummary) { window.addEventListener("load", () => { addSummarizeButtons(); - // listen for alert messages from the background script + // listen for alert messages from the service_worker script chrome.runtime.onMessage.addListener((message) => { if (message.action === "alert") { alert(message.message); diff --git a/manifest.json b/manifest.json index 0857f33..8dd8843 100644 --- a/manifest.json +++ b/manifest.json @@ -11,7 +11,7 @@ "activeTab","tabs","sidePanel" ], "background": { - "service_worker": "background.js", + "service_worker": "service_worker.js", "type": "module" }, "host_permissions": ["http://*/*", "https://*/*", "*://*/*"], diff --git a/background.js b/service_worker.js similarity index 99% rename from background.js rename to service_worker.js index 0e5ee0a..8e806b6 100644 --- a/background.js +++ b/service_worker.js @@ -35,7 +35,7 @@ chrome.tabs.onUpdated.addListener(async (tabId, info, tab) => { } }); -// background.js +// service_worker.js chrome.runtime.onMessage.addListener(async (message, sender, sendResponse) => { if (message.action === "summarize_comments") { if (isSummarizing) { diff --git a/sidepanel.js b/sidepanel.js index 7202e43..71c2a4c 100644 --- a/sidepanel.js +++ b/sidepanel.js @@ -55,7 +55,7 @@ chrome.runtime.onMessage.addListener((message) => { } }); -// Ensure the listener is set up before sending messages from the background +// Ensure the listener is set up before sending messages from the service_worker console.log("Side panel script loaded and listener set up."); chrome.runtime.sendMessage({ action: "sidepanel_ready" }); From 4806f4f62c366fc53a76cf2dec39bd3d016ac675 Mon Sep 17 00:00:00 2001 From: Gabriel Sroka Date: Fri, 27 Sep 2024 11:00:30 -0700 Subject: [PATCH 4/4] oops --- llm.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llm.js b/llm.js index d8819b1..5885692 100644 --- a/llm.js +++ b/llm.js @@ -15,7 +15,7 @@ class LLMProvider { } async postJson(url, body) { - const r = await post(url, body); + const r = await this.post(url, body); return r.json(); } }