Message Types and Session History in JavaScript

Message Types and Session History

Welcome back! In the previous lessons, you learned how to send a simple query to DeepSeek's language model and explored various model parameters to customize the AI's responses. Now, we will delve into the concept of message types and the importance of maintaining session history. These elements are crucial for creating dynamic and context-aware interactions with the AI, allowing your personal tutor to engage in more meaningful educational conversations.

Understanding Message Types

Before we dive into building and managing session history, it's important to understand the concept of message types and how a tutoring session history is structured. In a tutor-student interaction, messages are typically categorized by roles: "system", "user", and "assistant". While we'll explore system prompts more thoroughly in a later lesson, remember that these primary roles help define the flow of dialogue and ensure the AI understands who is speaking at any given time.

DeepSeek expects the session history to be formatted as an array of objects, where each object represents a message with two properties: role and content. Here's an example of what a simple tutoring session might look like:

[
    {"role": "user", "content": "Can you explain the theory of relativity?"},
    {"role": "assistant", "content": "Einstein's theory of relativity consists of two parts: special relativity and general relativity..."},
    {"role": "user", "content": "What practical applications does it have?"},
    {"role": "assistant", "content": "The theory of relativity has several practical applications including GPS systems, particle accelerators, and understanding astronomical phenomena."}
]

In this example, the session history consists of alternating messages between the user (the student) and the assistant (the AI tutor). Each message is stored with its respective role, providing context for the AI to generate appropriate educational responses. Understanding this structure is key to effectively managing tutoring sessions and ensuring that the AI can provide coherent and contextually relevant explanations.

Creating a Function to Handle Tutoring Sessions

To manage tutoring sessions effectively, we will create a function called sendQuery. This function will send queries to the AI and receive explanations, allowing us to handle multiple interactions seamlessly. Here's how the function is structured in JavaScript:

const { OpenAI } = require("openai");

// Initialize the DeepSeek client
const client = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY, // Make sure your API key is set in your environment variables
});

// Function to send a tutoring query and receive an explanation
async function sendQuery(messages) {
  const response = await client.chat.completions.create({
    model: "deepseek-ai/DeepSeek-V3",
    messages: messages,
  });
  return response.choices[0].message.content.trim();
}

In this function, we use the chat.completions.create method to send an array of messages to the AI. The messages parameter contains the session history, which provides context for the AI's response. The function returns the AI's explanation, which is extracted from the API result and trimmed of any leading or trailing whitespace.

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