How To Build An AI Tutor

By Mark Bruderer in on Apr 14, 2023

This blog post is a technical description 😎 of how to create your own AI writing tutor and integrate it into your website. Additionally we provide a guide on how to make a quick website from scratch in python using the streamlit library if you don't have an existing site.

We did this as part of our writing exams of our project zwiers training platform.

We will create an GPT-4 wrapper which uses the correct context based on the exam a student is practicing for to grade and review essays.

GPT-4 Django Server

We first create a django project.

$ django-admin startproject my_tutor

This django API can answer an http post request on the default root url /.

def grade_writing(request: HttpRequest) -> HttpResponse:
    if request.method != "POST":
        return HttpResponseNotFound()

    requestJson = json.loads(request.body)
    exercisesJson = requestJson["exercises"]

    exercises = parseWritingExercises(exercisesJson)
    level: ExamLevel = string_to_level(
        requestJson["level"] if "level" in requestJson else ""
    )
    # set level for all exercises
    for exercise in exercises:
        exercise.level = level

    for ex in exercises:
        gradeExercise(ex, openai_service=humanloop)

    response = HttpResponse(toJson(exercises), content_type="application/json")
    return response

The request receives a json array of essays to grade. Each essay question is composed of two parts: the essay prompt and the answer of the student. This input is parsed by our code into a Exercise object.

def gradeExercise(
    exercise: WritingExercise,
    openai_service: BaseService,
):
    generator: PromptGenerator = FewShotPromptGenerator(settings.prompts)
    messages = generator.create(exercise)

    assessment = openai_service.gradeWritingQuestion(messages=messages, inputs={})
    exercise.setAssessment(assessment)

We deployed this django API on fly.io.

Make Your Own Website