ai_zhipu.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. "strings"
  11. "time"
  12. )
  13. // 智谱清言-通用外围
  14. func PostZhiPuAI(content string) map[string]interface{} {
  15. // API的URL
  16. apiURL := "https://open.bigmodel.cn/api/paas/v4/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": ul.FlashModel,
  26. "messages": messages,
  27. "temperature": 0.1,
  28. "max_tokens": 4096,
  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.Set("Content-Type", "application/json")
  39. // 如果API需要认证,可以在这里设置认证信息
  40. req.Header.Set("Authorization", "Bearer 3d84d30b7ab4c94dbf71853cb7e44719.hLLS4CA2MqVQs6kR")
  41. // 发起请求 14543f0d69d6987c8782fd846e164f26.DXaoS1axLaMP892a
  42. client := &http.Client{}
  43. client.Timeout = 180 * time.Second
  44. resp, err := client.Do(req)
  45. if err != nil {
  46. return map[string]interface{}{}
  47. }
  48. defer resp.Body.Close()
  49. // 解析响应
  50. body, _ := ioutil.ReadAll(resp.Body)
  51. res := make(map[string]interface{})
  52. json.Unmarshal(body, &res)
  53. if res != nil {
  54. if choices := ul.IsMarkInterfaceMap(res["choices"]); len(choices) > 0 {
  55. if message := qu.ObjToMap(choices[0]["message"]); message != nil {
  56. result := qu.ObjToString((*message)["content"])
  57. result = strings.ReplaceAll(result, "\n", "")
  58. result = strings.ReplaceAll(result, "json", "")
  59. result = strings.ReplaceAll(result, "`", "")
  60. if new_result := ul.SaveResultReg.FindString(result); new_result != "" {
  61. result = new_result
  62. }
  63. dict := make(map[string]interface{})
  64. json.Unmarshal([]byte(result), &dict)
  65. return dict
  66. }
  67. }
  68. }
  69. return map[string]interface{}{}
  70. }
  71. // 智谱清言-分类字段
  72. func PostClassZhiPuAI(content string) map[string]interface{} {
  73. // API的URL
  74. apiURL := "https://open.bigmodel.cn/api/paas/v4/chat/completions"
  75. // 构造请求数据
  76. messages := []map[string]interface{}{}
  77. messages = append(messages, map[string]interface{}{
  78. "role": "user",
  79. "content": content,
  80. })
  81. //glm-4-air glm-4-0520 glm-4-flash
  82. requestData := map[string]interface{}{
  83. "model": ul.FlashModel,
  84. "messages": messages,
  85. "temperature": 0.2,
  86. "top_p": 0.7,
  87. //"max_tokens": 4096,
  88. }
  89. jsonData, _ := json.Marshal(requestData)
  90. // 创建HTTP请求
  91. req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonData))
  92. if err != nil {
  93. log.Debug("Error: %s", err)
  94. return map[string]interface{}{}
  95. }
  96. // 设置请求头
  97. req.Header.Set("Content-Type", "application/json")
  98. // 如果API需要认证,可以在这里设置认证信息
  99. req.Header.Set("Authorization", "Bearer 3d84d30b7ab4c94dbf71853cb7e44719.hLLS4CA2MqVQs6kR")
  100. // 发起请求 14543f0d69d6987c8782fd846e164f26.DXaoS1axLaMP892a
  101. client := &http.Client{}
  102. client.Timeout = 180 * time.Second
  103. resp, err := client.Do(req)
  104. if err != nil {
  105. return map[string]interface{}{}
  106. }
  107. defer resp.Body.Close()
  108. // 解析响应
  109. body, _ := ioutil.ReadAll(resp.Body)
  110. res := make(map[string]interface{})
  111. json.Unmarshal(body, &res)
  112. if res != nil {
  113. if choices := ul.IsMarkInterfaceMap(res["choices"]); len(choices) > 0 {
  114. if message := qu.ObjToMap(choices[0]["message"]); message != nil {
  115. result := qu.ObjToString((*message)["content"])
  116. result = strings.ReplaceAll(result, "\n", "")
  117. result = strings.ReplaceAll(result, "json", "")
  118. result = strings.ReplaceAll(result, "`", "")
  119. if new_result := ul.SaveResultReg.FindString(result); new_result != "" {
  120. result = new_result
  121. }
  122. dict := make(map[string]interface{})
  123. json.Unmarshal([]byte(result), &dict)
  124. return dict
  125. }
  126. }
  127. }
  128. return map[string]interface{}{}
  129. }
  130. // 智谱清言-分类字段
  131. func PostPackageZhiPuAI(content string) map[string]interface{} {
  132. // API的URL
  133. apiURL := "https://open.bigmodel.cn/api/paas/v4/chat/completions"
  134. // 构造请求数据
  135. messages := []map[string]interface{}{}
  136. messages = append(messages, map[string]interface{}{
  137. "role": "user",
  138. "content": content,
  139. })
  140. //glm-4-air glm-4-0520 glm-4-flash
  141. requestData := map[string]interface{}{
  142. "model": ul.FlashModel,
  143. "messages": messages,
  144. "temperature": 0.1,
  145. "max_tokens": 4096,
  146. }
  147. jsonData, _ := json.Marshal(requestData)
  148. // 创建HTTP请求
  149. req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonData))
  150. if err != nil {
  151. log.Debug("Error: %s", err)
  152. return map[string]interface{}{}
  153. }
  154. // 设置请求头
  155. req.Header.Set("Content-Type", "application/json")
  156. // 如果API需要认证,可以在这里设置认证信息
  157. req.Header.Set("Authorization", "Bearer 3d84d30b7ab4c94dbf71853cb7e44719.hLLS4CA2MqVQs6kR")
  158. // 发起请求 14543f0d69d6987c8782fd846e164f26.DXaoS1axLaMP892a
  159. client := &http.Client{}
  160. client.Timeout = 180 * time.Second
  161. resp, err := client.Do(req)
  162. if err != nil {
  163. return map[string]interface{}{}
  164. }
  165. defer resp.Body.Close()
  166. // 解析响应
  167. body, _ := ioutil.ReadAll(resp.Body)
  168. res := make(map[string]interface{})
  169. json.Unmarshal(body, &res)
  170. if res != nil {
  171. if choices := ul.IsMarkInterfaceMap(res["choices"]); len(choices) > 0 {
  172. if message := qu.ObjToMap(choices[0]["message"]); message != nil {
  173. result := qu.ObjToString((*message)["content"])
  174. //最终正确的结果
  175. arr := strings.Split(result, "最终正确的结果")
  176. if len(arr) > 1 {
  177. result = arr[1]
  178. }
  179. result = strings.ReplaceAll(result, "\n", "")
  180. result = strings.ReplaceAll(result, "json", "")
  181. result = strings.ReplaceAll(result, "`", "")
  182. if new_result := ul.SaveResultReg.FindString(result); new_result != "" {
  183. result = new_result
  184. }
  185. dict := make(map[string]interface{})
  186. json.Unmarshal([]byte(result), &dict)
  187. return dict
  188. }
  189. }
  190. }
  191. return map[string]interface{}{}
  192. }
  193. /*****************************
  194. ******************************
  195. ******************************
  196. ******************************
  197. ******************************
  198. ******************************/
  199. // 请求质谱数据外围字段...
  200. func PostZhiPuInfo(content string) map[string]interface{} {
  201. zp, ok := map[string]interface{}{}, 0
  202. for {
  203. ok++
  204. if zp = PostZhiPuAI(content); len(zp) > 0 {
  205. break
  206. }
  207. if ok >= 2 {
  208. break
  209. }
  210. }
  211. return zp
  212. }
  213. // 请求质谱多包字段...
  214. func PostZhiPuPackageInfo(content string) map[string]interface{} {
  215. zp, ok := map[string]interface{}{}, 0
  216. for {
  217. ok++
  218. if zp = PostPackageZhiPuAI(content); len(zp) > 0 {
  219. break
  220. }
  221. if ok >= 2 {
  222. break
  223. }
  224. }
  225. return zp
  226. }
  227. // 请求质谱数据-分类字段
  228. func PostZhiPuClassInfo(content string) (map[string]interface{}, bool) {
  229. zp := map[string]interface{}{}
  230. times := 0
  231. ok := false
  232. for {
  233. times++
  234. zp = PostClassZhiPuAI(content)
  235. if len(zp) > 0 {
  236. ok = true
  237. break
  238. }
  239. if times >= 2 {
  240. break
  241. }
  242. }
  243. return zp, ok
  244. }