Golang中調(diào)用deepseekr1的教程詳解
1.官方文檔

2.請求看一下
因?yàn)閐eepseek官方API的deepssek-r1響應(yīng)太慢,于是用了騰訊的API來測試
func main() {
cfg := config.Config{
BaseURL: "https://api.lkeap.cloud.tencent.com",
APIKey: "API-KEY",
HTTPClient: &http.Client{},
}
// 初始化deepseek
d := deepseek.NewDeepSeek(cfg)
// 封裝請求體
body := model.Request{
Model: "deepseek-r1",
Messages: []model.Message{{Role: "system", Content: "You are a helpful assistant."}, {Role: "user", Content: "你是誰"}},
}
// 同步調(diào)用
chat, err := d.Chat(context.Background(), body)
if err != nil {
panic(err)
}
fmt.Println(chat.Content)
// 流式調(diào)用
//stream, _ := d.Stream(context.Background(), body)
//console.ConsoleContent(stream)
}
響應(yīng)的格式
{"id":"ab6e60c5df865a38a",
"object":"chat.completion",
"created":1739782294,
"model":"deepseek-r1",
"choices":[
{"index":0, "message":{
"role":"assistant",
"content":"\n\n您好!我是DeepSeek-R1,一個(gè)由深度求索公司開發(fā)的智能學(xué),代碼和邏輯推理等理工類問題。我會保持誠實(shí),如果不知道答案會明確告知,并始終聚焦于提供最有幫助的信息。",
"reasoning_content":"\n嗯,用戶問我是誰,我需要用中文回答。首先,我要明確自己的身份,是DeepSeek-R1,一個(gè)由深度求索公司開發(fā),可能無法提供最新信息。同時(shí),要避免使用復(fù)雜術(shù)語,保持口語化,讓回答更自然??赡苡脩魟偨佑|我,所以需要簡潔明了,不需要太長的解釋。還要注意語氣友好,表現(xiàn)出愿意幫助的態(tài)度。有沒有遺漏什么?比如是否需要提到公司背景或者隱私政策?不過ens":230,
"total_tokens":246
}}
發(fā)現(xiàn)跟deepseek-chat的響應(yīng)只是多路一個(gè)reasoning_content,在響應(yīng)上面加上就可以了
type Response struct {
Content string `json:"content"`
ReasoningContent string `json:"reasoning_content"` // 思考回答
}
3.改造后的整個(gè)訪問流程
1. 調(diào)用流式請求
stream, _ := d.Stream(context.Background(), body)
2. 發(fā)起流式請求
func (d *DeepSeek) Stream(ctx context.Context, request model.Request) (<-chan model.Response, error) {
return http_parse.HandleStreaming(ctx, request, &d.Cfg)
}
3. 響應(yīng)處理
func HandleStreaming(ctx context.Context, req model.Request, c *config.Config) (<-chan model.Response, error) {
ch := make(chan model.Response)
go func() {
defer close(ch)
// 發(fā)起流式請求
resp, err := DoRequest(ctx, req, true, c)
if err != nil {
ch <- model.Response{Content: "request error!"}
return
}
defer resp.Body.Close()
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
select {
case <-ctx.Done():
ch <- model.Response{Content: "ctx done!"}
return
default:
// 解析事件流
event := ParseEvent(scanner.Bytes())
if event != nil {
ch <- *event
}
}
}
}()
return ch, nil
}
4. 打印結(jié)果
func Content(ch <-chan model.Response) {
for chunk := range ch {
if chunk.ReasoningContent != "" {
fmt.Printf(chunk.ReasoningContent)
continue
}
// 打印回答
fmt.Printf(chunk.Content)
}
}4.代碼地址
https://gitee.com/li-zhuoxuan/go_ai
到此這篇關(guān)于Golang中調(diào)用deepseekr1的教程詳解的文章就介紹到這了,更多相關(guān)Go調(diào)用deepseekr1內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Go實(shí)現(xiàn)自己的網(wǎng)絡(luò)流量解析和行為檢測引擎原理
這篇文章主要為大家介紹了Go實(shí)現(xiàn)自己的網(wǎng)絡(luò)流量解析和行為檢測引擎原理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
Go調(diào)用opencv實(shí)現(xiàn)圖片矯正的代碼示例
這篇文章主要為大家詳細(xì)介紹了Go調(diào)用opencv實(shí)現(xiàn)圖片矯正的代碼示例,文中的示例代碼簡潔易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-09-09
golang+vue打造高效多語言博客系統(tǒng)的完整指南
這篇文章主要為大家詳細(xì)介紹了如何使用golang和vue打造一個(gè)高效多語言博客系統(tǒng),本文為大家附上了完整版指南,有需要的小伙伴可以參考一下2025-03-03
關(guān)于golang?struct?中的?slice?無法原子賦值的問題
這篇文章主要介紹了為什么?golang?struct?中的?slice?無法原子賦值的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-01-01
關(guān)于Gin框架中的Cookie和Session的使用方法
為了實(shí)現(xiàn)跨請求的數(shù)據(jù)共享,我們可以使用Cookie和Session,本文將結(jié)合實(shí)際案例,詳細(xì)介紹在Go語言的Gin框架中如何使用Cookie和Session,并通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2024-10-10

