I’ve been playing a lot with OpenAI’s tools lately. OpenAI’s ChatGPT is amazingly powerful – frankly it’s scary. The day I installed its API I put out an article talking about some simple python scripts you can run with it, and this one is quite exciting.
Last year, I was obsessed with “Obsidian”, an excellent note-taking tool with backlinks. While I switched back to Notion, playing with this AI gave me an idea. Could I use ChatGPT to create markdown files in Obisidian that can make interesting use of their backlinks system?
Check out this python script. It’s not the most elegant in the world, but what it does is takes a few variables:
- Thingname – the name of the thing you want to create a note on.
- File_path – This is the path to your Obsidian vault.
- Instruction1,Instruction2,Instruction3 – This was more for ease of use than anything else – I put 3 different sets of related instructions together, which feed the “prompt” variable. You can, however, just type your instructions directly into the “Prompt” variable and ignore this variable altogether.
Remember, to get your openai.api_key you need to go to https://openai.com/ and create an account. Once you’re signed up you can generate a key, which you place in that variable.
import openai
import markdown
thingname = "The Gulag Archipelago"
file_path = "Your/Obisidan/Vault/File/path"
instruction1 = "First, print the author's name for" + thingname + "in square brackets using this format Author: [[First Name Last Name]]. "
instruction2 = "Print the 20 most important key terms in square brackets on the next line using this format Key Term: [[Key Term]]"
instruction3 = "Write a lengthy description of " + thingname + " 's most important concepts."
openai.api_key = "Your Key Here"
model_engine = "text-davinci-003"
prompt = instruction1 + instruction2 + instruction3
completion = openai.Completion.create(
engine=model_engine,
prompt=prompt,
max_tokens=2048,
n=1,
stop=None,
temperature=0.5,
)
response = completion.choices[0].text
print(response)
# Open the file for writing
with open(file_path + thingname + '.md', 'w') as f:
# Write the string to the file
f.write(response)
This essentially feeds the AI the set of instructions and asks it to put its output in a markdown file, which Obsidian can read. It saves it in your Obsidian vault’s folder with the same file name as you chose in the ‘Thingname’ variable. It produces this:
This is what it generates using the same code, with “thingname” changed to “Letters from a Stoic”
Isn’t that awesome? Not only does it give you a useful blurb about the thing you chose, it produces a set of tags linked to their own notes. If you did this for 100 ‘things’, each with 20 backlinks, you might start to notice some patterns in your inputs where they have key terms in common.
Depending on your instructions, you can essentially feed your Obsidian vault with any number of outputs from the AI! As your backlinks increase, you get an increasingly interconnected graph view, which you could, similarly, populate with information.
I’m definitely excited to see what else can be done with this API! Any cool ideas? Comment and let me know.