Skip to content

Commit

Permalink
Merge pull request #10 from taweili/master
Browse files Browse the repository at this point in the history
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
  • Loading branch information
omagdy7 authored Dec 11, 2023
2 parents 5b671c0 + f4a66c8 commit d215eea
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 41 deletions.
29 changes: 22 additions & 7 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import {
convertToFlashCardFromEvent,
DivideTaskIntoSubTasksFromEvent,
ollamaUI,
summarizeBlockFromEvent,
promptFromBlockEvent,
expandBlockEvent
promptFromBlockEventClosure
} from "./ollama";
import { useAppVisible } from "./utils";

Expand Down Expand Up @@ -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) {
Expand Down
73 changes: 39 additions & 34 deletions src/ollama.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
}
}
}

Expand All @@ -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 })
Expand Down

0 comments on commit d215eea

Please sign in to comment.