引言
DeepSeek 是一个强大的 AI 模型服务平台,本文将详细介绍如何使用 Go 语言调用 DeepSeek API,实现流式输出和对话功能。
Deepseek的api因为被功击已不能用,本文以 DeepSeek:https://cloud.siliconflow.cn/i/vnCCfVaQ 为例子进行讲解。
1. 环境准备
首先,我们需要准备以下内容:
- Go 语言环境
- DeepSeek API 访问权限
- 开发工具(如 VS Code)
2. 基础代码实现
2.1 创建项目结构
mkdir deepseek-go
cd deepseek-go
go mod init deepseek-go
2.2 核心代码实现
package main
import (
"bufio"
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"time"
)
// 定义响应结构
type ChatResponse struct {
Choices []struct {
Delta struct {
Content string `json:"content"`
} `json:"delta"`
} `json:"choices"`
}
func main() {
// 创建输出文件
file, err := os.OpenFile("conversation.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Printf("Error opening file: %v\n", err)
return