deepseek API 调用-golang

【1】golang使用POST提交请求

Go 复制代码
package main

import (
  "fmt"
  "strings"
  "net/http"
  "io/ioutil"
)

func main() {

  url := "https://api.deepseek.com/chat/completions"
  method := "POST"

  payload := strings.NewReader(`{
  "messages": [
    {
      "content": "You are a helpful assistant",
      "role": "system"
    },
    {
      "content": "Who are you ?",
      "role": "user"
    }
  ],
  "model": "deepseek-chat",
  "frequency_penalty": 0,
  "max_tokens": 2048,
  "presence_penalty": 0,
  "response_format": {
    "type": "text"
  },
  "stop": null,
  "stream": false,
  "stream_options": null,
  "temperature": 1,
  "top_p": 1,
  "tools": null,
  "tool_choice": "none",
  "logprobs": false,
  "top_logprobs": null
}`)

  client := &http.Client {
  }
  req, err := http.NewRequest(method, url, payload)

  if err != nil {
    fmt.Println(err)
    return
  }
  req.Header.Add("Content-Type", "application/json")
  req.Header.Add("Accept", "application/json")
  req.Header.Add("Authorization", "Bearer <TOKEN>")

  res, err := client.Do(req)
  if err != nil {
    fmt.Println(err)
    return
  }
  defer res.Body.Close()

  body, err := ioutil.ReadAll(res.Body)
  if err != nil {
    fmt.Println(err)
    return
  }
  fmt.Println(string(body))
}

【2】 content返回问题答案

css 复制代码
{"id":"d84be7ce-98be-4975-85fe-fd0a51d8837c","object":"chat.completion","created":1738826843,"model":"deepseek-chat","choices":[{"index":0,"message":{"role":"assistant","content":"您好!我是由中国的深度求索(DeepSeek)公司开发的智能助手DeepSeek-V3。如您有任何任何问题,我会尽我所能为您提供帮助。"},"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,"prompt_cache_miss_tokens":10},"system_fingerprint":"fp_3a5770e1b4"}
相关推荐
zq_6389几秒前
ObjectModel_diagram.pdf 文档详细总结
开发语言·matlab
kymjs张涛5 分钟前
DeepSeek Harness源码分析:非 AI 开发者的 Agent 学习总结
前端·后端·面试
jj_ccwgw6 分钟前
UOS系统 Kafka KRaft 集群安装指南
后端
rannn_1118 分钟前
Java 后端面试题总结:点赞功能怎么实现?
java·后端·面试
__zRainy__13 分钟前
Node系列 · 数据库:MySQL 安装
数据库·后端·mysql·node.js
Zane199414 分钟前
threading、multiprocessing、asyncio 到底怎么选?一张图 + 三组实测数据说清楚
后端·python
程序员cxuan29 分钟前
DeepSeek 多模态深度实测,确实有东西的
人工智能·后端·程序员
自己的九又四分之三站台1 小时前
C# 接入 RasterLite2:从原生 DLL 加载到 Coverage、Section、Tile 验证
开发语言·c#·地理信息
飞翔的火箭弹1 小时前
企业网络资产管理用什么系统工具
开发语言·网络·php
evans在进步1 小时前
Spring Boot 核心机制详解:自动装配、事件监听、异步任务与请求映射
java·spring boot·后端