Sending a Simple Query to DeepSeek

Welcome to the first lesson of our course on creating a personal tutor with DeepSeek! DeepSeek is an advanced AI platform that offers state-of-the-art language models capable of understanding and generating human-like text. Its unique features include high accuracy in language comprehension, the ability to generate contextually relevant responses, and a robust API that supports seamless integration into various applications. These capabilities make DeepSeek an excellent choice for building AI tutors, as it can provide personalized and insightful interactions with users.

In this lesson, we will explore the basics of interacting with DeepSeek's AI models using Rust. Our goal is to send a simple query to DeepSeek's language model and receive a response. This foundational step will set the stage for more complex interactions in future lessons. By understanding how to communicate with DeepSeek, you'll be equipped to harness its full potential in creating a personal tutor that can adapt to individual learning needs.

Setting Up Your Environment

Before we can send a query to DeepSeek, we need to set up our development environment for Rust. This involves installing the necessary tools and libraries. For this course, you will need the async-openai crate, which allows us to interact with DeepSeek's API, as well as some additional crates for environment variable management and error handling.

To get started, make sure you have Rust and Cargo installed on your system.

Next, create a new Rust project:

Open the Cargo.toml file and add the following dependencies:

  • async-openai is used to interact with the DeepSeek API.
  • tokio is an asynchronous runtime required by async-openai.
  • dotenv allows you to load environment variables from a .env file.
Setting the DeepSeek API Key as an Environment Variable

In this course, you'll be using a coding environment where everything you need to start working with DeepSeek models is already set up. This means you don't need to worry about setting up an API key or configuring environment variables — it's all taken care of for you.

However, it's still useful to understand how this process works in case you want to set it up on your own computer in the future. To work with DeepSeek models outside of this environment, you need to obtain an API key from their service. This API key is essential for accessing DeepSeek's services and making requests to their API.

To keep your API key secure, you can use environment variables. An environment variable is like a special note that your computer can read to find out important details, such as your DeepSeek API key, without having to write it directly in your code. This helps keep your key safe and secure.

If you were setting this up on your own system, here's how you would do it:

  • On macOS and Linux, open your terminal and use the export command to set the environment variables:

  • For Windows, you can set the environment variables using the set command in the Command Prompt:

  • If you are using PowerShell, use the following command:

Alternatively, you can create a .env file in the root of your project directory with the following contents:

In your Rust code, you can read these environment variables using the std::env::var function. This allows your application to securely access the API key and base URL without hardcoding them.

Initializing the Client for DeepSeek

Once the environment variables are set, you can initialize the client in your Rust program to use with DeepSeek. This is done by reading the environment variables and creating a configuration object, which is then used to initialize the client.

Here's how you can do this in Rust:

By initializing the client in this manner, you ensure that your program is ready to authenticate requests to DeepSeek's API securely.

Sending Your First Query to DeepSeek

Now that your environment is set up and your API client is configured, it's time to send your first query to DeepSeek. We'll start by defining a simple prompt and then use the client to send this query to the AI model.

First, define the prompt and build a user message:

  • ChatCompletionRequestUserMessageArgs is used to construct a message that represents the user's input to the AI. Here, we set the content of the message to our prompt ("Can you tell me a joke?") and build it into the required format for the API.

Next, create a chat completion request, send it, and handle the response:

  • CreateChatCompletionRequestArgs is used to build the request that will be sent to the DeepSeek model. Here, we specify the model ("deepseek-ai/DeepSeek-V3") and provide the user message as the input.
  • The client.chat().create(request).await? line sends the request to the DeepSeek API and waits for the response asynchronously.

The response contains a list of choices generated by the AI. Each choice contains a message with the AI's reply.

Displaying the Query and the AI's Reply

After receiving the response, let's display both the original query and the AI's reply:

  • Printing the query (println!("Query: {}", prompt);) helps you verify what was sent to the AI, making it easier to debug and understand the context of the response.
  • The answer is printed by accessing the first choice in the response and retrieving the message content. The use of as_deref().unwrap_or_default() ensures that if the content is missing (for example, if the API returns an empty message), the program will print an empty string instead of causing a panic. This makes your code more robust and user-friendly.

When you run this code, you should see an output similar to the following:

This output demonstrates a successful interaction with the AI, where it responds to the user's query with a joke.

Summary and Next Steps

In this lesson, we covered the essential steps to send a simple query to DeepSeek's language model. We set up our environment, configured API access, and sent a message to receive a response. This foundational knowledge is crucial as we move forward in building more complex tutoring interactions.

As you proceed to the practice exercises, I encourage you to experiment with different queries and explore the AI's responses. This hands-on practice will reinforce what you've learned and prepare you for the next unit, where we'll delve deeper into handling API parameters. Keep up the great work, and enjoy the journey of creating your personal tutor with DeepSeek!

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