Generate Anki flashcards

Setup

To get started, you can clone the repository containing the project files:

git clone https://github.com/awellis/anki-flashcard-generator

or simply download the zip file from my Github repository.

Once you cloned the repository, or downloaded and unzipped the zip file, you will find the following files:

assets/baroque-essay.md
assets/classical-essay.md
assets/romantic-essay.md
assets/modern-essay.md

These files contain the teaching material for the four musical eras, based on which we will generate the flashcards.

generate-anki-flashcards.py

This file contains a Python script to get you started.

Tasks

Task 1: Create an LLM client

First, you will to set up an LLM client. We will use the openai Python package to connect to an OpenAI model.

  1. import the openai package
  2. import the dotenv package to load the API key from the .env file
  3. set up the client with your OpenAI API key
TTask 2: Read the teaching material
  1. read one of the essay files, and print the contents
Task 3: Extract pairs of questions and answers

Now you can think about how you can extract pairs of questions and answers from the teaching material. You will need to write a suitable prompt, consisting of a system message and a user message, to guide the LLM in extracting the questions and answers.

  1. write a prompt to extract pairs of questions and answers from the teaching material
Task 4: Call the LLM
  1. call the LLM with the prompt, and print the result
  • try out both GPT-4o and GPT-4o Mini
  • try out different parameters settings for the LLM call (e.g. temperature, top_p)
Task 5: Use structured outputs
  1. use structured outputs to control the format of the LLMโ€™s response: Define a pydantic model to describe the format of the LLMโ€™s response
  2. call the LLM with the structured output format
Task 6: Make your code reusable
  1. write a function to call the LLM with the prompt and an arbitrary text
  2. call the function with the prompt, and print the result
Task 7: Write the results to a CSV file
  1. write the results to a CSV file. Code is provided for you.
Task 8: Extract questions and answers from all the teaching material
  1. write a loop to load all essays, extract questions and answers from all the teaching material, and write the results to a CSV file.
Back to top