用go语言通过deepseek api进行提问
专栏:web开发笔记 Sept. 25, 2025, 6:33 p.m. 13 阅读
介绍go语言的deepseek api调用方法

在deepseek网站申请api,把apikey记录下来,是一段字符串。同时定义一下api的请求url和model名字。"deepseek-chat"是v3,"deepseek-reasoner"就是r1。

myapikey = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
url = "https://api.deepseek.com/chat/completions"
mymodel = "deepseek-chat"
//mymodel = "deepseek-reasoner"

再来定义系统角色:

systemrole := "你是一位编程专家。"

提前准备好要提问的问题question,一个字符串。按照api要求的格式,把以上变量组装成json字符串payload。其中设置成流式,温度为0.3(数字越小越理性,数字越大越发散)。

payload := fmt.Sprintf(`{
    "model": "%s",
    "messages": [
         {
            "role": "system",
            "content": "%s"
        },
        {
            "role": "user",
            "content": "%s"
        }
    ],
    "stream": true,
    "temperature": 0.3
    }`, mymodel, systemrole, question)

发送http post请求:

req, _ := http.NewRequest("POST", url, strings.NewReader(payload))
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer " + myapikey)

res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()

先定义api回复的一个结构体,后面用来解析返回的数据。

type ChatResponse struct {
	Choices []struct {
		Delta struct {
			Content string `json:"content"`
		} `json:"delta"`
	} `json:"choices"`
}

再从res.Body中读取api的回复。api会返回多次,对返回的数据分行处理。用json.Unmarshal转成ChatResponse结构体,从中取出content,并打印。

scanner := bufio.NewReader(res.Body)
for {
    line, err := scanner.ReadString('\n')
    if err != nil {
        break
    }

    //回答结束
    line = strings.TrimSpace(line)
    if line == "" || line == "data: [DONE]" {
        break
    }

    //中途的应答数据
    if strings.HasPrefix(line, "data: ") {
        line = strings.TrimPrefix(line, "data: ")
		
        var response ChatResponse
		
        if err := json.Unmarshal([]byte(line), &response); err != nil {
            continue
        }
		
        if len(response.Choices) > 0 {
            content := response.Choices[0].Delta.Content
            if content != "" {
                fmt.Print(content)
            }
        }
    }
}

附:

  • 流式返回数据格式:总是以"data: xxx"开头,以"data: [DONE]"结束。

image.png

在倒数第二条回复会汇总本次回答消耗的token数量。

感谢阅读,更多文章点击这里:【专栏:web开发笔记】
最新20篇 开设专栏