postprocess0103.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import json
  2. with open("0103basic_rule", "r") as f:
  3. content = f.read()
  4. rule = json.loads(content)
  5. def aifilter(A, #options
  6. B, #data
  7. aiclient):
  8. options=[]
  9. letters = "ABCDEFGHIJKLMN"
  10. for i in range(len(A)):
  11. options.append("给定选项" + letters[i]+",内容为"+A[i])
  12. completion = aiclient.chat.completions.create(
  13. model="glm-4.5-flash",
  14. messages=[
  15. {"role": "system", "content": "You are a helpful assistant."},
  16. {"role": "user", "content": " 处理要求:如果清单描述的工作内容是管桩清理,则去除给定选项中的管桩填芯的选项; 如果清单描述的工作内容不是管桩清理,则不做处理"},
  17. {"role": "user", "content": "问题描述: 给定一段工程量清单描述: " + B['mc'] + " " + B['tz'] + "," + ",".join(options) + "。请根据处理要求,处理选项,并返回结果。例如,如果处理完后剩余A,B,C三个选项,请返回[A,B,C]"},
  18. ],
  19. extra_body={"thinking": {"type": "disabled"}},
  20. )
  21. json_string = completion.choices[0].message.content
  22. print(json_string)
  23. completion = aiclient.chat.completions.create(
  24. model="glm-4.5-flash",
  25. messages=[
  26. {"role": "system", "content": "You are a helpful assistant.请将最终结果以JSON格式输出"},
  27. {"role": "user", "content": " 给你一段文字如下, " + json_string + ",其中给出了一个类似于[A,B,C]的数组作为结果,请将该最终结果输出"},
  28. ],
  29. extra_body={"thinking": {"type": "disabled"}},
  30. )
  31. json_string = completion.choices[0].message.content
  32. print(json_string)
  33. answer=[]
  34. if 'A' in json_string:
  35. answer.append(A[0])
  36. if 'B' in json_string:
  37. answer.append(A[1])
  38. if 'C' in json_string:
  39. answer.append(A[2])
  40. if 'D' in json_string:
  41. answer.append(A[3])
  42. if 'E' in json_string:
  43. answer.append(A[4])
  44. if 'F' in json_string:
  45. answer.append(A[5])
  46. if 'G' in json_string:
  47. answer.append(A[6])
  48. if 'H' in json_string:
  49. answer.append(A[7])
  50. return answer
  51. def associate_jiezhuang(answer):
  52. hit = False
  53. for entry in answer:
  54. if entry in ['第三章 桩基工程 3.1 打桩工程 3.1.5 电焊接桩 电焊接桩 方桩包角钢','第三章 桩基工程 3.1 打桩工程 3.1.5 电焊接桩 电焊接桩 方桩包钢板','第三章 桩基工程 3.1 打桩工程 3.1.5 电焊接桩 电焊接桩 螺栓+电焊']:
  55. hit = True
  56. return answer
  57. if not hit:
  58. return answer + ['第三章 桩基工程 3.1 打桩工程 3.1.5 电焊接桩 电焊接桩 螺栓+电焊']
  59. def associate(answer):
  60. hit = False
  61. for entry in answer:
  62. if entry in rule['3.1.1.1']:
  63. hit = "3.1.1.2"
  64. elif entry in rule['3.1.2.1']:
  65. hit = "3.1.2.2"
  66. elif entry in rule['3.1.3.1']:
  67. hit = "3.1.3.2"
  68. elif entry in rule["3.1.4.1"]:
  69. hit = "3.1.4.2"
  70. if hit:
  71. already = False
  72. for entry in answer:
  73. if entry in rule[hit]:
  74. already = True
  75. if not already:
  76. return answer + [rule[hit][0]]
  77. else:
  78. return answer
  79. else:
  80. return answer
  81. def jiezhuang(
  82. data, #data
  83. aiclient):
  84. completion = aiclient.chat.completions.create(
  85. model="glm-4.5-flash",
  86. messages=[
  87. {"role": "system", "content": "You are a helpful assistant."},
  88. {"role": "user", "content": " 背景知识:已知预应力高强混凝土管桩(PHC)型号定义为PHC-AAA(BB)CC-DDD-E1,E2,E3,E4,其中AAA代表管桩外径,BB代表管桩壁厚,CC表示型号,DDD表示混凝土强度等级,E1/E2/E3/E4表示分段桩长。例如,PHC-500(125)-AB-C80-9,7 表示外径500mm,壁厚125mm,型号AB,混凝土强度C80, 分段桩长分别为9米和7米,总桩长16米,施工时需要将两个分段接桩;再例如,PHC-500(125)-AB-C80-9 表示外径500mm,壁厚125mm,型号AB,混凝土强度C80, 为一整段桩,长9米, 施工时不需要接桩"},
  89. {"role": "user", "content": " 背景知识:在施工过程中,若管桩长度不足,则需在原有管桩基础上连接新的管桩,这一过程即为接桩。 通过接桩,可以将多节管桩连续打入地基,直至达到设计深度"},
  90. {"role": "user", "content": "问题描述: 给定一段工作内容描述,内容为" + data['mc'] +data['tz'] + "。请判断完成该工作内容是否需要接桩。请回答是或者否"},
  91. ],
  92. extra_body={"thinking": {"type": "disabled"}},
  93. )
  94. json_string = completion.choices[0].message.content
  95. print(json_string)
  96. if "是" in json_string and "否" not in json_string:
  97. return True
  98. if "是" not in json_string and "否" in json_string:
  99. return False
  100. completion = aiclient.chat.completions.create(
  101. model="glm-4.5-flash",
  102. messages=[
  103. {"role": "system", "content": "You are a helpful assistant.请将最终答案以JSON格式输出"},
  104. {"role": "user", "content": " 给你一段文字如下, " + json_string + ",其中给出了一个是或者否的答案,请将该最终答案输出"},
  105. ],
  106. extra_body={"thinking": {"type": "disabled"}},
  107. )
  108. json_string = completion.choices[0].message.content
  109. print(json_string)
  110. if "是" in json_string and "否" not in json_string:
  111. return True
  112. if "是" not in json_string and "否" in json_string:
  113. return False
  114. def songzhuang(
  115. data, #data
  116. aiclient):
  117. completion = aiclient.chat.completions.create(
  118. model="glm-4.5-flash",
  119. messages=[
  120. {"role": "system", "content": "You are a helpful assistant."},
  121. {"role": "user", "content": " 背景知识:送桩 是指当桩顶设计标高低于地面时,需要把桩顶打入到设计标高。"},
  122. {"role": "user", "content": "问题描述: 给定一段工作内容描述,内容为" + data['mc'] +data['tz'] + "。请判断完成该工作内容是否需要送桩。请回答是或者否"},
  123. ],
  124. extra_body={"thinking": {"type": "disabled"}},
  125. )
  126. json_string = completion.choices[0].message.content
  127. print(json_string)
  128. if "是" in json_string and "否" not in json_string:
  129. return True
  130. if "是" not in json_string and "否" in json_string:
  131. return False
  132. completion = aiclient.chat.completions.create(
  133. model="glm-4.5-flash",
  134. messages=[
  135. {"role": "system", "content": "You are a helpful assistant.请将最终答案以JSON格式输出"},
  136. {"role": "user", "content": " 给你一段文字如下, " + json_string + ",其中给出了一个是或者否的答案,请将该最终答案输出"},
  137. ],
  138. extra_body={"thinking": {"type": "disabled"}},
  139. )
  140. json_string = completion.choices[0].message.content
  141. print(json_string)
  142. if "是" in json_string and "否" not in json_string:
  143. return True
  144. if "是" not in json_string and "否" in json_string:
  145. return False
  146. def ai(A, #options
  147. B, #data
  148. C, #entry
  149. aiclient):
  150. options=[]
  151. letters = "ABCDEFGHIJKLMN"
  152. for i in range(len(A)):
  153. options.append("给定选项" + letters[i]+",内容为"+A[i])
  154. completion = aiclient.chat.completions.create(
  155. model="glm-4.5-flash",
  156. messages=[
  157. {"role": "system", "content": "You are a helpful assistant."},
  158. {"role": "user", "content": " 背景知识:已知预应力高强混凝土管桩(PHC)型号定义为PHC-AAA(BB)CC-DDD-E1,E2,E3,E4,其中AAA代表管桩外径,BB代表管桩壁厚,CC表示型号,DDD表示混凝土强度等级,E1/E2/E3/E4表示分段桩长。例如,PHC-500(125)-AB-C80-9,7 表示外径500mm,壁厚125mm,型号AB,混凝土强度C80, 分段桩长分别为9米和7米,总桩长16米"},
  159. {"role": "user", "content": "问题描述: 给定一段工作内容描述,内容为" + B['mc'] +B['tz'] + "," + ",".join(options) + "。请从上述选项中选择(总)桩长最恰当的一个选项,并返回代号,如果无法得出总桩长,则默认桩长10米。例如,如果A选项最恰当,请返回A; "},
  160. ],
  161. extra_body={"thinking": {"type": "disabled"}},
  162. )
  163. answer2 = completion.choices[0].message.content
  164. print(answer2)
  165. if len(answer2) < 4:
  166. if 'A' in answer2:
  167. return A[0]
  168. if 'B' in answer2:
  169. return A[1]
  170. if 'C' in answer2:
  171. return A[2]
  172. if 'D' in answer2:
  173. return A[3]
  174. if 'E' in answer2:
  175. return A[4]
  176. if 'F' in answer2:
  177. return A[5]
  178. if 'G' in answer2:
  179. return A[6]
  180. if 'H' in answer2:
  181. return A[7]
  182. if 'I' in answer2:
  183. return A[8]
  184. if 'J' in answer2:
  185. return A[9]
  186. completion = aiclient.chat.completions.create(
  187. model="glm-4.5-flash",
  188. messages=[
  189. {"role": "system", "content": "You are a helpful assistant.请将最终答案以JSON格式输出"},
  190. {"role": "user", "content": " 给你一段文字如下, " + answer2 + ",其中给出了一个类似于A或者B或者C的表达式作为答案,请将该最终答案输出"},
  191. ],
  192. extra_body={"thinking": {"type": "disabled"}},
  193. )
  194. json_string = completion.choices[0].message.content
  195. print(json_string)
  196. answers = json_string.split("\n")
  197. answers = [x for x in answers if ':' in x]
  198. print(answers)
  199. if len(answers) == 0:
  200. return C
  201. answer2 = answers[0].split(":")[1].replace(" ", "")
  202. if 'A' in answer2:
  203. return A[0]
  204. if 'B' in answer2:
  205. return A[1]
  206. if 'C' in answer2:
  207. return A[2]
  208. if 'D' in answer2:
  209. return A[3]
  210. if 'E' in answer2:
  211. return A[4]
  212. if 'F' in answer2:
  213. return A[5]
  214. if 'G' in answer2:
  215. return A[6]
  216. if 'H' in answer2:
  217. return A[7]
  218. if 'I' in answer2:
  219. return A[8]
  220. if 'J' in answer2:
  221. return A[9]
  222. def select(options, data, entry, aiclient):
  223. if len([x for x in options if '桩长在' in x]) == len(options):
  224. return ai(options, data, entry, aiclient)
  225. else:
  226. return entry
  227. def postprocess0103(selected, data, aiclient):
  228. if jiezhuang(data, aiclient):
  229. selected = associate_jiezhuang(selected)
  230. if songzhuang(data, aiclient):
  231. selected = associate(selected)
  232. correct=[]
  233. for entry in selected:
  234. options = []
  235. for item in rule:
  236. l = rule[item]
  237. if entry in l:
  238. options = l
  239. if len(options) > 0:
  240. correct.append(select(options, data, entry, aiclient))
  241. else:
  242. correct.append(entry)
  243. return aifilter(correct, data, aiclient)