First, I disabled Notion's AI features. They're awfully invasive, and they have the gall to charge extra for them despite me paying Notion to essentially store my data & barely iterate on their product. 🤮.
There's one feature they have for using AI to automatically tag pages. This is really useful for things like journals where you want to write entries frequently, and not worry about tagging them properly; but then you also want tags to be able to sort them effectively.
ergo, I implemented it myself.
Requirements (you can see this in the .env.example file)
- Knowledge to run
pythonscripts & setup a venv - An OpenAI API key
- Notion API key (private integration)
- Get your Notion database ID that you want to tag pages on. You can get this by opening your database in a full page, pressing CMD L (or ctrl L) to get a link to the page, and then trimming to just get the numeric portion. I also wrote a helper function,
extract_notion_idinscripts.pyif you're so inclined. - Come up with your list of tags
- Come up with additional context that would be relevant for your journal entry
Also, after you create your Notion private integration, you have to give it access to page with your database on it. Here's how to do that.
Once you have these things, put them in the .env file.
Then, you can invoke the script(s) as follows:
python3 main.py 1 [YYYY-MM-DD]
python3 main.py 2
python3 main.py 3
python3 main.py 4The phases all checkpoint (save files locally) in case something goes wrong, so you can rerun failed stages. The phases are as follows:
- Get all the pageIDs within the DB. If you add in the optional date, it'll only get pageIDs for dates >= that (though it assumes you have a field called "Written on"
- Get all the contents for every page. This is only text & bullet points
- Ask ChatGPT 4o what it will label the page. This is using the new structured outputs model
- Write these new tags -> Notion. This is an overwrite.
I would have provided example of this working, but it's hard to separate since I used this on my personal journal, and my tags are private. Sorry!
Also, I wrote this almost completely using Cursor. Woo!!!
