DEV Community

seng
seng

Posted on

a introduction to deepseek API

  1. The DeepSeek API is compatible with OpenAI's. You can directly use the OpenAI SDK to access the DeepSeek API by simply modifying the configuration.

  2. For compatibility with OpenAI, the base_url should be configured as https://api.deepseek.com/v1. The deepseek-chat model corresponds to DeepSeek-V3-0324 when using model='deepseek-chat'. Using model='deepseek-reasoner' points to the DeepSeek-R1-0528 model.

  3. Once an API key has been created, you can call the DeepSeek API.

    For example:

    # Please install OpenAI SDK first: `pip3 install openai`
    
    from openai import OpenAI
    
    client = OpenAI(api_key="<DeepSeek API Key>", base_url="https://api.deepseek.com")
    
    response = client.chat.completions.create(
        model="deepseek-chat",
        messages=[
            {"role": "system", "content": "You are a helpful assistant"},
            {"role": "user", "content": "Hello"},
        ],
        stream=False
    )
    
    print(response.choices[0].message.content)
    
  4. The model price is measured in "per million tokens".

  5. A token is the most basic unit used to represent natural language text and is also the unit of charge. It can represent a Chinese character, an English word, a number, or a symbol.

  6. Although the DeepSeek API does not restrict the number of concurrent requests, a client's request to their servers may require a waiting period of up to 30 minutes when DeepSeek's servers are under the stress of high traffic. During this period, your HTTP connection will be kept alive. For streaming requests, you may persistently receive blank lines, and for non-streaming requests, you may receive SSE (Server-Sent Events) keep-alive annotations.

  7. If the request is still not completed after 30 minutes, the server will terminate the connection.

  8. The following is an example written in Python:

    # Please install OpenAI SDK first: `pip3 install openai`
    
    from openai import OpenAI
    
    client = OpenAI(api_key="sk-11111111111111111111111", base_url="https://api.deepseek.com")
    
    response = client.chat.completions.create(
        model="deepseek-chat",
        messages=[
            {"role": "system", "content": "You are a helpful assistant"},
            {"role": "user", "content": "Hello"},
        ],
        stream=False
    )
    
    print(response.choices[0].message.content)
    
  9. You can use Perl to call the DeepSeek API, for example:

    use LWP::UserAgent;
    use HTTP::Request;
    use JSON;
    
    my $api_key = "sk-1111111111111111111111111"; # Replace with your own key
    my $url = "https://api.deepseek.com/v1/chat/completions";
    
    my $ua = LWP::UserAgent->new;
    
    my $request = HTTP::Request->new("POST", $url);
    $request->header("Content-Type" => "application/json");
    $request->header("Authorization" => "Bearer $api_key");
    
    my $data = {
        model => "deepseek-chat",
        messages => [
            { role => "user", content => "Hello, DeepSeek!" }
        ]
    };
    
    my $json_data = encode_json($data);
    $request->content($json_data);
    
    my $response = $ua->request($request);
    
    if ($response->is_success) {
        my $result = decode_json($response->content);
        print $result->{"choices"}[0]{"message"}{"content"};
    } else {
        print "Request failed: " . $response->status_line;
    }
    
```bash
PS E:\learn\perl> # Set output encoding to UTF-8
PS E:\learn\perl> $OutputEncoding = [System.Text.Encoding]::UTF8
PS E:\learn\perl>
PS E:\learn\perl> # Set console output encoding
PS E:\learn\perl> [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
PS E:\learn\perl>
PS E:\learn\perl> # Set console input encoding
PS E:\learn\perl> [Console]::InputEncoding = [System.Text.Encoding]::UTF8
PS E:\learn\perl> perl 1.pl
Wide character in print at 1.pl line 28.
Hello! 😊 I'm DeepSeek, nice to meet you! How can I help you today?
```
Enter fullscreen mode Exit fullscreen mode
DeepSeek does not provide an official Perl interface, so you can only access the DeepSeek server using HTTP requests. Additionally, an HTTP client library is necessary for sending requests and receiving responses with Perl. You can install them using the following commands:
Enter fullscreen mode Exit fullscreen mode
```
cpan LWP::UserAgent
cpan HTTP::Request
cpan JSON
```
Enter fullscreen mode Exit fullscreen mode

Top comments (0)