1. Development environment
Before we start, we need to prepare the development environment. The following is the environment configuration used in this implementation:
operating system:Windows 11
Compiler:Visual Studio 2022
Dependency library: C++ REST SDK (for HTTP requests)
2. Preparation
Before calling the DeepSeek API, we need to complete the following preparations:
1. Apply for API Key
Visit DeepSeek official website, register an account and apply for API Key. API Key is the key authentication information for calling APIs and must be saved properly.
2. Install C++ REST SDK
The C++ REST SDK is an open source library for making HTTP requests in C++. It can be downloaded and installed from GitHub. Once the installation is complete, make sure that it contains the path and library paths are added to the project configuration.
Implement code
Here is a complete code example of calling DeepSeek native official website API using C++ REST SDK:
1. Contains the necessary header files
#include <cpprest/http_client.h> #include <cpprest/> #include <iostream>
2. Set API Key and API URL
using namespace web; using namespace web::http; using namespace web::http::client; using namespace utility;
3. Write calling code
int main() { // Set API Key and API URL const utility::string_t apiKey = U("your_api_key_here"); // Replace with your API Key const utility::string_t apiUrl = U("/chat/completions"); // Create an HTTP client http_client client(apiUrl); // Build the request body json::value request; request[U("model")] = json::value(U("deepseek-chat")); // Using the DeepSeek model json::value messages = json::value::array(); json::value userMessage; userMessage[U("role")] = json::value(U("user")); userMessage[U("content")] = json::value(U("Hello, DeepSeek!")); messages[0] = userMessage; request[U("messages")] = messages; // Create HTTP request http_request req(methods::POST); ().add(U("Authorization"), U("Bearer ") + apiKey); ().add(U("Content-Type"), U("application/json")); req.set_body(request); // Send a request and process the response (req) .then([=](http_response response) { return response.extract_json(); }) .then([=](json::value jsonResponse) { if (jsonResponse.has_field(U("choices"))) { auto choices = jsonResponse[U("choices")].as_array(); if (!()) { auto content = choices[0][U("message")][U("content")].as_string(); std::wcout << L"Response from DeepSeek: " << content.c_str() << std::endl; } } }) .wait(); return 0; }
4. Compile and run
Save the above code as a C++ file (e.g.) and compile and run using Visual Studio. Make sure the path to the C++ REST SDK is set correctly in the project configuration.
4. Operation results
After running the program, you should see an output similar to the following:
Response from DeepSeek: Hello! I'm glad to serve you.
This indicates that the call is successful and DeepSeek returns the corresponding conversation content.
5. Things to note
Security of API Key: API Key is sensitive information and should not be exposed directly in the code. In actual projects, it is recommended to store it in a configuration file or environment variable.
Error handling: In actual applications, it is necessary to handle HTTP request errors, such as network errors, API restrictions, etc.
HTTPS support: Make sure your development environment supports HTTPS requests, otherwise you may encounter the problem of certificate verification failure.
6. Summary
Through the introduction of this article, we successfully implemented calls to the DeepSeek native official website API using the C++ REST SDK. Although DeepSeek does not provide C++ call examples, we can still easily implement this function with the help of the C++ REST SDK. I hope this article can be helpful to developers with similar needs.
The above is the detailed content of the complete guide to quickly calling the DeepSeek API in C++. For more information about calling the DeepSeek API in C++, please follow my other related articles!