在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"
再来定义系统角色:
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)
}
}
}
}
附:

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