当前位置: 首页 > wzjs >正文

网站建设7个主要流程图网站怎么做外链

网站建设7个主要流程图,网站怎么做外链,可以上传自己做的视频的网站吗,做论坛网站怎么赚钱吗目录 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:…

目录

  • 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    stringAPIKey     stringHTTPClient *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    intMessage stringModel   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 = streambody, _ := 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!"}returndefault:// 解析事件流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.ContentfinishReason := chunk.Choices[0].FinishReasonif 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{},}// 初始化deepseekd := 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.dtcms.com/wzjs/92512.html

相关文章:

  • windows 网站建设怎么创建网址
  • 网站做权重中国新闻今日头条
  • 网站建设模板犀牛云如何推广自己的业务
  • 自己如何创建一个网站如何让百度收录网址
  • python做网站的开发影视后期培训机构全国排名
  • 做网站直接从网上的icon吗seo查询官网
  • 微信服务号菜单链接网站怎么做自助建站免费搭建个人网站
  • 普通网站能不能用vue做几个小功能seo精华网站
  • 织梦网站栏目不能更新贴吧友情链接在哪
  • 做网站公司哪个比较好网络推广专员是做什么的
  • 高校网站建设建议网络营销推广公司名称
  • 临汾做网站的公司成都seo优化公司排名
  • 汉中免费做网站公司百度竞价产品
  • 类似闲鱼网站怎么做有没有永久免费crm
  • 昆明高端网站建设公司合肥搜索引擎推广
  • 简单网站搭建关键词优化话术
  • 长沙有哪些网站建设公司好百度竞价是什么意思?
  • 网站建设江苏网络培训学校
  • 南宁保洁网站建设武汉做网络推广的公司
  • 上海成品网站seo网站关键词
  • 加强网站建设的制度百度推广收费多少
  • 小说网站系统怎么做今日热点新闻事件摘抄
  • 高密做网站谷歌关键词排名优化
  • 做海报可以在哪些网站下载素材推广图片大全
  • 小当网 绵阳网站建设域名买卖交易平台
  • 公司网站服务类型怎么填合肥网站seo公司
  • 做 理财网站有哪些内容龙岗网站推广
  • 网站制作与网页制作seo网站权重
  • 用什么做flash游戏下载网站营销网络推广哪家好
  • 如何做电影网站赚钱用html制作个人网页