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 through a PHP client. 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. This involves installing the necessary tools and libraries. For this course, you will need the guzzlehttp/guzzle library, which allows us to interact with DeepSeek's API through HTTP requests.

To install this library, you can use Composer, a dependency manager for PHP. Run the following command in your terminal:

You will be using a platform where this library is pre-installed, so you can focus on writing and running your code without worrying about installation.

Setting the DeepSeek API Key as an Environment Variable

In this course, you'll be using a coding environment where we've already set up everything you need to start working with DeepSeek models. 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 a pre-configured 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 set environment variables in your system or hosting environment. These can be accessed in PHP using the getenv() function. Here's how you might set up environment variables:

  1. On Linux/Mac, you can set environment variables in your terminal:

  2. On Windows, you can use the command:

  3. For web servers like Apache, you can set environment variables in your configuration files or through hosting control panels.

Initializing the OpenAI Client for DeepSeek

Once the environment variables are set, you can initialize the HTTP client in your script to use with DeepSeek. This is done by creating an instance of the GuzzleHttp\Client class and accessing the environment variables using the getenv() function:

Here, the base_uri parameter sets the default URL prefix for all requests made using this client. This means any relative paths in subsequent requests (like /v1/chat/completions) will automatically be appended to this base. The headers array defines HTTP headers sent with every request — in this case, the Authorization header includes the bearer token (API key) required to access DeepSeek, while Content-Type: application/json indicates we’re sending JSON data.

By initializing the client in this manner, you ensure that your script 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.

Here's the code to accomplish this:

In this code, we define a query asking the AI to tell an engineering joke. The post method of the GuzzleHttp\Client is used to send a message to the DeepSeek model and receive a response. It takes some basic parameters to function:

  • The model parameter specifies which AI model to use for generating the response. In this example, we use "deepseek-ai/DeepSeek-V3", which is DeepSeek's language model known for its advanced text understanding and generation capabilities.

  • The messages parameter is an array of associative arrays where each array represents a message in the conversation. Each array must include a "role", which indicates the role of the message sender, such as "user" for the person interacting with the AI, and "content", which contains the actual text of the message.

Extracting and Displaying the AI's Reply

After sending the query to DeepSeek, the next step is to extract the AI's reply from the API response and display it. Here's how you can do that:

Once the post method is called, it returns a response object. To extract the AI's reply, you need to access the body of the response and decode the JSON data. The json_decode function converts the JSON response into a PHP data structure. When passed true as the second argument, it returns an associative array instead of an object. This allows you to access elements using array syntax like $data['choices']. You then select the first choice with $data['choices'][0] and retrieve the message content using message['content'], which removes any extra spaces or newlines.

Finally, we print both the query and the AI's reply to see the interaction. This helps verify that the message was successfully sent and received. 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 an engineering joke.

Example: Full Code Implementation

Let's look at the complete code example for sending a query to DeepSeek. This example includes all the steps we've discussed so far:

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