tools.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. zhipu "github.com/itcwc/go-zhipu/model_api"
  6. util "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  7. "log"
  8. "strings"
  9. )
  10. // GetJyURLByID 获取剑鱼地址
  11. func GetJyURLByID(id string) string {
  12. var Url = "https://www.jianyu360.com/article/content/%s.html"
  13. url := fmt.Sprintf(Url, util.CommonEncodeArticle("content", id))
  14. return url
  15. }
  16. // GetIdByURL 解密url,获取bidding ID
  17. func GetIdByURL(url string) string {
  18. if strings.Contains(url, "work-bench") {
  19. return ""
  20. }
  21. if strings.Contains(url, "/article/content") {
  22. urls := strings.Split(url, "content/")
  23. res := strings.Split(urls[1], ".html")
  24. ids := util.CommonDecodeArticle("content", res[0])
  25. return ids[0]
  26. }
  27. if strings.HasSuffix(url, "appid") {
  28. urls := strings.Split(url, "entservice/")
  29. res := strings.Split(urls[1], ".html")
  30. se := util.SimpleEncrypt{Key: "entservice"}
  31. id := se.DecodeString(res[0])
  32. return id
  33. }
  34. return ""
  35. }
  36. // ZpAI 智普AI
  37. func ZpAI(apiKey, model, content string) (rest map[string]interface{}) {
  38. expireAtTime := int64(1719803252) // token 过期时间
  39. sys := "请根据我给出的公司名称,依据其单位性质、业务范围和单位职能,准确给出最符合的一个国标行业分类标签,分别给出大类、中类和小类。输出结果以JSON格式返回,格式如下:{\"label1\":\"大类\",\"label2\":\"中类\",\"label3\":\"小类\"}。我只要最匹配 的一个标签,不要返回多个字段;如果无法识别出类别,直接给我空字符串。按照以上要求输出,不要联想,不要无中生有,不要生成解释。单位名称是:"
  40. text := fmt.Sprintf(sys, content)
  41. mssage := zhipu.PostParams{
  42. Model: model,
  43. Messages: []zhipu.Message{
  44. {
  45. Role: "user", // 消息的角色信息 详见文档
  46. Content: text,
  47. },
  48. },
  49. }
  50. postResponse, err := zhipu.BeCommonModel(expireAtTime, mssage, apiKey)
  51. if err != nil {
  52. fmt.Println(err)
  53. return
  54. }
  55. if choices, ok := postResponse["choices"].([]interface{}); ok {
  56. if choice, ok2 := choices[0].(map[string]interface{}); ok2 {
  57. if message, ok3 := choice["message"].(map[string]interface{}); ok3 {
  58. if content, ok4 := message["content"].(string); ok4 {
  59. content = strings.ReplaceAll(content, "\n", "")
  60. content = strings.ReplaceAll(content, "json", "")
  61. content = strings.ReplaceAll(content, "`", "")
  62. err = json.Unmarshal([]byte(content), &rest)
  63. if err != nil {
  64. log.Println("Unmarshal err", err, "content:", content)
  65. }
  66. }
  67. }
  68. }
  69. }
  70. return
  71. }
  72. func checkString(s string) bool {
  73. for _, char := range s {
  74. if ('a' <= char && char <= 'z') || ('A' <= char && char <= 'Z') || strings.ContainsAny(string(char), "12345678910") {
  75. return true
  76. }
  77. }
  78. return false
  79. }
  80. // ZpAI1 智普问答,附带业务范围
  81. func ZpAI1(apiKey, model, name string, businessScope string) (rest map[string]interface{}) {
  82. expireAtTime := int64(1719803252) // token 过期时间
  83. sys := "请根据我给出的公司名称,依据其单位性质、单位职能和业务范围,准确给出最符合的一个国标行业分类标签,分别给出大类、中类和小类。输出结果以JSON格式返回,格式如下:{\"label1\":\"大类\",\"label2\":\"中类\",\"label3\":\"小类\"}。我只要最匹配 的一个标签,不要返回多个字段;如果无法识别出类别,直接给我空字符串。按照以上要求输出,不要联想,不要无中生有,不要生成解释。单位名称是:"
  84. text := fmt.Sprintf(sys, name)
  85. if businessScope != "" {
  86. text = text + ";业务范围是:" + businessScope
  87. }
  88. mssage := zhipu.PostParams{
  89. Model: model,
  90. Messages: []zhipu.Message{
  91. {
  92. Role: "user", // 消息的角色信息 详见文档
  93. Content: text,
  94. },
  95. },
  96. }
  97. postResponse, err := zhipu.BeCommonModel(expireAtTime, mssage, apiKey)
  98. if err != nil {
  99. fmt.Println(err)
  100. return
  101. }
  102. if choices, ok := postResponse["choices"].([]interface{}); ok {
  103. if choice, ok2 := choices[0].(map[string]interface{}); ok2 {
  104. if message, ok3 := choice["message"].(map[string]interface{}); ok3 {
  105. if content, ok4 := message["content"].(string); ok4 {
  106. content = strings.ReplaceAll(content, "\n", "")
  107. content = strings.ReplaceAll(content, "json", "")
  108. content = strings.ReplaceAll(content, "`", "")
  109. err = json.Unmarshal([]byte(content), &rest)
  110. if err != nil {
  111. log.Println("Unmarshal err", err, "content:", content)
  112. }
  113. }
  114. }
  115. }
  116. }
  117. return
  118. }
  119. func contains(list []string, target string) bool {
  120. for _, item := range list {
  121. if item == target {
  122. return true
  123. }
  124. }
  125. return false
  126. }