aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoromagdy7 <omar.professional8777@gmail.com>2023-11-23 19:28:27 +0200
committeromagdy7 <omar.professional8777@gmail.com>2023-11-23 19:28:27 +0200
commit611881839990a284ea15c8465dc42f1326f8a98c (patch)
tree874e8de94996a700dbf5294ed33f8468843b85ac /src
parent13b20a208205f5682f50b6ea1f5d010aea7a7e7c (diff)
downloadollama-logseq-611881839990a284ea15c8465dc42f1326f8a98c.tar.xz
ollama-logseq-611881839990a284ea15c8465dc42f1326f8a98c.zip
Enhanced flashcard genertation
Diffstat (limited to 'src')
-rw-r--r--src/App.tsx3
-rw-r--r--src/components/OllamaCommandPallete.tsx4
-rw-r--r--src/ollama.tsx61
3 files changed, 37 insertions, 31 deletions
diff --git a/src/App.tsx b/src/App.tsx
index bd6aaa0..c132365 100644
--- a/src/App.tsx
+++ b/src/App.tsx
@@ -1,7 +1,7 @@
import { AppUserInfo } from "@logseq/libs/dist/LSPlugin";
import React, { useEffect, useRef, useState } from "react";
import { OllamaCommandPallete } from "./components/OllamaCommandPallete";
-import { convertToFlashCardFromEvent, DivideTaskIntoSubTasksFromEvent, ollamaUI } from "./ollama";
+import { convertToFlashCardFromEvent, DivideTaskIntoSubTasksFromEvent, ollamaUI, summarizeBlockFromEvent } from "./ollama";
import { useAppVisible } from "./utils";
const options = [
@@ -42,6 +42,7 @@ function App() {
}
logseq.Editor.registerSlashCommand("ollama", ollamaUI)
logseq.Editor.registerBlockContextMenuItem("Create a flash card", convertToFlashCardFromEvent)
+ logseq.Editor.registerBlockContextMenuItem("Summarize block", summarizeBlockFromEvent)
logseq.Editor.registerBlockContextMenuItem("Make task into subtasks", DivideTaskIntoSubTasksFromEvent)
logseq.App.registerCommandShortcut(
{ "binding": logseq.settings.shortcut },
diff --git a/src/components/OllamaCommandPallete.tsx b/src/components/OllamaCommandPallete.tsx
index b251318..10e3418 100644
--- a/src/components/OllamaCommandPallete.tsx
+++ b/src/components/OllamaCommandPallete.tsx
@@ -6,7 +6,7 @@ import {
CommandItem,
CommandList,
} from "@/components/ui/command"
-import { convertToFlashCard, DivideTaskIntoSubTasks, summarize, summarizeBlock } from "@/ollama";
+import { convertToFlashCard, convertToFlashCardCurrentBlock, DivideTaskIntoSubTasks, summarize, summarizeBlock } from "@/ollama";
import { PromptAI } from "./PromptAI";
export function OllamaCommandPallete({ options, theme }: { options: string[], theme: string }) {
@@ -30,7 +30,7 @@ export function OllamaCommandPallete({ options, theme }: { options: string[], th
break;
case "convert to flash card":
logseq.hideMainUI()
- convertToFlashCard()
+ convertToFlashCardCurrentBlock()
break;
default:
break;
diff --git a/src/ollama.tsx b/src/ollama.tsx
index a2a078d..df6a568 100644
--- a/src/ollama.tsx
+++ b/src/ollama.tsx
@@ -133,7 +133,7 @@ export async function summarize() {
blocksContent += block.content + "/n"
}
if (lastBlock) {
- lastBlock = await logseq.Editor.insertBlock(lastBlock.uuid, '🚀 Summarizing....', { before: true })
+ lastBlock = await logseq.Editor.insertBlock(lastBlock.uuid, '⌛ Summarizing Page....', { before: true })
}
const summary = await promptLLM(`Summarize the following ${blocksContent}`)
await logseq.Editor.updateBlock(lastBlock.uuid, `Summary: ${summary}`)
@@ -179,48 +179,53 @@ export async function askAI(prompt: string) {
}
}
-
-export async function convertToFlashCardFromEvent(b: IHookEvent) {
+export async function summarizeBlockFromEvent(b: IHookEvent) {
try {
const currentBlock = await logseq.Editor.getBlock(b.uuid)
- if (!currentBlock) {
- throw new Error("Block not found");
- }
- const block = await logseq.Editor.insertBlock(currentBlock.uuid, 'Generating....', { before: false })
- if (!block) {
- throw new Error("Block not found");
+ if (currentBlock) {
+ let summaryBlock = await logseq.Editor.insertBlock(currentBlock.uuid, `⌛Summarizing Block...`, { before: true })
+ if (summaryBlock) {
+ const summary = await promptLLM(`Summarize the following ${currentBlock.content}`);
+ await logseq.Editor.updateBlock(summaryBlock.uuid, `Summary: ${summary}`)
+ }
}
- const response = await promptLLM(`Create a flashcard for:\n ${currentBlock.content}`)
- await logseq.Editor.updateBlock(block.uuid, `${response} #card`)
} catch (e: any) {
logseq.App.showMsg(e.toString(), 'warning')
console.error(e)
}
}
+export async function convertToFlashCardFromEvent(b: IHookEvent) {
+ const currentBlock = await logseq.Editor.getBlock(b.uuid)
+ if (!currentBlock) {
+ throw new Error("Block not found");
+ }
+ convertToFlashCard(currentBlock.uuid, currentBlock.content)
+}
+
+export async function convertToFlashCardCurrentBlock() {
+ const currentBlock = await logseq.Editor.getCurrentBlock()
+ if (!currentBlock) {
+ throw new Error("Block not found");
+ }
+ convertToFlashCard(currentBlock.uuid, currentBlock.content)
+}
+
-export async function convertToFlashCard() {
+export async function convertToFlashCard(uuid: string, blockContent: string) {
try {
- const currentBlock = await logseq.Editor.getCurrentBlock()
- if (!currentBlock) {
+ const questionBlock = await logseq.Editor.insertBlock(uuid, "Genearting question....", { before: false })
+ if (!questionBlock) {
throw new Error("Block not found");
}
- const block = await logseq.Editor.insertBlock(currentBlock.uuid, "Genearting todos....", { before: false })
- if (!block) {
+ const answerBlock = await logseq.Editor.insertBlock(questionBlock.uuid, "Genearting answer....", { before: false })
+ if (!answerBlock) {
throw new Error("Block not found");
}
- if (currentBlock) {
- let i = 0;
- const response = await promptLLM(`Divide this task into subtasks with numbers: ${currentBlock.content}`)
- for (const todo of response.split("\n")) {
- if (i == 0) {
- await logseq.Editor.updateBlock(block.uuid, `TODO ${todo.slice(3)}`)
- } else {
- await logseq.Editor.insertBlock(currentBlock.uuid, `TODO ${todo.slice(3)}`, { before: false })
- }
- i++;
- }
- }
+ const question = await promptLLM(`Create a question about this that would fit in a flashcard :\n ${blockContent}`)
+ const answer = await promptLLM(`Given the question ${question} and the context of ${blockContent} What is the answer? be as brief as possible and provide the answer only.`)
+ await logseq.Editor.updateBlock(questionBlock.uuid, `${question} #card`)
+ await logseq.Editor.updateBlock(answerBlock.uuid, answer)
} catch (e: any) {
logseq.App.showMsg(e.toString(), 'warning')
console.error(e)