postprocess0101.py 7.5 KB

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