package utils import ( "encoding/json" "fmt" "github.com/go-resty/resty/v2" ) type AiReqeust struct{} type DeepseekResponse struct { ID string `json:"id"` Object string `json:"object"` Created int64 `json:"created"` Model string `json:"model"` Choices []struct { Index int `json:"index"` Message struct { Role string `json:"role"` Content string `json:"content"` } `json:"message"` Logprobs interface{} `json:"logprobs"` // 可能是null或复杂结构 FinishReason string `json:"finish_reason"` } `json:"choices"` Usage struct { PromptTokens int `json:"prompt_tokens"` CompletionTokens int `json:"completion_tokens"` TotalTokens int `json:"total_tokens"` PromptTokensDetails struct { CachedTokens int `json:"cached_tokens"` } `json:"prompt_tokens_details"` PromptCacheHitTokens int `json:"prompt_cache_hit_tokens"` PromptCacheMissTokens int `json:"prompt_cache_miss_tokens"` } `json:"usage"` SystemFingerprint string `json:"system_fingerprint"` } func (*AiReqeust) Deepseek(text string) string { // 定义结构体 type Message struct { Role string `json:"role"` Content string `json:"content"` } type ChatRequest struct { Model string `json:"model"` Messages []Message `json:"messages"` } // 创建 resty 客户端 client := resty.New() // 目标URL url := "https://api.deepseek.com/v1/chat/completions" request := ChatRequest{ Model: "deepseek-chat", Messages: []Message{ { Role: "user", Content: text, }, }, } // 发送 POST 请求 resp, err := client.R(). SetHeader("Content-Type", "application/json"). // 必填头 SetHeader("Authorization", "Bearer sk-9ca769cb45c249559276b979a2e1a2cd"). // 认证头 SetBody(request). // 设置JSON体 Post(url) if err != nil { fmt.Println("请求失败:", err) return "" } // 解析响应 var response DeepseekResponse if err := json.Unmarshal(resp.Body(), &response); err != nil { fmt.Println("解析失败:", err) return "" } // 提取关键信息 if len(response.Choices) > 0 { firstChoice := response.Choices[0] // fmt.Printf("\nAI回复 (%s):\n%s\n", // firstChoice.Message.Role, // firstChoice.Message.Content) return firstChoice.Message.Content } return "" }