2024-09-23 17:44:34 +08:00
|
|
|
|
# -*- encoding:utf-8 -*-
|
2024-09-23 15:49:30 +08:00
|
|
|
|
import json
|
2024-09-23 17:44:34 +08:00
|
|
|
|
import re
|
2024-09-23 15:49:30 +08:00
|
|
|
|
|
|
|
|
|
from flask_app.main.基础信息整合 import combine_basic_info
|
|
|
|
|
from flask_app.main.通义千问long import qianwen_long,upload_file
|
|
|
|
|
from flask_app.main.多线程提问 import multi_threading
|
2024-09-23 17:44:34 +08:00
|
|
|
|
from flask_app.main.json_utils import combine_json_results,clean_json_string
|
2024-09-26 13:43:47 +08:00
|
|
|
|
from flask_app.main.形式响应评审 import update_json_data,extract_matching_keys
|
2024-09-26 18:06:23 +08:00
|
|
|
|
from flask_app.main.json_utils import extract_content_from_json
|
2024-09-23 17:44:34 +08:00
|
|
|
|
#这个字典可能有嵌套,你需要遍历里面的键名,对键名作判断,而不是键值,具体是这样的:如果处于同一层级的键的数量>1并且键名全由数字或点号组成。那么就将这些序号键名全部删除,重新组织成一个字典格式的数据,你可以考虑用字符串列表来保持部分平级的数据
|
|
|
|
|
#对于同级的键,如果数量>1且键名都统一,那么将键名去掉,用列表保持它们的键值
|
|
|
|
|
#对于同一个字典中,可能存在若干键值对,若它们的键值都是""或者"/" 你就将它们的键值删去,它们的键名用字符串列表保存
|
|
|
|
|
|
|
|
|
|
def is_numeric_key(key):
|
|
|
|
|
# 这个正则表达式匹配由数字、点、括号中的数字或单个字母(小写或大写)组成的字符串,
|
|
|
|
|
# 字母后跟数字,或数字后跟字母,单个字母后跟点,但不能是字母-数字-字母的组合
|
|
|
|
|
pattern = r'^[\d.]+$|^\(\d+\)$|^(\d+)$|^[a-zA-Z]$|^[a-zA-Z]\d+$|^\d+[a-zA-Z]$|^[a-zA-Z]\.$'
|
|
|
|
|
return re.match(pattern, key) is not None
|
|
|
|
|
#TODO:如果键值中存在数字就不行
|
|
|
|
|
#zbtest20也有问题
|
|
|
|
|
def contains_number_or_index(key, value):
|
|
|
|
|
# 判断值是否是数字或数字字符串
|
|
|
|
|
is_number = isinstance(value, (int, float)) or (isinstance(value, str) and value.isdigit())
|
|
|
|
|
# 判断键是否包含 "序号"
|
|
|
|
|
contains_index = '序号' in key
|
|
|
|
|
# 判断值中是否包含数字
|
|
|
|
|
contains_digit = isinstance(value, str) and re.search(r'\d+', value)
|
|
|
|
|
# 判断值中是否包含中文字符
|
|
|
|
|
contains_chinese = isinstance(value, str) and re.search(r'[\u4e00-\u9fff]', value)
|
|
|
|
|
# 如果值中包含数字但也有中文字符,则保留(返回 False)
|
|
|
|
|
if contains_digit and contains_chinese:
|
|
|
|
|
return False
|
|
|
|
|
# 如果值是数字或包含数字,且不包含中文字符,或者键包含 "序号",返回 True
|
|
|
|
|
return is_number or contains_index or contains_digit
|
|
|
|
|
|
2024-09-23 17:57:30 +08:00
|
|
|
|
#对于同一个字典中,可能存在若干键值对,若它们的键值都是""或者"/" 你就将它们的键值删去,它们的键名用字符串列表保存
|
|
|
|
|
#如果键名是"序号"或者键值中全是数字,删去序号
|
2024-09-23 17:44:34 +08:00
|
|
|
|
def preprocess_dict(data):
|
|
|
|
|
if isinstance(data, dict):
|
|
|
|
|
if len(data) > 1:
|
|
|
|
|
# 检查是否所有值都是 "" 或 "/"
|
|
|
|
|
if all(v == "" or v == "/" for v in data.values()):
|
|
|
|
|
return list(data.keys())
|
|
|
|
|
else:
|
|
|
|
|
processed = {}
|
|
|
|
|
for k, v in data.items():
|
|
|
|
|
if not contains_number_or_index(k, v):
|
|
|
|
|
processed_v = preprocess_dict(v)
|
|
|
|
|
if processed_v != "": # 只添加非空值
|
|
|
|
|
processed[k] = processed_v
|
|
|
|
|
return processed
|
|
|
|
|
else:
|
|
|
|
|
return {k: preprocess_dict(v) for k, v in data.items()}
|
|
|
|
|
elif isinstance(data, list):
|
|
|
|
|
return [preprocess_dict(item) for item in data]
|
|
|
|
|
else:
|
|
|
|
|
return data
|
|
|
|
|
def process_dict(data):
|
|
|
|
|
if not isinstance(data, dict):
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
result = {}
|
|
|
|
|
numeric_keys = []
|
|
|
|
|
non_numeric_keys = {}
|
|
|
|
|
|
|
|
|
|
for key, value in data.items():
|
|
|
|
|
if is_numeric_key(key):
|
|
|
|
|
numeric_keys.append((key, value))
|
|
|
|
|
else:
|
|
|
|
|
non_numeric_keys[key] = value
|
|
|
|
|
|
|
|
|
|
if numeric_keys:
|
|
|
|
|
result['items'] = [process_dict(item[1]) for item in sorted(numeric_keys)]
|
|
|
|
|
|
|
|
|
|
for key, value in non_numeric_keys.items():
|
2024-09-25 18:03:09 +08:00
|
|
|
|
if isinstance(value, list):
|
|
|
|
|
processed_list = []
|
|
|
|
|
for item in value:
|
|
|
|
|
if isinstance(item, dict):
|
|
|
|
|
# 处理字典中只有一个键值对的情况
|
|
|
|
|
if len(item) == 1:
|
|
|
|
|
processed_item = process_dict(list(item.values())[0])
|
|
|
|
|
else:
|
|
|
|
|
processed_item = process_dict(item)
|
|
|
|
|
else:
|
|
|
|
|
processed_item = process_dict(item)
|
|
|
|
|
|
|
|
|
|
# 如果处理后的项是只包含一个元素的列表,则展平它
|
|
|
|
|
if isinstance(processed_item, list) and len(processed_item) == 1:
|
|
|
|
|
processed_item = processed_item[0]
|
|
|
|
|
|
|
|
|
|
processed_list.append(processed_item)
|
|
|
|
|
|
|
|
|
|
result[key] = processed_list
|
2024-09-23 17:44:34 +08:00
|
|
|
|
else:
|
|
|
|
|
result[key] = process_dict(value)
|
|
|
|
|
|
|
|
|
|
if len(result) == 1 and 'items' in result:
|
|
|
|
|
return result['items']
|
|
|
|
|
|
|
|
|
|
return result
|
2024-09-26 13:43:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def find_chapter_clause_references(data, parent_key=""):
|
2024-09-26 18:06:23 +08:00
|
|
|
|
exclude_list=["格式要求"]
|
2024-09-26 13:43:47 +08:00
|
|
|
|
result = []
|
|
|
|
|
# 正则匹配"第x章"或"第x款"
|
|
|
|
|
chapter_clause_pattern = re.compile(r'第[一二三四五六七八九十\d]+[章款]')
|
|
|
|
|
|
|
|
|
|
# 遍历字典中的键值对
|
|
|
|
|
for key, value in data.items():
|
|
|
|
|
# 生成当前的完整键名
|
|
|
|
|
full_key = f"{parent_key}.{key}" if parent_key else key
|
|
|
|
|
|
2024-09-26 18:06:23 +08:00
|
|
|
|
# 检查是否应排除该键或值
|
|
|
|
|
if any(exclude_item in full_key for exclude_item in exclude_list) or \
|
|
|
|
|
(isinstance(value, str) and any(exclude_item in value for exclude_item in exclude_list)):
|
|
|
|
|
continue # 如果在排除项中,跳过处理
|
|
|
|
|
|
2024-09-26 13:43:47 +08:00
|
|
|
|
if isinstance(value, dict):
|
|
|
|
|
# 如果值是字典,递归调用函数
|
|
|
|
|
result.extend(find_chapter_clause_references(value, full_key))
|
|
|
|
|
elif isinstance(value, str):
|
|
|
|
|
# 如果值是字符串,检查是否匹配"第x章"或"第x款"
|
|
|
|
|
if chapter_clause_pattern.search(value):
|
|
|
|
|
result.append({full_key: value})
|
|
|
|
|
|
|
|
|
|
return result
|
2024-09-26 14:08:34 +08:00
|
|
|
|
|
2024-09-26 18:06:23 +08:00
|
|
|
|
def preprocess_value(value):
|
|
|
|
|
# 使用正则表达式查找"第X章"或"第X款"
|
|
|
|
|
chapter_match = re.search(r'第(.+?)章', value)
|
|
|
|
|
clause_match = re.search(r'第(.+?)款', value)
|
|
|
|
|
|
|
|
|
|
if chapter_match or clause_match:
|
|
|
|
|
# 以逗号、句号、问号、感叹号为分隔符
|
|
|
|
|
separators = r'[,。?!,\?!]'
|
|
|
|
|
|
|
|
|
|
# 分隔符检测函数,确保括号成对闭合时才用作分隔符
|
|
|
|
|
def is_separator(ch, count):
|
|
|
|
|
return count['('] == count[')'] and count['('] == count[')'] and re.match(separators, ch)
|
|
|
|
|
|
|
|
|
|
parts = []
|
|
|
|
|
current_part = []
|
|
|
|
|
count = {'(': 0, ')': 0, '(': 0, ')': 0}
|
|
|
|
|
|
|
|
|
|
for ch in value:
|
|
|
|
|
if ch in count:
|
|
|
|
|
count[ch] += 1
|
|
|
|
|
if is_separator(ch, count):
|
|
|
|
|
parts.append("".join(current_part).strip())
|
|
|
|
|
current_part = []
|
|
|
|
|
else:
|
|
|
|
|
current_part.append(ch)
|
|
|
|
|
|
|
|
|
|
if current_part:
|
|
|
|
|
parts.append("".join(current_part).strip())
|
|
|
|
|
|
|
|
|
|
# 查找包含章节或条款的部分
|
|
|
|
|
target_part = next((part for part in parts if '章' in part or '款' in part), None)
|
|
|
|
|
|
|
|
|
|
if target_part:
|
|
|
|
|
# 删除开头的"符合"或"应满足"
|
|
|
|
|
target_part = re.sub(r'^(符合|应满足)\s*', '', target_part.strip())
|
|
|
|
|
return target_part
|
|
|
|
|
|
|
|
|
|
# 如果没有找到特定章节或条款,返回原始值
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def generate_questions(input_list):
|
|
|
|
|
template = (
|
|
|
|
|
"关于'{key}',{value}的内容是怎样的?请按json格式给我提供信息,键名为'{key}',而键值需要完全与原文保持一致,不要擅自总结、删减,如果存在未知信息,请在对应键值处填'未知'。"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
questions = []
|
|
|
|
|
for input_dict in input_list:
|
|
|
|
|
for key, value in input_dict.items():
|
|
|
|
|
processed_value = preprocess_value(value)
|
|
|
|
|
question = template.format(key=key, value=processed_value)
|
|
|
|
|
questions.append(question)
|
|
|
|
|
return questions
|
|
|
|
|
|
|
|
|
|
def update_json_data(original_data,response_list):
|
|
|
|
|
def recursive_update(data, key, value):
|
|
|
|
|
# 处理点分隔的键,递归定位并更新嵌套字典
|
|
|
|
|
keys = key.split('.')
|
|
|
|
|
for k in keys[:-1]:
|
|
|
|
|
data = data.setdefault(k, {})
|
|
|
|
|
if isinstance(value, dict) and isinstance(data.get(keys[-1], None), dict):
|
|
|
|
|
data[keys[-1]] = {**data.get(keys[-1], {}), **value}
|
|
|
|
|
else:
|
|
|
|
|
data[keys[-1]] = value
|
|
|
|
|
for response_dict in response_list:
|
|
|
|
|
for key, value in response_dict.items():
|
|
|
|
|
recursive_update(original_data, key, value)
|
|
|
|
|
return original_data
|
2024-09-26 14:08:34 +08:00
|
|
|
|
|
2024-09-26 18:06:23 +08:00
|
|
|
|
def qualification_review(truncate_file,knowledge_name):
|
2024-09-23 15:49:30 +08:00
|
|
|
|
file_id=upload_file(truncate_file)
|
2024-09-26 13:43:47 +08:00
|
|
|
|
user_query=["该招标文件中规定的资格性审查标准是怎样的?请以json格式给出,外层为'资格性审查',你的回答要与原文一致,不可擅自总结删减,也不要回答有关符合性性审查的内容。","该招标文件中规定的符合性审查标准是怎样的?请以json格式给出,外层为'符合性审查',你的回答要与原文一致,不可擅自总结删减,也不要回答有关资格性审查的内容。"]
|
2024-09-23 15:49:30 +08:00
|
|
|
|
results=multi_threading(user_query,"",file_id,2)
|
2024-09-23 17:44:34 +08:00
|
|
|
|
combined_res = {}
|
2024-09-23 15:49:30 +08:00
|
|
|
|
for question, response in results:
|
2024-09-23 17:44:34 +08:00
|
|
|
|
cleaned_data = clean_json_string(response)
|
|
|
|
|
processed1 = preprocess_dict(cleaned_data)
|
|
|
|
|
processed2 = process_dict(processed1)
|
|
|
|
|
combined_res.update(processed2)
|
2024-09-26 18:06:23 +08:00
|
|
|
|
match_keys=find_chapter_clause_references(combined_res)
|
|
|
|
|
ques=generate_questions(match_keys)
|
|
|
|
|
results = multi_threading(ques, knowledge_name) # 无序号的直接问大模型
|
|
|
|
|
first_response_list = []
|
|
|
|
|
for _, response in results:
|
|
|
|
|
try:
|
|
|
|
|
if response and len(response) > 1: # 检查response存在且有至少两个元素
|
|
|
|
|
temp = extract_content_from_json(response[1])
|
|
|
|
|
first_response_list.append(temp)
|
|
|
|
|
else:
|
|
|
|
|
print(f"形式响应评审:Warning: Missing or incomplete response data for query index {_}.")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"形式响应评审:Error processing response for query index {_}: {e}")
|
|
|
|
|
# print(first_response_list)
|
|
|
|
|
final_result=update_json_data(combined_res,first_response_list)
|
|
|
|
|
return final_result
|
2024-09-23 17:44:34 +08:00
|
|
|
|
# 整合基础信息核心代码
|
2024-09-26 14:08:34 +08:00
|
|
|
|
#[{'资格性审查.资格要求': '符合本采购文件第一章第二款要求,并提供合格有效的证明材料'}, {'资格性审查.没有重大违法记录的书面声明': '是否提交参加政府采购活动前三年内在经营活动中没有重大违法记录的书面承诺或声明(格式要求详见本项目采购文件第六章相关格式要求)'}]
|
2024-09-26 18:06:23 +08:00
|
|
|
|
#TODO:有个严重的问题,对于{'资格性审查.资格要求': '符合本采购文件第一章第二款要求,并提供合格有效的证明材料'},调用百炼rag的时候容易得到一模一样的回答,而不是跳转到具体的地方,有两个思路,1.结构化第一章内容 2.优化提示词 3.构造问题的时候不带value,直接问key
|
2024-09-23 15:49:30 +08:00
|
|
|
|
if __name__ == "__main__":
|
2024-09-26 13:43:47 +08:00
|
|
|
|
truncate_file="C:\\Users\\Administrator\\Desktop\\货物标\\output3\\6.2定版视频会议磋商文件_qualification2.pdf"
|
2024-09-26 18:06:23 +08:00
|
|
|
|
knowledge_name="6.2视频会议docx"
|
|
|
|
|
res=qualification_review(truncate_file,knowledge_name)
|
2024-09-23 15:49:30 +08:00
|
|
|
|
print(json.dumps(res,ensure_ascii=False, indent=4))
|