feat: tab suggestions

This commit is contained in:
Fabian Freund
2025-07-24 23:47:34 +02:00
parent f0d17e78d7
commit 47151a2ba7
30 changed files with 1293 additions and 180 deletions
@@ -46,9 +46,9 @@ const SMART_TAB_GROUPING_CONFIG = {
*/
function createModelInput(keywords, documents) {
if (!keywords || keywords.length === 0) {
return `Topic from keywords: titles: \n${documents.join(" \n")}`;
return `Topic from keywords: titles: \n${documents.slice(0, 3).join(" \n")}`;
}
return `Topic from keywords: ${keywords.join(", ")}. titles: \n${documents.join(" \n")}`;
return `Topic from keywords: ${keywords.join(", ")}. titles: \n${documents.slice(0, 3).join(" \n")}`;
}
/**
@@ -81,7 +81,6 @@ function cutAtDuplicateWords(phrase) {
return phrase; // return original phrase
}
/**
*
* @param {MLEngine} engine the engine to check
@@ -96,7 +95,29 @@ this.ml = class extends ExtensionAPI {
return {
experiments: {
ml: {
async containerTopic(keywords, documents) {
async generateEmbeddings(textToEmbedList) {
const inputData = {
inputArgs: textToEmbedList,
runOptions: {
pooling: "mean",
normalize: true,
},
};
if (isEngineClosed(this.embeddingEngine)) {
this.embeddingEngine = await createEngine(SMART_TAB_GROUPING_CONFIG.embedding);
}
const request = {
args: [inputData.inputArgs],
options: inputData.runOptions,
};
const generated = await this.embeddingEngine.run(request);
return JSON.stringify(generated);
},
async predictTopic(keywords, documents) {
if (isEngineClosed(this.topicEngine)) {
const {
featureId,
@@ -26,15 +26,25 @@ function sendErrorForRequest(id) {
port.onMessage.addListener(async (message) => {
let requestId = message["id"]
switch (message["action"]) {
case "getContainerTopic":
case "predictDocumentTopic": {
const documents = message["args"];
const keywords = (documents.length > 1)
? await browser.experiments.nlp.extractKeywords([documents.slice(0, 3).join(" ")])
: [[]];
browser.experiments.ml.containerTopic(keywords[0], documents)
browser.experiments.ml.predictTopic(keywords[0], documents)
.then(sendJsonResultForRequest(requestId))
.catch(sendErrorForRequest(requestId))
break
.catch(sendErrorForRequest(requestId));
break;
}
case "generateDocumentEmbeddings": {
const documents = message["args"];
await browser.experiments.ml.generateEmbeddings(documents)
.then(sendJsonResultForRequest(requestId))
.catch(sendErrorForRequest(requestId));
break;
}
}
});
@@ -33,7 +33,23 @@
"description": "Machine Learning utilities",
"functions": [
{
"name": "containerTopic",
"name": "generateEmbeddings",
"type": "function",
"description": "Generate embeddings for a list of text strings using ML engine",
"async": true,
"parameters": [
{
"name": "textToEmbedList",
"type": "array",
"items": {
"type": "string"
},
"description": "Array of text strings to generate embeddings for"
}
]
},
{
"name": "predictTopic",
"type": "function",
"description": "Generate topic from keywords and documents using ML engine",
"async": true,