修正json repair安装和导入

This commit is contained in:
python 2025-12-09 14:18:32 +08:00
parent 7c30e59328
commit f1b5c52500
2 changed files with 10 additions and 9 deletions

View File

@ -7,5 +7,5 @@ flasgger==0.9.7.1
python-docx==1.1.0 python-docx==1.1.0
minio==7.2.3 minio==7.2.3
openpyxl==3.1.2 openpyxl==3.1.2
jsonrepair==0.27.0 json-repair

View File

@ -9,13 +9,14 @@ import requests
import json import json
from typing import Dict, List, Optional from typing import Dict, List, Optional
# 尝试导入jsonrepair库如果不可用则使用备用方案 # 尝试导入json-repair库如果不可用则使用备用方案
try: try:
import jsonrepair from json_repair import repair_json
JSONREPAIR_AVAILABLE = True JSONREPAIR_AVAILABLE = True
except ImportError: except ImportError:
JSONREPAIR_AVAILABLE = False JSONREPAIR_AVAILABLE = False
print("[AI服务] 警告: jsonrepair库未安装将使用基础JSON修复功能。建议运行: pip install jsonrepair") repair_json = None
print("[AI服务] 警告: json-repair库未安装将使用基础JSON修复功能。建议运行: pip install json-repair")
class AIService: class AIService:
@ -342,7 +343,7 @@ class AIService:
# 尝试使用jsonrepair如果可用进行最后修复 # 尝试使用jsonrepair如果可用进行最后修复
if JSONREPAIR_AVAILABLE: if JSONREPAIR_AVAILABLE:
try: try:
repaired_content = jsonrepair.repair_json(content) repaired_content = repair_json(content)
if repaired_content: if repaired_content:
try: try:
extracted_data = json.loads(repaired_content) extracted_data = json.loads(repaired_content)
@ -392,7 +393,7 @@ class AIService:
# 尝试使用jsonrepair修复如果可用 # 尝试使用jsonrepair修复如果可用
if JSONREPAIR_AVAILABLE: if JSONREPAIR_AVAILABLE:
try: try:
repaired = jsonrepair.repair_json(json_str) repaired = repair_json(json_str)
if repaired: if repaired:
return json.loads(repaired) return json.loads(repaired)
except Exception as repair_error: except Exception as repair_error:
@ -418,7 +419,7 @@ class AIService:
# 尝试使用jsonrepair修复如果可用 # 尝试使用jsonrepair修复如果可用
if JSONREPAIR_AVAILABLE: if JSONREPAIR_AVAILABLE:
try: try:
repaired = jsonrepair.repair_json(json_str) repaired = repair_json(json_str)
if repaired: if repaired:
return json.loads(repaired) return json.loads(repaired)
except Exception as repair_error: except Exception as repair_error:
@ -439,7 +440,7 @@ class AIService:
# 尝试使用jsonrepair修复如果可用 # 尝试使用jsonrepair修复如果可用
if JSONREPAIR_AVAILABLE: if JSONREPAIR_AVAILABLE:
try: try:
repaired = jsonrepair.repair_json(cleaned_text) repaired = repair_json(cleaned_text)
if repaired: if repaired:
return json.loads(repaired) return json.loads(repaired)
except Exception as repair_error: except Exception as repair_error:
@ -598,7 +599,7 @@ class AIService:
# 首先尝试使用jsonrepair库如果可用 # 首先尝试使用jsonrepair库如果可用
if JSONREPAIR_AVAILABLE: if JSONREPAIR_AVAILABLE:
try: try:
repaired = jsonrepair.repair_json(json_str) repaired = repair_json(json_str)
if repaired and repaired != json_str: if repaired and repaired != json_str:
print(f"[AI服务] 使用jsonrepair修复JSON成功") print(f"[AI服务] 使用jsonrepair修复JSON成功")
return repaired return repaired