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.
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. Please note that we'll be using the terms message and query interchangeably here. 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 a list of dictionaries, where each dictionary represents a message with two key-value pairs: "role"
and "content"
. Here's an example of what a simple tutoring session might look like:
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.
To manage tutoring sessions effectively, we will create a function called send_query
. 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 this function, we use the chat.completions.create
method to send a list 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 stripped of any leading or trailing whitespace.
Maintaining a session history is crucial for providing context to the AI tutor. This allows the AI to generate explanations that are relevant to the ongoing educational dialogue.
If we don't maintain session history, the AI will lack the context of previous interactions, leading to responses that may not be coherent or relevant to the current conversation: that is, it would treat each query as an isolated request, which could result in repetitive or disconnected explanations. Moreover, using a structured dictionary to store session history, as opposed to just storing responses in a list, offers several advantages. The dictionary format allows us to clearly define the role of each message (e.g., "user" or "assistant") and its content, ensuring that the AI can accurately interpret who is speaking and maintain the flow of dialogue. It also makes it easier to manage and update the session history, as each message is self-contained with its role and content, providing a clear and organized way to track the conversation.
Let's see how we can build and manage session history:
After sending the initial query, the AI responds with information about Einstein's achievements, showcasing its ability to provide educational content:
In this example, we start a tutoring session with an initial query from the user. The send_query
function is used to get the AI's explanation, which is then printed.
With the session history maintained, the AI provides a contextually relevant follow-up explanation, listing additional achievements of Einstein:
By maintaining this history, we provide context for subsequent interactions, allowing the AI to generate more coherent and relevant educational explanations.
To better understand how the tutoring session has evolved, we can print the entire session history:
This will output the complete dialogue, showing both student queries and tutor explanations:
Having access to the session history allows you to track the flow of educational dialogue and ensure that the AI tutor's explanations remain contextually relevant and build upon previous discussions.
In this lesson, you learned about query types and the importance of maintaining session history in tutoring interactions. We explored how to set up your environment, initialize the DeepSeek client, and create a function to handle tutoring sessions. You also saw how to build and manage session history, enabling the AI to generate contextually relevant educational explanations.
As you move on to the practice exercises, I encourage you to experiment with different tutoring scenarios and observe how the AI's explanations change based on the context provided. This hands-on practice will reinforce what you've learned and prepare you for the next unit, where we'll continue to build on these concepts. Keep up the great work, and enjoy the journey of creating your personal tutor with DeepSeek!
