extract.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package extract
  2. import (
  3. "data_ai/clean"
  4. "data_ai/prompt"
  5. "data_ai/ul"
  6. log "github.com/donnie4w/go-logger/logger"
  7. qu "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  8. "sync"
  9. "unicode/utf8"
  10. )
  11. // 识别结构化字段
  12. func ExtractFieldInfo(sid string, eid string) {
  13. q := map[string]interface{}{
  14. "_id": map[string]interface{}{
  15. "$gt": ul.StringTOBsonId(sid),
  16. "$lte": ul.StringTOBsonId(eid),
  17. },
  18. }
  19. //先查询抽取表-确定大模型需要识别到范围
  20. dict := ConfrimExtractInfo(q)
  21. log.Debug("查询语句...", q, "~", len(dict))
  22. pool_mgo := make(chan bool, ul.Reading)
  23. wg_mgo := &sync.WaitGroup{}
  24. sess := ul.SourceMgo.GetMgoConn()
  25. defer ul.SourceMgo.DestoryMongoConn(sess)
  26. total, isok := 0, 0
  27. it := sess.DB(ul.SourceMgo.DbName).C(ul.Bid_Name).Find(&q).Sort("_id").Iter()
  28. for tmp := make(map[string]interface{}); it.Next(&tmp); total++ {
  29. if total%5000 == 0 {
  30. log.Debug("cur index ", total)
  31. }
  32. tmpid := ul.BsonTOStringId(tmp["_id"])
  33. infoformat := qu.IntAll(tmp["infoformat"])
  34. if infoformat != 1 || dict[tmpid] == nil {
  35. tmp = make(map[string]interface{})
  36. continue
  37. }
  38. isok++
  39. pool_mgo <- true
  40. wg_mgo.Add(1)
  41. go func(tmp map[string]interface{}) {
  42. defer func() {
  43. <-pool_mgo
  44. wg_mgo.Done()
  45. }()
  46. u_id := ul.BsonTOStringId(tmp["_id"])
  47. data := ResolveInfo(tmp)
  48. if len(data) > 0 || u_id == "" {
  49. ul.SourceMgo.UpdateById(ul.Ext_Name, u_id, map[string]interface{}{
  50. "$set": map[string]interface{}{"ai_zhipu": data},
  51. })
  52. }
  53. }(tmp)
  54. tmp = make(map[string]interface{})
  55. }
  56. wg_mgo.Wait()
  57. log.Debug("ai is over ...", sid, "~", eid)
  58. }
  59. // 获取处理数据...
  60. func ResolveInfo(v map[string]interface{}) map[string]interface{} {
  61. detail := qu.ObjToString(v["detail"])
  62. filetext := qu.ObjToString(v["filetext"]) //此处为附件信息···
  63. title := qu.ObjToString(v["title"])
  64. fns := getpnsinfo(v) //获取附件名字
  65. f_data := map[string]interface{}{}
  66. if ul.IsTool && utf8.RuneCountInString(detail) < 100 {
  67. detail = filetext
  68. }
  69. if utf8.RuneCountInString(detail) < 100 {
  70. return f_data
  71. }
  72. //获取外围字段数据
  73. f_info := prompt.AcquireExtractFieldInfo(detail)
  74. //分包判断-获取信息
  75. ispkg, pkg := false, map[string]interface{}{}
  76. if ispkg = prompt.AcquireIsPackageInfo(detail); ispkg {
  77. f_info["ispkg"] = ispkg
  78. if pkg = prompt.AcquireMultiplePackageInfo(detail); len(pkg) > 0 {
  79. f_info["s_pkg"] = pkg
  80. }
  81. }
  82. //获取分类字段数据
  83. s_toptype, s_subtype := "", ""
  84. if qu.ObjToString(v["toptype"]) == "拟建" {
  85. s_toptype, s_subtype = "拟建", "拟建"
  86. } else if qu.ObjToString(v["toptype"]) == "产权" {
  87. s_toptype, s_subtype = "产权", "产权"
  88. } else {
  89. s_toptype, s_subtype = prompt.AcquireClassInfo(detail, title)
  90. }
  91. f_info["s_toptype"] = s_toptype
  92. f_info["s_subtype"] = s_subtype
  93. //字段清洗
  94. f_data = clean.CleanFieldInfo(f_info, fns)
  95. //对于某些字段进行二级校验
  96. if s_buyer := qu.ObjToString(f_data["s_buyer"]); s_buyer != "" {
  97. if zp_buyer := prompt.AcquireBuyerInfo(s_buyer); zp_buyer["实体单位"] != nil {
  98. if ns_buyer := clean.CleanBuyer(qu.ObjToString(zp_buyer["实体单位"])); ns_buyer != "" {
  99. f_data["s_buyer"] = ns_buyer
  100. }
  101. }
  102. }
  103. return f_data
  104. }
  105. func ConfrimExtractInfo(q map[string]interface{}) map[string]interface{} {
  106. dict := map[string]interface{}{}
  107. sess := ul.SourceMgo.GetMgoConn()
  108. defer ul.SourceMgo.DestoryMongoConn(sess)
  109. total := 0
  110. it := sess.DB(ul.SourceMgo.DbName).C(ul.Ext_Name).Find(&q).Select(map[string]interface{}{"_id": 1, "ai_zhipu": 1}).Iter()
  111. for tmp := make(map[string]interface{}); it.Next(&tmp); total++ {
  112. if total%1000 == 0 {
  113. log.Debug("cur index ", total)
  114. }
  115. if tmp["ai_zhipu"] == nil { //已经识别的数据-不再识别
  116. tmpid := ul.BsonTOStringId(tmp["_id"])
  117. dict[tmpid] = tmpid
  118. }
  119. tmp = make(map[string]interface{})
  120. }
  121. return dict
  122. }
  123. // 获取附件名字信息
  124. func getpnsinfo(tmp map[string]interface{}) []string {
  125. arr := []string{}
  126. if projectinfo := qu.ObjToMap(tmp["projectinfo"]); projectinfo != nil {
  127. if attachments := qu.ObjToMap((*projectinfo)["attachments"]); attachments != nil {
  128. for _, v := range *attachments {
  129. if info := qu.ObjToMap(v); info != nil {
  130. if filename := qu.ObjToString((*info)["filename"]); filename != "" {
  131. arr = append(arr, filename)
  132. }
  133. }
  134. }
  135. }
  136. }
  137. return arr
  138. }
  139. // 暂时不启用...无限重试
  140. func RunResetUpdateFieldInfo(arr []string, name string, s_name string) {
  141. //log.Debug("开始重置更新...", len(arr))
  142. //reset := []string{}
  143. //for k, v := range arr {
  144. // log.Debug("...", k, "...", v)
  145. // data := ul.SourceMgo.FindById(name, v)
  146. // content := PromptFieldText(qu.ObjToString(data["detail"]))
  147. // zp, ok := map[string]interface{}{}, 0
  148. // for {
  149. // ok++
  150. // if zp = ai.PostZhiPuAI(content); len(zp) > 0 {
  151. // break
  152. // }
  153. // if ok >= 5 {
  154. // log.Debug("请求数据失败...", v)
  155. // reset = append(reset, v)
  156. // break
  157. // }
  158. // }
  159. // ul.SourceMgo.UpdateById(s_name, v, map[string]interface{}{
  160. // "$set": map[string]interface{}{
  161. // "zhipu": zp,
  162. // },
  163. // })
  164. //}
  165. //if len(reset) > 0 { //无限尝试
  166. // RunResetUpdateFieldInfo(reset, name, s_name)
  167. //}
  168. //log.Debug("本轮重置更新结束......")
  169. }