SoFunction
Updated on 2025-04-05

Go language calls the DeepSeek API to implement streaming output and dialogue

introduction

DeepSeek is a powerful AI model service platform. This article will introduce in detail how to use the Go language to call the DeepSeek API to implement streaming output and dialogue functions.

Deepseek's API is no longer useful because it is hit hard. This article uses DeepSeek:/i/vnCCfVaQExplain as an example.

1. Environmental preparation

First, we need to prepare the following:

Go locale

DeepSeek API Access Permissions

Development tools (such as VS Code)

2. Basic code implementation

2.1 Create a project structure

mkdir deepseek-go
cd deepseek-go
go mod init deepseek-go

2.2 Core code implementation

package main

import (
    "bufio"
    "encoding/json"
    "fmt"
    "net/http"
    "os"
    "strings"
    "time"
)

// Define the response structuretype ChatResponse struct {
    Choices []struct {
        Delta struct {
            Content string `json:"content"`
        } `json:"delta"`
    } `json:"choices"`
}

func main() {
    // Create output file    file, err := ("", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
    if err != nil {
        ("Error opening file: %v\n", err)
        return
    }
    defer ()

    // API configuration    url := "/v1/chat/completions"
    
    for {
        // Get user input        ("\nPlease enter your question (enter q quit): ")
        reader := ()
        question, _ := ('\n')
        question = (question)
        
        if question == "q" {
            break
        }

        // Record the conversation time        timestamp := ().Format("2006-01-02 15:04:05")
        (("\n[%s] Question:\n%s\n\n", timestamp, question))

        // Build the request body        payload := (`{
            "model": "deepseek-ai/DeepSeek-V3",
            "messages": [
                {
                    "role": "user",
                    "content": "%s"
                }
            ],
            "stream": true,
            "max_tokens": 2048,
            "temperature": 0.7
        }`, question)

        // Send a request        req, _ := ("POST", url, (payload))
        ("Content-Type", "application/json")
        ("Authorization", "Bearer YOUR_API_KEY")  // Replace with your API Key
        // Get the response        res, _ := (req)
        defer ()

        // Handle streaming response        scanner := ()
        for {
            line, err := ('\n')
            if err != nil {
                break
            }

            line = (line)
            if line == "" || line == "data: [DONE]" {
                continue
            }

            if (line, "data: ") {
                line = (line, "data: ")
            }

            var response ChatResponse
            if err := ([]byte(line), &response); err != nil {
                continue
            }

            if len() > 0 {
                content := [0].
                if content != "" {
                    (content)
                    (content)
                }
            }
        }
    }
}

3. Main features description

3.1 Streaming Output

The DeepSeek API supports streaming output (Stream) and by setting "stream": true, we can achieve the effect of displaying AI replies in real time. This brings a better user experience:

  • See the response content instantly
  • Reduce waiting time
  • A more natural dialogue experience

3.2 Parameter configuration

{
    "model": "deepseek-ai/DeepSeek-V3",
    "messages": [...],
    "stream": true,
    "max_tokens": 2048,
    "temperature": 0.7,
    "top_p": 0.7,
    "top_k": 50,
    "frequency_penalty": 0.5
}

Parameter description:

  • model: Select the model to use
  • max_tokens: Maximum output length
  • temperature: temperature parameter, control the randomness of output
  • top_p, top_k: Control the sampling strategy
  • frequency_penalty: controls repetition

3.3 Dialogue Record

The program will automatically save all conversations to a file, including:

  • Timestamp
  • User Problems
  • AI answer
  • Formatted separators

4. Use examples

Run the program:

go run 

Input questions, such as:

Please enter your question: introduce the main features of DeepSeek

Observe real-time output and file records

5. Error handling and best practices

Key Management

  • Use environment variables to store API keys
  • Don't hardcode the key in the code
  • Regularly rotate keys

2. Error handling

  • Check network connection
  • Verify API response
  • Handle streaming output interrupts

3. Performance optimization

  • Use the appropriate buffer size
  • Close the connection in time
  • Process concurrent requests

Summarize

Through this article, you should have mastered the basic methods of how to use Go to call the DeepSeek API. DeepSeek provides powerful AI capabilities, and can build various interesting applications with the efficient performance of the Go language.

This is the article about this complete guide to calling the DeepSeek API in Go. For more information about calling the DeepSeek API in Go, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!