Building the Tutor Service Layer in Ruby

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 Ruby 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 require the necessary libraries, including the session_manager from our previous lesson, the openai gem (which we'll use to access DeepSeek models), and securerandom to generate unique session IDs. Here's how the class is initialized in Ruby:

require 'securerandom'
require 'openai'
require_relative '../models/session_manager'

class TutorService
  def initialize
    @session_manager = SessionManager.new
    @deepseek_client = OpenAI::Client.new
    @system_prompt = load_system_prompt('data/system_prompt.txt')
  end

In this setup, we instantiate SessionManager to manage tutoring data, initialize the OpenAI::Client for DeepSeek model access, and load the system_prompt using the load_system_prompt method, which we implemented in the first lesson.

Creating a New Tutoring Session

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

  def create_session(student_id)
    session_id = SecureRandom.uuid
    @session_manager.create_session(student_id, session_id, @system_prompt)
    session_id
  end

In this method, we generate a unique session_id using SecureRandom.uuid. We then call the create_session method of SessionManager, passing the student_id, session_id, and system_prompt. 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