Sending a Simple Query to DeepSeek Using Go

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 the Go programming language. 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 this course, you will need the official openai-go library, which allows us to interact with DeepSeek's API using the OpenAI-compatible client.

To install this library, run the following command in your project directory:

This will add the openai-go package to your project, making it easy to work with DeepSeek's API in Go.
If you are using the CodeSignal platform, 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 the CodeSignal coding environment, where everything is already set up for you to start working with DeepSeek models in Go. This means you don't need to worry about setting up an API key or configuring environment variables — it's all taken care of behind the scenes.

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 CodeSignal, you need to obtain an API key from DeepSeek. This API key is required to access DeepSeek's services and make requests to their API.

To keep your API key secure, you should use environment variables. An environment variable is a way to store sensitive information, like your DeepSeek API key, outside of your source 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:

These commands will set the environment variables for the current session. But while using CodeSignal, you can skip these steps and start experimenting with DeepSeek models in Go right away.

Initializing the OpenAI Client for DeepSeek

Once the environment variables are set, you can initialize the OpenAI client in your Go code to use with DeepSeek. This is done by importing the openai-go library and creating a new client instance. The client will automatically use your API key and base URL from the environment variables if you don't specify them directly.

Here's how you can initialize the client using the official OpenAI Go SDK:

By initializing the client in this way, your Go application is ready to securely authenticate and send requests to DeepSeek's API.

Sending Your First Query to DeepSeek

Now that your environment is set up and your OpenAI client is initialized, it's time to send your first query to DeepSeek.

You will:

  • Define a prompt (the question you want to ask the AI).
  • Specify the model name and your message.
  • Use the OpenAI Go client to send the request to the DeepSeek API.
  • Receive and process the response.

Here is how you can send a chat completion request using the official OpenAI Go SDK:

This approach uses the OpenAI Go client to handle the request and response, so you don't need to manually construct or encode JSON. The client takes care of communicating with DeepSeek's API and parsing the response for you.

Extracting and Displaying the AI's Reply

After sending the request using the OpenAI Go client, you will receive a response object that contains the AI's reply. The client automatically parses the JSON response for you, so you can directly access the content of the AI's message.

Here's how you can extract and display the AI's reply:

The chatCompletion.Choices[0].Message.Content field contains the AI's reply. This makes it easy to display both your original query and the AI's answer using fmt.Println.

Example: Full Code Implementation

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

This implementation uses the official OpenAI Go SDK to handle authentication, request construction, and response parsing, making it easy to interact with DeepSeek's API in Go.

Summary and Next Steps

In this lesson, you learned how to send a simple query to DeepSeek's language model using Go and the official OpenAI Go SDK. We covered setting up your environment, configuring API access, initializing the client, sending a chat completion request, and extracting the AI's response. These foundational steps will enable you to build more advanced tutoring features in future lessons.

As you move on to the practice exercises, try experimenting with different prompts and observe how the AI responds. This hands-on experience will help solidify your understanding and prepare you for the next unit, where you'll learn how to customize and fine-tune your interactions with DeepSeek's API.

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