golangAPI调用deepseek

news2025/3/12 12:26:12

目录

  • 1.deepseek官方API调用文档
    • 1.访问格式
    • 2.curl组装
  • 2.go代码
      • 1. config 配置
      • 2.模型相关
      • 3.错误处理
      • 4.deepseekAPI接口实现
      • 5. 调用使用
  • 3.响应实例

1.deepseek官方API调用文档

1.访问格式

在这里插入图片描述
现在我们来解析这个curl

2.curl组装

// 这是请求头要加的参数
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <DeepSeek API Key>" \

// 这是请求体要加的参数
-d '{
        "model": "deepseek-chat",
        "messages": [
          {"role": "system", "content": "You are a helpful assistant."},
          {"role": "user", "content": "Hello!"}
        ],
        "stream": false
      }'

这个里面可以看出,user角色使我们要输入的问题,stream选择是否为流式响应

2.go代码

1. config 配置

type Config struct {
	BaseURL    string
	APIKey     string
	HTTPClient *http.Client
}

2.模型相关

type Model interface {
	Chat(ctx context.Context, req Request) (*Response, error)
	Stream(ctx context.Context, req Request) (<-chan Response, error)
}

type Request struct {
	Model    string    `json:"model"`
	Messages []Message `json:"messages"`
	Stream   bool      `json:"stream"`
}

type Message struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

type Response struct {
	Content string `json:"content"`
}

3.错误处理

// Error 标准错误类型
type Error struct {
	Code    int
	Message string
	Model   string
}

func (e *Error) Error() string {
	return fmt.Sprintf("[%s] %d: %s", e.Model, e.Code, e.Message)
}

4.deepseekAPI接口实现

type DeepSeek struct {
	Cfg ai.Config
}

func NewDeepSeek(cfg ai.Config) *DeepSeek {
	return &DeepSeek{Cfg: cfg}
}

func (d *DeepSeek) Stream(ctx context.Context, request ai.Request) (<-chan ai.Response, error) {
	return d.handleStreaming(ctx, request)
}

func (d *DeepSeek) Chat(ctx context.Context, request ai.Request) (*ai.Response, error) {
	doRequest, err := d.doRequest(ctx, request, false)
	if err != nil {
		return nil, err
	}
	return d.parseResponse(doRequest)
}

func (d *DeepSeek) parseResponse(resp *http.Response) (*ai.Response, error) {
	all, err := io.ReadAll(resp.Body)
	if err != nil {
		return nil, fmt.Errorf("io.ReadAll failed, err: %v\n", err)
	}
	defer resp.Body.Close()
	return &ai.Response{Content: string(all)}, nil
}

// 私有方法
func (d *DeepSeek) doRequest(ctx context.Context, req ai.Request, stream bool) (*http.Response, error) {
	req.Stream = stream
	body, _ := json.Marshal(req)

	httpReq, err := http.NewRequestWithContext(
		ctx,
		"POST",
		d.Cfg.BaseURL+"/chat/completions",
		bytes.NewReader(body),
	)
	// 设置请求头
	httpReq.Header.Set("Content-Type", "application/json")
	httpReq.Header.Set("Authorization", "Bearer "+apiKey)
	resp, err := d.Cfg.HTTPClient.Do(httpReq)
	if err != nil {
		return nil, &ai.Error{
			Model:   "deepseek",
			Code:    http.StatusInternalServerError,
			Message: fmt.Sprintf("HTTP error: %v", err),
		}
	}

	if resp.StatusCode != http.StatusOK {
		return nil, &ai.Error{
			Model:   "deepseek",
			Code:    http.StatusInternalServerError,
			Message: fmt.Sprintf("failed to request: %v", err),
		}
	}
	return resp, nil
}

func (d *DeepSeek) handleStreaming(ctx context.Context, req ai.Request) (<-chan ai.Response, error) {
	ch := make(chan ai.Response)
	go func() {
		defer close(ch)
		// 发起流式请求
		resp, err := d.doRequest(ctx, req, true)
		if err != nil {
			ch <- ai.Response{Content: "request error!"}
			return
		}
		defer resp.Body.Close()

		scanner := bufio.NewScanner(resp.Body)
		for scanner.Scan() {
			select {
			case <-ctx.Done():
				ch <- ai.Response{Content: "ctx done!"}
				return
			default:
				// 解析事件流
				event := parseEvent(scanner.Bytes())
				if event != nil {
					ch <- *event
				}
			}
		}
	}()

	return ch, nil
}

func parseEvent(line []byte) *ai.Response {
	// 处理事件流格式
	if !bytes.HasPrefix(line, []byte("data: ")) {
		return nil
	}

	payload := bytes.TrimPrefix(line, []byte("data: "))
	if string(payload) == "[DONE]" {
		return nil
	}

	// 解析响应结构
	var chunk struct {
		Choices []struct {
			Delta struct {
				Content string `json:"content"`
			}
			FinishReason string `json:"finish_reason"`
		}
	}

	if err := json.Unmarshal(payload, &chunk); err != nil {
		return nil
	}

	if len(chunk.Choices) > 0 {
		content := chunk.Choices[0].Delta.Content
		finishReason := chunk.Choices[0].FinishReason

		if content != "" {
			return &ai.Response{Content: content}
		}

		if finishReason == "stop" {
			return nil
		}
	}
	return nil
}

5. 调用使用

func main() {
	cfg := ai.Config{
		BaseURL:    "https://api.deepseek.com/",
		APIKey:     "key",
		HTTPClient: &http.Client{},
	}

	// 初始化deepseek
	d := deepseek.NewDeepSeek(cfg)

	// 封装请求体
	body := ai.Request{
		Model:    "deepseek-chat",
		Messages: []ai.Message{{Role: "system", Content: "You are a helpful assistant."}, {Role: "user", Content: "你是谁"}},
	}

	// 同步调用
	chat, err := d.Chat(context.Background(), body)
	if err != nil {
		panic(err)
	}
	fmt.Println(chat.Content)

	// 流式调用
	stream, _ := d.Stream(context.Background(), body)
	for chunk := range stream {
		fmt.Printf(chunk.Content)
	}
}

3.响应实例

// 同步
{"id":"","object":"chat.completion","created":,"model":"deepseek-chat","choices":[{"index":0,"message":{"role":"assistant","content":"您好!我是由中国的深度求索(DeepSeek)公司开发的何任何问题,我会尽我所能为您提供帮助。"},"logprobs":null,"finish_reason":"stop"}],"usage":{"prompt_tokens":10,"completion_tokens":37,"total_tokens":47,"prompt_tokens_details":{"cached_tokens":0},"prompt_cache_hit_tokens":0,"proms":10},"system_fingerprint":"fp_3a5770e1b4"}


// 流式
您好!我是由中国的深度求索(DeepSeek)公司开发的智能助手DeepSeek-V3。如您有任何任何问题,我会尽我所能为您提供帮助。

代码地址:https://gitee.com/li-zhuoxuan/go_ai

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2299666.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【第15章:量子深度学习与未来趋势—15.3 量子深度学习在图像处理、自然语言处理等领域的应用潜力分析】

一、开篇:为什么我们需要关注这场"量子+AI"的世纪联姻? 各位技术爱好者们,今天我们要聊的这个话题,可能是未来十年最值得押注的技术革命——量子深度学习。这不是简单的"1+1=2"的物理叠加,而是一场可能彻底改写AI发展轨迹的范式转移。 想象这样一个…

JAVA安全—Shiro反序列化DNS利用链CC利用链AES动态调试

前言 讲了FastJson反序列化的原理和利用链&#xff0c;今天讲一下Shiro的反序列化利用&#xff0c;这个也是目前比较热门的。 原生态反序列化 我们先来复习一下原生态的反序列化&#xff0c;之前也是讲过的&#xff0c;打开我们写过的serialization_demo。代码也很简单&…

LangChain大模型应用开发:提示词工程应用与实践

介绍 大家好&#xff0c;博主又来给大家分享知识了。今天给大家分享的内容是LangChain提示词工程应用与实践。 在如今火热的大语言模型应用领域里&#xff0c;LangChain可是一个相当强大且实用的工具。而其中的提示词(Prompt)&#xff0c;更是我们与语言模型进行有效沟通的关…

2025 N1CTF crypto 复现

近一个月都没有学习了&#xff0c;一些比赛也没有打&#xff0c;很惭愧自己还是处在刚放假时的水平啊&#xff0c;马上开学了&#xff0c;抓紧做一些训练来康复。 CheckIn import os from Crypto.Util.number import * from secret import FLAGp, q getPrime(512), getPrime…

Windows Defender Control--禁用Windows安全中心

Windows Defender Control--禁用Windows安全中心 链接&#xff1a;https://pan.xunlei.com/s/VOJDuy2ZEqswU4sEgf12JthZA1?pwdtre6#

mount 出现 2038 问题

在 linux 上挂载 ext4 文件系统时出现了 2038 年问题&#xff0c;如下&#xff1a; [ 236.388500] EXT4-fs (mmcblk0p2): mounted filesystem with ordered data mode. Opts: (null) [ 236.388560] ext4 filesystem being mounted at /root/tmp supports timestamps until 2…

【第12章:深度学习与伦理、隐私—12.4 深度学习与伦理、隐私领域的未来挑战与应对策略】

凌晨三点的自动驾驶测试场,AI系统突然在暴雨中做出惊人决策——它选择撞向隔离带而不是紧急变道,因为算法推演发现隔离带后的应急车道站着五个工程师。这个惊悚的伦理困境,揭开了深度学习伦理危机最尖锐的冰山一角。 一、潘多拉魔盒已开:深度学习伦理的四大原罪 1.1 数据原…

RL--2

强化学习当中最难的两个点是&#xff1a; 1.reward delay&#xff1b; 2.agent的行为会影响到之后看到的东西&#xff0c;所以agent要学会探索世界&#xff1b; 关于强化学习的不同类型&#xff0c;可以分为以下三种&#xff1a; 一种是policy based&#xff1a;可以理解为它是…

SpringMVC新版本踩坑[已解决]

问题&#xff1a; 在使用最新版本springMVC做项目部署时&#xff0c;浏览器反复500&#xff0c;如下图&#xff1a; 异常描述&#xff1a; 类型异常报告 消息Request processing failed: java.lang.IllegalArgumentException: Name for argument of type [int] not specifie…

2025 pwn_A_childs_dream

文章目录 fc/sfc mesen下载和使用推荐 fc/sfc https://www.mesen.ca/docs/ mesen2安装&#xff0c;vscode安装zg 任天堂yyds w d 左右移动 u结束游戏 i崩溃或者卡死了 L暂停 D658地方有个flag 发现DEEE会使用他。且只有这个地方&#xff0c;maybe会输出flag&#xff0c;应…

pandas(11 分类数据和数据可视化)

前面内容&#xff1a;pandas(10 日期和Timedelta) 目录 一、Python Pandas 分类数据 1.1 pd.Categorical() 1.2 describe() 1.3 获取类别的属性 1.4 分类操作 1.5 分类数据的比较 二、Python Pandas 数据可视化 2.1 基础绘图&#xff1a;plot 2.2 条形图 2.3 直方…

Redis 03章——10大数据类型概述

一、which10 &#xff08;1&#xff09;一图 &#xff08;2&#xff09;提前声明 这里说的数据类型是value的数据类型&#xff0c;key的类型都是字符串 官网&#xff1a;Understand Redis data types | Docs &#xff08;3&#xff09;分别是 1.3.1redis字符串&#xff0…

bps是什么意思

本文来自DeepSeek "bps" 是 "bits per second" 的缩写&#xff0c;表示每秒传输的比特数&#xff0c;用于衡量数据传输速率。1 bps 即每秒传输 1 比特。 常见单位 bps&#xff1a;比特每秒 Kbps&#xff1a;千比特每秒&#xff08;1 Kbps 1,000 bps&am…

撕碎QT面具(1):Tab Widget转到某个Tab页

笔者未系统学过C语法&#xff0c;仅有Java基础&#xff0c;具体写法仿照于大模型以及其它博客。自我感觉&#xff0c;如果会一门对象语言&#xff0c;没必要先刻意学C&#xff0c;因为自己具有对象语言的基础&#xff0c;等需要用什么再学也不迟。毕竟不是专门学C去搞算法。 1…

项目版本号生成

需求 项目想要生成一个更新版本号&#xff0c;格式为v2.0.20250101。 其中v2.0为版本号&#xff0c;更新时进行配置&#xff1b;20250101为更新日期&#xff0c;版本更新时自动生成。 实现思路 创建一个配置文件version.properties&#xff0c;在其中配置版本号&#xff1b…

善筹网设计与实现(代码+数据库+LW)

摘 要 信息数据从传统到当代&#xff0c;是一直在变革当中&#xff0c;突如其来的互联网让传统的信息管理看到了革命性的曙光&#xff0c;因为传统信息管理从时效性&#xff0c;还是安全性&#xff0c;还是可操作性等各个方面来讲&#xff0c;遇到了互联网时代才发现能补上自…

使用 MindSpore 训练 DeepSeek-V3 模型

MindeSpore 已经适配 DeepSeek-V3 的训练推理啦&#xff0c;下面是使用 MindSpore 对DeepSeek-V3做训练的过程。 一、环境确认 这里呢我使用的是 8张 910B2 的显卡&#xff1a; 其中 MindSpore Transformers 的环境依赖如下&#xff1a; PythonMindSporeCANN固件与驱动3.1…

DeepSeek R1完全本地部署实战教程01-课程大纲

一、课程体系 二、学习目标: 了解基础知识掌握安装部署学会搭建可视化界面能力水平进阶三、课程特点: 案例驱动工程实战完整体系四、课程大纲 1.DeepSeek R1 项目运行演示 【视频课程】 (1)可视化交互 (2)联网搜索 (3)本地知识库 2.环境安装部署 【视频课程】 (1)软…

redis cluster测试

集群节点信息这时候停掉一个master 172.30.60.31 从集群信息集中我们可以看到172.30.60.31的slave是172.30.60.41&#xff0c;查看41的日志&#xff0c;发现他成为了新的master 这时候我们在将172.30.60.41也杀死&#xff0c;会发现集群异常了 尝试把172.30.60.31启动&#xff…

数据恢复-01-机械硬盘的物理与逻辑结构

磁盘存储原理 磁盘存储数据的原理&#xff1a; 磁盘存储数据的原理是利用磁性材料在磁场作用下的磁化性质&#xff0c;通过在磁盘表面上划分成许多小区域&#xff0c;根据不同的磁化方向来表示0和1的二进制数据&#xff0c;通过读写磁头在磁盘上的移动&#xff0c;可以实现数据…