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.
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.
In this setup, self.sessions
is 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.
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
.
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.
To access a specific tutoring session, we need the get_session
method. This method retrieves a session based on the student_id
and session_id
.
The get_session
method uses the get
function to safely access the nested dictionary, returning the session data if it exists.
Now, let's add the add_message
method. This method allows us to append messages to a session. It requires the student_id
, session_id
, role
, and content
of the message.
The add_message
method first retrieves the session using get_session
. If the session exists, it appends the message to the session's message list.
Finally, we'll implement the get_conversation
method. This method returns the entire conversation, including the system prompt and all messages.
The get_conversation
method retrieves the session and constructs a list starting with the system prompt, followed by the session messages. If the session does not exist, it returns an empty list.
By following these steps, we've built a fully functional SessionManager
class that can create and manage tutoring sessions, add messages, and retrieve conversation histories.
To create a new tutoring session, we use the create_session
method. This method takes three parameters: student_id
, session_id
, and system_prompt
. It initializes a new session entry in the self.sessions
dictionary. If the student_id
does not already exist in the dictionary, a new entry is created. The session entry includes the system prompt and an empty list for messages.
Here's an example of how to create a new tutoring session:
In this example, we initialize a SessionManager
instance and create a new tutoring session for a student with the ID "student_test"
. The session is identified by "session_test"
and is initialized with a system prompt. This setup allows us to manage tutoring session data efficiently.
Once a session is created, we can add messages to it using the add_message
method. This method requires the student_id
, session_id
, role
, and content
of the message. The role indicates whether the message is from the student or the tutor assistant. The message is then appended to the session's message list.
Here's how you can add messages to a tutoring session:
In this example, we add a student message, "Hello, I need help with geometry."
, and an assistant response, "Hi there! What specific geometry topic are you struggling with?"
, to the session. The add_message
method ensures that messages are stored in the correct session entry.
To retrieve the entire conversation, including the system prompt, we use the get_conversation
method. This method returns a list of messages, starting with the system prompt followed by the session messages.
The get_conversation
method compiles the session history, allowing us to access the full context of the conversation. This is particularly useful when we need to provide the full conversation history to the DeepSeek model to ensure contextual responses.
In this lesson, we explored the SessionManager
class and its role in managing tutoring session data within the model layer of our application. We learned how to create and manage sessions, add messages, and retrieve conversation histories. The SessionManager
is a crucial component for organizing session data, ensuring that our personal tutor can handle multiple tutoring sessions efficiently.
As you move on to the practice exercises, take the opportunity to experiment with modifying and extending the SessionManager
functionality. This hands-on practice will reinforce the concepts covered in this lesson and prepare you for the next steps in our course. Keep up the great work, and I look forward to seeing your progress!
