Managing Multiple Tutoring Sessions with DeepSeek Using PHP
Managing Multiple Tutoring Sessions with DeepSeek
Welcome to the next step in your journey of creating a personal tutor with DeepSeek! In the previous lessons, you learned how to send queries to DeepSeek's language model, explored model parameters, maintained tutoring session history, and personalized AI behavior with system prompts. Now, we will focus on managing multiple tutoring sessions. This is crucial for applications where you need to handle several educational interactions simultaneously, such as a tutoring platform serving multiple students. By the end of this lesson, you will be able to create and manage multiple tutoring sessions using DeepSeek's API, setting the stage for more complex educational interactions.
Creating Unique Tutoring Sessions
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal
In a tutoring application, each educational interaction should be treated as a separate session. To achieve this, we use unique identifiers for each tutoring session. This ensures that queries and explanations are correctly associated with their respective sessions. In our code example, we use PHP's uniqid() function to generate a unique identifier for each tutoring session. When a new tutoring session is created, a unique session_id is generated, and an empty history is initialized.
PHP
<?phprequire 'vendor/autoload.php';use GuzzleHttp\Client;// Initialize the HTTP client$client = new Client([ 'base_uri' => getenv('OPENAI_BASE_URL'), 'headers' => [ 'Authorization' => 'Bearer ' . getenv('OPENAI_API_KEY'), 'Content-Type' => 'application/json', ]]);// Store all active tutoring sessions$tutoring_sessions = [];// Define a common system prompt for all sessions$system_prompt = [ "role" => "system", "content" => "You are a knowledgeable and patient tutor, ready to assist with various academic subjects."];// Create a new tutoring session with a unique identifierfunction create_session() { global $tutoring_sessions, $system_prompt; $session_id = uniqid(); // Create unique session identifier $tutoring_sessions[$session_id] = []; // Initialize empty tutoring history $tutoring_sessions[$session_id][] = $system_prompt; // Add system prompt to tutoring history return $session_id;}
In our example, we store tutoring history in an associative array called $tutoring_sessions, where each key is a unique session_id. When a student sends a query, it is added to the tutoring history, ensuring that the AI has access to the full context when generating an explanation. This approach helps create a seamless and coherent educational interaction between the student and the AI tutor.
Sending Queries and Receiving Explanations
Handling Multiple Tutoring Sessions
Managing multiple tutoring sessions simultaneously is a crucial feature for advanced educational applications. By using unique identifiers, you can create and interact with different tutoring sessions independently, ensuring that each educational interaction remains distinct and contextually accurate. Below, we demonstrate this by initiating a first session and sending queries to it.
<?phprequire 'vendor/autoload.php';use GuzzleHttp\Client;// Initialize the HTTP client$client = new Client([ 'base_uri' => getenv('OPENAI_BASE_URL'), 'headers' => [ 'Authorization' => 'Bearer ' . getenv('OPENAI_API_KEY'), 'Content-Type' => 'application/json', ]]);// Create the first tutoring session and send queries$session_id1 = create_session();echo "Session 1, Query 1: " . send_query($session_id1, "Can you explain the concept of limits in calculus?") . "\n";echo "Session 1, Query 2: " . send_query($session_id1, "How does the epsilon-delta definition work?") . "\n";
Output for the first tutoring session:
Session 1, Query 1: In calculus, a limit is the value that a function approaches as the input approaches a certain value. Formally, we write lim(x→a) f(x) = L, meaning that as x gets closer to a, f(x) gets closer to L.Session 1, Query 2: The epsilon-delta definition formalizes the concept of limits. It states that lim(x→a) f(x) = L if for every ε > 0, there exists a δ > 0 such that if 0 < |x - a| < δ, then |f(x) - L| < ε.
Now, let's create a second tutoring session and interact with it.
<?phprequire 'vendor/autoload.php';use GuzzleHttp\Client;// Initialize the HTTP client$client = new Client([ 'base_uri' => getenv('OPENAI_BASE_URL'), 'headers' => [ 'Authorization' => 'Bearer ' . getenv('OPENAI_API_KEY'), 'Content-Type' => 'application/json', ]]);// Create the second tutoring session and send queries$session_id2 = create_session();echo "Session 2, Query 1: " . send_query($session_id2, "What is the significance of chemical bonds in molecules?") . "\n";echo "Session 2, Query 2: " . send_query($session_id2, "Can you explain covalent bonding?") . "\n";
Output for the second tutoring session:
Session 2, Query 1: Chemical bonds are forces of attraction that hold atoms together in molecules. They are crucial for determining a molecule's structure, properties, and reactivity.Session 2, Query 2: Covalent bonding occurs when atoms share pairs of electrons to achieve a more stable electron configuration. In this type of bond, the shared electrons orbit around both atomic nuclei.
This approach not only maintains the integrity of each tutoring session but also enhances scalability, making it ideal for applications like online tutoring platforms where multiple educational interactions occur simultaneously. By keeping tutoring sessions separate, you can provide personalized educational support to each student.
Summary and Preparation for Practice
In this lesson, you learned how to manage multiple tutoring sessions using DeepSeek's API. We covered creating unique tutoring sessions, maintaining educational interaction history, and handling multiple sessions simultaneously. These skills are essential for building scalable educational applications that can support numerous students at once. As you move on to the practice exercises, I encourage you to apply what you've learned by creating and managing tutoring sessions independently. This hands-on practice will reinforce your understanding and prepare you for more advanced educational AI development. Keep up the great work, and enjoy the journey of creating your personal tutor with DeepSeek!
Once a tutoring session is established, you can send queries and receive explanations from the DeepSeek model. It's important to maintain the context by sending the full tutoring history to the model. However, keep in mind that language models have a context window, which limits the amount of conversation history they can process at once. If the conversation becomes too large, you should trim the history by cutting the oldest messages and passing only the most recent ones.
In the following code example, we use the send_query function to handle this process. The function takes a session_id and a user_query as inputs, adds the query to the tutoring history, and requests an explanation from the AI. The explanation is then processed and added to the tutoring history, ensuring continuity in the educational interaction.
PHP
<?phprequire 'vendor/autoload.php';use GuzzleHttp\Client;// Initialize the HTTP client$client = new Client([ 'base_uri' => getenv('OPENAI_BASE_URL'), 'headers' => [ 'Authorization' => 'Bearer ' . getenv('OPENAI_API_KEY'), 'Content-Type' => 'application/json', ]]);function send_query($session_id, $user_query) { global $tutoring_sessions, $client; // Verify tutoring session exists if (!isset($tutoring_sessions[$session_id])) { throw new Exception("Tutoring session not found!"); } // Add student's query to history $tutoring_sessions[$session_id][] = ["role" => "user", "content" => $user_query]; // Get AI explanation using tutoring history $response = $client->post('chat/completions', [ 'json' => [ 'model' => 'deepseek-ai/DeepSeek-V3', 'messages' => $tutoring_sessions[$session_id], ] ]); $responseBody = json_decode($response->getBody(), true); $answer = trim($responseBody['choices'][0]['message']['content']); // Add AI's explanation to history $tutoring_sessions[$session_id][] = ["role" => "assistant", "content" => $answer]; // Return AI's explanation return $answer;}
PHP
<?phprequire 'vendor/autoload.php';use GuzzleHttp\Client;// Initialize the HTTP client$client = new Client([ 'base_uri' => getenv('OPENAI_BASE_URL'), 'headers' => [ 'Authorization' => 'Bearer ' . getenv('OPENAI_API_KEY'), 'Content-Type' => 'application/json', ]]);// Create the first tutoring session and send queries$session_id1 = create_session();echo "Session 1, Query 1: " . send_query($session_id1, "Can you explain the concept of limits in calculus?") . "\n";echo "Session 1, Query 2: " . send_query($session_id1, "How does the epsilon-delta definition work?") . "\n";
text
Session 1, Query 1: In calculus, a limit is the value that a function approaches as the input approaches a certain value. Formally, we write lim(x→a) f(x) = L, meaning that as x gets closer to a, f(x) gets closer to L.Session 1, Query 2: The epsilon-delta definition formalizes the concept of limits. It states that lim(x→a) f(x) = L if for every ε > 0, there exists a δ > 0 such that if 0 < |x - a| < δ, then |f(x) - L| < ε.
PHP
<?phprequire 'vendor/autoload.php';use GuzzleHttp\Client;// Initialize the HTTP client$client = new Client([ 'base_uri' => getenv('OPENAI_BASE_URL'), 'headers' => [ 'Authorization' => 'Bearer ' . getenv('OPENAI_API_KEY'), 'Content-Type' => 'application/json', ]]);// Create the second tutoring session and send queries$session_id2 = create_session();echo "Session 2, Query 1: " . send_query($session_id2, "What is the significance of chemical bonds in molecules?") . "\n";echo "Session 2, Query 2: " . send_query($session_id2, "Can you explain covalent bonding?") . "\n";
text
Session 2, Query 1: Chemical bonds are forces of attraction that hold atoms together in molecules. They are crucial for determining a molecule's structure, properties, and reactivity.Session 2, Query 2: Covalent bonding occurs when atoms share pairs of electrons to achieve a more stable electron configuration. In this type of bond, the shared electrons orbit around both atomic nuclei.