Skip to content

Commit

Permalink
Added new model and custom prompt to bard.
Browse files Browse the repository at this point in the history
  • Loading branch information
frianasoa committed Dec 15, 2023
1 parent fc25d30 commit 718ba9f
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 24 deletions.
53 changes: 50 additions & 3 deletions content/notes/menus.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Menus = {

var items0 = {
"edit": {name: "Edit note", icon: "fa-pencil-alt"},
"editannotation": {name: "Edit annotation", icon: "fa-edit"},
"editannotationcomment": {name: "Edit annotation comments", icon: "fa-edit"},
"showannotation": {name: "Show annotation", icon: "fa-edit"},
"sep": "---------",
}
Expand Down Expand Up @@ -67,6 +67,7 @@ Menus = {
if(Zotero.ZeNotes.Prefs.getb("bard-api-key")!="")
{
items_ai["paraphrase-bard"] = {name: "Paraphrase annotation (Bard)", icon: "fa-b"};
items_ai["custom-prompt-bard"] = {name: "Custom prompt (Bard)", icon: "fa-b"};
items_ai["sep-ai-01"] = "---------";
}

Expand Down Expand Up @@ -247,7 +248,7 @@ Menus = {
this.opennote(notekey);
}
}
else if(key=="editannotation")
else if(key=="editannotationcomment")
{
if(!annotationkey)
{
Expand Down Expand Up @@ -334,6 +335,51 @@ Menus = {
});
}

else if(key=="custom-prompt-bard")
{
var customprompt = Zotero.ZeNotes.Prefs.get("bard-custom-prompt");
if(!annotationkey)
{
alert("Annotation not found!");
return;
}

if(Zotero.ZeNotes.Prefs.getb("bard-api-key")=="")
{
alert("Please set API key first.\nGo to ZeNotes > Settings > General Settings > AI API settings");
return;
}

var annotation = Zotero.Items.get(annotationid);
var currentcomment = annotation.annotationComment;
if(currentcomment==null)
{
currentcomment = "";
}
Zotero.ZeNotes.Ai.Bard.customprompt(annotation["annotationText"]).then(r=>{
var table = AiUi.createdialog(annotation, currentcomment, r, "bard");
var model = Zotero.ZeNotes.Prefs.get("bard-model");

var div = document.createElement("div");
div.innerHTML = "<h2>Custom prompt</h2> "+customprompt+"<br/><br/>"+annotation["annotationText"]+"<br/><br/>"
div.appendChild(table);

Dialog.open(div, function(){}, "Edit and choose a candidate [Bard: "+model+"]", "close");
}).catch(r=>{
var html = "";
if(Array.isArray(r))
{
html = r.join("<br/>");
}
else
{
html="-"+r;
}
Dialog.open(html, function(){
});
});
}

else if(key=="paraphrase-bard")
{
if(!annotationkey)
Expand All @@ -356,7 +402,8 @@ Menus = {
}
Zotero.ZeNotes.Ai.Bard.paraphrase(annotation["annotationText"]).then(r=>{
var table = AiUi.createdialog(annotation, currentcomment, r, "bard");
Dialog.open(table, function(){}, "Choose paraprahse [Bard]", "close");
var model = Zotero.ZeNotes.Prefs.get("bard-model");
Dialog.open(table, function(){}, "Edit and choose a paraphrase [Bard: "+model+"]", "close");
}).catch(r=>{
var html = "";
if(Array.isArray(r))
Expand Down
13 changes: 10 additions & 3 deletions content/settings/ai.xhtml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
<div xmlns="http:https://www.w3.org/1999/xhtml">
<table id="ai">
<tr><td>Bard API key</td><td colspan="2"><input id="zn-bard-api-key" type="password" onchange="Zotero_Preferences.ZeNotes.setpreference(event, 'bard-api-key', 'encrypt');" style="width:100%;" placeholder="Enter your Bard API key"/></td><td></td></tr>
<tr><td>Google translate API key</td><td colspan="2"><input id="zn-google-translate-key" type="password" onchange="Zotero_Preferences.ZeNotes.setpreference(event, 'google-translate-key', 'encrypt');" style="width:100%;" placeholder="Enter your google translate API key"/></td><td></td></tr>
<table id="ai" width="100%">
<tr>
<td>Bard API key</td><td><input id="zn-bard-api-key" type="password" onchange="Zotero_Preferences.ZeNotes.setpreference(event, 'bard-api-key', 'encrypt');" style="width:100%;" placeholder="Enter your Bard API key"/></td><td style="width:1.1em;"> </td>
<td>Model</td><td> <select id="zn-bard-model" onchange="Zotero_Preferences.ZeNotes.setpreference(event, 'bard-model');" style="width:100%;" placeholder="Model">
<option value="gemini-pro">Gemini Pro</option>
<option value="text-bison-001">Bison 1</option>
</select></td>
</tr>
<tr><td>Bard custom prompt</td><td colspan="4"><input id="zn-bard-custom-prompt" type="text" onchange="Zotero_Preferences.ZeNotes.setpreference(event, 'bard-custom-prompt');" style="width:100%;" placeholder="The annotation will be appended to your prompt."/></td></tr>
<tr><td>Google translate API key</td><td><input id="zn-google-translate-key" type="password" onchange="Zotero_Preferences.ZeNotes.setpreference(event, 'google-translate-key', 'encrypt');" style="width:100%;" placeholder="Enter your google translate API key"/></td><td></td></tr>
</table>
</div>
1 change: 1 addition & 0 deletions content/settings/display.xhtml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<div xmlns="http:https://www.w3.org/1999/xhtml">
<table id="display">
<!-- <tr><td><label for="zn-add-to-menu">Add ZeNotes to main menu</label></td><td><input type="checkbox" onchange="Zotero_Preferences.ZeNotes.setpreference(event, 'add-to-menu');" id="zn-add-to-menu"/></td><td></td></tr> -->
<tr><td><label for="zn-vertical-table">Vertical table</label></td><td><input type="checkbox" onchange="Zotero_Preferences.ZeNotes.setpreference(event, 'vertical-table');" id="zn-vertical-table"/></td><td></td></tr>
<tr><td>Highlight opacity</td><td><input type="range" min="0" max="255" value="255" id="zn-bg-slider" onchange="Zotero_Preferences.ZeNotes.setpreference(event, 'bg-opacity');" oninput="Zotero_Preferences.ZeNotes.updateopacity(event);"/></td><td class='highlight' style='background-color: #FFFF00; height: 1.1em;' id="zn-bg-sample">Lorem ipsum</td></tr>
<tr><td>Header size</td><td><input id="zn-header-size" type="range" value="100" min="20" max="1500" onchange="Zotero_Preferences.ZeNotes.setpreference(event, 'header-size');" oninput="Zotero_Preferences.ZeNotes.updatecolumnwidth(event);"/></td><td><input id="zn-header-size-val" /></td></tr>
Expand Down
5 changes: 4 additions & 1 deletion content/settings/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ Zotero_Preferences.ZeNotes = {
Zotero_Preferences.ZeNotes.loadpreference("html-filter", "zn-html-filter");
Zotero_Preferences.ZeNotes.loadpreference("html-filter-replacement", "zn-html-filter-replacement");

Zotero_Preferences.ZeNotes.loadpreference("add-to-menu", "zn-add-to-menu");
Zotero_Preferences.ZeNotes.loadpreference("load-on-change", "zn-reload-on-change");
Zotero_Preferences.ZeNotes.loadpreference("vertical-table", "zn-vertical-table");

Expand All @@ -314,8 +315,10 @@ Zotero_Preferences.ZeNotes = {

Zotero_Preferences.ZeNotes.loadpreference("bard-api-key", "zn-bard-api-key", "encrypt");

Zotero_Preferences.ZeNotes.loadpreference("google-translate-project-id", "zn-google-translate-project-id", "encrypt");
Zotero_Preferences.ZeNotes.loadpreference("google-translate-key", "zn-google-translate-key", "encrypt");

Zotero_Preferences.ZeNotes.loadpreference("bard-model", "zn-bard-model");
Zotero_Preferences.ZeNotes.loadpreference("bard-custom-prompt", "zn-bard-custom-prompt");
})
.catch(error => {
console.log('Error loading content: ' + error);
Expand Down
67 changes: 59 additions & 8 deletions core/ai.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Ai={
return res.json()
})
.then(data => {
if(mode=="bard")
if(mode=="text-bison-001")
{
try {
return Promise.resolve(data.candidates.map(function(v){return v.output}));
Expand All @@ -15,6 +15,16 @@ Ai={
return Promise.resolve([data.error.message]);
}
}
else if(mode=="gemini-pro")
{
// alert(JSON.stringify(data));
try {
return Promise.resolve(data.candidates.map(function(v){return v.content.parts.map(function(w){return w.text})}));
}
catch(e) {
return Promise.resolve([data.error.message]);
}
}
else if(mode=="g-translate")
{
try {
Expand Down Expand Up @@ -73,19 +83,41 @@ Ai.Bard = {
return this.sendprompt(sentence, prompts)
},

async summarize(sentence, ratio=1/4)
async summarize(sentence, ratio=1/3)
{
var prompts = "Summarize the following in about "+Math.round(sentence.split(" ").length*ratio)+" words:"
return this.sendprompt(sentence, prompts);
model = Zotero.ZeNotes.Prefs.get("bard-model");
if(model=="")
{
model = "gemini-pro";
}
var prompts = "Your are an academic. Summarize the following in about "+Math.round(sentence.split(" ").length*ratio)+" words:"
return this.sendprompt(sentence, prompts, model);
},

async paraphrase(sentence)
{
var prompts = "Paraphrase the following:"
return this.sendprompt(sentence, prompts)
model = Zotero.ZeNotes.Prefs.get("bard-model");
if(model=="")
{
model = "gemini-pro";
}

var prompts = "Your are an academic. Paraphrase the following. "
return this.sendprompt(sentence, prompts, model)
},

async sendprompt(sentence, prompts) {
async customprompt(sentence)
{
var model = Zotero.ZeNotes.Prefs.get("bard-model");
var prompts = Zotero.ZeNotes.Prefs.get("bard-custom-prompt");
if(model=="")
{
model = "gemini-pro";
}
return this.sendprompt(sentence, prompts+" \n\n"+sentence, model)
},

async sendprompt(sentence, prompts, model) {
var apikey = Zotero.ZeNotes.Prefs.getb("bard-api-key");
var url = "https://generativelanguage.googleapis.com/v1beta3/models/text-bison-001:generateText?key="+apikey;

Expand All @@ -99,6 +131,23 @@ Ai.Bard = {
"candidate_count": 3
}

if(model=="gemini-pro")
{
url = "https://generativelanguage.googleapis.com/v1/models/gemini-pro:generateContent?key="+apikey;

payload = {
"contents":
[
{
"role": "user",
"parts":[{"text": p}]
}
],
// "temperature": 1.0,
// "candidate_count": 3
}
}

var options = {
method: 'POST',
headers: {
Expand All @@ -107,7 +156,9 @@ Ai.Bard = {
},
body: JSON.stringify(payload),
}
return Ai.request(url, options, "bard");


return Ai.request(url, options, model);
},
}

Expand Down
11 changes: 10 additions & 1 deletion core/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ Menu = {
}
Ui.opentab("chrome:https://ze-notes/content/notes/notes.xhtml", title);
},

mainmenu(w){
var showmain = Prefs.get("add-to-menu");
showmain = showmain==true || showmain=="true";

let stringBundle = Services.strings.createBundle(
'chrome:https://ze-notes/locale/zenotes.properties'
);
Expand All @@ -36,7 +40,12 @@ Menu = {

// Add menu in front of tools
var toolsmenu = document.getElementById("toolsMenu");
menubar.insertBefore(menu, toolsmenu);

// if(showmain)
// {
menubar.insertBefore(menu, toolsmenu);
// }

ZeNotes.storeAddedElement(toolsmenu);

// Add popup
Expand Down
2 changes: 1 addition & 1 deletion install.rdf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Description about="urn:mozilla:install-manifest">
<em:id>[email protected]</em:id>
<em:name>ZeNotes</em:name>
<em:version>0.6.0</em:version>
<em:version>0.6.1</em:version>
<em:multiprocessCompatible>true</em:multiprocessCompatible>
<em:updateURL>https://raw.githubusercontent.com/frianasoa/zenotes/main/zenote-update.json</em:updateURL>
<em:homepageURL>https://github.com/frianasoa/zenotes</em:homepageURL>
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "Ze Notes",
"version": "0.6.0",
"version": "0.6.1",
"description": "Advanced notes manager",
"homepage_url": "https://github.com/frianasoa/zenotes",
"author": "Fanantenana Rianasoa Andriariniaina",
Expand Down
6 changes: 3 additions & 3 deletions zenote-update.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"[email protected]": {
"updates": [
{
"version": "0.6.0",
"update_link": "https://github.com/frianasoa/Ze-Notes/releases/download/v0.6.0/zenotes-v0.6.0.xpi",
"update_hash": "sha256:ee5538f06c17b8b49e7b22ef06a84b1b08fe38f7d5e7eac172156150edce2359",
"version": "0.6.1",
"update_link": "https://github.com/frianasoa/Ze-Notes/releases/download/v0.6.1/zenotes-v0.6.1.xpi",
"update_hash": "sha256:c173988f9f280d6a634ebc98d2da40822cd135bbfe443f1c161c22225986daa5",
"applications": {
"gecko": {
"strict_min_version": "60.0"
Expand Down
6 changes: 3 additions & 3 deletions zenote-update.rdf
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
<rdf:Seq>
<rdf:li>
<rdf:Description>
<ns1:version>0.6.0</ns1:version>
<ns1:version>0.6.1</ns1:version>
<ns1:targetApplication>
<rdf:Description>
<ns1:id>[email protected]</ns1:id>
<ns1:minVersion>5.0.0</ns1:minVersion>
<ns1:maxVersion>6.*</ns1:maxVersion>
<ns1:updateLink>https://github.com/frianasoa/Ze-Notes/releases/download/v0.6.0/zenotes-v0.6.0.xpi</ns1:updateLink>
<ns1:updateLink>https://github.com/frianasoa/Ze-Notes/releases/download/v0.6.1/zenotes-v0.6.1.xpi</ns1:updateLink>
</rdf:Description>
</ns1:targetApplication>

Expand All @@ -20,7 +20,7 @@
<ns1:id>[email protected]</ns1:id>
<ns1:minVersion>4.999</ns1:minVersion>
<ns1:maxVersion>6.*</ns1:maxVersion>
<ns1:updateLink>https://github.com/frianasoa/Ze-Notes/releases/download/v0.6.0/zenotes-v0.6.0.xpi</ns1:updateLink>
<ns1:updateLink>https://github.com/frianasoa/Ze-Notes/releases/download/v0.6.1/zenotes-v0.6.1.xpi</ns1:updateLink>
</rdf:Description>
</ns1:targetApplication>

Expand Down

0 comments on commit 718ba9f

Please sign in to comment.