From f1b5c5250058778d09925c1694af547920987c47 Mon Sep 17 00:00:00 2001 From: python Date: Tue, 9 Dec 2025 14:18:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3json=20repair=E5=AE=89?= =?UTF-8?q?=E8=A3=85=E5=92=8C=E5=AF=BC=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- requirements.txt | 2 +- services/ai_service.py | 17 +++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/requirements.txt b/requirements.txt index b82ed6e..1d93615 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,5 +7,5 @@ flasgger==0.9.7.1 python-docx==1.1.0 minio==7.2.3 openpyxl==3.1.2 -jsonrepair==0.27.0 +json-repair diff --git a/services/ai_service.py b/services/ai_service.py index ae9e9ec..7b4c30a 100644 --- a/services/ai_service.py +++ b/services/ai_service.py @@ -9,13 +9,14 @@ import requests import json from typing import Dict, List, Optional -# 尝试导入jsonrepair库,如果不可用则使用备用方案 +# 尝试导入json-repair库,如果不可用则使用备用方案 try: - import jsonrepair + from json_repair import repair_json JSONREPAIR_AVAILABLE = True except ImportError: JSONREPAIR_AVAILABLE = False - print("[AI服务] 警告: jsonrepair库未安装,将使用基础JSON修复功能。建议运行: pip install jsonrepair") + repair_json = None + print("[AI服务] 警告: json-repair库未安装,将使用基础JSON修复功能。建议运行: pip install json-repair") class AIService: @@ -342,7 +343,7 @@ class AIService: # 尝试使用jsonrepair(如果可用)进行最后修复 if JSONREPAIR_AVAILABLE: try: - repaired_content = jsonrepair.repair_json(content) + repaired_content = repair_json(content) if repaired_content: try: extracted_data = json.loads(repaired_content) @@ -392,7 +393,7 @@ class AIService: # 尝试使用jsonrepair修复(如果可用) if JSONREPAIR_AVAILABLE: try: - repaired = jsonrepair.repair_json(json_str) + repaired = repair_json(json_str) if repaired: return json.loads(repaired) except Exception as repair_error: @@ -418,7 +419,7 @@ class AIService: # 尝试使用jsonrepair修复(如果可用) if JSONREPAIR_AVAILABLE: try: - repaired = jsonrepair.repair_json(json_str) + repaired = repair_json(json_str) if repaired: return json.loads(repaired) except Exception as repair_error: @@ -439,7 +440,7 @@ class AIService: # 尝试使用jsonrepair修复(如果可用) if JSONREPAIR_AVAILABLE: try: - repaired = jsonrepair.repair_json(cleaned_text) + repaired = repair_json(cleaned_text) if repaired: return json.loads(repaired) except Exception as repair_error: @@ -598,7 +599,7 @@ class AIService: # 首先尝试使用jsonrepair库(如果可用) if JSONREPAIR_AVAILABLE: try: - repaired = jsonrepair.repair_json(json_str) + repaired = repair_json(json_str) if repaired and repaired != json_str: print(f"[AI服务] 使用jsonrepair修复JSON成功") return repaired