Managing Multiple Tutoring Sessions with DeepSeek in Ruby

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

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 Ruby, you can use SecureRandom.uuid 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.

require "securerandom"

# 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 identifier
def create_session(tutoring_sessions, system_prompt)
  session_id = SecureRandom.uuid  # Create unique session identifier
  tutoring_sessions[session_id] = []  # Initialize empty tutoring history
  tutoring_sessions[session_id] << system_prompt  # Add system prompt to tutoring history
  session_id
end

In this example, tutoring history is stored in a Ruby hash 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

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 removing the oldest messages and passing only the most recent ones.

In the following code example, the send_query method handles this process. The method 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.

require "openai"

# Initialize the DeepSeek client
client = OpenAI::Client.new(
  access_token: "deepseek_api_key",
  uri_base: "https://api.deepseek.com"
)

def send_query(client, tutoring_sessions, session_id, user_query)
  # Verify tutoring session exists
  unless tutoring_sessions.key?(session_id)
    raise "Tutoring session not found!"
  end
  # Add student's query to history
  tutoring_sessions[session_id] << { role: "user", content: user_query }
  # Get AI explanation using tutoring history
  response = client.chat(
    parameters: {
      model: "deepseek-ai/DeepSeek-V3",
      messages: tutoring_sessions[session_id]
    }
  )
  # Extract and clean AI's explanation
  answer = response.dig("choices", 0, "message", "content").to_s.strip
  # Add AI's explanation to history
  tutoring_sessions[session_id] << { role: "assistant", content: answer }
  # Return AI's explanation
  answer
end
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