Building the Tutor Service Layer and Integrating the Language Model

Building the Tutor Service Layer

In the previous lesson, we explored the SessionManager class, which is responsible for managing tutoring session data within our application. Now, we will take the next step by building the Tutor Service Layer. This layer is essential for integrating a 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 a language model.

The service layer acts as a bridge between the part of the application that manages data and the part that 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 language model to generate educational responses. To begin, we need to set up the class and its components.

First, we need to make sure we have access to the SessionManager from our previous lesson. We also need a way to make requests to the language model's API and a method to load the system prompt from a file. Here's how the class is initialized:

PHP
<?php

namespace app\Service;

require_once __DIR__ . '/../Models/SessionManager.php';

use app\Model\SessionManager;
use GuzzleHttp\Client;

class TutorService
{
    private SessionManager $sessionManager;
    private $client;
    private string $systemPrompt;

    public function __construct()
    {
        $this->sessionManager = new SessionManager();
        $this->client = new Client([
            'base_uri' => getenv('DEEPSEEK_BASE_URL'),
            'headers' => [
                'Authorization' => 'Bearer ' . getenv('DEEPSEEK_API_KEY'),
                'Content-Type' => 'application/json',
            ]
        ]);
        $this->systemPrompt = $this->loadSystemPrompt(__DIR__ . '/../../data/system_prompt.txt');
    }
    // ...
}

In this setup, we instantiate SessionManager to manage tutoring data, set up a Guzzle HTTP client for API requests, and load the systemPrompt using the loadSystemPrompt method, which we'll discuss next.

Loading the System Prompt

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