postprocess0101.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import json
  2. def huitianfilter(A, #options
  3. B, #data
  4. aiclient):
  5. options=[]
  6. letters = "ABCDEFGHIJKLMN"
  7. for i in range(len(A)):
  8. options.append("给定选项" + letters[i]+",内容为"+A[i])
  9. completion = aiclient.chat.completions.create(
  10. model="glm-4.5-flash",
  11. messages=[
  12. {"role": "system", "content": "You are a helpful assistant."},
  13. {"role": "user", "content": " 背景知识:回填方一般不使用单轮车或者双轮车运输"},
  14. {"role": "user", "content": " 背景知识:室内回填、房心回填一般不使用压路机、挖掘机、推土机、单轮车、双轮车"},
  15. {"role": "user", "content": " 背景知识:回填方时,挖掘机挖土与自卸汽车运土需要配套使用,也就是说,如果选择两者之一,另一个也不许选择。如果不选择两者之一,则另一个也不能选择"},
  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 aifilter(A, #options
  52. B, #data
  53. aiclient):
  54. options=[]
  55. letters = "ABCDEFGHIJKLMN"
  56. for i in range(len(A)):
  57. options.append("给定选项" + letters[i]+",内容为"+A[i])
  58. completion = aiclient.chat.completions.create(
  59. model="glm-4.5-flash",
  60. messages=[
  61. {"role": "system", "content": "You are a helpful assistant."},
  62. {"role": "user", "content": " 背景知识:如果工作内容中没有明确要求使用人工挖土石方或者人工运土石方,则去掉给定选项中的人工挖土石方、运土石方选项"},
  63. {"role": "user", "content": "问题描述: 给定一段工作内容: " + B['mc'] + " " + B['tz'] + "," + ",".join(options) + "。请做出处理,并返回结果。例如,如果处理完后剩余A,B,C三个选项,请返回[A,B,C]"},
  64. ],
  65. extra_body={"thinking": {"type": "disabled"}},
  66. )
  67. json_string = completion.choices[0].message.content
  68. print(json_string)
  69. completion = aiclient.chat.completions.create(
  70. model="glm-4.5-flash",
  71. messages=[
  72. {"role": "system", "content": "You are a helpful assistant.请将最终答案以JSON格式输出"},
  73. {"role": "user", "content": " 给你一段文字如下, " + json_string + ",其中给出了一个类似于[A,B,C]的数组作为答案,请将该最终答案输出"},
  74. ],
  75. extra_body={"thinking": {"type": "disabled"}},
  76. )
  77. json_string = completion.choices[0].message.content
  78. print(json_string)
  79. answer=[]
  80. if 'A' in json_string:
  81. answer.append(A[0])
  82. if 'B' in json_string:
  83. answer.append(A[1])
  84. if 'C' in json_string:
  85. answer.append(A[2])
  86. if 'D' in json_string:
  87. answer.append(A[3])
  88. if 'E' in json_string:
  89. answer.append(A[4])
  90. if 'F' in json_string:
  91. answer.append(A[5])
  92. if 'G' in json_string:
  93. answer.append(A[6])
  94. if 'H' in json_string:
  95. answer.append(A[7])
  96. return answer
  97. def ai(A, #options
  98. B, #data
  99. aiclient):
  100. options=[]
  101. letters = "ABCDEFGHIJKLMN"
  102. for i in range(len(A)):
  103. options.append("给定选项" + letters[i]+",内容为"+A[i])
  104. completion = aiclient.chat.completions.create(
  105. model="glm-4.5-flash",
  106. messages=[
  107. {"role": "system", "content": "You are a helpful assistant."},
  108. {"role": "user", "content": " 背景知识:平整场地可以使用人工的方法,也可以使用机械的方法,机械的方法更经济"},
  109. {"role": "user", "content": "问题描述: " + ",".join(options) + "。请从上述选项中选择一更经济的选项,并返回代号。例如,如果A选项最恰当,请返回A"},
  110. ],
  111. extra_body={"thinking": {"type": "disabled"}},
  112. )
  113. json_string = completion.choices[0].message.content
  114. print(json_string)
  115. if len(json_string) < 4:
  116. if 'A' in json_string:
  117. return A[0]
  118. if 'B' in json_string:
  119. return A[1]
  120. if 'C' in json_string:
  121. return A[2]
  122. if 'D' in json_string:
  123. return A[3]
  124. if 'E' in json_string:
  125. return A[4]
  126. if 'F' in json_string:
  127. return A[5]
  128. if 'G' in json_string:
  129. return A[6]
  130. if 'H' in json_string:
  131. return A[7]
  132. if 'I' in json_string:
  133. return A[8]
  134. if 'J' in json_string:
  135. return A[9]
  136. completion = aiclient.chat.completions.create(
  137. model="glm-4.5-flash",
  138. messages=[
  139. {"role": "system", "content": "You are a helpful assistant.请将最终答案以JSON格式输出"},
  140. {"role": "user", "content": " 给你一段文字如下, " + json_string + ",其中给出了一个类似于A或者B或者C的表达式作为答案,请将该最终答案输出.例如,文字提到答案为A,请输出A;文字提到答案是B,请输出B"},
  141. ],
  142. extra_body={"thinking": {"type": "disabled"}},
  143. )
  144. json_string = completion.choices[0].message.content
  145. print(json_string)
  146. answers = json_string.split("\n")
  147. answers = [x for x in answers if ':' in x]
  148. print(answers)
  149. if len(answers) == 0:
  150. return A[0]
  151. answer2 = answers[0].split(":")[1].replace(" ", "")
  152. if 'A' in answer2:
  153. return A[0]
  154. if 'B' in answer2:
  155. return A[1]
  156. if 'C' in answer2:
  157. return A[2]
  158. if 'D' in answer2:
  159. return A[3]
  160. if 'E' in answer2:
  161. return A[4]
  162. if 'F' in answer2:
  163. return A[5]
  164. if 'G' in answer2:
  165. return A[6]
  166. if 'H' in answer2:
  167. return A[7]
  168. if 'I' in answer2:
  169. return A[8]
  170. if 'J' in answer2:
  171. return A[9]
  172. def tihuan(selected):
  173. hit = False
  174. for entry in selected:
  175. if '自动平地机' in entry:
  176. hit = True
  177. if hit:
  178. left = [x for x in selected if '自动平地机' not in x]
  179. left.append('第一章 土、石方工程 1.2 机械土、石方 1.2.9 平整场地、碾压 平整场地(厚300mm以内) 推土机(kW以内) 75')
  180. return left
  181. else:
  182. return selected
  183. def postprocess0101(selected, data, aiclient):
  184. if data['bianma'].startswith("010101001"):##平整场地
  185. if len(selected) > 1:
  186. return tihuan([ai(selected, data, aiclient)])
  187. else:
  188. return tihuan(selected)
  189. elif data['bianma'].startswith("010103"):##回填
  190. return huitianfilter(selected, data, aiclient)
  191. else:
  192. return aifilter(selected, data, aiclient)