howcloudy commited on
Commit
dc8f5ff
·
1 Parent(s): 37473fb

deploy: sync agent updates to Hugging Face Space

Browse files
__pycache__/__init__.cpython-313.pyc ADDED
Binary file (149 Bytes). View file
 
__pycache__/config.cpython-313.pyc ADDED
Binary file (4 kB). View file
 
__pycache__/graph.cpython-313.pyc ADDED
Binary file (5.38 kB). View file
 
__pycache__/main.cpython-313.pyc ADDED
Binary file (13.4 kB). View file
 
__pycache__/prompts.cpython-313.pyc ADDED
Binary file (17.6 kB). View file
 
__pycache__/state.cpython-313.pyc ADDED
Binary file (3.97 kB). View file
 
main.py CHANGED
@@ -117,6 +117,12 @@ async def _stream_graph_response(initial_state: AgentState, thread_id: str):
117
  config = _make_config(thread_id)
118
  final_state: dict = {}
119
 
 
 
 
 
 
 
120
  async for event in graph.astream_events(initial_state, config=config, version="v2"):
121
  kind = event.get("event", "")
122
 
@@ -128,9 +134,45 @@ async def _stream_graph_response(initial_state: AgentState, thread_id: str):
128
  node_name = metadata.get("langgraph_node")
129
  if node_name != "tutor":
130
  continue
 
 
 
 
131
  chunk = event.get("data", {}).get("chunk")
132
  if chunk and hasattr(chunk, "content") and chunk.content:
133
- yield _sse(json.dumps({"type": "chunk", "content": chunk.content}))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
 
135
  # 图执行完毕,取最终 state
136
  elif kind == "on_chain_end" and event.get("name") == "LangGraph":
 
117
  config = _make_config(thread_id)
118
  final_state: dict = {}
119
 
120
+ # 流式思考块 (Thinking Block) 实时过滤器的状态变量
121
+ accumulated = ""
122
+ has_thought = False
123
+ thought_ended = False
124
+ yielded_length = 0
125
+
126
  async for event in graph.astream_events(initial_state, config=config, version="v2"):
127
  kind = event.get("event", "")
128
 
 
134
  node_name = metadata.get("langgraph_node")
135
  if node_name != "tutor":
136
  continue
137
+ # 过滤掉总结调用的 stream,防止它污染前端的总览气泡
138
+ tags = event.get("tags", []) or []
139
+ if "document_summary" in tags:
140
+ continue
141
  chunk = event.get("data", {}).get("chunk")
142
  if chunk and hasattr(chunk, "content") and chunk.content:
143
+ accumulated += chunk.content
144
+
145
+ # A. 确定是否包含思考标签
146
+ if not has_thought and not thought_ended:
147
+ if "<thought>" in accumulated or "<think>" in accumulated:
148
+ has_thought = True
149
+ elif len(accumulated) >= 30:
150
+ # 积累了 30 个字仍无思考标签前缀,说明模型没有进入 Thinking 模式,直接放行
151
+ thought_ended = True
152
+
153
+ # B. 如果在思考模式中,检测结束标签并做分割
154
+ if has_thought and not thought_ended:
155
+ if "</thought>" in accumulated:
156
+ thought_ended = True
157
+ start_idx = accumulated.find("</thought>") + len("</thought>")
158
+ to_yield = accumulated[start_idx:]
159
+ if to_yield:
160
+ yield _sse(json.dumps({"type": "chunk", "content": to_yield}))
161
+ yielded_length = len(accumulated)
162
+ elif "</think>" in accumulated:
163
+ thought_ended = True
164
+ start_idx = accumulated.find("</think>") + len("</think>")
165
+ to_yield = accumulated[start_idx:]
166
+ if to_yield:
167
+ yield _sse(json.dumps({"type": "chunk", "content": to_yield}))
168
+ yielded_length = len(accumulated)
169
+
170
+ # C. 如果已经退出思考模式,正常流式输出
171
+ if thought_ended:
172
+ if len(accumulated) > yielded_length:
173
+ to_yield = accumulated[yielded_length:]
174
+ yield _sse(json.dumps({"type": "chunk", "content": to_yield}))
175
+ yielded_length = len(accumulated)
176
 
177
  # 图执行完毕,取最终 state
178
  elif kind == "on_chain_end" and event.get("name") == "LangGraph":
nodes/__pycache__/__init__.cpython-313.pyc ADDED
Binary file (155 Bytes). View file
 
nodes/__pycache__/anchor_locator.cpython-313.pyc ADDED
Binary file (12.2 kB). View file
 
nodes/__pycache__/decomposer.cpython-313.pyc ADDED
Binary file (4.39 kB). View file
 
nodes/__pycache__/memory_summarizer.cpython-313.pyc ADDED
Binary file (8.4 kB). View file
 
nodes/__pycache__/offtopic_eval.cpython-313.pyc ADDED
Binary file (2.27 kB). View file
 
nodes/__pycache__/tree_writer.cpython-313.pyc ADDED
Binary file (4.03 kB). View file
 
nodes/__pycache__/tutor.cpython-313.pyc ADDED
Binary file (3.68 kB). View file
 
nodes/tutor.py CHANGED
@@ -26,6 +26,13 @@ from agent.prompts import (
26
  from agent.state import AgentState
27
 
28
 
 
 
 
 
 
 
 
29
  def tutor_node(state: AgentState) -> dict:
30
  """LangGraph node:生成讲解 / 追问回答(完整文本,供非流式路径使用)"""
31
  document = state["document"]
@@ -54,13 +61,15 @@ def tutor_node(state: AgentState) -> dict:
54
  HumanMessage(content=str(content))
55
  ]
56
  try:
57
- summary_response = llm.invoke(summary_messages)
58
  summary_content = summary_response.content if hasattr(summary_response, "content") else str(summary_response)
59
  document_summary = str(summary_content).strip()
60
  except Exception:
61
  document_summary = document.get("title", "")
62
 
63
- return {"answer": str(content), "document_summary": document_summary}
 
 
64
 
65
  else:
66
  path = state.get("conversation_path", [])
@@ -75,4 +84,4 @@ def tutor_node(state: AgentState) -> dict:
75
  ]
76
  response = llm.invoke(messages)
77
  content = response.content if hasattr(response, "content") else str(response)
78
- return {"answer": str(content)}
 
26
  from agent.state import AgentState
27
 
28
 
29
+ def clean_thought_tags(text: str) -> str:
30
+ import re
31
+ text = re.sub(r"<thought>.*?</thought>", "", text, flags=re.DOTALL)
32
+ text = re.sub(r"<think>.*?</think>", "", text, flags=re.DOTALL)
33
+ return text.strip()
34
+
35
+
36
  def tutor_node(state: AgentState) -> dict:
37
  """LangGraph node:生成讲解 / 追问回答(完整文本,供非流式路径使用)"""
38
  document = state["document"]
 
61
  HumanMessage(content=str(content))
62
  ]
63
  try:
64
+ summary_response = llm.invoke(summary_messages, config={"tags": ["document_summary"]})
65
  summary_content = summary_response.content if hasattr(summary_response, "content") else str(summary_response)
66
  document_summary = str(summary_content).strip()
67
  except Exception:
68
  document_summary = document.get("title", "")
69
 
70
+ cleaned_content = clean_thought_tags(str(content))
71
+ cleaned_summary = clean_thought_tags(document_summary)
72
+ return {"answer": cleaned_content, "document_summary": cleaned_summary}
73
 
74
  else:
75
  path = state.get("conversation_path", [])
 
84
  ]
85
  response = llm.invoke(messages)
86
  content = response.content if hasattr(response, "content") else str(response)
87
+ return {"answer": clean_thought_tags(str(content))}
utils/__pycache__/security.cpython-313.pyc ADDED
Binary file (5.04 kB). View file