如何在 Obsidian 笔记中利用 LLM:Ollama 博客教程
Leveraging LLMs in your Obsidian Notes · Ollama Blog
Ollama 博客发布教程,介绍如何为 Obsidian 构建插件,用嵌入向量索引笔记并让本地 LLM 与笔记对话。方案使用 Llama Index 建索引、检索 top 5 个 256 token 的文本块,再交给通过 ollama pull llama2 下载的 llama2 模型生成回答,几秒内即可返回结果。
正文
当前显示原文,中文全文译文尚未就绪。
September 21, 2023
Today I saw a post on Hacker News about another plugin for Obsidian that integrates with ChatGPT. There are a bunch of these tools out there, and I love seeing the different ways to use them with the Obsidian. Making connections, letting you go further with your notes. Some commenters suggested it’s doing the work you need to do yourself, but I think it empowers you in new and incredible ways.
Talk to your notes
The first and perhaps most obvious thing you probably want to do is be able to converse with your notes. Ask it questions to gain further insights. It would be convenient if you could just point the model at your notes and be done with it. But most models can’t accept all that content all at once.
When you ask a question, not all of your notes are relevant. So you need to find the parts that are relevant and hand that to the model. Obsidian has a Search function, but it’s just searching for exact words and phrases, and we need to search for concepts. That’s where embeddings come in. We have to create an index. It turns out to be pretty easy to do.
Let’s build the indexer
When you create an Obsidian plugin, you can have it do something when the plugin loads up, and then other things when you trigger a command or open a note, or other activities in Obsidian. So we want something to understand your notes at the start of the plugin, and it should save its progress, so it doesn’t have to regenerate the index again. Let’s look at a code sample to index one of our notes. I am going to use Llama Index in this one, but LangChain is another great option.
First, we need to initialize an in-memory data store. This is the in-memory store that comes with Llama Index, but Chroma DB is another popular option. And this second line is saying we’ll persist everything we index. Next, I get the path for the file and initialize a reader. Then I read the file. Llama Index knows Markdown, so it reads it in appropriately and indexes it. It also knows about PDFs, and text files, and notion docs, and more. It’s not just storing words, but also understanding the meanings of the words and how they relate to the other words in this text.
Now, this part is using a service from OpenAI, but it’s separate from ChatGPT, different model, different product, and there are alternatives in Langchain that will do this locally, but a bit slower. Ollama also has an embed function. You could also use those services on a superfast self-hosted instance in the cloud and then close that down when indexing is done.
And now let’s search our notes
Now we have an index for this file. Obsidian can give us a list of all the files, so we can run that over and over again. And we are persisting, so this is a one-time action. Now, how can we ask a question? We want some code that will find the relevant bits in our notes, hand it off to the model, and use that info to come up with an answer.
So in this code sample, we are initializing the index using the content we already processed. The Retriever.retrieve line is going to take the prompt and find all the chunks of notes that are relevant and return the text to us. We said to use the top 5 matches here. So I will get 5 chunks of text from our notes. With that raw information, we can generate a system prompt to help our model know what to do when we ask a question.
And so now we get to use the model. I am using a library I created a few days ago that is on npm. I can set the model to use llama2, which is already downloaded to my machine using the command ollama pull llama2. You can try different models to find the one that works best for you.
To get answers back quickly, you will want to stick to a small model. But you also want a model that will have an input context size large enough to accept all of our text chunks. I have up to 5 chunks that are 256 tokens each. I set the model to use that System Prompt that includes our text chunks. Just ask the question, and it will give you the answer within a few seconds.
Awesome. Now, our obsidian plugin would display that answer appropriately.
What else can we do?
You can also think about summarizing the text or finding the best keywords that match your text and adding them to the front matter, so you can make better connections between notes. I’ve played around with making 10 good questions and answers to send to Anki. You will want to experiment with different models and prompts to accomplish these different things. It is very easy to change the prompts and even the model weights to something more appropriate to the task.
I hope this post has given you some ideas for how to build the next great plugin for Obsidian or any other note-taking tool. Using the latest local AI tools, like those you can find at ollama.com, this kind of power is a breeze, and I hope you’ll show me what you’ve got going on.