Welcome to the next step in your journey of creating a personal tutor with Spring AI! In the previous lessons, you learned how to send messages to DeepSeek's language model, explored model parameters, and understood the importance of maintaining tutoring session history.
Now, we will focus on personalizing your AI tutor by using system prompts. System Prompts
allow you to define the tutor's personality and expertise, enabling you to customize the behavior and tone of the AI's responses. This lesson will guide you through creating a specialized math and science tutor, demonstrating how system prompts can shape the tutor's interactions.
A system prompt is a key tool for customizing the AI tutor's behavior, setting the stage for how it interacts with students. It acts as a directive that defines the tutor's personality and expertise, guiding its responses to align with a specific teaching style or subject focus. We call it a system prompt
because it serves as an overarching instruction to the AI, distinct from user
or assistant
messages, which are part of the ongoing tutoring session. While user
messages are inputs from the student interacting with the tutor, and assistant
messages are the tutor's responses, the system prompt is a one-time setup that influences all subsequent interactions within a session.
The system prompt
is applied at the start of each session and affects the AI's behavior for that session only. It is not preserved automatically between separate sessions — you need to reinclude it when starting a new conversation. If you change the system prompt mid-session, the AI will immediately adopt the new personality and expertise as defined by the updated prompt. However, for consistency and to avoid confusion, it's generally recommended to set the system prompt at the beginning of a session and maintain it throughout.
Let's see a practical example of setting system prompts in Java
using Spring AI
. First, define the tutor's personality and expertise as a system prompt:
You then configure the chat client to use this system prompt for all interactions in the session:
In this setup, the system prompt
is set as a constant and passed to the chat client during initialization. This ensures that when the student asks a question, the AI responds in a manner consistent with its defined role. By placing the system prompt at the start, you create a cohesive and educational interaction, allowing the AI to maintain its teaching approach throughout the tutoring session.
To illustrate how a system prompt shapes the AI tutor's behavior, let's interact with an AI that has the math and science tutor personality set in our system prompt.
The following code demonstrates how to send a question to the tutor and display the response:
When you run this code, the tutor generates a response that reflects the experienced math and science tutor personality. The output might look something like this:
This educational response showcases how the system prompt shapes the tutor's interactions, allowing it to respond with clear, concise explanations focused on mathematical concepts — precisely what we defined in our system prompt.
One of the powerful aspects of system prompts is that they can be tailored to create tutors with different specializations or teaching styles. To create a tutor with a different focus, simply update the system prompt in the ChatService
class. For example, to create a history tutor, you can change the system prompt as follows:
Now, when you run the application and ask a history-related question, such as:
You might receive an output similar to the following:
By simply changing the system prompt, you can create tutors with different areas of expertise, allowing you to build specialized educational tools for various subjects.
In this lesson, you learned how to personalize your AI tutor using system prompts. By defining the tutor's personality and expertise, you can customize its behavior and tone, creating educational interactions tailored to specific subject areas. We walked through an example of building a specialized math and science tutor, demonstrating how system prompts influence the tutor's responses.
As you move on to the practice exercises, experiment with different system prompts to see how they affect the tutor's behavior. This hands-on practice will reinforce what you've learned and prepare you for the next unit, where we'll explore managing multiple tutoring sessions. Keep up the great work, and enjoy the journey of creating your personal tutor!
