ai_deekseek.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. var req_retry_deepseek = 1
  13. // 通用提示语···
  14. func PostDeepSeekAI(content string) map[string]interface{} {
  15. // API的URL
  16. apiURL := "https://api.deepseek.com/chat/completions"
  17. // 构造请求数据
  18. messages := []map[string]interface{}{}
  19. messages = append(messages, map[string]interface{}{
  20. "role": "user",
  21. "content": content,
  22. })
  23. //glm-4-air glm-4-0520 glm-4-flash
  24. requestData := map[string]interface{}{
  25. "model": "deepseek-chat",
  26. "temperature": 1,
  27. "messages": messages,
  28. }
  29. jsonData, _ := json.Marshal(requestData)
  30. // 创建HTTP请求
  31. req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonData))
  32. if err != nil {
  33. log.Debug("Error: %s", err)
  34. return map[string]interface{}{}
  35. }
  36. // 设置请求头
  37. req.Header.Add("Content-Type", "application/json")
  38. req.Header.Add("Accept", "application/json")
  39. req.Header.Add("Authorization", "Bearer sk-7e40165d06a04f3c87e2fc15e69df625")
  40. client := &http.Client{}
  41. client.Timeout = 120 * 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. //func PostClassDeepSeekAI(content string) map[string]interface{} {
  69. // // API的URL
  70. // apiURL := "https://api.deepseek.com/chat/completions"
  71. // // 构造请求数据
  72. // messages := []map[string]interface{}{}
  73. // messages = append(messages, map[string]interface{}{
  74. // "role": "user",
  75. // "content": content,
  76. // })
  77. // //glm-4-air glm-4-0520 glm-4-flash
  78. // requestData := map[string]interface{}{
  79. // "model": "deepseek-chat",
  80. // "messages": messages,
  81. // }
  82. // jsonData, _ := json.Marshal(requestData)
  83. // // 创建HTTP请求
  84. // req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonData))
  85. // if err != nil {
  86. // log.Debug("Error: %s", err)
  87. // return map[string]interface{}{}
  88. // }
  89. // // 设置请求头
  90. // req.Header.Add("Content-Type", "application/json")
  91. // req.Header.Add("Accept", "application/json")
  92. // req.Header.Add("Authorization", "Bearer sk-832c259e371743a0bb7b280cb86ad806")
  93. //
  94. // client := &http.Client{}
  95. // client.Timeout = 120 * time.Second
  96. // resp, err := client.Do(req)
  97. // if err != nil {
  98. // return map[string]interface{}{}
  99. // }
  100. // defer resp.Body.Close()
  101. //
  102. // // 解析响应
  103. // body, _ := ioutil.ReadAll(resp.Body)
  104. // res := make(map[string]interface{})
  105. // json.Unmarshal(body, &res)
  106. // if res != nil {
  107. // if choices := ul.IsMarkInterfaceMap(res["choices"]); len(choices) > 0 {
  108. // if message := qu.ObjToMap(choices[0]["message"]); message != nil {
  109. // result := qu.ObjToString((*message)["content"])
  110. // result = ul.Escape.ReplaceAllString(result, "")
  111. // if new_result := ul.SaveResultReg.FindString(result); new_result != "" {
  112. // result = new_result
  113. // }
  114. // dict := make(map[string]interface{})
  115. // json.Unmarshal([]byte(result), &dict)
  116. // return dict
  117. // }
  118. // }
  119. // }
  120. // return map[string]interface{}{}
  121. //}
  122. // 分包字段
  123. //func PostPackageDeepSeekAI(content string) map[string]interface{} {
  124. // // API的URL
  125. // apiURL := "https://api.deepseek.com/chat/completions"
  126. // // 构造请求数据
  127. // messages := []map[string]interface{}{}
  128. // messages = append(messages, map[string]interface{}{
  129. // "role": "system",
  130. // "content": "你是一名’招标工程师’,拥有写标书及阅读理解公告的能力,根据要求抽取所需的内容,抽取内容要实事求是,不会无中生有。",
  131. // })
  132. // messages = append(messages, map[string]interface{}{
  133. // "role": "user",
  134. // "content": content,
  135. // })
  136. // //glm-4-air glm-4-0520 glm-4-flash
  137. // requestData := map[string]interface{}{
  138. // "model": "deepseek-chat",
  139. // "messages": messages,
  140. // }
  141. // jsonData, _ := json.Marshal(requestData)
  142. // // 创建HTTP请求
  143. // req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonData))
  144. // if err != nil {
  145. // log.Debug("Error: %s", err)
  146. // return map[string]interface{}{}
  147. // }
  148. // // 设置请求头
  149. // req.Header.Add("Content-Type", "application/json")
  150. // req.Header.Add("Accept", "application/json")
  151. // req.Header.Add("Authorization", "Bearer sk-832c259e371743a0bb7b280cb86ad806")
  152. //
  153. // client := &http.Client{}
  154. // client.Timeout = 180 * time.Second
  155. // resp, err := client.Do(req)
  156. // if err != nil {
  157. // return map[string]interface{}{}
  158. // }
  159. // defer resp.Body.Close()
  160. //
  161. // // 解析响应
  162. // body, _ := ioutil.ReadAll(resp.Body)
  163. // res := make(map[string]interface{})
  164. // json.Unmarshal(body, &res)
  165. // if res != nil {
  166. // if choices := ul.IsMarkInterfaceMap(res["choices"]); len(choices) > 0 {
  167. // if message := qu.ObjToMap(choices[0]["message"]); message != nil {
  168. // result := qu.ObjToString((*message)["content"])
  169. // //最终正确的结果
  170. // arr := strings.Split(result, "最终正确的结果")
  171. // if len(arr) > 1 {
  172. // result = arr[1]
  173. // }
  174. // result = ul.Escape.ReplaceAllString(result, "")
  175. // if new_result := ul.SaveResultReg.FindString(result); new_result != "" {
  176. // result = new_result
  177. // }
  178. // dict := make(map[string]interface{})
  179. // json.Unmarshal([]byte(result), &dict)
  180. // return dict
  181. // }
  182. // }
  183. // }
  184. // return map[string]interface{}{}
  185. //}
  186. /*****************************
  187. ******************************
  188. ******************************
  189. ******************************
  190. ******************************
  191. ******************************/
  192. // 请求数据外围字段···重试1次···
  193. func PostDeepSeekInfo(content string) map[string]interface{} {
  194. zp, ok := map[string]interface{}{}, 0
  195. for {
  196. ok++
  197. if zp = PostDeepSeekAI(content); len(zp) > 0 {
  198. break
  199. }
  200. if ok >= req_retry {
  201. break
  202. }
  203. }
  204. return zp
  205. }
  206. // 请求多包字段...
  207. //func PostDeepSeekPackageInfo(content string) map[string]interface{} {
  208. // zp, ok := map[string]interface{}{}, 0
  209. // for {
  210. // ok++
  211. // if zp = PostPackageDeepSeekAI(content); len(zp) > 0 {
  212. // break
  213. // }
  214. // if ok >= req_retry_deepseek {
  215. // break
  216. // }
  217. // }
  218. // return zp
  219. //}
  220. // 请求质谱数据-分类字段
  221. //func PostDeepSeekClassInfo(content string) (map[string]interface{}, bool) {
  222. // zp := map[string]interface{}{}
  223. // times := 0
  224. // ok := false
  225. // for {
  226. // times++
  227. // zp = PostClassDeepSeekAI(content)
  228. // if len(zp) > 0 {
  229. // ok = true
  230. // break
  231. // }
  232. // if times >= req_retry_deepseek {
  233. // break
  234. // }
  235. // }
  236. // return zp, ok
  237. //}