ai_zhipu.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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": "system",
  138. "content": "你是一名’招标工程师’,拥有写标书及阅读理解公告的能力,根据要求抽取所需的内容,抽取内容要实事求是,不会无中生有。",
  139. })
  140. messages = append(messages, map[string]interface{}{
  141. "role": "user",
  142. "content": content,
  143. })
  144. //glm-4-air glm-4-0520 glm-4-flash
  145. requestData := map[string]interface{}{
  146. "model": ul.FlashModel,
  147. "messages": messages,
  148. "temperature": 0.1,
  149. "max_tokens": 4096,
  150. }
  151. jsonData, _ := json.Marshal(requestData)
  152. // 创建HTTP请求
  153. req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonData))
  154. if err != nil {
  155. log.Debug("Error: %s", err)
  156. return map[string]interface{}{}
  157. }
  158. // 设置请求头
  159. req.Header.Set("Content-Type", "application/json")
  160. // 如果API需要认证,可以在这里设置认证信息
  161. req.Header.Set("Authorization", "Bearer 3d84d30b7ab4c94dbf71853cb7e44719.hLLS4CA2MqVQs6kR")
  162. // 发起请求 14543f0d69d6987c8782fd846e164f26.DXaoS1axLaMP892a
  163. client := &http.Client{}
  164. client.Timeout = 180 * time.Second
  165. resp, err := client.Do(req)
  166. if err != nil {
  167. return map[string]interface{}{}
  168. }
  169. defer resp.Body.Close()
  170. // 解析响应
  171. body, _ := ioutil.ReadAll(resp.Body)
  172. res := make(map[string]interface{})
  173. json.Unmarshal(body, &res)
  174. if res != nil {
  175. if choices := ul.IsMarkInterfaceMap(res["choices"]); len(choices) > 0 {
  176. if message := qu.ObjToMap(choices[0]["message"]); message != nil {
  177. result := qu.ObjToString((*message)["content"])
  178. //最终正确的结果
  179. arr := strings.Split(result, "最终正确的结果")
  180. if len(arr) > 1 {
  181. result = arr[1]
  182. }
  183. result = strings.ReplaceAll(result, "\n", "")
  184. result = strings.ReplaceAll(result, "json", "")
  185. result = strings.ReplaceAll(result, "`", "")
  186. if new_result := ul.SaveResultReg.FindString(result); new_result != "" {
  187. result = new_result
  188. }
  189. dict := make(map[string]interface{})
  190. json.Unmarshal([]byte(result), &dict)
  191. return dict
  192. }
  193. }
  194. }
  195. return map[string]interface{}{}
  196. }
  197. /*****************************
  198. ******************************
  199. ******************************
  200. ******************************
  201. ******************************
  202. ******************************/
  203. // 请求质谱数据外围字段...
  204. func PostZhiPuInfo(content string) map[string]interface{} {
  205. zp, ok := map[string]interface{}{}, 0
  206. for {
  207. ok++
  208. if zp = PostZhiPuAI(content); len(zp) > 0 {
  209. break
  210. }
  211. if ok >= 2 {
  212. break
  213. }
  214. }
  215. return zp
  216. }
  217. // 请求质谱多包字段...
  218. func PostZhiPuPackageInfo(content string) map[string]interface{} {
  219. zp, ok := map[string]interface{}{}, 0
  220. for {
  221. ok++
  222. if zp = PostPackageZhiPuAI(content); len(zp) > 0 {
  223. break
  224. }
  225. if ok >= 2 {
  226. break
  227. }
  228. }
  229. return zp
  230. }
  231. // 请求质谱数据-分类字段
  232. func PostZhiPuClassInfo(content string) (map[string]interface{}, bool) {
  233. zp := map[string]interface{}{}
  234. times := 0
  235. ok := false
  236. for {
  237. times++
  238. zp = PostClassZhiPuAI(content)
  239. if len(zp) > 0 {
  240. ok = true
  241. break
  242. }
  243. if times >= 2 {
  244. break
  245. }
  246. }
  247. return zp, ok
  248. }