postprocess0103.py 11 KB

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