From c6b9988e027f186262a1b65700edbec1ed6ed7d1 Mon Sep 17 00:00:00 2001 From: David Li Date: Sat, 2 Dec 2023 21:12:59 +0800 Subject: Testing out ideas with context --- src/ollama.tsx | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/ollama.tsx b/src/ollama.tsx index f792613..a59b03b 100644 --- a/src/ollama.tsx +++ b/src/ollama.tsx @@ -101,10 +101,8 @@ async function ollamaGenerate(prompt: string, parameters?: OllamaGenerateParamet throw new Error("Error in Ollama request: " + response.statusText) } const data = await response.json() - - console.log(data) - return data.response + return data } catch (e: any) { console.log(e) logseq.UI.showMsg("Error in Ollama request") @@ -212,9 +210,14 @@ export async function promptFromBlockEvent(b: IHookEvent) { const answerBlock = await logseq.Editor.insertBlock(currentBlock!.uuid, '🦙Generating ...', { before: false }) const params = await getOllamaParametersFromBlockProperties(currentBlock!) const prompt = currentBlock!.content.replace(/^.*::.*$/gm, '') // nasty hack to remove properties from block content - const response = await ollamaGenerate(prompt, params); + const result = await ollamaGenerate(prompt, params); - await logseq.Editor.updateBlock(answerBlock!.uuid, `${response}`) + console.log(result) + + if (params.usecontext) { + await logseq.Editor.upsertBlockProperty(currentBlock!.uuid, 'ollama-generate-context', result.context) + } + await logseq.Editor.updateBlock(answerBlock!.uuid, `${result.response}`) } catch (e: any) { logseq.UI.showMsg(e.toString(), 'warning') console.error(e) -- cgit v1.2.3 From 5edf9a522deaed225d15724fe32d65124a5af590 Mon Sep 17 00:00:00 2001 From: David Li Date: Mon, 4 Dec 2023 14:13:07 +0800 Subject: Use the ollama properties from the parent block --- src/ollama.tsx | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/ollama.tsx b/src/ollama.tsx index 38cb0f6..c6db897 100644 --- a/src/ollama.tsx +++ b/src/ollama.tsx @@ -198,15 +198,26 @@ async function getOllamaParametersFromBlockProperties(b: BlockEntity) { return ollamaParameters } -export async function promptFromBlockEvent(b: IHookEvent) { +export async function promptFromBlockEvent(event: IHookEvent) { try { - const currentBlock = await logseq.Editor.getBlock(b.uuid) + const currentBlock = await logseq.Editor.getBlock(event.uuid) const answerBlock = await logseq.Editor.insertBlock(currentBlock!.uuid, '🦙Generating ...', { before: false }) - const params = await getOllamaParametersFromBlockProperties(currentBlock!) + let p_params: OllamaGenerateParameters = {} + + if (currentBlock?.parent) { + let parentBlock = await logseq.Editor.getBlock(currentBlock.parent.id) + if (parentBlock) + p_params = await getOllamaParametersFromBlockProperties(parentBlock) + } + const c_params = await getOllamaParametersFromBlockProperties(currentBlock!) + const params = { ...p_params, ...c_params } + + console.log("params", params) + const prompt = currentBlock!.content.replace(/^.*::.*$/gm, '') // nasty hack to remove properties from block content const result = await ollamaGenerate(prompt, params); - console.log(result) + console.log("result", result) if (params.usecontext) { await logseq.Editor.upsertBlockProperty(currentBlock!.uuid, 'ollama-generate-context', result.context) -- cgit v1.2.3 From c487b7b37a9f9e2de82d386c30ea7d62097a847e Mon Sep 17 00:00:00 2001 From: David Li Date: Mon, 4 Dec 2023 16:17:48 +0800 Subject: Abstract out the prompt and block event functions into a closure so it can be use to create new menu events with different prompt prefix. --- src/App.tsx | 13 +++++----- src/ollama.tsx | 80 +++++++++++++++++++++++++--------------------------------- 2 files changed, 42 insertions(+), 51 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 6ab84a8..3e50f38 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -4,9 +4,7 @@ import { convertToFlashCardFromEvent, DivideTaskIntoSubTasksFromEvent, ollamaUI, - summarizeBlockFromEvent, - promptFromBlockEvent, - expandBlockEvent + promptFromBlockEventClosure } from "./ollama"; import { useAppVisible } from "./utils"; @@ -48,10 +46,13 @@ function App() { } logseq.Editor.registerSlashCommand("ollama", ollamaUI) logseq.Editor.registerBlockContextMenuItem("Ollama: Create a flash card", convertToFlashCardFromEvent) - logseq.Editor.registerBlockContextMenuItem("Ollama: Summarize block", summarizeBlockFromEvent) logseq.Editor.registerBlockContextMenuItem("Ollama: Divide into subtasks", DivideTaskIntoSubTasksFromEvent) - logseq.Editor.registerBlockContextMenuItem("Ollama: Prompt from Block", promptFromBlockEvent) - logseq.Editor.registerBlockContextMenuItem("Ollama: Expand Block", expandBlockEvent) + logseq.Editor.registerBlockContextMenuItem("Ollama: Prompt from Block", promptFromBlockEventClosure()) + logseq.Editor.registerBlockContextMenuItem("Ollama: Summarize block", promptFromBlockEventClosure("Summarize: ")) + logseq.Editor.registerBlockContextMenuItem("Ollama: 总结 Block", promptFromBlockEventClosure("总结: ")) + logseq.Editor.registerBlockContextMenuItem("Ollama: Expand Block", promptFromBlockEventClosure("Expand: ")) + logseq.Editor.registerBlockContextMenuItem("Ollama: 扩展 Block", promptFromBlockEventClosure("扩展: ")) + logseq.App.registerCommandShortcut( { "binding": logseq.settings.shortcut }, ollamaUI diff --git a/src/ollama.tsx b/src/ollama.tsx index c6db897..5ba47ab 100644 --- a/src/ollama.tsx +++ b/src/ollama.tsx @@ -198,46 +198,48 @@ async function getOllamaParametersFromBlockProperties(b: BlockEntity) { return ollamaParameters } -export async function promptFromBlockEvent(event: IHookEvent) { - try { - const currentBlock = await logseq.Editor.getBlock(event.uuid) - const answerBlock = await logseq.Editor.insertBlock(currentBlock!.uuid, '🦙Generating ...', { before: false }) - let p_params: OllamaGenerateParameters = {} - - if (currentBlock?.parent) { - let parentBlock = await logseq.Editor.getBlock(currentBlock.parent.id) - if (parentBlock) - p_params = await getOllamaParametersFromBlockProperties(parentBlock) - } - const c_params = await getOllamaParametersFromBlockProperties(currentBlock!) - const params = { ...p_params, ...c_params } +async function getOllamaParametersFromBlockAndParentProperties(b: BlockEntity) { + let p_params: OllamaGenerateParameters = {} + if (b.parent) { + let parentBlock = await logseq.Editor.getBlock(b.parent.id) + if (parentBlock) + p_params = await getOllamaParametersFromBlockProperties(parentBlock) + } + const b_params = await getOllamaParametersFromBlockProperties(b) + return {...p_params, ...b_params} +} + +async function promptFromBlock(block: BlockEntity, prefix?: string) { + const answerBlock = await logseq.Editor.insertBlock(block!.uuid, '🦙Generating ...', { before: false }) + const params = await getOllamaParametersFromBlockAndParentProperties(block!) + console.log("ollama params", params) - console.log("params", params) + let prompt = block!.content.replace(/^.*::.*$/gm, '') // hack to remove properties from block content + if (prefix) { + prompt = prefix + " " + prompt + } + console.log("prompt", prompt) - const prompt = currentBlock!.content.replace(/^.*::.*$/gm, '') // nasty hack to remove properties from block content - const result = await ollamaGenerate(prompt, params); + const result = await ollamaGenerate(prompt, params); - console.log("result", result) + console.log("ollama response", result) - if (params.usecontext) { - await logseq.Editor.upsertBlockProperty(currentBlock!.uuid, 'ollama-generate-context', result.context) - } - await logseq.Editor.updateBlock(answerBlock!.uuid, `${result.response}`) - } catch (e: any) { - logseq.UI.showMsg(e.toString(), 'warning') - console.error(e) + if (params.usecontext) { //FIXME: work out the best way to story context + await logseq.Editor.upsertBlockProperty(block!.uuid, 'ollama-generate-context', result.context) } + + await logseq.Editor.updateBlock(answerBlock!.uuid, `${result.response}`) } -export async function expandBlockEvent(b: IHookEvent) { - try { - const currentBlock = await logseq.Editor.getBlock(b.uuid) - const answerBlock = await logseq.Editor.insertBlock(currentBlock!.uuid, '⌛Generating ...', { before: false }) - const response = await promptLLM(`Expand: ${currentBlock!.content}`); - await logseq.Editor.updateBlock(answerBlock!.uuid, `${response}`) - } catch (e: any) { - logseq.UI.showMsg(e.toString(), 'warning') - console.error(e) +export function promptFromBlockEventClosure(prefix?: string) { + return async (event: IHookEvent) => { + try { + const currentBlock = await logseq.Editor.getBlock(event.uuid) + await promptFromBlock(currentBlock!, prefix) + } catch (e: any) { + logseq.UI.showMsg(e.toString(), 'warning') + console.error(e) + } } } @@ -259,18 +261,6 @@ export async function askAI(prompt: string, context: string) { } } -export async function summarizeBlockFromEvent(b: IHookEvent) { - try { - const currentBlock = await logseq.Editor.getBlock(b.uuid) - let summaryBlock = await logseq.Editor.insertBlock(currentBlock!.uuid, `⌛Summarizing Block...`, { before: true }) - const summary = await promptLLM(`Summarize the following ${currentBlock!.content}`); - await logseq.Editor.updateBlock(summaryBlock!.uuid, `Summary: ${summary}`) - } catch (e: any) { - logseq.App.showMsg(e.toString(), 'warning') - console.error(e) - } -} - export async function convertToFlashCard(uuid: string, blockContent: string) { try { const questionBlock = await logseq.Editor.insertBlock(uuid, "⌛Genearting question....", { before: false }) -- cgit v1.2.3 From f4a66c83658c354a9c5e3d7f2476de4e429cae94 Mon Sep 17 00:00:00 2001 From: David Li Date: Sun, 10 Dec 2023 21:54:44 +0800 Subject: Use a configuration page instead of hard coding the context menu commands. --- src/App.tsx | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 3e50f38..b6b6918 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -44,19 +44,33 @@ function App() { if (!logseq.settings) { return } + +logseq.Editor.getPageBlocksTree("ollama-logseq-config").then((blocks) => { + blocks!.forEach((block) => { + logseq.Editor.getBlockProperty(block.uuid, "ollama-context-menu-title").then((title) => { + logseq.Editor.getBlockProperty(block.uuid, "ollama-prompt-prefix").then((prompt_prefix) => { + logseq.Editor.registerBlockContextMenuItem(title, promptFromBlockEventClosure(prompt_prefix)) + }) + }).catch((reason) => { + }) + }) +}).catch((reason) => { + console.log("Can not find the configuration page named 'ollama-logseq-config'") + console.log(reason) +}) + + logseq.Editor.registerSlashCommand("ollama", ollamaUI) logseq.Editor.registerBlockContextMenuItem("Ollama: Create a flash card", convertToFlashCardFromEvent) logseq.Editor.registerBlockContextMenuItem("Ollama: Divide into subtasks", DivideTaskIntoSubTasksFromEvent) logseq.Editor.registerBlockContextMenuItem("Ollama: Prompt from Block", promptFromBlockEventClosure()) logseq.Editor.registerBlockContextMenuItem("Ollama: Summarize block", promptFromBlockEventClosure("Summarize: ")) - logseq.Editor.registerBlockContextMenuItem("Ollama: 总结 Block", promptFromBlockEventClosure("总结: ")) logseq.Editor.registerBlockContextMenuItem("Ollama: Expand Block", promptFromBlockEventClosure("Expand: ")) - logseq.Editor.registerBlockContextMenuItem("Ollama: 扩展 Block", promptFromBlockEventClosure("扩展: ")) logseq.App.registerCommandShortcut( { "binding": logseq.settings.shortcut }, ollamaUI - ); + ); }, []) if (visible) { -- cgit v1.2.3