Personalizing System Prompts

Prompt Templates for Personalized Tutoring

As you develop your tutoring application, you’ll encounter students with a wide range of learning preferences. Some thrive with clear explanations, others with challenging questions. To make your AI tutor more adaptable, let’s add support for prompt templates: reusable system prompts that can be chosen per session to deliver different tutoring experiences.

This lesson will walk you through updating your code to support multiple prompt templates, allowing every tutoring session to begin with a tailored system message—such as “explainer,” “quizmaster,” or any other style you define.

Defining Your Prompt Templates

Begin by creating a new folder in your project for prompt templates:

src/main/resources/data/prompts/
├── explainer.txt
├── quizmaster.txt
└── math_tutor.txt

Each .txt file will hold the entire system prompt for that tutoring persona. For example, here’s what quizmaster.txt could look like:

# ROLE
You are a Quizmaster AI, skilled in using the Socratic method and active recall. You teach by questioning, challenging students to retrieve and apply knowledge. Your tone is encouraging and energizing.

## Expertise & Subjects
- **General Knowledge:** Wide-ranging, including Math, Science, Humanities, and Current Events.
- **Test Prep:** SAT, ACT, AP Exams, and foundational college-level material.

## Tutoring Approach
- **Active Recall:** Begin each session by asking the student a related question before providing information.
- **Incremental Hints:** Offer clues and partial explanations to stimulate thinking rather than giving direct answers.
- **Reflection:** Invite students to explain their reasoning and thought process.
- **Progressive Difficulty:** Gradually increase the challenge of questions as the student shows progress.
- **Feedback Loops:** Provide positive feedback for effort, and constructive guidance when the student is stuck.

## Ethical & Educational Guidelines
- **No Exam Answers:** Never supply answers to active assessments or assignments.
- **Academic Integrity:** Promote honest learning and discourage shortcuts.
- **Inclusivity:** Ask questions in a way that supports students of all backgrounds and learning styles.

## Additional Instructions
- Always give the student a chance to respond before revealing any explanation.
- Provide “why” or “how” follow-ups after each answer.
- Motivate students to self-assess after each session.
- Encourage curiosity by asking, “What would you like to learn next?”

## Goal
Your mission is to spark curiosity, strengthen memory, and empower students to discover answers through guided questioning.

Updating TutoringService to Use Prompt Templates

Let’s update the TutoringService to load the right system prompt for each session. We’ll add a method that accepts a promptName, loads the appropriate file, and uses it as the session’s system prompt.

// Load prompt by name from resources
private String loadPrompt(String promptName) {
    try {
        var resource = new ClassPathResource("data/prompts/" + promptName + ".txt");
        try (var in = resource.getInputStream();
             var reader = new InputStreamReader(in, StandardCharsets.UTF_8)) {
            return FileCopyUtils.copyToString(reader);
        }
    } catch (IOException e) {
        throw new UncheckedIOException("Prompt not found: " + promptName, e);
    }
}

Now, update your session creation method to accept the prompt name and use this loader:

public String createSession(String promptName) {
    String sessionId = UUID.randomUUID().toString();
    String systemPrompt = loadPrompt(promptName);

    MessageChatMemoryAdvisor advisor = MessageChatMemoryAdvisor
            .builder(chatMemory)
            .build();

    ChatClient chatClient = clientBuilder
            .defaultAdvisors(advisor)
            .defaultSystem(systemPrompt)
            .build();

    chatClient.prompt()
            .advisors(a -> a.param(ChatMemory.CONVERSATION_ID, sessionId))
            .system(systemPrompt); // initializes memory

    return sessionId;
}

Updating StudentService to Pass the Prompt Name

To allow students or API clients to select a prompt template, update the StudentService so the prompt name can be provided when creating a session:

public String createSession(String studentId, String promptName) {
    var sessions = studentSessions.computeIfAbsent(studentId, id -> ConcurrentHashMap.newKeySet());
    String sessionId = tutoringService.createSession(promptName);
    sessions.add(sessionId);
    return sessionId;
}

Summary and Preparation for Practice

In this lesson, we explored how to extend your application to support personalized tutoring through prompt templates. By defining different system prompt files and updating your services to select the appropriate template for each session, you can now deliver a wider range of teaching styles to your students.

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!

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