GuXiaopeng commited on
Commit
99b708a
·
verified ·
1 Parent(s): 8031ff8

Update core/pipeline_agent.py

Browse files
Files changed (1) hide show
  1. core/pipeline_agent.py +51 -20
core/pipeline_agent.py CHANGED
@@ -14,8 +14,13 @@ import logging
14
  import concurrent.futures
15
  from tenacity import retry, stop_after_attempt, wait_random_exponential, retry_if_exception_type
16
 
 
17
  logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s", handlers=[logging.StreamHandler(sys.stdout)])
18
 
 
 
 
 
19
  try:
20
  from langdetect import detect
21
  LANGDETECT_AVAILABLE = True
@@ -69,6 +74,8 @@ class SSciPipelineAgent:
69
  except:
70
  self.tokenizer = tiktoken.get_encoding("gpt2")
71
 
 
 
72
  def _extract_json_from_text(self, text):
73
  if not text:
74
  return None
@@ -93,7 +100,7 @@ class SSciPipelineAgent:
93
  else:
94
  return json.loads(text[start_list:end_list+1])
95
  except Exception as e:
96
- logging.error(f"❌ JSON 提取失败: {e}")
97
  return None
98
 
99
  def export_state(self):
@@ -117,6 +124,7 @@ class SSciPipelineAgent:
117
  self.citation_map = state_dict.get("citation_map", {})
118
  self.used_citations = set(state_dict.get("used_citations", []))
119
  self.global_memory = state_dict.get("global_memory", self.global_memory)
 
120
 
121
  def _calc_paragraph_count(self, target_words, lang_hint="en"):
122
  words_per_para = 100 if lang_hint in ["ru", "kk"] else 130
@@ -135,7 +143,7 @@ class SSciPipelineAgent:
135
  resp = requests.get(url, headers=headers, params=params, timeout=timeout)
136
 
137
  if resp.status_code == 429:
138
- logging.warning(f"⚠️ [API 限流] 触发 429 (Too Many Requests)。启动指数退避。URL: {url}")
139
  time.sleep(random.uniform(2.0, 5.0))
140
  resp.raise_for_status()
141
 
@@ -143,7 +151,7 @@ class SSciPipelineAgent:
143
  return resp
144
 
145
  if resp.status_code != 200:
146
- logging.error(f"❌ [API 错误] 状态码: {resp.status_code} | URL: {url}")
147
  resp.raise_for_status()
148
  return resp
149
 
@@ -166,11 +174,13 @@ class SSciPipelineAgent:
166
  hypo_matches = re.findall(r'(H\d+[:\s].*?)(?=\n|$)', text, re.IGNORECASE)
167
  if hypo_matches:
168
  self.global_memory["hypotheses"] = "\n".join(hypo_matches[:10])
 
169
 
170
  if any(kw in lower_sec for kw in ["method", "measure", "variable"]):
171
  var_matches = re.findall(r'([A-Z][a-z]+(?:\s+[A-Z][a-z]+)*)\s+(?:was|is|refers to)\s+([^\.]+\.)', text)
172
  if var_matches:
173
  self.global_memory["variables"] = "\n".join([f"- {v[0]}: {v[1][:100]}" for v in var_matches[:8]])
 
174
 
175
  if "model" in lower_sec or "figure" in lower_sec:
176
  model_match = re.search(r'(Figure\s*\d+[^\.]+\.)', text, re.IGNORECASE)
@@ -250,7 +260,9 @@ class SSciPipelineAgent:
250
  return full
251
 
252
  def call_deepseek(self, system_prompt, user_prompt, max_tokens=8000, temp=0.3, model_name="deepseek-reasoner"):
253
- logging.info(f"🚀 [DeepSeek API] 准备调用 {model_name}, 提示词预计 Tokens: {self._count_tokens(user_prompt)}")
 
 
254
  for attempt in range(3):
255
  with self.lock:
256
  self.ds_calls += 1
@@ -258,7 +270,7 @@ class SSciPipelineAgent:
258
  if attempt == 2 and model_name == "deepseek-reasoner":
259
  current_model = "deepseek-chat"
260
  temp = 0.3
261
- logging.warning("⚠️ 强制降级为 deepseek-chat 执行本节点!")
262
 
263
  payload = {
264
  "model": current_model,
@@ -270,10 +282,11 @@ class SSciPipelineAgent:
270
  payload["temperature"] = temp
271
 
272
  full_content = ""
 
273
  try:
274
  with requests.post(self.deepseek_api_url, headers={"Authorization": f"Bearer {self.ds_key}"}, json=payload, stream=True, timeout=self.timeouts["standard"]) as response:
275
  if response.status_code != 200:
276
- logging.error(f"❌ 尝试 {attempt+1}/3 失败: 状态码 {response.status_code}")
277
  if attempt == 2:
278
  raise requests.exceptions.HTTPError("API 熔断")
279
  time.sleep(3 ** attempt)
@@ -288,15 +301,19 @@ class SSciPipelineAgent:
288
  full_content += content
289
  except Exception:
290
  pass
 
 
 
291
  return full_content.strip()
292
  except Exception as e:
293
- logging.error(f"❌ [网络异常] {e}")
294
  if attempt == 2:
295
  raise e
296
  time.sleep(3 ** attempt)
297
 
298
  def stream_deepseek(self, system_prompt, user_prompt, max_tokens=8000, model_name="deepseek-reasoner"):
299
- logging.info(f"🌊 [DeepSeek API - Stream] 启动流式生成 | 模型: {model_name} | 预计 Tokens: {self._count_tokens(user_prompt)}")
 
300
 
301
  temp = 0.3 if model_name == "deepseek-chat" else 0.2
302
  for attempt in range(3):
@@ -306,7 +323,7 @@ class SSciPipelineAgent:
306
  if attempt >= 1 and model_name == "deepseek-reasoner":
307
  current_model = "deepseek-chat"
308
  temp = 0.3
309
- logging.warning("⚠️ 推理节点异常,强制降级至 deepseek-chat 并重试。")
310
 
311
  payload = {
312
  "model": current_model,
@@ -319,10 +336,12 @@ class SSciPipelineAgent:
319
 
320
  think_heartbeat = 0
321
  has_started_reasoning = False
 
322
 
323
  try:
324
  with requests.post(self.deepseek_api_url, headers={"Authorization": f"Bearer {self.ds_key}"}, json=payload, stream=True, timeout=self.timeouts["stream"]) as response:
325
  if response.status_code != 200:
 
326
  if attempt == 2:
327
  response.raise_for_status()
328
  time.sleep(2)
@@ -340,10 +359,14 @@ class SSciPipelineAgent:
340
  if not has_started_reasoning:
341
  yield "\n<think>\n"
342
  has_started_reasoning = True
 
343
 
344
  think_heartbeat += 1
345
- if think_heartbeat % 30 == 0:
346
- logging.info(f"🧠 [心跳探针] {current_model} 正在深度思考中... (Packet: {think_heartbeat})")
 
 
 
347
 
348
  yield reasoning_content
349
  continue
@@ -351,17 +374,20 @@ class SSciPipelineAgent:
351
  if has_started_reasoning and not reasoning_content and content:
352
  yield "\n</think>\n\n"
353
  has_started_reasoning = False
 
 
354
 
355
  if content:
356
  yield content
357
 
358
  except Exception:
359
  pass
 
360
  return
361
  except GeneratorExit:
362
  raise
363
  except Exception as e:
364
- logging.error(f"❌ [流式中断]: {e}")
365
  if attempt == 2:
366
  raise
367
  time.sleep(2)
@@ -399,6 +425,7 @@ class SSciPipelineAgent:
399
 
400
  for attempt in range(2):
401
  try:
 
402
  response = self.call_deepseek(sys_prompt, user_prompt, temp=0.1, model_name="deepseek-chat")
403
  papers = self._extract_json_from_text(response)
404
  if not papers:
@@ -440,6 +467,7 @@ class SSciPipelineAgent:
440
  self.used_citations.add(cite_tag)
441
 
442
  rag_context.append(f"{cite_tag} [USER CORE PAPER] Title: {title}. Core Thesis: {core_thesis}. Abstract: {abstract}")
 
443
  return "\n".join(rag_context)
444
  except Exception as e:
445
  time.sleep(2)
@@ -465,7 +493,6 @@ You MUST output your response EXACTLY using the following two delimiters. Do not
465
  base_directive = APP_CONFIG.get('anchor_generation_directive', '')
466
  user_prompt = f"Topic: {topic}\nKeywords: {keywords}\nLiterature Synthesis:\n{synthesis_result}\nCitation Context:\n{refs_text[:3000]}\n{base_directive}{format_mandate}"
467
 
468
- # Brain-Hand: Anchors need deep reasoning
469
  response = self.call_deepseek(sys_prompt, user_prompt, max_tokens=1500, temp=0.3, model_name="deepseek-reasoner")
470
 
471
  abstract_match = re.search(r'-+\s*ABSTRACT\s*-+\s*(.*?)(?=\s*-+\s*CONCLUSION\s*-+|$)', response, re.DOTALL | re.IGNORECASE)
@@ -480,7 +507,6 @@ You MUST output your response EXACTLY using the following two delimiters. Do not
480
  sys_prompt = "You are a senior academic journal reviewer. Design a strict empirical research paper outline."
481
  user_prompt = f'Topic: {topic}\nKeywords: {keywords}\nSynthesis:\n{synthesis_result}\nReturn ONLY a JSON: {{"en_topic": "...", "en_keywords": "...", "structure": [{{"section": "...", "weight": 0.15, "is_method_or_result": false}}]}}'
482
  for attempt in range(3):
483
- # Brain-Hand: Outline creation MUST use deep reasoning for strong logical foundation
484
  plan = self._extract_json_from_text(self.call_deepseek(sys_prompt, user_prompt, temp=0.2, model_name="deepseek-reasoner"))
485
  if plan and isinstance(plan, dict) and "structure" in plan:
486
  return plan
@@ -503,7 +529,7 @@ You MUST output your response EXACTLY using the following two delimiters. Do not
503
  if not queries_to_fetch:
504
  return ""
505
 
506
- logging.info(f"🔍 [Pre-fetch JIT] 生成了 {len(queries_to_fetch)} 个定向查询词: {queries_to_fetch}")
507
 
508
  with concurrent.futures.ThreadPoolExecutor(max_workers=self.lit_max_workers) as executor:
509
  futures = []
@@ -520,7 +546,7 @@ You MUST output your response EXACTLY using the following two delimiters. Do not
520
  self.citation_map[cite_tag] = {"in_text": res["in_text"], "ref": res["ref"], "id": res["id"]}
521
  jit_rag_texts.append(f"{cite_tag} {res['rag_text']}")
522
 
523
- logging.info(f"✅ [Pre-fetch JIT] 成功抓取并注入了 {len(jit_rag_texts)} 篇底层支撑文献。")
524
  return "\n".join(jit_rag_texts)
525
 
526
  def generate_search_queries(self, topic, keywords):
@@ -541,6 +567,7 @@ You MUST output your response EXACTLY using the following two delimiters. Do not
541
  time.sleep(random.uniform(0.5, 1.5))
542
  headers = {"x-api-key": str(self.s2_key).strip()} if self.s2_key else {}
543
  results = []
 
544
  try:
545
  resp = self._requests_get_with_retry(
546
  f"{self.s2_api_url}/paper/search",
@@ -549,6 +576,7 @@ You MUST output your response EXACTLY using the following two delimiters. Do not
549
  )
550
  if resp.status_code == 400: return results
551
  papers = resp.json().get("data", [])
 
552
  for paper in papers:
553
  title = paper.get("title")
554
  year = paper.get("year")
@@ -589,7 +617,7 @@ You MUST output your response EXACTLY using the following two delimiters. Do not
589
  "source": "s2"
590
  })
591
  except Exception as e:
592
- logging.warning(f"S2 Worker 异常 (term: {term}): {e}")
593
  return results
594
 
595
  def _oa_worker(self, term, page, filter_str):
@@ -597,6 +625,7 @@ You MUST output your response EXACTLY using the following two delimiters. Do not
597
  headers = {"User-Agent": f"mailto:ssci_researcher_{random.randint(10000, 99999)}@gmail.com"}
598
  if self.openalex_key: headers["Authorization"] = f"Bearer {self.openalex_key}"
599
  results = []
 
600
  try:
601
  resp = self._requests_get_with_retry(
602
  self.openalex_api_url,
@@ -604,6 +633,7 @@ You MUST output your response EXACTLY using the following two delimiters. Do not
604
  params={"search": term, "per-page": self.lit_per_page, "page": page, "filter": filter_str}
605
  )
606
  raw_items = resp.json().get("results", [])
 
607
  for item in raw_items:
608
  title = item.get("title")
609
  if not title or not self._is_english_dominant(title): continue
@@ -646,7 +676,7 @@ You MUST output your response EXACTLY using the following two delimiters. Do not
646
  "source": "oa"
647
  })
648
  except Exception as e:
649
- logging.warning(f"OA Worker 异常 (term: {term}): {e}")
650
  return results
651
 
652
  def fetch_references_parallel(self, topic, keywords, min_required=50, progress_callback=None):
@@ -663,6 +693,7 @@ You MUST output your response EXACTLY using the following two delimiters. Do not
663
  if expansion_phase == 0:
664
  search_terms = self.generate_search_queries(topic, keywords)
665
  else:
 
666
  sys_prompt = "You are an expert academic librarian. We must broaden the search scope."
667
  user_prompt = f"Topic: {topic}\nKeywords: {keywords}\nUsed Queries: {list(all_used_terms)}\nGenerate 3 NEW, BROADER English search queries. Return ONLY a JSON array of strings."
668
  response = self.call_deepseek(sys_prompt, user_prompt, temp=0.5, model_name="deepseek-chat")
@@ -676,7 +707,7 @@ You MUST output your response EXACTLY using the following two delimiters. Do not
676
  if not current_batch_terms: continue
677
  for t in current_batch_terms: all_used_terms.add(t)
678
 
679
- logging.info(f"🌐 [真·并发检索] 启动扩张阶段 {expansion_phase+1}, 任务下发至线程池...")
680
 
681
  with concurrent.futures.ThreadPoolExecutor(max_workers=self.lit_max_workers) as executor:
682
  futures = []
@@ -712,7 +743,7 @@ You MUST output your response EXACTLY using the following two delimiters. Do not
712
  if len(self.citation_map) >= self.lit_target_papers:
713
  break
714
 
715
- logging.info(f"✅ 并发检索结束。成功整合 {len(self.citation_map)} 篇有效文献。")
716
  return "\n".join(llm_rag_context)
717
 
718
  def resolve_citations(self, text, quarantine=False):
 
14
  import concurrent.futures
15
  from tenacity import retry, stop_after_attempt, wait_random_exponential, retry_if_exception_type
16
 
17
+ # 基础日志配置
18
  logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s", handlers=[logging.StreamHandler(sys.stdout)])
19
 
20
+ # 辅助函数:专门用于向 Hugging Face 容器输出强制刷新的彩色/醒目日志
21
+ def hf_log(msg):
22
+ print(f"[HF Container Log] {msg}", flush=True)
23
+
24
  try:
25
  from langdetect import detect
26
  LANGDETECT_AVAILABLE = True
 
74
  except:
75
  self.tokenizer = tiktoken.get_encoding("gpt2")
76
 
77
+ hf_log("✅ SSciPipelineAgent 初始化完成。三维检索节点与大语言模型已就绪。")
78
+
79
  def _extract_json_from_text(self, text):
80
  if not text:
81
  return None
 
100
  else:
101
  return json.loads(text[start_list:end_list+1])
102
  except Exception as e:
103
+ hf_log(f"❌ JSON 提取失败: {e}")
104
  return None
105
 
106
  def export_state(self):
 
124
  self.citation_map = state_dict.get("citation_map", {})
125
  self.used_citations = set(state_dict.get("used_citations", []))
126
  self.global_memory = state_dict.get("global_memory", self.global_memory)
127
+ hf_log(f"🔄 状态恢复成功: DS API={self.ds_calls}, 现有文献={len(self.citation_map)}")
128
 
129
  def _calc_paragraph_count(self, target_words, lang_hint="en"):
130
  words_per_para = 100 if lang_hint in ["ru", "kk"] else 130
 
143
  resp = requests.get(url, headers=headers, params=params, timeout=timeout)
144
 
145
  if resp.status_code == 429:
146
+ hf_log(f"⚠️ [API 限流] 触发 429 (Too Many Requests)。启动指数退避。URL: {url}")
147
  time.sleep(random.uniform(2.0, 5.0))
148
  resp.raise_for_status()
149
 
 
151
  return resp
152
 
153
  if resp.status_code != 200:
154
+ hf_log(f"❌ [API 错误] 状态码: {resp.status_code} | URL: {url}")
155
  resp.raise_for_status()
156
  return resp
157
 
 
174
  hypo_matches = re.findall(r'(H\d+[:\s].*?)(?=\n|$)', text, re.IGNORECASE)
175
  if hypo_matches:
176
  self.global_memory["hypotheses"] = "\n".join(hypo_matches[:10])
177
+ hf_log(f"🧠 [记忆更新] 从 {section_name} 中提取了假设 H1-Hn")
178
 
179
  if any(kw in lower_sec for kw in ["method", "measure", "variable"]):
180
  var_matches = re.findall(r'([A-Z][a-z]+(?:\s+[A-Z][a-z]+)*)\s+(?:was|is|refers to)\s+([^\.]+\.)', text)
181
  if var_matches:
182
  self.global_memory["variables"] = "\n".join([f"- {v[0]}: {v[1][:100]}" for v in var_matches[:8]])
183
+ hf_log(f"🧠 [记忆更新] 从 {section_name} 中提取了核心变量定义")
184
 
185
  if "model" in lower_sec or "figure" in lower_sec:
186
  model_match = re.search(r'(Figure\s*\d+[^\.]+\.)', text, re.IGNORECASE)
 
260
  return full
261
 
262
  def call_deepseek(self, system_prompt, user_prompt, max_tokens=8000, temp=0.3, model_name="deepseek-reasoner"):
263
+ tokens_est = self._count_tokens(user_prompt)
264
+ hf_log(f"🚀 [DeepSeek API - 静态请求] 模型: {model_name} | MaxTokens: {max_tokens} | 输入估算: {tokens_est} tokens")
265
+
266
  for attempt in range(3):
267
  with self.lock:
268
  self.ds_calls += 1
 
270
  if attempt == 2 and model_name == "deepseek-reasoner":
271
  current_model = "deepseek-chat"
272
  temp = 0.3
273
+ hf_log("⚠️ 强制降级为 deepseek-chat 执行本节点!")
274
 
275
  payload = {
276
  "model": current_model,
 
282
  payload["temperature"] = temp
283
 
284
  full_content = ""
285
+ start_time = time.time()
286
  try:
287
  with requests.post(self.deepseek_api_url, headers={"Authorization": f"Bearer {self.ds_key}"}, json=payload, stream=True, timeout=self.timeouts["standard"]) as response:
288
  if response.status_code != 200:
289
+ hf_log(f"❌ 尝试 {attempt+1}/3 失败: 状态码 {response.status_code} - {response.text[:200]}")
290
  if attempt == 2:
291
  raise requests.exceptions.HTTPError("API 熔断")
292
  time.sleep(3 ** attempt)
 
301
  full_content += content
302
  except Exception:
303
  pass
304
+
305
+ elapsed = time.time() - start_time
306
+ hf_log(f"✅ [DeepSeek API - 返回] 耗时: {elapsed:.2f}s | 输出长度: {len(full_content)} 字符")
307
  return full_content.strip()
308
  except Exception as e:
309
+ hf_log(f"❌ [网络异常] 尝试 {attempt+1}: {e}")
310
  if attempt == 2:
311
  raise e
312
  time.sleep(3 ** attempt)
313
 
314
  def stream_deepseek(self, system_prompt, user_prompt, max_tokens=8000, model_name="deepseek-reasoner"):
315
+ tokens_est = self._count_tokens(user_prompt)
316
+ hf_log(f"🌊 [DeepSeek API - 流式生成] 模型: {model_name} | 输入估算: {tokens_est} tokens")
317
 
318
  temp = 0.3 if model_name == "deepseek-chat" else 0.2
319
  for attempt in range(3):
 
323
  if attempt >= 1 and model_name == "deepseek-reasoner":
324
  current_model = "deepseek-chat"
325
  temp = 0.3
326
+ hf_log("⚠️ 推理节点异常,强制降级至 deepseek-chat 并重试。")
327
 
328
  payload = {
329
  "model": current_model,
 
336
 
337
  think_heartbeat = 0
338
  has_started_reasoning = False
339
+ start_time = time.time()
340
 
341
  try:
342
  with requests.post(self.deepseek_api_url, headers={"Authorization": f"Bearer {self.ds_key}"}, json=payload, stream=True, timeout=self.timeouts["stream"]) as response:
343
  if response.status_code != 200:
344
+ hf_log(f"❌ 流式尝试 {attempt+1}/3 失败: 状态码 {response.status_code}")
345
  if attempt == 2:
346
  response.raise_for_status()
347
  time.sleep(2)
 
359
  if not has_started_reasoning:
360
  yield "\n<think>\n"
361
  has_started_reasoning = True
362
+ hf_log(f"🤔 [R1 推理启动] 开始输出深度思考块...")
363
 
364
  think_heartbeat += 1
365
+
366
+ # 针对 Hugging Face 容器底层的实时心跳探针,直接 flush 到 stdout
367
+ if think_heartbeat % 25 == 0:
368
+ elapsed = time.time() - start_time
369
+ hf_log(f"⏳ [DeepSeek 心跳] {current_model} 思考中... ({elapsed:.1f}秒, 接收 {think_heartbeat} 块)")
370
 
371
  yield reasoning_content
372
  continue
 
374
  if has_started_reasoning and not reasoning_content and content:
375
  yield "\n</think>\n\n"
376
  has_started_reasoning = False
377
+ elapsed = time.time() - start_time
378
+ hf_log(f"💡 [R1 推理结束] 思考完成,总耗时 {elapsed:.1f}秒。开始流式输出正文...")
379
 
380
  if content:
381
  yield content
382
 
383
  except Exception:
384
  pass
385
+ hf_log(f"🏁 [流式生成结束] 总耗时 {(time.time() - start_time):.1f}秒。")
386
  return
387
  except GeneratorExit:
388
  raise
389
  except Exception as e:
390
+ hf_log(f"❌ [流式中断]: {e}")
391
  if attempt == 2:
392
  raise
393
  time.sleep(2)
 
425
 
426
  for attempt in range(2):
427
  try:
428
+ hf_log("📄 [本地解析] 正在榨取用户上传的核心文献...")
429
  response = self.call_deepseek(sys_prompt, user_prompt, temp=0.1, model_name="deepseek-chat")
430
  papers = self._extract_json_from_text(response)
431
  if not papers:
 
467
  self.used_citations.add(cite_tag)
468
 
469
  rag_context.append(f"{cite_tag} [USER CORE PAPER] Title: {title}. Core Thesis: {core_thesis}. Abstract: {abstract}")
470
+ hf_log(f"✅ [本地解析] 成功提取 {len(papers)} 篇本地文献元数据。")
471
  return "\n".join(rag_context)
472
  except Exception as e:
473
  time.sleep(2)
 
493
  base_directive = APP_CONFIG.get('anchor_generation_directive', '')
494
  user_prompt = f"Topic: {topic}\nKeywords: {keywords}\nLiterature Synthesis:\n{synthesis_result}\nCitation Context:\n{refs_text[:3000]}\n{base_directive}{format_mandate}"
495
 
 
496
  response = self.call_deepseek(sys_prompt, user_prompt, max_tokens=1500, temp=0.3, model_name="deepseek-reasoner")
497
 
498
  abstract_match = re.search(r'-+\s*ABSTRACT\s*-+\s*(.*?)(?=\s*-+\s*CONCLUSION\s*-+|$)', response, re.DOTALL | re.IGNORECASE)
 
507
  sys_prompt = "You are a senior academic journal reviewer. Design a strict empirical research paper outline."
508
  user_prompt = f'Topic: {topic}\nKeywords: {keywords}\nSynthesis:\n{synthesis_result}\nReturn ONLY a JSON: {{"en_topic": "...", "en_keywords": "...", "structure": [{{"section": "...", "weight": 0.15, "is_method_or_result": false}}]}}'
509
  for attempt in range(3):
 
510
  plan = self._extract_json_from_text(self.call_deepseek(sys_prompt, user_prompt, temp=0.2, model_name="deepseek-reasoner"))
511
  if plan and isinstance(plan, dict) and "structure" in plan:
512
  return plan
 
529
  if not queries_to_fetch:
530
  return ""
531
 
532
+ hf_log(f"🔍 [Pre-fetch JIT] 生成了 {len(queries_to_fetch)} 个定向检索词: {queries_to_fetch}")
533
 
534
  with concurrent.futures.ThreadPoolExecutor(max_workers=self.lit_max_workers) as executor:
535
  futures = []
 
546
  self.citation_map[cite_tag] = {"in_text": res["in_text"], "ref": res["ref"], "id": res["id"]}
547
  jit_rag_texts.append(f"{cite_tag} {res['rag_text']}")
548
 
549
+ hf_log(f"✅ [Pre-fetch JIT] 成功抓取并注入了 {len(jit_rag_texts)} 篇底层支撑文献。")
550
  return "\n".join(jit_rag_texts)
551
 
552
  def generate_search_queries(self, topic, keywords):
 
567
  time.sleep(random.uniform(0.5, 1.5))
568
  headers = {"x-api-key": str(self.s2_key).strip()} if self.s2_key else {}
569
  results = []
570
+ hf_log(f"📡 [S2 Worker] 正在 Semantic Scholar 检索: '{term}' (offset={offset})")
571
  try:
572
  resp = self._requests_get_with_retry(
573
  f"{self.s2_api_url}/paper/search",
 
576
  )
577
  if resp.status_code == 400: return results
578
  papers = resp.json().get("data", [])
579
+ hf_log(f"✅ [S2 Worker] '{term}' 检索完成,捕获 {len(papers)} 条原始数据")
580
  for paper in papers:
581
  title = paper.get("title")
582
  year = paper.get("year")
 
617
  "source": "s2"
618
  })
619
  except Exception as e:
620
+ hf_log(f"⚠️ S2 Worker 异常 (term: {term}): {e}")
621
  return results
622
 
623
  def _oa_worker(self, term, page, filter_str):
 
625
  headers = {"User-Agent": f"mailto:ssci_researcher_{random.randint(10000, 99999)}@gmail.com"}
626
  if self.openalex_key: headers["Authorization"] = f"Bearer {self.openalex_key}"
627
  results = []
628
+ hf_log(f"🌐 [OA Worker] 正在 OpenAlex 检索: '{term}' (page={page})")
629
  try:
630
  resp = self._requests_get_with_retry(
631
  self.openalex_api_url,
 
633
  params={"search": term, "per-page": self.lit_per_page, "page": page, "filter": filter_str}
634
  )
635
  raw_items = resp.json().get("results", [])
636
+ hf_log(f"✅ [OA Worker] '{term}' 检索完成,捕获 {len(raw_items)} 条原始数据")
637
  for item in raw_items:
638
  title = item.get("title")
639
  if not title or not self._is_english_dominant(title): continue
 
676
  "source": "oa"
677
  })
678
  except Exception as e:
679
+ hf_log(f"⚠️ OA Worker 异常 (term: {term}): {e}")
680
  return results
681
 
682
  def fetch_references_parallel(self, topic, keywords, min_required=50, progress_callback=None):
 
693
  if expansion_phase == 0:
694
  search_terms = self.generate_search_queries(topic, keywords)
695
  else:
696
+ hf_log(f"🔄 [文献检索] 阶段 {expansion_phase}: 数量不足 ({len(self.citation_map)}/{min_required}),正在扩大检索范围...")
697
  sys_prompt = "You are an expert academic librarian. We must broaden the search scope."
698
  user_prompt = f"Topic: {topic}\nKeywords: {keywords}\nUsed Queries: {list(all_used_terms)}\nGenerate 3 NEW, BROADER English search queries. Return ONLY a JSON array of strings."
699
  response = self.call_deepseek(sys_prompt, user_prompt, temp=0.5, model_name="deepseek-chat")
 
707
  if not current_batch_terms: continue
708
  for t in current_batch_terms: all_used_terms.add(t)
709
 
710
+ hf_log(f"🚀 [并发下发] 启动扩张阶段 {expansion_phase+1}, 关键词: {current_batch_terms}")
711
 
712
  with concurrent.futures.ThreadPoolExecutor(max_workers=self.lit_max_workers) as executor:
713
  futures = []
 
743
  if len(self.citation_map) >= self.lit_target_papers:
744
  break
745
 
746
+ hf_log(f"🎯 [并发检索结束] 成功入库并排重了 {len(self.citation_map)} 篇核心文献。")
747
  return "\n".join(llm_rag_context)
748
 
749
  def resolve_citations(self, text, quarantine=False):