aboutsummaryrefslogtreecommitdiff
path: root/src/ollama.tsx
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/ollama.tsx
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/ollama.tsx')
-rw-r--r--src/ollama.tsx73
1 files changed, 39 insertions, 34 deletions
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 })