extract.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. "strings"
  9. "sync"
  10. "time"
  11. "unicode/utf8"
  12. )
  13. // 识别结构化字段
  14. func ExtractFieldInfo(sid string, eid string) {
  15. q := map[string]interface{}{
  16. "_id": map[string]interface{}{
  17. "$gt": ul.StringTOBsonId(sid),
  18. "$lte": ul.StringTOBsonId(eid),
  19. },
  20. }
  21. //先查询抽取表-确定大模型需要识别到范围
  22. dict := ConfrimExtractInfo(q)
  23. log.Debug("查询语句...", q, "~", len(dict))
  24. if len(dict) >= ul.MaxUdp { //根据数量限制使用具体模型
  25. log.Debug("数量超过限制临时使用:glm-4-flashx")
  26. ul.FlashModel = "glm-4-flashx"
  27. } else {
  28. ul.FlashModel = "glm-4-flash"
  29. }
  30. pool_mgo := make(chan bool, ul.Reading)
  31. wg_mgo := &sync.WaitGroup{}
  32. sess := ul.BidMgo.GetMgoConn()
  33. defer ul.BidMgo.DestoryMongoConn(sess)
  34. total, isok := 0, 0
  35. it := sess.DB(ul.BidMgo.DbName).C(ul.Bid_Name).Find(&q).Sort("_id").Iter()
  36. for tmp := make(map[string]interface{}); it.Next(&tmp); total++ {
  37. if total%200 == 0 {
  38. log.Debug("cur ai index ", total)
  39. }
  40. tmpid := ul.BsonTOStringId(tmp["_id"])
  41. infoformat := qu.IntAll(tmp["infoformat"])
  42. if infoformat > 1 || dict[tmpid] == nil {
  43. tmp = make(map[string]interface{})
  44. continue
  45. }
  46. isok++
  47. pool_mgo <- true
  48. wg_mgo.Add(1)
  49. go func(tmp map[string]interface{}) {
  50. defer func() {
  51. <-pool_mgo
  52. wg_mgo.Done()
  53. }()
  54. u_id := ul.BsonTOStringId(tmp["_id"])
  55. data := ResolveInfo(tmp)
  56. if len(data) > 0 || u_id == "" {
  57. ul.SourceMgo.UpdateById(ul.Ext_Name, u_id, map[string]interface{}{
  58. "$set": map[string]interface{}{"ai_zhipu": data},
  59. })
  60. }
  61. }(tmp)
  62. tmp = make(map[string]interface{})
  63. }
  64. wg_mgo.Wait()
  65. log.Debug("ai is over ...", sid, "~", eid)
  66. }
  67. // 获取处理数据...
  68. func ResolveInfo(v map[string]interface{}) map[string]interface{} {
  69. tmpid := ul.BsonTOStringId(v["_id"])
  70. title := qu.ObjToString(v["title"])
  71. old_detail := getDetailText(v, tmpid) //获取正文文本
  72. //是否表格
  73. isTable := false
  74. if strings.Contains(old_detail, "<table>") {
  75. isTable = false //可以屏蔽表格的识别内容
  76. }
  77. //过滤信息
  78. if NotInProgressInfo(title, old_detail, v) {
  79. return map[string]interface{}{}
  80. }
  81. //识别结构,短文本结构
  82. f_data, shorText := map[string]interface{}{}, false
  83. if utf8.RuneCountInString(old_detail) < 100 {
  84. shorText = true
  85. }
  86. //文本格式转换
  87. new_detail := ul.HttpConvertToMarkdown(old_detail)
  88. //短文本判断是否有效性
  89. if shorText {
  90. if info := prompt.AcquireJudgeShortInfo(new_detail); info["结果"] != "是" {
  91. return map[string]interface{}{}
  92. }
  93. }
  94. //获取外围字段数据-拆分合并字段
  95. f_info_1 := prompt.AcquireExtractFieldInfoFirst(new_detail)
  96. f_info_2 := prompt.AcquireExtractFieldInfoSecond(new_detail)
  97. f_info_3 := prompt.AcquireExtractFieldInfoThird(new_detail)
  98. f_info := MergeInfo([]map[string]interface{}{f_info_1, f_info_2, f_info_3})
  99. //非短文本以下识别-纯测试
  100. if !shorText {
  101. //获取分包信息
  102. if pkg := prompt.AcquireNewMultiplePackageInfo(new_detail, isTable); len(pkg) > 0 {
  103. f_info["s_pkg"] = pkg
  104. }
  105. //获取分类字段数据
  106. s_toptype, s_subtype := prompt.AcquireClassInfo(new_detail, title, qu.ObjToString(v["toptype"]))
  107. f_info["s_toptype"] = s_toptype
  108. f_info["s_subtype"] = s_subtype
  109. //调用标的物识别
  110. if !ul.IsTool && !ul.IsLocal {
  111. if s_purchasinglist := getPurList(v, old_detail, f_info); len(s_purchasinglist) > 0 {
  112. f_info["s_purchasinglist"] = s_purchasinglist
  113. }
  114. }
  115. }
  116. //字段清洗
  117. fns := GetFnsInfo(v) //获取附件名字
  118. f_data = clean.CleanFieldInfo(f_info, fns, isTable)
  119. //采购单位二级校验
  120. CheckOutBuyerInfo(f_data)
  121. //强制逻辑判断-
  122. ForcedLogicDecideInfo(f_data)
  123. //返回数据
  124. return f_data
  125. }
  126. // 暂时不启用...无限重试
  127. func RunResetUpdateFieldInfo(arr []string, name string, s_name string) {
  128. //log.Debug("开始重置更新...", len(arr))
  129. //reset := []string{}
  130. //for k, v := range arr {
  131. // log.Debug("...", k, "...", v)
  132. // data := ul.SourceMgo.FindById(name, v)
  133. // content := PromptFieldText(qu.ObjToString(data["detail"]))
  134. // zp, ok := map[string]interface{}{}, 0
  135. // for {
  136. // ok++
  137. // if zp = ai.PostZhiPuAI(content); len(zp) > 0 {
  138. // break
  139. // }
  140. // if ok >= 5 {
  141. // log.Debug("请求数据失败...", v)
  142. // reset = append(reset, v)
  143. // break
  144. // }
  145. // }
  146. // ul.SourceMgo.UpdateById(s_name, v, map[string]interface{}{
  147. // "$set": map[string]interface{}{
  148. // "zhipu": zp,
  149. // },
  150. // })
  151. //}
  152. //if len(reset) > 0 { //无限尝试
  153. // RunResetUpdateFieldInfo(reset, name, s_name)
  154. //}
  155. //log.Debug("本轮重置更新结束......")
  156. }
  157. /*
  158. ************************************************************
  159. ************************************************************
  160. ************************************************************
  161. 支持新模型-deepseek的轮询查询
  162. */
  163. func RunDeepSeek() {
  164. log.Debug("执行轮询定时···deepseek···")
  165. tmp := ul.BidMgo.FindById("bidding", "65ab0ff666cf0db42a81ecb5")
  166. ExtractDeepSeekInfo(tmp)
  167. return
  168. for {
  169. pool_mgo := make(chan bool, ul.Reading)
  170. wg_mgo := &sync.WaitGroup{}
  171. sess := ul.BidMgo.GetMgoConn()
  172. defer ul.BidMgo.DestoryMongoConn(sess)
  173. q, total, isok := map[string]interface{}{}, 0, 0
  174. it := sess.DB(ul.BidMgo.DbName).C("zktest_sample_data_source_0").Find(&q).Iter()
  175. for tmp := make(map[string]interface{}); it.Next(&tmp); total++ {
  176. if total%1000 == 0 {
  177. log.Debug("cur ai index ", total)
  178. }
  179. isok++
  180. pool_mgo <- true
  181. wg_mgo.Add(1)
  182. go func(tmp map[string]interface{}) {
  183. defer func() {
  184. <-pool_mgo
  185. wg_mgo.Done()
  186. }()
  187. //抽取deepseek数据···并更新
  188. data := ExtractDeepSeekInfo(tmp)
  189. if len(data) > 0 {
  190. data["_id"] = tmp["_id"]
  191. ul.BidMgo.Save("zktest_deepseek_0122", data)
  192. }
  193. }(tmp)
  194. tmp = make(map[string]interface{})
  195. }
  196. wg_mgo.Wait()
  197. log.Debug("新模型deepseek处理完毕······", isok)
  198. time.Sleep(time.Second * 1800)
  199. }
  200. }
  201. // deepseek模型识别字段
  202. func ExtractDeepSeekInfo(tmp map[string]interface{}) map[string]interface{} {
  203. //基础信息
  204. tmpid := ul.BsonTOStringId(tmp["_id"])
  205. title := qu.ObjToString(tmp["title"])
  206. old_detail := getDetailText(tmp, tmpid) //获取正文文本
  207. //过滤信息
  208. if NotInProgressInfo(title, old_detail, tmp) {
  209. return map[string]interface{}{}
  210. }
  211. //识别结构,短文本结构,不想进行分类识别
  212. shorText := false
  213. if utf8.RuneCountInString(old_detail) < 100 {
  214. shorText = true
  215. }
  216. //文本格式转换
  217. new_detail := ul.HttpConvertToMarkdown(title + "\n" + old_detail)
  218. //短文本判断是否有效性
  219. if shorText {
  220. if info := prompt.AcquireJudgeDeepSeekShortInfo(new_detail); info["结果"] != "是" {
  221. return map[string]interface{}{}
  222. }
  223. }
  224. //获取通用该字段
  225. f_info := prompt.AcquireExtractFieldDeepSeekInfo(new_detail)
  226. //******字段清洗******
  227. f_data := clean.CleanDeepSeekInfo(f_info, tmp)
  228. //******二级校验******
  229. CheckOutDeepSeekBuyerInfo(f_data)
  230. //******强制判断******
  231. ForcedLogicDecideInfo(f_data)
  232. return f_data
  233. }