ai_deekseek.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. log "github.com/donnie4w/go-logger/logger"
  6. "io/ioutil"
  7. "net/http"
  8. "time"
  9. )
  10. // 通用提示语···
  11. func PostDouBaoDSAI(content string) string {
  12. // API的URL
  13. apiURL := "https://ark.cn-beijing.volces.com/api/v3/chat/completions"
  14. // 构造请求数据
  15. messages := []map[string]interface{}{}
  16. messages = append(messages, map[string]interface{}{
  17. "role": "user",
  18. "content": content,
  19. })
  20. //glm-4-air glm-4-0520 glm-4-flash
  21. requestData := map[string]interface{}{
  22. "model": "ep-20250313104433-ptcxr",
  23. //"temperature": 0.1,
  24. //"top_p": 0.7,
  25. "messages": messages,
  26. }
  27. jsonData, _ := json.Marshal(requestData)
  28. // 创建HTTP请求
  29. req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonData))
  30. if err != nil {
  31. log.Debug("Error: %s", err)
  32. return ""
  33. }
  34. // 设置请求头
  35. req.Header.Add("Content-Type", "application/json")
  36. req.Header.Add("Authorization", "Bearer df50c86b-24f5-4475-b09b-f1f85e5e6564")
  37. client := &http.Client{}
  38. client.Timeout = 300 * time.Second
  39. resp, err := client.Do(req)
  40. if err != nil {
  41. return ""
  42. }
  43. defer resp.Body.Close()
  44. // 解析响应
  45. body, _ := ioutil.ReadAll(resp.Body)
  46. res := make(map[string]interface{})
  47. json.Unmarshal(body, &res)
  48. if res != nil {
  49. if choices, ok := res["choices"].([]interface{}); ok {
  50. if choice, ok2 := choices[0].(map[string]interface{}); ok2 {
  51. if message, ok3 := choice["message"].(map[string]interface{}); ok3 {
  52. if rescontent, ok4 := message["content"].(string); ok4 {
  53. return rescontent
  54. }
  55. }
  56. }
  57. }
  58. }
  59. //log.Debug(res)
  60. return ""
  61. //return map[string]interface{}{}
  62. }