-
Notifications
You must be signed in to change notification settings - Fork 430
Expand file tree
/
Copy pathanswer.py
More file actions
1796 lines (1574 loc) · 73.4 KB
/
Copy pathanswer.py
File metadata and controls
1796 lines (1574 loc) · 73.4 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
import configparser
import json
import os
import random
import re
import shutil
import tempfile
import threading
import time
from abc import ABC, abstractmethod
from pathlib import Path
from re import sub
from typing import Optional
import httpx
import requests
from openai import OpenAI
from urllib3 import disable_warnings, exceptions
from api.answer_check import check_answer
from api.logger import logger
# 关闭警告
disable_warnings(exceptions.InsecureRequestWarning)
__all__ = ["CacheDAO", "Tiku", "TikuFallback", "TikuYanxi", "TikuGo", "TikuLike", "TikuAdapter", "AI", "SiliconFlow", "TikuManual"]
class CacheDAO:
"""
@Author: SocialSisterYi
@Reference: https://github.com/SocialSisterYi/xuexiaoyi-to-xuexitong-tampermonkey-proxy
"""
DEFAULT_CACHE_FILE = "cache.json"
def __init__(self, file: str = DEFAULT_CACHE_FILE):
self.cache_file = Path(file)
self._lock = threading.RLock()
if not self.cache_file.is_file():
self._write_cache({})
def _read_cache(self) -> dict:
# 新增缓存文件读取的异常处理
try:
with self._lock:
if not self.cache_file.is_file():
return {}
try:
with self.cache_file.open("r", encoding="utf8") as fp:
return json.load(fp)
except json.JSONDecodeError as e:
logger.error(f"缓存文件 JSON 解析失败: {e}, 尝试恢复...")
# 尝试从原始二进制中以 utf-8 忽略错误地恢复有效 JSON 段
try:
raw = self.cache_file.read_bytes()
text = raw.decode("utf-8", errors="ignore")
start = text.find('{')
end = text.rfind('}')
if start != -1 and end != -1 and start < end:
try:
return json.loads(text[start:end+1])
except Exception:
pass
except Exception:
pass
# 若无法恢复,备份损坏文件并返回空缓存
try:
bak_name = f"{self.cache_file.name}.bak.{int(time.time())}"
bak_path = self.cache_file.with_name(bak_name)
shutil.copy2(self.cache_file, bak_path)
logger.error(f"缓存文件已损坏,已备份为: {bak_path},将使用空缓存继续运行")
except Exception as ex:
logger.error(f"备份损坏缓存失败: {ex}")
return {}
except UnicodeDecodeError as e:
logger.error(f"缓存文件编码读取失败: {e}, 采用恢复策略...")
try:
raw = self.cache_file.read_bytes()
text = raw.decode("utf-8", errors="ignore")
start = text.find('{')
end = text.rfind('}')
if start != -1 and end != -1 and start < end:
try:
return json.loads(text[start:end+1])
except Exception:
pass
except Exception:
pass
try:
bak_name = f"{self.cache_file.name}.bak.{int(time.time())}"
bak_path = self.cache_file.with_name(bak_name)
shutil.copy2(self.cache_file, bak_path)
logger.error(f"缓存文件编码错误,已备份为: {bak_path},将使用空缓存继续运行")
except Exception as ex:
logger.error(f"备份损坏缓存失败: {ex}")
return {}
except Exception as e:
logger.error(f"读取缓存异常: {e}")
return {}
def _write_cache(self, data: dict) -> None:
# 为缓存写入加锁,防止并发写入损坏文件
try:
with self._lock:
parent = self.cache_file.parent
if not parent.exists():
parent.mkdir(parents=True, exist_ok=True)
# 写入临时文件后原子替换,减少并发写入时的损坏风险
fd, tmp_path = tempfile.mkstemp(prefix=self.cache_file.name, dir=str(parent))
try:
with os.fdopen(fd, "w", encoding="utf8") as fp:
json.dump(data, fp, ensure_ascii=False, indent=4)
fp.flush()
os.fsync(fp.fileno())
os.replace(tmp_path, str(self.cache_file))
except Exception as e:
# 清理临时文件
try:
if os.path.exists(tmp_path):
os.remove(tmp_path)
except Exception:
pass
logger.error(f"Failed to write cache atomically: {e}")
except IOError as e:
logger.error(f"Failed to write cache: {e}")
def get_cache(self, question: str) -> Optional[str]:
data = self._read_cache()
return data.get(question)
def add_cache(self, question: str, answer: str) -> None:
# 为缓存写入加锁,防止并发写入损坏文件
with self._lock:
data = self._read_cache()
data[question] = answer
self._write_cache(data)
class Tiku(ABC):
CONFIG_PATH = os.path.join(os.getcwd(), "config.ini")
DISABLE = False # 停用标志
SUBMIT = False # 提交标志
COVER_RATE = 0.8 # 覆盖率
true_list = []
false_list = []
def __init__(self, config_path: Optional[str] = None) -> None:
self._name = None
self._api = None
self._conf = None
self._config_path = config_path or self.CONFIG_PATH
@property
def name(self):
return self._name
@name.setter
def name(self, value):
self._name = value
@property
def api(self):
return self._api
@api.setter
def api(self, value):
self._api = value
@property
def token(self):
return self._token
@token.setter
def token(self, value):
self._token = value
def init_tiku(self):
# 仅用于题库初始化, 应该在题库载入后作初始化调用, 随后才可以使用题库
# 尝试根据配置文件设置提交模式
if not self._conf:
self.config_set(self._get_conf())
if not self.DISABLE:
# 设置提交模式
self.SUBMIT = True if self._conf['submit'] == 'true' else False
self.COVER_RATE = float(self._conf['cover_rate'])
self.true_list = self._conf['true_list'].split(',')
self.false_list = self._conf['false_list'].split(',')
# 调用自定义题库初始化
self._init_tiku()
def _init_tiku(self):
# 仅用于题库初始化, 例如配置token, 交由自定义题库完成
pass
def config_set(self,config):
self._conf = config
def _get_conf(self):
"""
从默认配置文件查询配置, 如果未能查到, 停用题库
"""
try:
config = configparser.ConfigParser()
config.read(self._config_path, encoding="utf8")
return config['tiku']
except (KeyError, FileNotFoundError):
logger.info("未找到tiku配置, 已忽略题库功能")
self.DISABLE = True
return None
def query(self,q_info:dict) -> Optional[str]:
if self.DISABLE:
return None
is_manual = (
getattr(self, 'is_manual', False) or
self.__class__.__name__ == 'TikuManual' or
(self.__class__.__name__ == 'TikuFallback' and any(getattr(p, 'is_manual', False) or p.__class__.__name__ == 'TikuManual' for p in getattr(self, 'providers', [])))
)
# 预处理, 去除【单选题】这样与标题无关的字段
if not is_manual:
logger.debug(f"原始标题:{q_info['title']}")
q_info['title'] = sub(r'^\d+', '', q_info['title'])
q_info['title'] = sub(r'(\d+\.\d+分)$', '', q_info['title'])
if not is_manual:
logger.debug(f"处理后标题:{q_info['title']}")
# 先过缓存
cache_dao = CacheDAO()
answer = cache_dao.get_cache(q_info['title'])
if answer:
logger.info(f"从缓存中获取答案:{q_info['title']} -> {answer}")
return answer.strip()
else:
answer = self._query(q_info)
if answer:
answer = answer.strip()
logger.info(f"从{self.name}获取答案:{q_info['title']} -> {answer}")
if check_answer(answer, q_info['type'], self):
cache_dao.add_cache(q_info['title'], answer)
return answer
else:
logger.info(f"从{self.name}获取到的答案类型与题目类型不符,已舍弃")
return None
logger.error(f"从{self.name}获取答案失败:{q_info['title']}")
return None
def query_all(self, q_list: list[dict], query_delay: float = 0.0) -> list[Optional[str]]:
if self.DISABLE:
return [None] * len(q_list)
is_manual = (
getattr(self, 'is_manual', False) or
self.__class__.__name__ == 'TikuManual' or
(self.__class__.__name__ == 'TikuFallback' and any(getattr(p, 'is_manual', False) or p.__class__.__name__ == 'TikuManual' for p in getattr(self, 'providers', [])))
)
results = [None] * len(q_list)
pending_indices = []
cache_dao = CacheDAO()
for idx, q in enumerate(q_list):
if not is_manual:
logger.debug(f"原始标题:{q['title']}")
q['title'] = sub(r'^\d+', '', q['title'])
q['title'] = sub(r'(\d+\.\d+分)$', '', q['title'])
if not is_manual:
logger.debug(f"处理后标题:{q['title']}")
answer = cache_dao.get_cache(q['title'])
if answer:
logger.info(f"从缓存中获取答案:{q['title']} -> {answer}")
results[idx] = answer.strip()
else:
pending_indices.append(idx)
if not pending_indices:
return results
sub_q_list = [q_list[idx] for idx in pending_indices]
sub_results = self._query_all(sub_q_list, query_delay=query_delay)
if not isinstance(sub_results, list):
logger.error(f"{self.name} _query_all 返回结果格式异常,期望列表")
sub_results = [None] * len(pending_indices)
elif len(sub_results) != len(pending_indices):
logger.error(f"{self.name} _query_all 返回结果长度不匹配,期望 {len(pending_indices)},实际 {len(sub_results)}")
# 补齐或截断 sub_results 防止错位
sub_results = list(sub_results) + [None] * (len(pending_indices) - len(sub_results))
sub_results = sub_results[:len(pending_indices)]
for idx, ans in zip(pending_indices, sub_results):
q_info = q_list[idx]
if ans:
ans = ans.strip()
logger.info(f"从{self.name}获取答案:{q_info['title']} -> {ans}")
if check_answer(ans, q_info['type'], self):
cache_dao.add_cache(q_info['title'], ans)
results[idx] = ans
else:
logger.info(f"从{self.name}获取到的答案类型与题目类型不符,已舍弃")
else:
logger.error(f"从{self.name}获取答案失败:{q_info['title']}")
return results
@abstractmethod
def _query(self, q_info:dict) -> Optional[str]:
"""
查询接口, 交由自定义题库实现
"""
pass
def _query_all(self, q_list: list[dict], query_delay: float = 0.0) -> list[Optional[str]]:
"""
批量查询的实现接口,默认循环调用单个查询 _query。
子类若有批量查询或交互需求(如手动模式),可重写此方法。
"""
results = []
for q in q_list:
if query_delay > 0:
time.sleep(query_delay)
try:
results.append(self._query(q))
except Exception as e:
logger.error(f"{self.name} 查询单个题目发生异常: {e}")
results.append(None)
return results
@staticmethod
def get_tiku_from_config(config: Optional[dict] = None, config_path: Optional[str] = None):
"""
从配置文件加载题库, 这个配置可以是用户提供, 可以是默认配置文件
"""
conf = config
path = config_path or Tiku.CONFIG_PATH
if not conf:
# 尝试从默认配置文件加载
try:
config_parser = configparser.ConfigParser()
config_parser.read(path, encoding="utf8")
conf = config_parser['tiku']
except (KeyError, FileNotFoundError):
logger.error("未找到题库配置, 已忽略题库功能")
dummy = DummyTiku(config_path=path)
return dummy
try:
cls_name = conf['provider']
if not cls_name:
raise KeyError
except KeyError:
logger.error("未找到题库配置, 已忽略题库功能")
dummy = DummyTiku(config_path=path)
return dummy
providers = [name.strip() for name in cls_name.split(',') if name.strip()]
if not providers:
logger.error("题库provider配置为空, 已忽略题库功能")
dummy = DummyTiku(config_path=path)
return dummy
invalid_providers = [name for name in providers if name not in PROVIDER_REGISTRY]
if invalid_providers:
logger.error(f"题库provider配置无效: {', '.join(invalid_providers)}")
dummy = DummyTiku(config_path=path)
return dummy
if len(providers) == 1:
provider_cls = PROVIDER_REGISTRY[providers[0]]
if not isinstance(provider_cls, type) or not issubclass(provider_cls, Tiku):
logger.error(f"题库provider配置无效: {providers[0]}")
dummy = DummyTiku(config_path=path)
return dummy
new_cls = provider_cls(config_path=path)
new_cls.config_set(conf)
return new_cls
chain_providers = []
for provider_name in providers:
provider_cls = PROVIDER_REGISTRY[provider_name]
if not isinstance(provider_cls, type) or not issubclass(provider_cls, Tiku):
logger.error(f"题库provider配置无效: {provider_name}")
dummy = DummyTiku(config_path=path)
return dummy
provider = provider_cls(config_path=path)
provider.config_set(conf)
chain_providers.append(provider)
fallback = TikuFallback(chain_providers, config_path=path)
fallback.config_set(conf)
return fallback
def judgement_select(self, answer: str) -> bool:
"""
这是一个专用的方法, 要求配置维护两个选项列表, 一份用于正确选项, 一份用于错误选项, 以应对题库对判断题答案响应的各种可能的情况
它的作用是将获取到的答案answer与可能的选项列对比并返回对应的布尔值
"""
if self.DISABLE:
return False
# 对响应的答案作处理
answer = answer.strip().lower()
# 内置的高频通用判断词规整
if answer in ['true', 't', '1', '对', '正确', '√', '是', 'yes', 'y']:
return True
if answer in ['false', 'f', '0', '错', '错误', '×', '否', 'no', 'n', '不对', '不正确']:
return False
# 兼容自定义配置列表
if answer in [x.lower() for x in self.true_list] or answer in self.true_list:
return True
elif answer in [x.lower() for x in self.false_list] or answer in self.false_list:
return False
else:
# 无法判断, 随机选择
logger.error(f'无法判断答案 -> {answer} 对应的是正确还是错误, 请自行判断并加入配置文件重启脚本, 本次将会随机选择选项')
return random.choice([True,False])
def get_submit_params(self):
"""
这是一个专用方法, 用于根据当前设置的提交模式, 响应对应的答题提交API中的pyFlag值
"""
# 留空直接提交, 1保存但不提交
if self.SUBMIT:
return ""
else:
return "1"
def check_llm_connection(self) -> bool:
"""
检查大模型连接是否可用
默认返回 True(非大模型题库不需要检查)
"""
return True
class TikuFallback(Tiku):
# 多题库回退实现,按 provider 中配置顺序依次查询。
def __init__(self, providers=None, config_path: Optional[str] = None):
super().__init__(config_path)
self.name = '多题库回退'
self.providers = providers or []
def _init_tiku(self):
active = []
for provider in self.providers:
try:
provider.init_tiku()
if not provider.DISABLE:
active.append(provider)
except Exception as e:
logger.error(f'初始化题库 {provider.name} 失败: {e}')
self.providers = active
if not self.providers:
logger.error('多题库回退初始化失败: 没有可用题库')
self.DISABLE = True
else:
logger.info(f"多题库回退已启用,查询顺序: {', '.join([p.__class__.__name__ for p in self.providers])}")
def _query(self, q_info:dict) -> Optional[str]:
for provider in self.providers:
try:
answer = provider._query(q_info)
except Exception as e:
provider_id = f'{provider.name}({provider.__class__.__name__})'
logger.exception(f'{self.name} 查询时 {provider_id} 异常: {e}')
continue
if not answer:
logger.info(f'{provider.name} 未命中,回退到下一个题库')
continue
# 若当前题库返回答案但类型不符,则继续回退。
if check_answer(answer, q_info['type'], provider):
logger.info(f'{provider.name} 命中答案')
return answer
logger.info(f'{provider.name} 返回答案类型不符,回退到下一个题库')
return None
def _query_all(self, q_list: list[dict], query_delay: float = 0.0) -> list[Optional[str]]:
results = [None] * len(q_list)
pending_indices = list(range(len(q_list)))
for provider in self.providers:
if not pending_indices:
break
if provider.DISABLE:
continue
sub_q_list = [q_list[idx] for idx in pending_indices]
try:
sub_results = provider.query_all(sub_q_list, query_delay=query_delay)
except Exception as e:
provider_id = f'{provider.name}({provider.__class__.__name__})'
logger.exception(f'{self.name} 批量查询时 {provider_id} 异常: {e}')
continue
if not isinstance(sub_results, list):
logger.error(f"{provider.name} 批量查询返回数据格式异常(非列表),跳过该题库")
continue
if len(sub_results) != len(pending_indices):
logger.error(f"{provider.name} 批量查询返回结果长度({len(sub_results)})与请求题目数({len(pending_indices)})不匹配,跳过该题库以防答案错位")
continue
next_pending_indices = []
for sub_idx, (orig_idx, ans) in enumerate(zip(pending_indices, sub_results)):
if ans:
logger.info(f'{provider.name} 命中答案: {q_list[orig_idx]["title"]} -> {ans}')
results[orig_idx] = ans
else:
logger.info(f'{provider.name} 未命中或返回答案无效,将回退')
next_pending_indices.append(orig_idx)
pending_indices = next_pending_indices
return results
def check_llm_connection(self) -> bool:
for provider in self.providers:
if not provider.check_llm_connection():
logger.error(f'{provider.name} 连接检查失败')
return False
return True
# 按照以下模板实现更多题库
class TikuYanxi(Tiku):
# 言溪题库实现
def __init__(self, config_path: Optional[str] = None) -> None:
super().__init__(config_path)
self.name = '言溪题库'
self.api = 'https://tk.enncy.cn/query'
self._token = None
self._token_index = 0 # token队列计数器
self._times = 100 # 查询次数剩余, 初始化为100, 查询后校对修正
def _query(self,q_info:dict):
res = requests.get(
self.api,
params={
'question':q_info['title'],
'token': self._token,
# 'type':q_info['type'], #修复478题目类型与答案类型不符(不想写后处理了)
# 没用,就算有type和options,言溪题库还是可能返回类型不符,问了客服,type仅用于收集
},
verify=False
)
if res.status_code == 200:
res_json = res.json()
if not res_json['code']:
# 如果是因为TOKEN次数到期, 则更换token
if self._times == 0 or '次数不足' in res_json['data']['answer']:
logger.info(f'TOKEN查询次数不足, 将会更换并重新搜题')
self._token_index += 1
self.load_token()
# 重新查询
return self._query(q_info)
logger.error(f'{self.name}查询失败:\n\t剩余查询数{res_json["data"].get("times",f"{self._times}(仅参考)")}:\n\t消息:{res_json["message"]}')
return None
self._times = res_json["data"].get("times",self._times)
return res_json['data']['answer'].strip()
else:
logger.error(f'{self.name}查询失败:\n{res.text}')
return None
def load_token(self):
token_list = self._conf['tokens'].split(',')
if self._token_index == len(token_list):
# TOKEN 用完
logger.error('TOKEN用完, 请自行更换再重启脚本')
raise PermissionError(f'{self.name} TOKEN 已用完, 请更换')
self._token = token_list[self._token_index]
def _init_tiku(self):
self.load_token()
class TikuGo(Tiku):
# GO题(网课小工具题库)实现
def __init__(self, config_path: Optional[str] = None) -> None:
super().__init__(config_path)
self.name = 'GO题(网课小工具题库)'
self.api = 'https://q.icodef.com/wyn-nb?v=4'
self._headers = {
'Authorization': '',
'Content-Type': 'application/x-www-form-urlencoded'
}
self._request_lock = threading.Lock()
self._last_request_time = 0.0
self._min_interval = 1.0
self._retry_times = 3
self._retry_backoff = 1.2
def _sleep_for_next_request(self) -> None:
with self._request_lock:
now = time.time()
wait_time = max(0.0, self._last_request_time + self._min_interval - now)
self._last_request_time = now + wait_time
if wait_time > 0:
time.sleep(wait_time)
def _mark_request_finished(self) -> None:
with self._request_lock:
self._last_request_time = time.time()
def _request_question(self, question: str, attempt: int) -> Optional[requests.Response]:
try:
self._sleep_for_next_request()
res = requests.post(
self.api,
data={'question': question},
headers=self._headers,
verify=True,
timeout=15
)
self._mark_request_finished()
return res
except requests.exceptions.RequestException as e:
logger.error(f'{self.name}查询异常 ({attempt}/{self._retry_times}): {e}')
self._mark_request_finished()
return None
def _parse_response(self, res: requests.Response) -> Optional[dict]:
if res.status_code != 200:
logger.error(f'{self.name}查询失败: 状态码 {res.status_code}, 响应: {res.text}')
return None
try:
res_json = res.json()
except ValueError:
logger.error(f'{self.name}查询失败: 返回内容不是有效JSON, 响应: {res.text}')
return None
try:
code = int(str(res_json.get('code', '')).strip())
except ValueError:
code = 0
answer = str(res_json.get('data', '')).strip()
msg = str(res_json.get('msg', '')).strip()
raw_text = f'{answer} {msg}'
is_throttled = any(key in raw_text for key in ['流控限制', '速度太快', '并发限制', '忙不过来'])
return {
'code': code,
'answer': answer,
'msg': msg,
'is_throttled': is_throttled,
}
def _sleep_retry(self, attempt: int, reason: str, include_min_interval: bool = False) -> None:
if include_min_interval:
sleep_seconds = max(self._min_interval, self._retry_backoff * attempt)
else:
sleep_seconds = self._retry_backoff * attempt
logger.warning(f'{self.name}{reason},{sleep_seconds:.1f}s 后重试 ({attempt}/{self._retry_times})')
time.sleep(sleep_seconds)
@staticmethod
def _is_placeholder_answer(answer: str, msg: str) -> bool:
return '李恒雅' in answer or '李恒雅' in msg
def _query(self, q_info: dict):
title = q_info.get('title', '')
candidates = [
title,
re.sub(r'^【[^】]+】\s*', '', title).strip(),
re.sub(r'^\[[^\]]+\]\s*', '', title).strip(),
]
seen = set()
normalized_titles = []
for item in candidates:
if item and item not in seen:
seen.add(item)
normalized_titles.append(item)
for query_title in normalized_titles:
answer = self._query_once(query_title)
if answer:
return answer
return None
def _query_once(self, question: str) -> Optional[str]:
for attempt in range(1, self._retry_times + 1):
res = self._request_question(question, attempt)
if res is None:
if attempt < self._retry_times:
self._sleep_retry(attempt, '查询异常', include_min_interval=True)
continue
break
parsed = self._parse_response(res)
if not parsed:
return None
code = parsed['code']
answer = parsed['answer']
msg = parsed['msg']
is_throttled = parsed['is_throttled']
if code != 1:
if is_throttled and attempt < self._retry_times:
self._sleep_retry(attempt, '触发流控')
continue
logger.info(f"{self.name}未命中或失败: {msg or '未知错误'}")
return None
if not answer:
return None
# GO题库在未搜到时可能在 data/msg 中返回“李恒雅正在努力撰写中...”。
if self._is_placeholder_answer(answer, msg):
if is_throttled and attempt < self._retry_times:
self._sleep_retry(attempt, '命中流控提示')
continue
return None
return answer
return None
def _init_tiku(self):
self._headers['Authorization'] = self._conf.get('go_authorization', self._headers['Authorization'])
try:
min_interval = float(self._conf.get('go_min_interval', self._min_interval))
if min_interval < 0:
raise ValueError('go_min_interval must be non-negative')
self._min_interval = min_interval
except (TypeError, ValueError):
logger.warning(f'{self.name}配置 go_min_interval 无效,使用默认值 {self._min_interval}')
try:
retry_times = int(self._conf.get('go_retry_times', self._retry_times))
if retry_times < 1:
raise ValueError('go_retry_times must be >= 1')
self._retry_times = retry_times
except (TypeError, ValueError):
logger.warning(f'{self.name}配置 go_retry_times 无效,使用默认值 {self._retry_times}')
try:
retry_backoff = float(self._conf.get('go_retry_backoff', self._retry_backoff))
if retry_backoff < 0:
raise ValueError('go_retry_backoff must be non-negative')
self._retry_backoff = retry_backoff
except (TypeError, ValueError):
logger.warning(f'{self.name}配置 go_retry_backoff 无效,使用默认值 {self._retry_backoff}')
class TikuLike(Tiku):
# LIKE知识库实现 参考 https://www.datam.site/
def __init__(self, config_path: Optional[str] = None) -> None:
super().__init__(config_path)
self.name = 'LIKE知识库'
self.ver = '2.0.0' #对应官网API版本
self.query_api = 'https://app.datam.site/api/v1/query'
self.models_api = 'https://app.datam.site/api/v1/query/models'
self.balance_api = 'https://app.datam.site/api/v1/balance'
self.homepage = 'https://www.datam.site'
self._model = None
self._timeout = 300
self._retry = True
self._retry_times = 3
self._tokens = []
self._balance = {}
self._search = False
self._vision = True
self._count = 0
self._headers = {"Content-Type": "application/json"}
def _query(self, q_info:dict = None):
if not q_info:
logger.error("当前无题目信息,请检查")
return ""
q_info_map = {"single": "【单选题】", "multiple": "【多选题】", "completion": "【填空题】", "judgement": "【判断题】"}
q_info_prefix = q_info_map.get(q_info['type'], "【其他类型题目】")
options = ', '.join(q_info['options']) if isinstance(q_info['options'], list) else q_info['options']
question = f"{q_info_prefix}{q_info['title']}\n"
if q_info['type'] in ['single', 'multiple']:
question += f"选项为: {options}\n"
# 随机选择一个token进行查询
token = random.choice(self._tokens)
# 检查该token是否有余额
if self._balance.get(token, 0) <= 0:
logger.error(f'{self.name}当前Token查询次数不足: ...{token[-5:]}')
# 尝试选择其他有余额的token
available_tokens = [t for t in self._tokens if self._balance.get(t, 0) > 0]
if available_tokens:
token = random.choice(available_tokens)
else:
logger.error(f'{self.name}所有Token查询次数都不足')
return None
ans = None
try_times = 0
# 尝试查询,直到成功或达到重试次数
while not ans and self._retry and try_times < self._retry_times:
ans = self._query_single(token, question)
try_times += 1
if ans: # 如果查询成功,减少余额
self._balance[token] -= 1
logger.info(f'使用Token ...{token[-5:]} 查询成功,剩余次数: {self._balance[token]}')
break
elif try_times < self._retry_times:
logger.warning(f'使用Token ...{token[-5:]} 查询失败,进行第 {try_times + 1} 次重试...')
# 10次查询后更新余额
self._count = (self._count + 1) % 10
if self._count == 0:
self.update_times()
return ans
def _query_single(self, token: str = "", query: str = "") -> str:
"""
查询单个问题的答案
Args:
token: API访问令牌
query: 查询的问题内容
Returns:
查询到的答案,如果失败则返回None
"""
# 验证输入参数
if not token:
logger.error(f'{self.name}查询失败: 未提供有效的token')
return None
if not query:
logger.error(f'{self.name}查询失败: 查询内容为空')
return None
# 设置请求头
temp_headers = self._headers.copy()
temp_headers['Authorization'] = f'Bearer {token}'
# 准备请求数据
request_data = {
'query': query,
'model': self._model if self._model else '',
'search': self._search,
'vision': self._vision
}
# 发送API请求
try:
res = requests.post(
self.query_api,
json=request_data,
headers=temp_headers,
verify=False,
timeout=self._timeout # 添加超时设置
)
except requests.exceptions.Timeout:
logger.error(f'{self.name}查询超时: 请求超过300秒')
return None
except requests.exceptions.ConnectionError:
logger.error(f'{self.name}网络连接错误: 无法连接到API服务器')
return None
except requests.exceptions.RequestException as e:
logger.error(f'{self.name}查询异常: \n{e}')
return None
except Exception as e:
logger.error(f'{self.name}查询发生未知错误: \n{e}')
return None
# 处理HTTP响应
if res.status_code == 200:
return self._parse_response(res)
elif res.status_code == 401:
logger.error(f'{self.name}认证失败: 请检查Token是否正确或已过期')
elif res.status_code == 429:
logger.error(f'{self.name}请求过于频繁: 已达到API速率限制')
elif res.status_code == 500:
logger.error(f'{self.name}服务器内部错误: API服务暂时不可用')
elif res.status_code == 400:
logger.error(f'{self.name}请求参数错误: 请检查查询内容格式')
elif res.status_code == 403:
logger.error(f'{self.name}访问被拒绝: 可能是Token权限不足')
else:
logger.error(f'{self.name}查询失败: 状态码 {res.status_code}, 响应内容: \n{res.text}')
return None
def _parse_response(self, response):
"""
解析API响应
Args:
response: HTTP响应对象
Returns:
解析后的答案,如果解析失败则返回None
"""
try:
res_json = response.json()
except json.JSONDecodeError:
logger.error(f'{self.name}响应解析失败: 响应不是有效的JSON格式')
return None
except Exception as e:
logger.error(f'{self.name}响应解析异常: {e}')
return None
# 记录响应消息
msg = res_json.get('message', '')
if msg:
logger.info(f'{self.name}响应消息: {msg}')
results = res_json.get('results', {})
if not results or not isinstance(results, dict):
logger.error(f'{self.name}查询结果格式错误: API返回结果中results字段格式不正确')
return None
output = results.get('output', None)
if output is None or not isinstance(output, dict):
logger.error(f'{self.name}查询结果中output字段格式错误或不存在')
return None
q_type = output.get('questionType', None)
if q_type is None:
logger.error(f'{self.name}查询结果中questionType字段不存在')
return None
answer = output.get('answer', None)
if answer is None:
logger.error(f'{self.name}查询结果中answer字段不存在')
return None
# 根据题目类型提取答案
return self._extract_answer_by_type(q_type, answer)
def _extract_answer_by_type(self, q_type: str, answer: dict) -> str:
"""
根据题目类型提取答案
Args:
q_type: 题目类型
answer: 答案字典
Returns:
提取的答案文本
"""
if not isinstance(answer, dict):
logger.error(f'{self.name}答案格式错误: 不是有效的字典格式')
return None
if q_type == "CHOICE":
selected_options = answer.get('selectedOptions', None)
if selected_options is not None:
if isinstance(selected_options, list) and selected_options:
# 过滤掉None和空字符串
valid_options = [opt for opt in selected_options if opt is not None and str(opt).strip()]
if valid_options:
return '\n'.join(str(opt) for opt in valid_options)
else:
logger.error(f'{self.name}CHOICE类型题目没有有效的选项内容')
else:
logger.error(f'{self.name}CHOICE类型题目没有有效的选项内容')
else:
logger.error(f'{self.name}CHOICE类型题目缺少selectedOptions字段')
elif q_type == "FILL_IN_BLANK":
blanks = answer.get('blanks', None)
if blanks is not None:
if isinstance(blanks, list) and blanks:
# 过滤掉None和空字符串
valid_blanks = [blank for blank in blanks if blank is not None and str(blank).strip()]
if valid_blanks:
return "\n".join(str(blank) for blank in valid_blanks)
else:
logger.error(f'{self.name}FILL_IN_BLANK类型题目没有有效的填空内容')
else:
logger.error(f'{self.name}FILL_IN_BLANK类型题目没有有效的填空内容')
else:
logger.error(f'{self.name}FILL_IN_BLANK类型题目缺少blanks字段')
elif q_type == "JUDGMENT":
is_correct = answer.get('isCorrect', None)
if is_correct is not None:
return "正确" if is_correct else "错误"
else:
logger.error(f'{self.name}JUDGMENT类型题目缺少isCorrect字段')
else:
otherText = answer.get('otherText', None)
if otherText is not None:
return str(otherText)
else:
logger.error(f'{self.name}未知题目类型{q_type}且缺少otherText字段')
return None
def get_api_balance(self, token:str = ""):
if not token:
logger.error(f'{self.name}获取余额失败: 未提供有效的token')
return 0