Welcome back! In the previous lesson, we explored how a robust system prompt shapes the behavior of our personal tutor. In this lesson, we’ll focus on the StudentService class, which forms the core of our model layer within the MVC (Model-View-Controller) architecture. The goal is to enable multiple students to create and manage multiple tutoring sessions efficiently. By the end of this lesson, you’ll understand how to create, manage, and retrieve session data for different students using the StudentService
class.
The StudentService
class is responsible for storing and managing tutoring session data for each student. It serves as the backbone of our data management system, supporting multiple students and multiple sessions per student. We start by setting up the class and its constructor, which initializes a ConcurrentHashMap
called studentSessions
to store all session data.
In this structure, studentSessions
is a map where each studentId
maps to a set of sessionId
s. This allows us to efficiently track multiple tutoring sessions for any number of students.
Next, let’s add the createSession
method. This method is responsible for creating a new session entry for a given student and returning the new session’s unique ID.
The createSession
method checks if the studentId
exists in studentSessions
. If it doesn’t, a new entry is created. Then, a new session is created and added to that student’s set of sessions.
The askQuestion
method lets a student send a question to a specific session. It takes the studentId
, sessionId
, and the question text, then delegates the message handling to the tutoring service.
This ensures that questions are only sent to valid sessions for each student.
To create a new tutoring session for a student, use the createSession
method. This initializes a new session entry for the student in the studentSessions
map and returns a new sessionId
.
Here, a new session is created for the student with ID "student_test"
, and the unique sessionId
can be used for subsequent questions and retrieval.
Once a session is created, messages can be sent using askQuestion
. This will add the message to the session’s conversation history and return the assistant’s response.
Each message is processed within the correct session context, ensuring conversation continuity for each student.
To get the entire conversation history for a session, including the system prompt and all messages, add the getHistory
method to the StudentService
class:
You can then print out the session’s full history like this:
This will display the session ID and each message in the conversation, showing both the role and the content for review or display in the user interface.
In this lesson, we explored the StudentService
class and its key role in enabling multiple students to create and manage multiple tutoring sessions. You learned how to initialize session management, send questions, and retrieve conversation histories for each student. This flexible structure makes it easy to support personalized tutoring at scale.
As you move on to the practice exercises, try modifying or extending the StudentService
to fit different use cases. Experiment with session management for multiple students and see how you can further improve the flow. This hands-on work will deepen your understanding and prepare you for the next steps. Great job so far, and keep building!
