aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorOmar Magdy <99906646+omagdy7@users.noreply.github.com>2023-12-11 12:55:02 +0200
committerGitHub <noreply@github.com>2023-12-11 12:55:02 +0200
commitd215eea3cde99707380d1e8ee3e2daddb3b26ebf (patch)
tree382f63adc9e56658dc77d4115db620d1dfc9cdcb /src
parent5b671c04685acd36b49a20d0104829f6f91e728f (diff)
parentf4a66c83658c354a9c5e3d7f2476de4e429cae94 (diff)
downloadollama-logseq-d215eea3cde99707380d1e8ee3e2daddb3b26ebf.tar.xz
ollama-logseq-d215eea3cde99707380d1e8ee3e2daddb3b26ebf.zip
Merge pull request #10 from taweili/master
Use closure to make creating new menu event easier and added a way to add extra block menu commands through reading the props of ollama-logseq-page
Diffstat (limited to 'src')
-rw-r--r--src/App.tsx29
-rw-r--r--src/ollama.tsx73
2 files changed, 61 insertions, 41 deletions
diff --git a/src/App.tsx b/src/App.tsx
index 6ab84a8..b6b6918 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";
@@ -46,16 +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: 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: Expand Block", promptFromBlockEventClosure("Expand: "))
+
logseq.App.registerCommandShortcut(
{ "binding": logseq.settings.shortcut },
ollamaUI
- );
+ );
}, [])
if (visible) {
diff --git a/src/ollama.tsx b/src/ollama.tsx
index ca493ac..5ba47ab 100644
--- a/src/ollama.tsx
+++ b/src/ollama.tsx
@@ -97,8 +97,7 @@ async function ollamaGenerate(prompt: string, parameters?: OllamaGenerateParamet
throw new Error("Error in Ollama request: " + response.statusText)
}
const data = await response.json()
- return data.response
-
+ return data
} catch (e: any) {
console.error("ERROR: ", e)
logseq.App.showMsg("Coudln't fulfull request make sure that ollama service is running and make sure there is no typo in host or model name")
@@ -199,30 +198,48 @@ async function getOllamaParametersFromBlockProperties(b: BlockEntity) {
return ollamaParameters
}
-export async function promptFromBlockEvent(b: IHookEvent) {
- try {
- const currentBlock = await logseq.Editor.getBlock(b.uuid)
- 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);
+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}
+}
- await logseq.Editor.updateBlock(answerBlock!.uuid, `${response}`)
- } catch (e: any) {
- logseq.UI.showMsg(e.toString(), 'warning')
- console.error(e)
+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)
+
+ let prompt = block!.content.replace(/^.*::.*$/gm, '') // hack to remove properties from block content
+ if (prefix) {
+ prompt = prefix + " " + prompt
+ }
+ console.log("prompt", prompt)
+
+ const result = await ollamaGenerate(prompt, params);
+
+ console.log("ollama response", result)
+
+ 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)
+ }
}
}
@@ -244,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 })