ai_doubao.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package ai
  2. import (
  3. "bytes"
  4. "data_ai/ul"
  5. "encoding/json"
  6. log "github.com/donnie4w/go-logger/logger"
  7. "io/ioutil"
  8. qu "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  9. "net/http"
  10. "time"
  11. )
  12. // 通用提示语···
  13. func PostDouBaoDSAI(content string) map[string]interface{} {
  14. // API的URL
  15. apiURL := "https://ark.cn-beijing.volces.com/api/v3/chat/completions"
  16. // 构造请求数据
  17. messages := []map[string]interface{}{}
  18. messages = append(messages, map[string]interface{}{
  19. "role": "user",
  20. "content": content,
  21. })
  22. //ep-20250225173301-w557p 线上
  23. //ep-20250313104433-ptcxr 测试
  24. requestData := map[string]interface{}{
  25. "model": "ep-20250225173301-w557p",
  26. "temperature": 0.1,
  27. "top_p": 0.7,
  28. "messages": messages,
  29. }
  30. jsonData, _ := json.Marshal(requestData)
  31. // 创建HTTP请求
  32. req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonData))
  33. if err != nil {
  34. log.Debug("Error: %s", err)
  35. return map[string]interface{}{}
  36. }
  37. // 设置请求头
  38. req.Header.Add("Content-Type", "application/json")
  39. req.Header.Add("Authorization", "Bearer df50c86b-24f5-4475-b09b-f1f85e5e6564")
  40. client := &http.Client{}
  41. client.Timeout = 300 * time.Second
  42. resp, err := client.Do(req)
  43. if err != nil {
  44. return map[string]interface{}{}
  45. }
  46. defer resp.Body.Close()
  47. // 解析响应
  48. body, _ := ioutil.ReadAll(resp.Body)
  49. res := make(map[string]interface{})
  50. json.Unmarshal(body, &res)
  51. if res != nil {
  52. if choices := ul.IsMarkInterfaceMap(res["choices"]); len(choices) > 0 {
  53. if message := qu.ObjToMap(choices[0]["message"]); message != nil {
  54. result := qu.ObjToString((*message)["content"])
  55. result = ul.Escape.ReplaceAllString(result, "")
  56. if new_result := ul.SaveResultReg.FindString(result); new_result != "" {
  57. result = new_result
  58. }
  59. dict := make(map[string]interface{})
  60. json.Unmarshal([]byte(result), &dict)
  61. return dict
  62. }
  63. }
  64. }
  65. return map[string]interface{}{}
  66. }
  67. /*****************************
  68. ******************************
  69. ******************************
  70. ******************************
  71. ******************************
  72. ******************************/
  73. // 请求数据外围字段···重试1次···
  74. func PostDouBaoDSInfo(content string) map[string]interface{} {
  75. zp, ok := map[string]interface{}{}, 0
  76. for {
  77. ok++
  78. if zp = PostDouBaoDSAI(content); len(zp) > 0 {
  79. break
  80. }
  81. if ok >= req_retry_deepseek {
  82. break
  83. }
  84. }
  85. return zp
  86. }