优化调整抽取
This commit is contained in:
parent
f1b5c52500
commit
8bebc13efe
@ -213,7 +213,7 @@ class AIService:
|
||||
"messages": [
|
||||
{
|
||||
"role": "system",
|
||||
"content": "你是一个专业的数据提取助手。请仔细分析用户提供的输入文本,提取所有相关信息,并严格按照指定的JSON格式返回结果。\n\n重要要求:\n1. 必须仔细阅读输入文本的每一个字,不要遗漏任何信息\n2. 对于每个字段,请从多个角度思考:直接提及、同义词、隐含信息、可推断信息\n3. 如果文本中明确提到某个信息(如性别、年龄、职务等),必须提取出来,不能设为空\n4. 如果可以通过已有信息合理推断(如根据出生年月推算年龄,从单位及职务中拆分单位和职务),请进行推断并填写\n5. 只返回JSON对象,不要包含任何其他文字说明、思考过程或markdown代码块标记"
|
||||
"content": "你是一个专业的数据提取助手。请仔细分析用户提供的输入文本,提取所有相关信息,并严格按照指定的JSON格式返回结果。\n\n重要要求:\n1. 必须仔细阅读输入文本的每一个字,不要遗漏任何信息\n2. 对于每个字段,请从多个角度思考:直接提及、同义词、隐含信息、可推断信息\n3. 如果文本中明确提到某个信息(如性别、年龄、职务、职级、线索来源等),必须提取出来,不能设为空\n4. 特别关注性别字段:如果文本中出现\"男\"、\"女\"、\"男性\"、\"女性\"、\"先生\"、\"女士\"等任何表示性别的词汇,必须提取并转换为\"男\"或\"女\"\n5. 如果可以通过已有信息合理推断(如根据出生年月推算年龄,从单位及职务中拆分单位和职务),请进行推断并填写\n6. 只返回JSON对象,不要包含任何其他文字说明、思考过程或markdown代码块标记\n7. 字段名必须严格按照JSON示例中的字段编码,不能使用下划线前缀(如不能使用\"_professional_rank\",应使用\"target_professional_rank\";不能使用\"_source\",应使用\"clue_source\")"
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
@ -523,13 +523,16 @@ class AIService:
|
||||
key = match.group(1)
|
||||
value = match.group(2)
|
||||
# 清理键名(移除可能的转义字符)
|
||||
key = key.replace('\\"', '').strip()
|
||||
key = key.replace('\\"', '').replace('\\', '').strip()
|
||||
if key:
|
||||
# 处理以_开头的字段名(如_professional_rank -> professional_rank)
|
||||
original_key = key
|
||||
if key.startswith('_') and len(key) > 1:
|
||||
key = key[1:]
|
||||
print(f"[AI服务] 部分JSON提取:处理下划线前缀 '{original_key}' -> '{key}'")
|
||||
if key not in result: # 避免覆盖已有值
|
||||
result[key] = value
|
||||
print(f"[AI服务] 部分JSON提取:提取字段 '{key}' = '{value}'")
|
||||
|
||||
# 模式2: "key": value (非字符串值,如数字、布尔值)
|
||||
pattern2 = r'"([^"]+?)"\s*:\s*([^,}\]]+?)(?=\s*[,}\]])'
|
||||
@ -538,12 +541,13 @@ class AIService:
|
||||
key = match.group(1).strip()
|
||||
value_str = match.group(2).strip()
|
||||
# 清理键名
|
||||
key = key.replace('\\"', '').strip()
|
||||
key = key.replace('\\"', '').replace('\\', '').strip()
|
||||
if key and key not in result: # 避免覆盖已有值
|
||||
# 处理以_开头的字段名
|
||||
original_key = key
|
||||
if key.startswith('_') and len(key) > 1:
|
||||
key = key[1:]
|
||||
print(f"[AI服务] 部分JSON提取:处理下划线前缀 '{original_key}' -> '{key}'")
|
||||
# 尝试解析值
|
||||
if value_str.lower() in ('true', 'false'):
|
||||
result[key] = value_str.lower() == 'true'
|
||||
@ -559,15 +563,22 @@ class AIService:
|
||||
result[key] = value_str
|
||||
else:
|
||||
result[key] = value_str
|
||||
print(f"[AI服务] 部分JSON提取:提取字段 '{key}' = '{result[key]}'")
|
||||
|
||||
# 模式3: 处理字段名缺少引号的情况(如 key: "value")
|
||||
# 模式3: 处理字段名缺少引号的情况(如 key: "value" 或 _key: "value")
|
||||
pattern3 = r'([a-zA-Z_][a-zA-Z0-9_]*)\s*:\s*"([^"]*?)"'
|
||||
matches3 = re.finditer(pattern3, json_content, re.DOTALL)
|
||||
for match in matches3:
|
||||
key = match.group(1).strip()
|
||||
value = match.group(2)
|
||||
if key and key not in result: # 避免覆盖已有值
|
||||
# 处理以_开头的字段名
|
||||
original_key = key
|
||||
if key.startswith('_') and len(key) > 1:
|
||||
key = key[1:]
|
||||
print(f"[AI服务] 部分JSON提取:处理下划线前缀 '{original_key}' -> '{key}'")
|
||||
result[key] = value
|
||||
print(f"[AI服务] 部分JSON提取:提取字段 '{key}' = '{value}'")
|
||||
|
||||
# 如果提取到了字段,返回结果
|
||||
if result:
|
||||
@ -608,12 +619,14 @@ class AIService:
|
||||
|
||||
# 基础修复方法
|
||||
# 1. 移除控制字符(除了换行符、制表符等)
|
||||
# 保留换行符(\n)、回车符(\r)、制表符(\t),移除其他控制字符
|
||||
json_str = re.sub(r'[\x00-\x08\x0B-\x0C\x0E-\x1F\x7F]', '', json_str)
|
||||
|
||||
# 2. 修复字段名中的反斜杠转义问题(如 "_professional_rank\" -> "_professional_rank")
|
||||
# 处理字段名前的反斜杠
|
||||
# 处理字段名前的反斜杠(多次处理,确保修复所有情况)
|
||||
json_str = re.sub(r'\\"([^"]+?)\\":', r'"\1":', json_str)
|
||||
json_str = re.sub(r'\\"([^"]+?)\\":', r'"\1":', json_str) # 再次处理,确保修复所有情况
|
||||
json_str = re.sub(r'\\"([^"]+?)\\":', r'"\1":', json_str) # 第三次处理,确保修复嵌套转义
|
||||
|
||||
# 3. 修复字段名缺少开头引号的问题(如 _professional_rank" -> "_professional_rank")
|
||||
json_str = re.sub(r'([{,]\s*)([a-zA-Z_][a-zA-Z0-9_]*)"\s*:', r'\1"\2":', json_str)
|
||||
@ -621,6 +634,9 @@ class AIService:
|
||||
# 4. 修复字段名缺少结尾引号的问题(如 "_professional_rank -> "_professional_rank")
|
||||
json_str = re.sub(r'"([^"]+?)\s*:\s*"', r'"\1": "', json_str)
|
||||
|
||||
# 5. 修复字段名中的转义引号问题(如 \"_professional_rank\" -> "_professional_rank")
|
||||
json_str = re.sub(r'\\"([^"]+?)\\"\s*:', r'"\1":', json_str)
|
||||
|
||||
# 5. 移除末尾的逗号(在 } 或 ] 之前)
|
||||
json_str = re.sub(r',\s*}', '}', json_str)
|
||||
json_str = re.sub(r',\s*]', ']', json_str)
|
||||
@ -712,6 +728,7 @@ class AIService:
|
||||
'professional_rank': 'target_professional_rank',
|
||||
'clueSource': 'clue_source',
|
||||
'clue_source': 'clue_source',
|
||||
'source': 'clue_source', # 添加 source -> clue_source 的映射(处理 _source 去掉下划线后的情况)
|
||||
'issueDescription': 'target_issue_description',
|
||||
'issue_description': 'target_issue_description',
|
||||
'description': 'target_issue_description', # description可能是问题描述
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user