Skip to content

Commit

Permalink
Call llm endpoint from JS
Browse files Browse the repository at this point in the history
Also adjusted the buttons to toggle the summary
  • Loading branch information
Endle authored Sep 1, 2024
1 parent ec610aa commit d7e6911
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 62 deletions.
174 changes: 117 additions & 57 deletions fireSeqSearch_addon/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// MIT License
// Copyright (c) 2021-2023 Zhenbo Li
// Copyright (c) 2021-2024 Zhenbo Li

const fireSeqSearchDomId = "fireSeqSearchDom";

Expand Down Expand Up @@ -128,79 +128,103 @@ function checkUserOptions() {
ShowHighlight: res[2].ShowHighlight,
ShowScore: res[3].ShowScore
}
consoleLogForDebug(options);
return options;
});
}


async function appendResultToSearchResult(fetchResultArray, _container) {
const serverInfo = fetchResultArray[0];
const rawSearchResult = fetchResultArray[1];
const firefoxExtensionUserOption = await checkUserOptions();

consoleLogForDebug('Loaded user option: ' + JSON.stringify(firefoxExtensionUserOption));

function createTitleBarDom(count) {
const titleBar = createElementWithText("div");
titleBar.classList.add('fireSeqSearchTitleBar');
const hitCount = `<span>We found <b>${count.toString()}</b> results in your logseq notebook</span>`;
titleBar.insertAdjacentHTML("afterbegin",hitCount);
const btn = document.createElement("button");
btn.classList.add("hideSummary");
const text = document.createTextNode("Hide Summary (Tmp)");
btn.appendChild(text);
btn.onclick = function () {
// alert("Button is clicked");
for (const el of document.querySelectorAll('.fireSeqSearchHitSummary')) {
// el.style.visibility = 'hidden';
el.remove();
}
};
titleBar.appendChild(btn);
return titleBar;
function parseRawList(rawSearchResult) {
const hits = [];
for (const rawRecord of rawSearchResult) {
const record = JSON.parse(rawRecord);
hits.push(record);
}
function createFireSeqDom() {
const div = document.createElement("div");
div.setAttribute("id", fireSeqSearchDomId);
return div;
return hits;
}

function createTitleBarDom(count) {
const titleBar = createElementWithText("div");
titleBar.classList.add('fireSeqSearchTitleBar');
const hitCount = `<span>We found <b>${count.toString()}</b> results in your logseq notebook</span>`;
titleBar.insertAdjacentHTML("afterbegin",hitCount);

function setSummaryState(cl, state) {
let prop = 'none';
if (state) { prop = ''; }
for (const el of document.querySelectorAll(cl)) {
el.style.display=prop;
}
}

const dom = createFireSeqDom();
dom.appendChild(createTitleBarDom(rawSearchResult.length));
consoleLogForDebug(dom);
let btn = document.createElement("button");
btn.classList.add("hideSummary");
let text = document.createTextNode("Hide Summary");
btn.appendChild(text);
btn.onclick = function () {
setSummaryState(".fireSeqSearchHitSummary", false);
setSummaryState(".fireSeqSearchLlmSummary", false);
};
titleBar.appendChild(btn);

btn = document.createElement("button");
btn.classList.add("showSummary");
text = document.createTextNode("Summary");
btn.appendChild(text);
btn.onclick = function () {
setSummaryState(".fireSeqSearchHitSummary", true);
setSummaryState(".fireSeqSearchLlmSummary", false);
};
titleBar.appendChild(btn);


btn = document.createElement("button");
btn.classList.add("showLlm");
text = document.createTextNode("LLM");
btn.appendChild(text);
btn.onclick = function () {
setSummaryState(".fireSeqSearchHitSummary", false);
setSummaryState(".fireSeqSearchLlmSummary", true);
};
titleBar.appendChild(btn);


return titleBar;
}
function createFireSeqDom(count) {
const div = document.createElement("div");
div.setAttribute("id", fireSeqSearchDomId);
const bar = createTitleBarDom(count);
div.appendChild(bar);
return div;
}

const hitList = document.createElement("ul");
async function appendResultToSearchResult(serverInfo, parsedSearchResult, dom) {
const firefoxExtensionUserOption = await checkUserOptions();

consoleLogForDebug(rawSearchResult);
for (const rawRecord of rawSearchResult) {
// const e = document.createTextNode(record);
consoleLogForDebug(rawRecord);
const record = JSON.parse(rawRecord);
consoleLogForDebug(typeof record);
consoleLogForDebug('Loaded user option: ' + JSON.stringify(firefoxExtensionUserOption));

const li = createElementWithText("li", "");

function buildListItems(parsedSearchResult) {
const hitList = document.createElement("ul");
for (const record of parsedSearchResult) {
const li = createElementWithText("li", "");
if (firefoxExtensionUserOption.ShowScore) {
const score = createElementWithText("span", String(record.score));
li.appendChild(score);
}
const href = createHrefToLogseq(record, serverInfo);
li.appendChild(href);

if (firefoxExtensionUserOption.ShowScore) {
const score = createElementWithText("span", String(record.score));
li.appendChild(score);
}
const href = createHrefToLogseq(record, serverInfo);
li.appendChild(href);
li.append(' ')
if (firefoxExtensionUserOption.ShowHighlight) {
const summary = createElementWithText("span", "");
summary.innerHTML = record.summary;
summary.classList.add('fireSeqSearchHitSummary');
li.appendChild(summary);
}
// let e = wrapRawRecordIntoElement(record, serverInfo);

// e.style.
hitList.appendChild(li);
// consoleLogForDebug("Added an element to the list");
hitList.appendChild(li);
}
return hitList;
}
const hitList = buildListItems(parsedSearchResult);
dom.appendChild(hitList);

if (firefoxExtensionUserOption.ExperimentalLayout) {
Expand Down Expand Up @@ -228,6 +252,41 @@ async function appendResultToSearchResult(fetchResultArray, _container) {
insertDivToWebpage(dom);
}

async function processLlmSummary(serverInfo, parsedSearchResult, dom) {
for (const record of parsedSearchResult) {
// TODO remove hard code port
const llm_api = "http://127.0.0.1:3030/summarize/" + record.title;
console.log("llm called");
console.log(record.title);
const response = await fetch(llm_api);
const text = await response.text();
console.log(text);
}
}

async function mainProcess(fetchResultArray) {
consoleLogForDebug("main process");

const serverInfo = fetchResultArray[0];
const rawSearchResult = fetchResultArray[1];
consoleLogForDebug(serverInfo);
const parsedSearchResult = parseRawList(rawSearchResult);

console.log("in main");
console.log(rawSearchResult);
console.log(parsedSearchResult);

const fireDom = createFireSeqDom(parsedSearchResult.length);

appendResultToSearchResult(serverInfo, parsedSearchResult, fireDom);

if (serverInfo.llm_enabled) {
consoleLogForDebug("llm");
processLlmSummary(serverInfo, parsedSearchResult, fireDom);
}
}


function getSearchParameterFromCurrentPage() {
let searchParam = "";

Expand Down Expand Up @@ -269,8 +328,9 @@ function getSearchParameterFromCurrentPage() {
]).then(function (responses) {
return Promise.all(responses.map(function (response) {return response.json();}));
}).then(function (data) {
consoleLogForDebug(data);
return appendResultToSearchResult(data);
//consoleLogForDebug(data);
mainProcess(data);
//return appendResultToSearchResult(data);
}).then((_e) => {
const highlightedItems = document.querySelectorAll('.fireSeqSearchHighlight');
consoleLogForDebug(highlightedItems);
Expand Down
2 changes: 1 addition & 1 deletion fireSeqSearch_addon/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "fireSeqSearch",
"version": "0.1.4",
"version": "0.2.2",

"description": "Everytime you use the search engine, this plugin will search against your personal logseq notes.",

Expand Down
3 changes: 2 additions & 1 deletion fire_seq_search_server/debug_server_mac.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ cp target/debug/fire_seq_search_server ./fire_seq_search_server
export RUST_LOG="warn,fire_seq_search_server=info"
#export RUST_LOG="debug"
export RUST_BACKTRACE=1
./fire_seq_search_server --notebook_path ~/logseq --enable-journal-query
./fire_seq_search_server --notebook_path ~/logseq
#--enable-journal-query
1 change: 1 addition & 0 deletions fire_seq_search_server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ pub fn generate_server_info_for_test() -> ServerInformation {
obsidian_md: false,
convert_underline_hierarchy: true,
host: "127.0.0.1:22024".to_string(),
llm_enabled: false,
};
server_info
}
Expand Down
5 changes: 5 additions & 0 deletions fire_seq_search_server/src/local_llm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ impl LlmEngine{

}

pub async fn quick_fetch(&self, title: &str) -> Option<String> {
let jcache = self.job_cache.lock().await;
return jcache.done_job.get(title).cloned();
}

pub async fn health(&self) -> Result<(), Box<dyn std::error::Error>> {
info!("Calling health check");
let resp = reqwest::get(self.endpoint.to_owned() + "/health")
Expand Down
2 changes: 2 additions & 0 deletions fire_seq_search_server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ async fn main() {
llm_poll.call_llm_engine().await;
let wait_llm = tokio::time::Duration::from_millis(500);
tokio::time::sleep(wait_llm).await;
info!("main loop: poll again");
}
});
// poll_handle.await;
Expand Down Expand Up @@ -132,6 +133,7 @@ fn build_server_info(args: Cli) -> ServerInformation {
obsidian_md: args.obsidian_md,
convert_underline_hierarchy: true,
host,
llm_enabled: cfg!(feature="llm"),
}
}

Expand Down
19 changes: 17 additions & 2 deletions fire_seq_search_server/src/query_engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub struct ServerInformation {
pub convert_underline_hierarchy: bool,

pub host: String,

pub llm_enabled: bool,
}

use crate::language_tools::tokenizer::FireSeqTokenizer;
Expand Down Expand Up @@ -142,11 +144,24 @@ impl QueryEngine {
}

impl QueryEngine {
async fn wait_for_summarize(&self, title: String) -> String {
let llm = self.llm.as_ref().unwrap();
let wait_llm = tokio::time::Duration::from_millis(50);
// TODO maybe add a guard to make sure don't wait too long?
loop {
let result = llm.quick_fetch(&title).await;
match result {
Some(s) => { return s; },
None => { }
};
tokio::time::sleep(wait_llm).await;
}
// llm.summarize(&title).await
}
pub async fn summarize(&self, title: String) -> String {
info!("Called summarize on {}", &title);
if cfg!(feature="llm") {
let llm = self.llm.as_ref().unwrap();
llm.summarize(&title).await
self.wait_for_summarize(title).await
} else {
"LLM turned off".to_owned()
}
Expand Down
2 changes: 1 addition & 1 deletion pack_firefox_extension.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cd fireSeqSearch_addon
zip -r -FS ../fireSeqSearch.zip * --exclude '*.git*' --exclude "monkeyscript.user.js" --exclude "violentmonkeyscript.user.js"
cd ..
cp -f fireSeqSearch.zip /dev/shm
cp -f fireSeqSearch.zip ~/Downloads #/dev/shm

0 comments on commit d7e6911

Please sign in to comment.