tools.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. zhipu "github.com/itcwc/go-zhipu/model_api"
  6. "log"
  7. "strings"
  8. )
  9. // ZpAI 智普AI
  10. func ZpAI(apiKey, model, content string) (rest map[string]interface{}) {
  11. expireAtTime := int64(1719803252) // token 过期时间
  12. sys := "请根据我给出的公司名称,依据其单位性质、业务范围和单位职能,准确给出最符合的一个国标行业分类标签,分别给出大类、中类和小类。输出结果以JSON格式返回,格式如下:{\"label1\":\"大类\",\"label2\":\"中类\",\"label3\":\"小类\"}。我只要最匹配 的一个标签,不要返回多个字段;如果无法识别出类别,直接给我空字符串。按照以上要求输出,不要联想,不要无中生有,不要生成解释。单位名称是:"
  13. text := fmt.Sprintf(sys, content)
  14. mssage := zhipu.PostParams{
  15. Model: model,
  16. Messages: []zhipu.Message{
  17. {
  18. Role: "user", // 消息的角色信息 详见文档
  19. Content: text,
  20. },
  21. },
  22. }
  23. postResponse, err := zhipu.BeCommonModel(expireAtTime, mssage, apiKey)
  24. if err != nil {
  25. fmt.Println(err)
  26. return
  27. }
  28. if choices, ok := postResponse["choices"].([]interface{}); ok {
  29. if choice, ok2 := choices[0].(map[string]interface{}); ok2 {
  30. if message, ok3 := choice["message"].(map[string]interface{}); ok3 {
  31. if content, ok4 := message["content"].(string); ok4 {
  32. content = strings.ReplaceAll(content, "\n", "")
  33. content = strings.ReplaceAll(content, "json", "")
  34. content = strings.ReplaceAll(content, "`", "")
  35. err = json.Unmarshal([]byte(content), &rest)
  36. if err != nil {
  37. log.Println("Unmarshal err", err, "content:", content)
  38. }
  39. }
  40. }
  41. }
  42. }
  43. return
  44. }
  45. func checkString(s string) bool {
  46. for _, char := range s {
  47. if ('a' <= char && char <= 'z') || ('A' <= char && char <= 'Z') || strings.ContainsAny(string(char), "12345678910") {
  48. return true
  49. }
  50. }
  51. return false
  52. }
  53. // ZpAI1 智普问答,附带业务范围
  54. func ZpAI1(apiKey, model, name string, businessScope string) (rest map[string]interface{}) {
  55. expireAtTime := int64(1719803252) // token 过期时间
  56. sys := "请根据我给出的公司名称,依据其单位性质、单位职能和业务范围,准确给出最符合的一个国标行业分类标签,分别给出大类、中类和小类。输出结果以JSON格式返回,格式如下:{\"label1\":\"大类\",\"label2\":\"中类\",\"label3\":\"小类\"}。我只要最匹配 的一个标签,不要返回多个字段;如果无法识别出类别,直接给我空字符串。按照以上要求输出,不要联想,不要无中生有,不要生成解释。单位名称是:"
  57. text := fmt.Sprintf(sys, name)
  58. if businessScope != "" {
  59. text = text + ";业务范围是:" + businessScope
  60. }
  61. mssage := zhipu.PostParams{
  62. Model: model,
  63. Messages: []zhipu.Message{
  64. {
  65. Role: "user", // 消息的角色信息 详见文档
  66. Content: text,
  67. },
  68. },
  69. }
  70. postResponse, err := zhipu.BeCommonModel(expireAtTime, mssage, apiKey)
  71. if err != nil {
  72. fmt.Println(err)
  73. return
  74. }
  75. if choices, ok := postResponse["choices"].([]interface{}); ok {
  76. if choice, ok2 := choices[0].(map[string]interface{}); ok2 {
  77. if message, ok3 := choice["message"].(map[string]interface{}); ok3 {
  78. if content, ok4 := message["content"].(string); ok4 {
  79. content = strings.ReplaceAll(content, "\n", "")
  80. content = strings.ReplaceAll(content, "json", "")
  81. content = strings.ReplaceAll(content, "`", "")
  82. err = json.Unmarshal([]byte(content), &rest)
  83. if err != nil {
  84. log.Println("Unmarshal err", err, "content:", content)
  85. }
  86. }
  87. }
  88. }
  89. }
  90. return
  91. }