diff options
| author | omagdy7 <omar.professional8777@gmail.com> | 2023-11-23 20:02:42 +0200 |
|---|---|---|
| committer | omagdy7 <omar.professional8777@gmail.com> | 2023-11-23 20:02:42 +0200 |
| commit | d136b8546517e5a910d953bc16f8393044f0a6c9 (patch) | |
| tree | 84b7e4ada9c1b95e6f2cc9410402da9f76a53329 /src/ollama.tsx | |
| parent | 9c50d78cc5c1d611edba988f65c58a1bbbfd5697 (diff) | |
| download | ollama-logseq-d136b8546517e5a910d953bc16f8393044f0a6c9.tar.xz ollama-logseq-d136b8546517e5a910d953bc16f8393044f0a6c9.zip | |
Retain prompt with response
Diffstat (limited to 'src/ollama.tsx')
| -rw-r--r-- | src/ollama.tsx | 38 |
1 files changed, 20 insertions, 18 deletions
diff --git a/src/ollama.tsx b/src/ollama.tsx index eac27fb..2277f53 100644 --- a/src/ollama.tsx +++ b/src/ollama.tsx @@ -98,7 +98,7 @@ async function promptLLM(prompt: string) { } export async function defineWord(word: string) { - askAI(`What's the formal defintion of ${word}`) + askAI(`What's the defintion of ${word}`, "") } @@ -113,7 +113,7 @@ export async function askWithContext(prompt: string) { for (const block of currentBlocksTree) { blocksContent += await getTreeContent(block) } - askAI(`With the Context of : ${blocksContent}, ${prompt}`) + askAI(prompt, `With the Context of : ${blocksContent}`) } catch (e: any) { logseq.App.showMsg(e.toString(), 'warning') console.error(e) @@ -158,17 +158,19 @@ export async function summarizeBlock() { } } -export async function askAI(prompt: string) { +export async function askAI(prompt: string, context: string) { await delay(300) try { - const currentSelectedBlocks = await logseq.Editor.getCurrentPageBlocksTree() - if (currentSelectedBlocks) { - let lastBlock: any = currentSelectedBlocks[currentSelectedBlocks.length - 1] - if (lastBlock) { - lastBlock = await logseq.Editor.insertBlock(lastBlock.uuid, 'Generating....', { before: true }) + const currentBlock = await logseq.Editor.getCurrentBlock() + if (currentBlock) { + const block = await logseq.Editor.insertBlock(currentBlock.uuid, 'Generating....', { before: true }) + let response = ""; + if (context == "") { + response = await promptLLM(prompt) + } else { + response = await promptLLM(`With the context of: ${context}, ${prompt}`) } - const response = await promptLLM(prompt) - await logseq.Editor.updateBlock(lastBlock.uuid, response) + await logseq.Editor.updateBlock(block!.uuid, `${prompt}\n${response}`) } } catch (e: any) { @@ -198,7 +200,7 @@ export async function convertToFlashCardFromEvent(b: IHookEvent) { if (!currentBlock) { throw new Error("Block not found"); } - convertToFlashCard(currentBlock.uuid, currentBlock.content) + await convertToFlashCard(currentBlock.uuid, currentBlock.content) } export async function convertToFlashCardCurrentBlock() { @@ -206,7 +208,7 @@ export async function convertToFlashCardCurrentBlock() { if (!currentBlock) { throw new Error("Block not found"); } - convertToFlashCard(currentBlock.uuid, currentBlock.content) + await convertToFlashCard(currentBlock.uuid, currentBlock.content) } @@ -220,7 +222,7 @@ export async function convertToFlashCard(uuid: string, blockContent: string) { if (!answerBlock) { throw new Error("Block not found"); } - const question = await promptLLM(`Create a question about this that would fit in a flashcard :\n ${blockContent}`) + 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 delay(300) @@ -243,12 +245,12 @@ export async function DivideTaskIntoSubTasksFromEvent(b: IHookEvent) { } if (currentBlock) { let i = 0; - const response = await promptLLM(`Divide this task into subtasks with numbers: ${currentBlock.content}`) + 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)}`) + await logseq.Editor.updateBlock(block.uuid, `TODO ${todo.slice(3)} `) } else { - await logseq.Editor.insertBlock(currentBlock.uuid, `TODO ${todo.slice(3)}`, { before: false }) + await logseq.Editor.insertBlock(currentBlock.uuid, `TODO ${todo.slice(3)} `, { before: false }) } i++; } @@ -263,9 +265,9 @@ export async function DivideTaskIntoSubTasks() { try { const currentBlock = await logseq.Editor.getCurrentBlock() if (currentBlock) { - const response = await promptLLM(`Divide this task into subtasks with numbers: ${currentBlock.content}`) + const response = await promptLLM(`Divide this task into subtasks with numbers: ${currentBlock.content} `) for (const todo of response.split("\n")) { - await logseq.Editor.insertBlock(currentBlock.uuid, `TODO ${todo.slice(3)}`, { before: false }) + await logseq.Editor.insertBlock(currentBlock.uuid, `TODO ${todo.slice(3)} `, { before: false }) } } } catch (e: any) { |
