# -*- encoding:utf-8 -*- import json import os.path import re from flask_app.main.json_utils import extract_content_from_json # 可以选择性地导入特定的函数 from flask_app.main.提取打勾符号 import read_pdf_and_judge_main from flask_app.main.多线程提问 import multi_threading from flask_app.main.通义千问long import qianwen_long,upload_file #调用qianwen-ask之后,组织提示词问百炼。 def construct_judge_questions(json_data): """ 根据提供的 JSON 数据,生成一个问题列表。每个问题针对一个值为 '未知' 的键, 要求以 JSON 格式提供该键的值,仅限于 '是', '否', '未知'。 Args: json_data (str or dict): 输入的 JSON 数据,可以是 JSON 字符串或字典。 Returns: list: 包含生成的问题的列表。如果没有符合条件的键,则返回空列表。 """ # 如果 json_data 是字符串,尝试将其解析为字典 if isinstance(json_data, str): try: parsed_data = json.loads(json_data) except json.JSONDecodeError: print("输入的 json_data 不是有效的 JSON 字符串。") return [] elif isinstance(json_data, dict): parsed_data = json_data else: print("输入的 json_data 必须是 JSON 字符串或字典。") return [] # 提取值为 '未知' 的键 question_keys = [key for key, value in parsed_data.items() if value == '未知'] # 如果没有符合条件的键,返回空列表 if not question_keys: return [] # 生成问题列表 questions = [] for key in question_keys: question = ( f"请你根据投标人须知前附表中的信息回答,本项目{key}," f"请按json格式给我提供信息,键名为'{key}',键值仅限于'是','否','未知',如果原文中对应内容处为'/'或者'\',键值填'否'。" ) questions.append(question) return questions def merge_json_to_list(merged): """Merge updates into the original data by modifying specific keys based on their value ('是' or '否'), and create a list based on these values.""" chosen_numbers = [] # 处理是否允许分包 保持'是否允许分包'键名主要是由于存在'未知'的情况。 if merged.get('是否允许分包') == '是': chosen_numbers.append(1) merged.pop('是否允许分包', None) elif merged.get('是否允许分包') == '否': merged['分包'] = '不允许' merged.pop('是否允许分包', None) guarantee_key = '是否递交投标保证金' if '是否递交投标保证金' in merged else '是否递交磋商保证金' if merged.get(guarantee_key) == '是': chosen_numbers.extend([2, 3]) merged.pop(guarantee_key, None) elif merged.get(guarantee_key) == '否': guarantee_type = '投标' if '投标' in guarantee_key else '磋商' merged[f'{guarantee_type}保证金'] = '不提交' merged[f'退还{guarantee_type}保证金'] = '/' merged.pop(guarantee_key, None) # 处理是否有履约保证金 if merged.get('是否提交履约保证金') == '是': chosen_numbers.append(4) merged.pop('是否提交履约保证金', None) elif merged.get('是否提交履约保证金') == '否': merged['履约保证金'] = '不提交' merged.pop('是否提交履约保证金', None) # 处理是否有招标代理服务费 if merged.get('是否有招标代理服务费') == '是': chosen_numbers.append(5) merged.pop('是否有招标代理服务费', None) elif merged.get('是否有招标代理服务费') == '否': merged['招标代理服务费'] = '无' merged.pop('是否有招标代理服务费', None) if merged.get('是否组织踏勘现场') == '是': chosen_numbers.append(6) merged.pop('是否组织踏勘现场',None) elif merged.get('是否组织踏勘现场') == '否': merged['踏勘现场']='不组织' merged.pop('是否组织踏勘现场', None) preparation_key = '是否召开投标预备会' if '是否召开投标预备会' in merged else '是否召开投标答疑会' if merged.get(preparation_key) == '是': chosen_numbers.append(7) merged.pop(preparation_key, None) elif merged.get(preparation_key) == '否': meeting_type = '预备会' if '预备会' in preparation_key else '答疑会' merged[f'投标{meeting_type}']='不召开' merged.pop(preparation_key,None) if merged.get('是否允许偏离') == '是': chosen_numbers.append(8) merged.pop('是否允许偏离',None) elif merged.get('是否允许偏离') == '否': merged['偏离']='不允许' merged.pop('是否允许偏离', None) return chosen_numbers, merged def read_questions_from_judge(file_path, indices): questions = [] # 读取文件内容 with open(file_path, 'r', encoding='utf-8') as file: content = file.read() # 正则表达式提取问题 pattern = r'(\d+)\.(.*?)#pdf提取之后的提示词|(\d+)\.(.*?)(?=\d+\.|$)' matches = re.findall(pattern, content, re.DOTALL) # 解析匹配到的内容并提取对应序号的问题 for match in matches: num = match[0] or match[2] question = match[1].strip() or match[3].strip() if int(num) in indices: questions.append(question) return questions def judge_whether_main(file_path,output_folder): #传入招标文件中‘投标人须知前附表’ user_query1 = "请你依据以上信息,回答以下问题:是否组织踏勘现场?是否召开投标预备会?是否允许偏离?是否退还投标文件?是否允许分包? 是否需要递交投标保证金?是否需要提交履约保证金(履约担保)?是否有招标代理服务费?请按json格式给我提供信息,键名分别为'是否组织踏勘现场','是否召开投标预备会','是否允许偏离','是否退还投标文件',是否允许分包','是否递交投标保证金','是否提交履约保证金','是否有招标代理服务费',键值仅限于'是','否','未知',若存在矛盾信息,请回答'未知'。" output_txt_path = os.path.join(output_folder,'judge_exist.txt') read_pdf_and_judge_main(file_path, output_txt_path) #提取打勾符号 file_id=upload_file(output_txt_path) qianwen_answer=qianwen_long(file_id,user_query1) user_querys = construct_judge_questions(extract_content_from_json(qianwen_answer)) # 提取回答为”未知“的键 # 判断user_query2是否为空 if user_querys: file_id = upload_file(file_path) qianwen_results = multi_threading(user_querys, "", file_id, 2) # 1代表使用百炼rag 2代表使用qianwen-long qianwen_list = [extract_content_from_json(res) for _, res in qianwen_results] if qianwen_results else [] #整个前附表一起传问千问long return process_judge_content(qianwen_answer, qianwen_list) else: print("正常现象,没有'未知',无需调用qianwen-long") original = extract_content_from_json(qianwen_answer) return merge_json_to_list(original) def process_judge_content(original_json, qianwen_list): #用新的数据合并旧数据 """Process judging content by merging updates into the original JSON data.""" original = extract_content_from_json(original_json) for i in qianwen_list: original.update(i) return merge_json_to_list(original) if __name__ == "__main__": file_path="C:\\Users\Administrator\\Desktop\\fsdownload\\ee2d8828-bae0-465a-9171-7b2dd7453251\\ztbfile_tobidders_notice_table.pdf" output_dir='C:\\Users\Administrator\\Desktop\\fsdownload\\ee2d8828-bae0-465a-9171-7b2dd7453251\\tmp' chosen_numbers, merged=judge_whether_main(file_path,output_dir) print(chosen_numbers) print(merged)