Building the Tutor Service Layer in JavaScript

Building the Tutor Service Layer

In the previous lesson, we explored the SessionManager class, which plays a crucial role in managing tutoring session data within our application. Now, we will take the next step in our journey by building the Tutor Service Layer. This layer is essential for integrating the DeepSeek language model with tutoring sessions, allowing us to process student queries and generate tailored explanations. By the end of this lesson, you will understand how to set up the TutorService class, create tutoring sessions, and process academic questions using DeepSeek models via the OpenAI SDK.

The service layer acts as a bridge between the model layer, where data is managed, and the AI model, which generates educational responses. It is responsible for orchestrating the flow of data and ensuring that student interactions are handled effectively. Let's dive into the details of setting up this important component.

Setting Up the TutorService Class

The TutorService class is the heart of our service layer. It is responsible for managing tutoring sessions and interacting with the DeepSeek model to generate educational responses. To begin, we need to set up the class and its components.

First, we import the necessary modules, including the SessionManager from our previous lesson, the OpenAI SDK client (which we'll use to access DeepSeek models), the fs module for file operations, and the crypto module to generate unique session IDs. Here’s how the class is initialized in JavaScript:

// Import required modules
import fs from 'fs';
import path from 'path';
import crypto from 'crypto';
import { OpenAI } from 'openai';
import SessionManager from './models/session.js';

// Define the TutorService class
class TutorService {
    constructor() {
        this.sessionManager = new SessionManager();
        this.deepseekClient = new OpenAI();
        this.systemPrompt = this.loadSystemPrompt('data/system_prompt.txt');
    }

    // ... (other methods will be added here)
}

In this setup, we instantiate SessionManager to manage tutoring data, initialize the OpenAI client for DeepSeek model access, and load the systemPrompt using the same function in the class, which we have implemented in the Unit 1.

Creating a New Tutoring Session

Creating a new tutoring session is a fundamental task of the TutorService. The createSession method is responsible for generating a unique session ID and initializing a tutoring session using the SessionManager.

    /**
     * Create a new tutoring session.
     * @param {string} studentId
     * @returns {string} sessionId
     */
    createSession(studentId) {
        const sessionId = crypto.randomUUID();
        this.sessionManager.createSession(studentId, sessionId, this.systemPrompt);
        return sessionId;
    }

In this method, we generate a unique sessionId using the crypto.randomUUID() function. We then call the createSession method of SessionManager, passing the studentId, sessionId, and systemPrompt. This initializes a new tutoring session, which is ready to receive student queries.

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