Building a Session Manager for Tutoring Sessions

Building the Session Manager

Welcome back! In the previous lesson, we explored the importance of a robust system prompt and how it guides the behavior of our personal tutor. Now, we will delve into the next step of our journey: building the Session Manager. This lesson will focus on the model layer of the MVC (Model-View-Controller) architecture, which is crucial for organizing and managing data in a structured way. The SessionManager class will be the core component of our model layer, responsible for managing tutoring session data effectively. By the end of this lesson, you will understand how to create, manage, and retrieve session data using the SessionManager class.

Initializing the SessionManager

The SessionManager class is designed to handle the storage and management of tutoring session data. It serves as the backbone of our tutor's data management system. We'll start by setting up the class and then gradually add methods to handle session creation, message addition, and conversation retrieval.

Let's begin by defining the SessionManager class and its constructor. The constructor initializes an empty dictionary, self.sessions, which will store all session data.

class SessionManager:
    def __init__(self):
        self.sessions = {}  # Will store nested session data

In this setup, self.sessions will become a nested dictionary where the first key is the student_id, and the second key is the session_id. This structure allows us to efficiently manage multiple tutoring sessions for different students.

For example, the structure will look like this:

{
    "student_1": {
        "session_a": {
            "system_prompt": "You are a helpful math tutor...",
            "messages": [
                {"role": "user", "content": "Hi! I need help with algebra."},
                {"role": "assistant", "content": "Hello! I'd be happy to help with algebra."}
            ]
        },
        "session_b": {
            "system_prompt": "You are a helpful science tutor...",
            "messages": []
        }
    },
    "student_2": {
        "session_c": {
            "system_prompt": "You are a helpful history tutor...",
            "messages": []
        }
    }
}

Creating a New Session

Next, we'll add the create_session method. This method is responsible for creating a new session entry for a student. It takes three parameters: student_id, session_id, and system_prompt.

class SessionManager:
    def __init__(self):
        self.sessions = {}

    def create_session(self, student_id, session_id, system_prompt):
        """Create a new tutoring session for a student."""
        if student_id not in self.sessions:
            self.sessions[student_id] = {}
        
        self.sessions[student_id][session_id] = {
            'system_prompt': system_prompt,
            'messages': []
        }

The create_session method checks if the student_id exists in self.sessions. If not, it creates a new entry. Then, it initializes the session with the provided system_prompt and an empty list for messages.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal