Building the Tutor Service Layer in Go

Building the Tutor Service Layer in Go

In the previous lesson, we explored the SessionManager struct, which plays a crucial role in managing tutoring session data within our application. Now, we will take the next step by building the Tutor Service Layer in Go. 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 struct, create tutoring sessions, and process academic questions using DeepSeek models via the OpenAI Go 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 Struct

The TutorService struct is the heart of our service layer. It is responsible for managing tutoring sessions and interacting with the DeepSeek model to generate educational responses. In Go, we use structs and associated methods instead of classes. To begin, we need to define the struct and its components.

First, we import the necessary packages, including the SessionManager from our previous lesson (now located at app/session/session_manager.go), the OpenAI Go SDK, and the UUID package for generating unique session IDs. Here is how the struct is defined and initialized:

Go
package services

import (
    "context"
    "fmt"
    "log"
    "os"

    "main/app/session"
    "github.com/google/uuid"
    "github.com/openai/openai-go"
    "github.com/openai/openai-go/option"
)

type TutorService struct {
    sessionManager *session.SessionManager
    deepseekClient *openai.Client
    systemPrompt   string
}

// NewTutorService initializes a new TutorService with required dependencies.
func NewTutorService(apiKey, baseURL, promptPath string) *TutorService {
    client := openai.NewClient(
        option.WithAPIKey(apiKey),
        option.WithBaseURL(baseURL),
    )
    sm := session.NewSessionManager()
    prompt := loadSystemPrompt(promptPath)
    return &TutorService{
        sessionManager: sm,
        deepseekClient: &client,
        systemPrompt:   prompt,
    }
}

In this setup, we instantiate SessionManager to manage tutoring data, initialize the OpenAI client for DeepSeek model access, and load the systemPrompt using the loadSystemPrompt function, which we'll discuss next.

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