Integral Review

Welcome to my personal blog! I use it to share what I'm currently learning or thinking about, usually on topics related to technology, business, and health.

Enriching or modifying an Anki deck with LLMs

A few days ago, I was fortunate enough to find an existing deck that was close to what I was about to create from scratch, but it lacked a field. I thought it would be faster to write a script to modify the deck instead of fixing it myself. I'll share this script in this post. If you've ever wanted to use OpenAI's GPT or Perplexity to improve a deck, this article is for you!

Why use an LLM to improve a deck

With shared decks in particular, it's likely you'll want to make some large-scale changes before getting started. For example, I was looking at the Great Works of Art deck. It's an awesome resource, but I would have preferred having more notes on each artwork to better understand their significance. To speed things up, I decided to use an LLM.

LLMs can be great for reformatting existing content, but asking direct questions has a high risk of hallucination. In particular, with paintings, it seems likely that information on various artworks by the same artist would get partially merged together. For this reason, I decided instead to use the API of Perplexity. If you don't know, Perplexity integrates LLMs with search and sources every fact in its response. This significantly reduces the risk of hallucination, although it also slows down answer times massively. For our purpose, this isn't a problem.

Finally, once I had created a new field with Perplexity, I still used GPT-4 to format the responses to HTML to have them properly displayed within Anki.

Using the script

Note that you do need to be slightly familiar with using the terminal to use this script. If you aren't, you'll need to find another solution. I have a friend building an app similar to Anki called Recall that has an OpenAI integration. However, for now, it's missing Perplexity (I've made a feature request).

You can download the script right away on GitHub: https://github.com/antoinefink/anki-ai-enrichment

Once it's downloaded, change the name of .env.example to .env. That's where we'll be doing our configuration.

In the same directory where you have the script, export the deck you'll be manipulating. To export, make sure you export in plain text with all the available options:

Necessary export options


The txt export is actually a kind of CSV with a couple of lines of metadata at the start of the file. Update the .env file based on the start of the file you just exported (most likely with SKIP_INITIAL_LINES=6).

You'll also want to update the other variables inside the .env file. In particular:

  • PERPLEXITY_API_KEY
  • PERPLEXITY_MODEL (I recommend using the best available right now: llama-3.1-sonar-huge-128k-online)
  • GPT_API_KEY
  • GPT_MODEL_NAME (I recommend gpt-4)
  • CSV_SEPARATOR=\t (this is what Anki uses)
  • SKIP_INITIAL_LINES=6

You can finally run the script. First, confirm the columns are what you expect with:

ruby script.rb --input input.csv columns
lang-shell

Then, you can finally run the script. Here's what I did to first have Perplexity add a field answering why an artwork by a specific artist was significant:

ruby script.rb --input art.txt --output output.csv perplexity 18 "What is the significance of column_4 by column_5?"
lang-shell

Finally, I wanted the new column to be formatted using HTML for it to be displayed nicely in Anki. That's where GPT came in to help. To get the best result possible, I passed the instructions in the system prompt which you can configure in the environment variable:

GPT_SYSTEM_PROMPT=You are an assistant that converts plain text to HTML markup and removes the heading. First, remove the introductory heading, you should only keep the list of facts. Finally, to convert text formatting like bold to <b>tags</b>, convert line breaks to <br> tags, and transform bullet points into proper HTML lists. Keep the HTML output clean and valid. Do not add anything before or after.

This way my prompt was simply:

ruby script.rb --input art.txt --output output.csv gpt 18 "column_18"
lang-shell

And that's it! The output can be imported back to Anki. As long as you have created the field if necessary and update the card, you'll see your new data. In the case of my example, here is the output:

The result

#ai #productivity

💬 Comments
Subscribe to future posts