-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.py
More file actions
1256 lines (1055 loc) · 59.1 KB
/
Copy pathmain.py
File metadata and controls
1256 lines (1055 loc) · 59.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from typing import List, Dict, Any, Optional, Union, Tuple, AsyncGenerator, TypedDict, cast, Callable
import os
import json
import re
import asyncio
from langchain_core.output_parsers import JsonOutputParser
from langchain_core.prompts import PromptTemplate
from langchain.chat_models import init_chat_model
from langchain_core.messages import AIMessage
from langchain_core.language_models.chat_models import BaseChatModel
from langchain_core.runnables import RunnableSerializable
from prompts import letter_correction_prompt, ERROR_PATTERNS
from logger import setup_logger
from dotenv import load_dotenv
# 配置日志
logger = setup_logger(__name__)
# 加载 .env 文件
load_dotenv()
class ModelConfig:
"""模型配置类,用于存储和管理LLM模型的配置参数"""
api_key: str
model_name: str
base_url: str
temperature: float
max_tokens: Optional[int]
request_timeout: int
provider: str
def __init__(
self,
api_key: str,
model_name: str,
base_url: str,
provider: str = "openai_compatible",
temperature: float = 0.0,
max_tokens: Optional[int] = None,
request_timeout: int = 120
) -> None:
"""
初始化模型配置
Args:
api_key: API密钥
model_name: 模型名称
base_url: API基础URL
temperature: 温度参数,控制随机性,默认0.0
max_tokens: 最大生成token数,默认None
request_timeout: 请求超时时间(秒),默认120
"""
self.api_key = api_key
self.model_name = model_name
self.base_url = base_url
self.provider = provider
self.temperature = temperature
self.max_tokens = max_tokens
self.request_timeout = request_timeout
@classmethod
def from_env(cls, model_type: str = "deepseek") -> "ModelConfig":
"""
从环境变量加载模型配置
Args:
model_type: 模型类型,支持 "deepseek" 或 "openai"
Returns:
ModelConfig: 模型配置对象
Raises:
ValueError: 当环境变量未设置或模型类型不支持时抛出异常
"""
if model_type.lower() == "deepseek":
api_key = os.getenv("DEEPSEEK_API_KEY")
if not api_key:
raise ValueError("环境变量 DEEPSEEK_API_KEY 未设置")
return cls(
api_key=api_key,
model_name="deepseek-chat",
base_url="https://api.deepseek.com",
provider="deepseek",
temperature=0.0,
request_timeout=120
)
elif model_type.lower() == "openai":
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
raise ValueError("环境变量 OPENAI_API_KEY 未设置")
return cls(
api_key=api_key,
model_name="gpt-4o",
base_url="https://api.openai.com/v1",
provider="openai",
temperature=0.0,
request_timeout=120
)
else:
raise ValueError(f"不支持的模型类型: {model_type}")
@classmethod
def from_config(cls, config_path: str = "config.json") -> "ModelConfig":
"""
从JSON配置文件加载模型配置
Args:
config_path: 配置文件路径,默认为 "config.json"
Returns:
ModelConfig: 模型配置对象
Raises:
FileNotFoundError: 当配置文件不存在时抛出异常
ValueError: 当配置文件格式错误或缺少必要字段时抛出异常
"""
if not os.path.exists(config_path):
raise FileNotFoundError(f"配置文件 {config_path} 不存在")
try:
with open(config_path, 'r', encoding='utf-8') as f:
config = json.load(f)
model_config = config.get('model', {})
provider = model_config.get('provider', 'deepseek').lower()
if provider not in model_config:
raise ValueError(f"配置文件中缺少 {provider} 的配置信息")
provider_config = model_config[provider]
api_key = cls._resolve_api_key(provider, provider_config.get("base_url", ""))
return cls(
api_key=api_key,
model_name=provider_config.get('model_name'),
base_url=provider_config.get('base_url'),
provider=provider,
temperature=provider_config.get('temperature', 0.0),
max_tokens=provider_config.get('max_tokens'),
request_timeout=provider_config.get('request_timeout', 120)
)
except json.JSONDecodeError as e:
raise ValueError(f"配置文件格式错误: {e}")
except KeyError as e:
raise ValueError(f"配置文件缺少必要字段: {e}")
@staticmethod
def _resolve_api_key(provider: str, base_url: str = "") -> str:
"""解析不同 provider 的 API Key,支持 OpenAI 兼容 provider。"""
provider_lower = provider.lower()
if provider_lower == "deepseek":
api_key = os.getenv("DEEPSEEK_API_KEY")
if not api_key:
raise ValueError("环境变量 DEEPSEEK_API_KEY 未设置")
return api_key
if provider_lower == "openai":
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
raise ValueError("环境变量 OPENAI_API_KEY 未设置")
return api_key
api_key_env = f"{provider_upper}_API_KEY" if (provider_upper := provider.upper()) else "API_KEY"
api_key = os.getenv(api_key_env) or os.getenv("API_KEY")
if api_key:
if os.getenv(api_key_env):
logger.info(f"使用环境变量 {api_key_env} 作为 {provider} 的密钥")
else:
logger.info(f"使用通用环境变量 API_KEY 作为 {provider} 的密钥")
return api_key
# 兼容本地/无鉴权服务(如 Ollama 的本地网关)
if "localhost" in base_url or "127.0.0.1" in base_url:
logger.warning(f"{provider} 未配置密钥,检测到本地地址,使用占位密钥")
return "ollama"
raise ValueError(f"环境变量 {api_key_env} 或 API_KEY 未设置")
def create_model(self) -> BaseChatModel:
"""
根据配置创建对应的模型实例
Returns:
BaseChatModel: 大语言模型实例
"""
# 统一使用 OpenAI 兼容链路(DeepSeek / OpenAI / Ollama / 自定义 Provider)
model = init_chat_model(
model=self.model_name,
model_provider="openai",
api_key=self.api_key,
base_url=self.base_url,
temperature=self.temperature,
max_tokens=self.max_tokens,
timeout=self.request_timeout,
)
return cast(BaseChatModel, model)
class ErrorPosition(TypedDict):
"""错误位置类型定义"""
start: int
end: int
class ErrorInfo(TypedDict):
"""错误信息类型定义"""
错误文本: str
错误位置: List[int]
错误说明: str
修改建议: str
模糊匹配: Optional[bool]
class ErrorAnalysis(TypedDict):
"""错误分析类型定义"""
语法错误: List[ErrorInfo]
拼写错误: List[ErrorInfo]
标点错误: List[ErrorInfo]
用词错误: List[ErrorInfo]
其他错误: List[ErrorInfo]
class ScoreInfo(TypedDict):
"""评分信息类型定义"""
分数: str
class CorrectionResult(TypedDict):
"""批改结果类型定义"""
评分: ScoreInfo
错误分析: ErrorAnalysis
亮点分析: Dict[str, List[str]]
写作建议: str
class StreamResult(TypedDict):
"""流式结果类型定义"""
status: str
partial_result: Optional[Dict[str, Any]]
result: Optional[CorrectionResult]
error: Optional[str]
def log_llm_response(response: Dict[str, Any], log_type: str = "response") -> None:
"""
将LLM的响应保存到日志文件中,并打印到主日志
Args:
response: LLM的响应数据
log_type: 日志类型,用于文件名
"""
try:
# 确保日志目录存在
log_dir = os.path.join(os.getcwd(), 'logs')
if not os.path.exists(log_dir):
os.makedirs(log_dir)
# 创建日志文件名
import datetime
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
log_file = os.path.join(log_dir, f'llm_{log_type}_{timestamp}.json')
# 以美化格式写入日志
with open(log_file, 'w', encoding='utf-8') as f:
json.dump(response, f, ensure_ascii=False, indent=2)
# 打印响应体到主日志
logger.info(f"LLM {log_type} 已保存到 {log_file}")
logger.info(f"LLM 响应体: {json.dumps(response, ensure_ascii=False)}")
except Exception as e:
logger.error(f"保存LLM {log_type}失败: {str(e)}")
class ErrorAnalysisValidator:
"""
错误分析验证器类,用于校验和修复错误位置索引
"""
@staticmethod
def validate_error_positions(essay: str, error_data: Dict[str, Any]) -> Dict[str, Any]:
"""
验证错误位置,确保索引准确
Args:
essay: 原始作文文本
error_data: 错误分析数据
Returns:
Dict[str, Any]: 修正后的错误分析数据
"""
validated_data = error_data.copy()
# 记录原始错误数据
log_llm_response(error_data, "original_response")
# 遍历所有错误类别
for category, errors in error_data['错误分析'].items():
validated_errors: List[ErrorInfo] = []
for error in errors:
error_dict = cast(ErrorInfo, error)
# 确保错误文本字段存在
if '错误文本' not in error_dict and '错误位置' in error_dict:
start, end = error_dict['错误位置']
if 0 <= start < len(essay) and 0 <= end <= len(essay) and start < end:
error_dict['错误文本'] = essay[start:end]
logger.info(f"添加错误文本: '{error_dict['错误文本']}' 位置: {start}-{end}")
else:
logger.warning(f"索引超出范围: {start}-{end}, 文章长度: {len(essay)}")
continue
# 使用正则表达式定位错误
if '错误文本' in error_dict:
error_text = error_dict['错误文本']
original_positions = error_dict.get('错误位置', [0, 0])
try:
# 准备正则表达式模式 - 处理特殊字符
pattern = re.escape(error_text)
# 查找所有匹配
matches = list(re.finditer(pattern, essay))
if matches:
# 如果有多个匹配,选择最接近原始位置的匹配
if len(matches) > 1 and original_positions != [0, 0]:
original_start = original_positions[0]
closest_match = min(matches, key=lambda m: abs(m.start() - original_start))
error_dict['错误位置'] = [closest_match.start(), closest_match.end()]
logger.info(f"找到多个匹配,选择最接近原始位置的匹配: '{error_text}' 在 {closest_match.start()}-{closest_match.end()} 处")
else:
# 使用第一个匹配
first_match = matches[0]
error_dict['错误位置'] = [first_match.start(), first_match.end()]
logger.info(f"位置已更新: '{error_text}' 在 {first_match.start()}-{first_match.end()} 处找到")
else:
# 如果没有精确匹配,尝试模糊匹配
logger.warning(f"无法精确匹配错误文本: '{error_text}',尝试模糊匹配")
# 移除多余空格和标点符号进行模糊匹配
simplified_error = re.sub(r'[^\w\s]', '', error_text).lower().strip()
simplified_essay = essay.lower()
fuzzy_match = re.search(simplified_error, simplified_essay)
if fuzzy_match:
# 从模糊匹配位置查找最接近的原始文本段落
approx_start = fuzzy_match.start()
context_start = max(0, approx_start - 20)
context_end = min(len(essay), approx_start + len(simplified_error) + 20)
context = essay[context_start:context_end]
error_dict['错误位置'] = [approx_start, approx_start + len(simplified_error)]
error_dict['模糊匹配'] = True
logger.info(f"使用模糊匹配: '{error_text}' 可能在 {approx_start}-{approx_start + len(simplified_error)} 处,上下文: '{context}'")
else:
logger.warning(f"无法匹配错误文本: '{error_text}',保留原始位置信息")
except Exception as e:
logger.error(f"处理错误文本时出错: {str(e)}")
continue
validated_errors.append(error_dict)
validated_data['错误分析'][category] = validated_errors
# 记录验证后的错误数据
log_llm_response(validated_data, "validated_response")
return validated_data
class BaseTask:
"""通用任务基类,用于构建提示模板"""
def __init__(self, prompt: str, input_variables: List[str], format_instruction: str) -> None:
"""
初始化任务
Args:
prompt: 提示模板字符串
input_variables: 输入变量列表
format_instruction: 格式说明
"""
self.prompt_template = prompt
self.input_variables = input_variables
self.format_instruction = format_instruction
def get_prompt(self) -> PromptTemplate:
"""
构造 PromptTemplate 对象
Returns:
PromptTemplate: 构造好的提示模板
"""
return PromptTemplate(
template=self.prompt_template,
input_variables=self.input_variables,
partial_variables={"format_instructions": self.format_instruction}
)
class TaskHandler:
"""任务处理器,负责执行LLM调用和结果处理"""
def __init__(self, model: BaseChatModel, parser: JsonOutputParser) -> None:
"""
初始化任务处理器
Args:
model: 大语言模型实例
parser: JSON输出解析器
"""
self.model = model
self.parser = parser
self.validator = ErrorAnalysisValidator()
def process_task(self, task: BaseTask, inputs: Dict[str, Any]) -> Dict[str, Any]:
"""
通用任务处理方法
Args:
task: 任务对象
inputs: 输入数据的字典
Returns:
Dict[str, Any]: 任务处理结果
Raises:
Exception: 任务处理失败时抛出异常
"""
prompt = task.get_prompt()
chain: RunnableSerializable = prompt | self.model | self.parser
# 记录处理开始
logger.info(f"开始处理任务: {task.__class__.__name__}")
if 'essay' in inputs:
logger.info(f"作文长度: {len(inputs['essay'])} 字符")
try:
# 执行LLM调用
result = chain.invoke(inputs)
# 记录原始结果
logger.info("LLM调用成功")
# 如果有essay输入,进行错误位置验证
if 'essay' in inputs:
result = self.validator.validate_error_positions(inputs['essay'], result)
return result
except Exception as e:
logger.error(f"任务处理失败: {str(e)}")
raise
async def process_task_async(self, task: BaseTask, inputs: Dict[str, Any]) -> Dict[str, Any]:
"""
异步处理任务方法
Args:
task: 任务对象
inputs: 输入数据的字典
Returns:
Dict[str, Any]: 任务处理结果
Raises:
Exception: 任务处理失败时抛出异常
"""
prompt = task.get_prompt()
chain: RunnableSerializable = prompt | self.model | self.parser
# 记录处理开始
logger.info(f"开始异步处理任务: {task.__class__.__name__}")
if 'essay' in inputs:
logger.info(f"作文长度: {len(inputs['essay'])} 字符")
try:
# 执行LLM调用
result = await chain.ainvoke(inputs)
# 记录原始结果
logger.info("异步LLM调用成功")
# 如果有essay输入,进行错误位置验证
if 'essay' in inputs:
result = self.validator.validate_error_positions(inputs['essay'], result)
return result
except Exception as e:
logger.error(f"异步任务处理失败: {str(e)}")
raise
async def stream_task(self, task: BaseTask, inputs: Dict[str, Any]) -> AsyncGenerator[StreamResult, None]:
"""
流式处理任务方法,支持实时返回部分结果
Args:
task: 任务对象
inputs: 输入数据的字典
Yields:
StreamResult: 流式处理的部分或完整结果
Raises:
Exception: 流式处理失败时可能抛出异常
"""
prompt = task.get_prompt()
chain: RunnableSerializable = prompt | self.model
# 记录处理开始
logger.info(f"开始流式处理任务: {task.__class__.__name__}")
if 'essay' in inputs:
logger.info(f"作文长度: {len(inputs['essay'])} 字符")
collected_content = ""
result_dict: Dict[str, Any] = {}
last_yield_time = 0 # 记录上次发送的时间
token_buffer = [] # 用于累积token
# 定义要监控的关键部分
key_sections = ["评分", "错误分析", "亮点分析", "写作建议"]
section_progress: Dict[str, Dict[str, Any]] = {
section: {"complete": False, "content": {}, "partial_content": {}} for section in key_sections
}
# 错误对象的实时处理
error_objects: Dict[str, List[Dict[str, Any]]] = {}
current_error_object: Optional[Dict[str, Any]] = None
current_error_type: Optional[str] = None
try:
# 流式处理LLM调用
async for chunk in chain.astream(inputs):
if isinstance(chunk, AIMessage):
content = chunk.content
else:
content = str(chunk)
# 累积内容
collected_content += content
token_buffer.append(content)
# 控制发送频率,避免过于频繁的更新
current_time = asyncio.get_event_loop().time()
if current_time - last_yield_time < 0.2 and len(token_buffer) < 5: # 降低间隔时间和token阈值
continue
# 重置buffer和时间
token_buffer = []
last_yield_time = current_time
# 尝试解析已经收集到的内容
try:
# 首先尝试解析完整JSON
if "{" in collected_content and "}" in collected_content:
# 尝试提取最外层的JSON部分
start_idx = collected_content.find("{")
end_idx = collected_content.rfind("}")
if start_idx != -1 and end_idx != -1 and start_idx < end_idx:
possible_json = collected_content[start_idx:end_idx+1]
try:
partial_result = json.loads(possible_json)
if isinstance(partial_result, dict):
result_dict = partial_result # 完全替换为新解析的内容
# 如果有essay输入,实时验证错误位置
if 'essay' in inputs and '错误分析' in result_dict:
# 只验证错误位置,不替换整个结果
self._validate_error_positions_streaming(inputs['essay'], result_dict)
yield {"status": "processing", "partial_result": result_dict, "result": None, "error": None}
continue # 如果完整解析成功,跳过部分解析
except json.JSONDecodeError:
pass # 继续尝试部分解析
# 更细粒度地解析和返回部分内容
should_yield = False
# 1. 尝试提取各个主要部分
for section in key_sections:
# 检查是否已经完成该部分的解析
if section_progress[section]["complete"]:
continue
# 尝试匹配整个部分及其内容
# 例如: "评分": {"分数": "14/15", "评分理由": "这篇作文..."}
section_pattern = f'"{section}"\\s*:\\s*(\\{{[^{{}}]*(?:\\{{[^{{}}]*\\}}[^{{}}]*)*\\}}|\\[[^\\[\\]]*(?:\\[[^\\[\\]]*\\][^\\[\\]]*)*\\]|"[^"]*")'
section_match = re.search(section_pattern, collected_content, re.DOTALL)
if section_match:
try:
section_content = section_match.group(1)
# 确保获取到完整的JSON对象或数组
if self._is_balanced_json(section_content):
# 确保它是有效的JSON
if section_content.startswith('{') and section_content.endswith('}'):
section_json = json.loads(section_content)
result_dict[section] = section_json
section_progress[section]["complete"] = True
section_progress[section]["content"] = section_json
should_yield = True
logger.info(f"完整解析 {section}: {json.dumps(section_json, ensure_ascii=False)[:100]}...")
elif section_content.startswith('[') and section_content.endswith(']'):
section_json = json.loads(section_content)
result_dict[section] = section_json
section_progress[section]["complete"] = True
section_progress[section]["content"] = section_json
should_yield = True
logger.info(f"完整解析 {section}: {json.dumps(section_json, ensure_ascii=False)[:100]}...")
elif section_content.startswith('"') and section_content.endswith('"'):
# 处理字符串值(如"写作建议")
string_value = section_content.strip('"')
result_dict[section] = string_value
section_progress[section]["complete"] = True
section_progress[section]["content"] = string_value
should_yield = True
logger.info(f"完整解析 {section}: {string_value[:100]}...")
else:
# 如果JSON不完整,尝试提取部分内容
self._extract_partial_content(section, section_content, result_dict, section_progress)
should_yield = True
except json.JSONDecodeError as e:
logger.debug(f"解析 {section} 时出错: {str(e)}, 尝试提取部分内容")
# 尝试提取部分内容
self._extract_partial_content(section, section_match.group(1), result_dict, section_progress)
should_yield = True
else:
# 2. 尝试提取部分内容
if section == "评分":
# 尝试提取评分中的分数
score_match = re.search(r'"分数"\s*:\s*"([^"]+)"', collected_content)
if score_match:
if section not in result_dict:
result_dict[section] = {}
result_dict[section]["分数"] = score_match.group(1)
section_progress[section]["partial_content"]["分数"] = score_match.group(1)
should_yield = True
logger.info(f"部分解析评分-分数: {score_match.group(1)}")
# 尝试提取评分理由
reason_match = re.search(r'"评分理由"\s*:\s*"([^"]+)"', collected_content)
if reason_match:
if section not in result_dict:
result_dict[section] = {}
result_dict[section]["评分理由"] = reason_match.group(1)
section_progress[section]["partial_content"]["评分理由"] = reason_match.group(1)
should_yield = True
logger.info(f"部分解析评分-理由: {reason_match.group(1)[:50]}...")
elif section == "错误分析":
# 实时解析错误对象
self._parse_error_objects_streaming(collected_content, result_dict, error_objects)
# 如果有错误对象被解析出来,标记为需要发送
if error_objects and any(len(errors) > 0 for errors in error_objects.values()):
if "错误分析" not in result_dict:
result_dict["错误分析"] = {}
# 将解析出的错误对象添加到结果中
for error_type, errors in error_objects.items():
if errors:
result_dict["错误分析"][error_type] = errors
should_yield = True
# 尝试提取各种错误类型
error_types = ["语法错误", "拼写错误", "标点错误", "用词错误", "用词不当", "其他错误"]
for error_type in error_types:
# 使用更宽松的模式匹配错误数组
error_pattern = f'"{error_type}"\\s*:\\s*(\\[[^\\[\\]]*(?:\\{{[^{{}}]*\\}}[^\\[\\]]*)*\\])'
error_match = re.search(error_pattern, collected_content, re.DOTALL)
if error_match:
try:
error_content = error_match.group(1)
# 检查是否是完整的JSON数组
if self._is_balanced_json(error_content):
error_json = json.loads(error_content)
# 初始化错误分析结构
if "错误分析" not in result_dict:
result_dict["错误分析"] = {}
result_dict["错误分析"][error_type] = error_json
section_progress[section]["partial_content"][error_type] = error_json
should_yield = True
logger.info(f"部分解析错误分析-{error_type}: {json.dumps(error_json, ensure_ascii=False)[:100]}...")
# 实时验证错误位置
if 'essay' in inputs:
self._validate_error_type_positions(inputs['essay'], result_dict, error_type)
except json.JSONDecodeError:
pass
elif section == "亮点分析":
# 尝试提取亮点分析的各个部分
highlight_types = ["高级词汇", "亮点表达"]
for highlight_type in highlight_types:
highlight_pattern = f'"{highlight_type}"\\s*:\\s*(\\[[^\\[\\]]*(?:\\{{[^{{}}]*\\}}[^\\[\\]]*)*\\])'
highlight_match = re.search(highlight_pattern, collected_content, re.DOTALL)
if highlight_match:
try:
highlight_content = highlight_match.group(1)
# 检查是否是完整的JSON数组
if self._is_balanced_json(highlight_content):
highlight_json = json.loads(highlight_content)
# 初始化亮点分析结构
if "亮点分析" not in result_dict:
result_dict["亮点分析"] = {}
result_dict["亮点分析"][highlight_type] = highlight_json
section_progress[section]["partial_content"][highlight_type] = highlight_json
should_yield = True
logger.info(f"部分解析亮点分析-{highlight_type}: {json.dumps(highlight_json, ensure_ascii=False)[:100]}...")
except json.JSONDecodeError:
pass
elif section == "写作建议":
# 尝试提取写作建议的各个部分
suggestion_types = ["结构建议", "表达建议", "内容建议", "实用技巧"]
for suggestion_type in suggestion_types:
# 字符串类型的建议
string_pattern = f'"{suggestion_type}"\\s*:\\s*"([^"]*)"'
string_match = re.search(string_pattern, collected_content)
if string_match:
if section not in result_dict:
result_dict[section] = {}
result_dict[section][suggestion_type] = string_match.group(1)
section_progress[section]["partial_content"][suggestion_type] = string_match.group(1)
should_yield = True
logger.info(f"部分解析写作建议-{suggestion_type}: {string_match.group(1)[:50]}...")
continue
# 数组类型的建议
array_pattern = f'"{suggestion_type}"\\s*:\\s*(\\[[^\\[\\]]*(?:"[^"]*"[^\\[\\]]*)*\\])'
array_match = re.search(array_pattern, collected_content, re.DOTALL)
if array_match:
try:
array_content = array_match.group(1)
if self._is_balanced_json(array_content):
array_json = json.loads(array_content)
if "写作建议" not in result_dict:
result_dict["写作建议"] = {}
result_dict["写作建议"][suggestion_type] = array_json
section_progress[section]["partial_content"][suggestion_type] = array_json
should_yield = True
logger.info(f"部分解析写作建议-{suggestion_type}: {json.dumps(array_json, ensure_ascii=False)[:100]}...")
except json.JSONDecodeError:
pass
# 如果有解析结果,返回部分结果
if should_yield and result_dict:
# 如果有essay输入,尝试对已解析的错误进行位置验证
if 'essay' in inputs and '错误分析' in result_dict:
self._validate_error_positions_streaming(inputs['essay'], result_dict)
yield {"status": "processing", "partial_result": result_dict, "result": None, "error": None}
except Exception as e:
logger.error(f"解析流式内容时出错: {str(e)}")
# 流处理完成后,尝试完整解析所有内容
try:
# 查找最外层的JSON部分
start_idx = collected_content.find("{")
end_idx = collected_content.rfind("}")
if start_idx != -1 and end_idx != -1 and start_idx < end_idx:
possible_json = collected_content[start_idx:end_idx+1]
final_result = json.loads(possible_json)
# 验证错误位置
if 'essay' in inputs:
final_result = self.validator.validate_error_positions(inputs['essay'], final_result)
yield {"status": "completed", "partial_result": None, "result": cast(CorrectionResult, final_result), "error": None}
except Exception as e:
logger.error(f"解析最终流式结果时出错: {str(e)}")
yield {"status": "error", "partial_result": None, "result": None, "error": str(e)}
except Exception as e:
logger.error(f"流式任务处理失败: {str(e)}")
yield {"status": "error", "partial_result": None, "result": None, "error": str(e)}
def _parse_error_objects_streaming(self, content: str, result_dict: Dict[str, Any],
error_objects: Dict[str, List[Dict[str, Any]]]) -> None:
"""
实时解析错误对象
Args:
content: 收集到的内容
result_dict: 结果字典
error_objects: 错误对象字典
"""
# 尝试提取单个错误对象
error_object_pattern = r'\{\s*"错误文本"\s*:\s*"([^"]*)"\s*,\s*"错误位置"\s*:\s*\[\s*(\d+)\s*,\s*(\d+)\s*\]\s*,\s*"错误说明"\s*:\s*"([^"]*)"\s*,\s*"修改建议"\s*:\s*"([^"]*)"\s*\}'
# 查找当前错误类型上下文
error_types = ["语法错误", "拼写错误", "标点错误", "用词错误", "用词不当", "其他错误"]
current_error_type = None
for error_type in error_types:
if f'"{error_type}"' in content:
# 检查这个错误类型是否是最近出现的
type_pos = content.rfind(f'"{error_type}"')
array_start = content.find('[', type_pos)
if array_start != -1:
# 找到了错误类型的数组开始
current_error_type = error_type
# 初始化错误类型数组
if error_type not in error_objects:
error_objects[error_type] = []
if current_error_type:
# 查找所有匹配的错误对象
for match in re.finditer(error_object_pattern, content):
error_text = match.group(1)
start_pos = int(match.group(2))
end_pos = int(match.group(3))
error_desc = match.group(4)
fix_suggestion = match.group(5)
# 创建错误对象
error_obj = {
"错误文本": error_text,
"错误位置": [start_pos, end_pos],
"错误说明": error_desc,
"修改建议": fix_suggestion
}
# 检查是否已经存在相同的错误对象
is_new = True
for existing_error in error_objects.get(current_error_type, []):
if (existing_error.get("错误文本") == error_text and
existing_error.get("错误位置") == [start_pos, end_pos]):
is_new = False
break
# 如果是新的错误对象,添加到列表中
if is_new:
if current_error_type not in error_objects:
error_objects[current_error_type] = []
error_objects[current_error_type].append(error_obj)
logger.info(f"解析到新的错误对象: {error_type} - {error_text}")
def _validate_error_positions_streaming(self, essay: str, result_dict: Dict[str, Any]) -> None:
"""
实时验证错误位置
Args:
essay: 原始作文文本
result_dict: 包含错误分析的结果字典
"""
if '错误分析' not in result_dict:
return
for category, errors in result_dict['错误分析'].items():
if not isinstance(errors, list):
continue
for i, error in enumerate(errors):
if not isinstance(error, dict):
continue
# 确保错误文本字段存在
if '错误文本' not in error and '错误位置' in error:
start, end = error['错误位置']
if 0 <= start < len(essay) and 0 <= end <= len(essay) and start < end:
error['错误文本'] = essay[start:end]
logger.info(f"实时添加错误文本: '{error['错误文本']}' 位置: {start}-{end}")
# 使用正则表达式定位错误
if '错误文本' in error:
error_text = error['错误文本']
original_positions = error.get('错误位置', [0, 0])
try:
# 准备正则表达式模式 - 处理特殊字符
pattern = re.escape(error_text)
# 查找所有匹配
matches = list(re.finditer(pattern, essay))
if matches:
# 如果有多个匹配,选择最接近原始位置的匹配
if len(matches) > 1 and original_positions != [0, 0]:
original_start = original_positions[0]
closest_match = min(matches, key=lambda m: abs(m.start() - original_start))
error['错误位置'] = [closest_match.start(), closest_match.end()]
logger.info(f"实时更新位置: '{error_text}' 在 {closest_match.start()}-{closest_match.end()} 处")
else:
# 使用第一个匹配
first_match = matches[0]
error['错误位置'] = [first_match.start(), first_match.end()]
logger.info(f"实时更新位置: '{error_text}' 在 {first_match.start()}-{first_match.end()} 处")
else:
# 如果没有精确匹配,尝试模糊匹配
logger.debug(f"无法精确匹配错误文本: '{error_text}',尝试模糊匹配")
# 移除多余空格和标点符号进行模糊匹配
simplified_error = re.sub(r'[^\w\s]', '', error_text).lower().strip()
simplified_essay = essay.lower()
fuzzy_match = re.search(simplified_error, simplified_essay)
if fuzzy_match:
# 从模糊匹配位置查找最接近的原始文本段落
approx_start = fuzzy_match.start()
error['错误位置'] = [approx_start, approx_start + len(simplified_error)]
error['模糊匹配'] = True
logger.info(f"实时模糊匹配: '{error_text}' 可能在 {approx_start}-{approx_start + len(simplified_error)} 处")
except Exception as e:
logger.error(f"实时处理错误文本时出错: {str(e)}")
def _validate_error_type_positions(self, essay: str, result_dict: Dict[str, Any], error_type: str) -> None:
"""
验证特定错误类型的位置
Args:
essay: 原始作文文本
result_dict: 结果字典
error_type: 错误类型
"""
if '错误分析' not in result_dict or error_type not in result_dict['错误分析']:
return
errors = result_dict['错误分析'][error_type]
if not isinstance(errors, list):
return
for i, error in enumerate(errors):
if not isinstance(error, dict):
continue
# 确保错误文本字段存在
if '错误文本' not in error and '错误位置' in error:
start, end = error['错误位置']
if 0 <= start < len(essay) and 0 <= end <= len(essay) and start < end:
error['错误文本'] = essay[start:end]
logger.info(f"实时添加错误文本({error_type}): '{error['错误文本']}' 位置: {start}-{end}")
# 使用正则表达式定位错误
if '错误文本' in error:
error_text = error['错误文本']
original_positions = error.get('错误位置', [0, 0])
try:
# 准备正则表达式模式 - 处理特殊字符
pattern = re.escape(error_text)
# 查找所有匹配
matches = list(re.finditer(pattern, essay))
if matches:
# 如果有多个匹配,选择最接近原始位置的匹配
if len(matches) > 1 and original_positions != [0, 0]:
original_start = original_positions[0]
closest_match = min(matches, key=lambda m: abs(m.start() - original_start))
error['错误位置'] = [closest_match.start(), closest_match.end()]
logger.info(f"实时更新位置({error_type}): '{error_text}' 在 {closest_match.start()}-{closest_match.end()} 处")
else:
# 使用第一个匹配
first_match = matches[0]
error['错误位置'] = [first_match.start(), first_match.end()]
logger.info(f"实时更新位置({error_type}): '{error_text}' 在 {first_match.start()}-{first_match.end()} 处")
else:
# 如果没有精确匹配,尝试模糊匹配
logger.debug(f"无法精确匹配错误文本({error_type}): '{error_text}',尝试模糊匹配")
# 移除多余空格和标点符号进行模糊匹配
simplified_error = re.sub(r'[^\w\s]', '', error_text).lower().strip()
simplified_essay = essay.lower()
fuzzy_match = re.search(simplified_error, simplified_essay)
if fuzzy_match:
# 从模糊匹配位置查找最接近的原始文本段落
approx_start = fuzzy_match.start()
error['错误位置'] = [approx_start, approx_start + len(simplified_error)]
error['模糊匹配'] = True
logger.info(f"实时模糊匹配({error_type}): '{error_text}' 可能在 {approx_start}-{approx_start + len(simplified_error)} 处")
except Exception as e:
logger.error(f"实时处理错误文本({error_type})时出错: {str(e)}")
def _is_balanced_json(self, text: str) -> bool:
"""
检查JSON文本是否平衡(括号、引号等匹配)
Args:
text: 要检查的JSON文本
Returns:
bool: 如果JSON文本平衡则返回True,否则返回False
"""
# 检查括号是否平衡
stack = []
in_string = False
escape_next = False
for char in text:
if escape_next:
escape_next = False
continue
if char == '\\':
escape_next = True
continue
if char == '"' and not escape_next:
in_string = not in_string
continue