Repository navigation
Expand file tree
/
Copy pathnodes.py
More file actions
364 lines (308 loc) · 12.1 KB
/
Copy pathnodes.py
File metadata and controls
364 lines (308 loc) · 12.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
import asyncio
from typing import Any, Dict, List, Literal, Optional
import numpy as np
import tiktoken
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
from langchain_core.runnables import RunnableConfig
from langgraph.graph import END
from langgraph.types import Command
from pydantic import BaseModel, Field
from sklearn.cluster import AgglomerativeClustering
from sklearn.metrics.pairwise import cosine_similarity
from backend.core.spdrag.prompts import (
LEAD_RESEARCHER_PROMPT,
RESEARCH_SYSTEM_PROMPT,
SYNTHESIS_PROMPT,
)
from backend.core.spdrag.state import (
AgentAction,
AgentState,
SubAgentInput,
Summary,
TodoItem,
)
from backend.core.tools.rag import search_specific_document_for_research
from backend.pipeline.vector import generate_embeddings
from backend.shared.constants import (
RESEARCH_LLM_FAST,
RESEARCH_LLM_REASONING,
get_synthesizer_token_limit_for_fast,
)
from backend.shared.logger import get_logger
logger = get_logger("SPDRAG")
_ENCODER: Optional[tiktoken.Encoding] = None
def _get_encoder() -> tiktoken.Encoding:
"""Lazily initialize and return a shared tiktoken encoder."""
global _ENCODER
if _ENCODER is None:
_ENCODER = tiktoken.get_encoding("cl100k_base")
return _ENCODER
def _estimate_tokens(text: str) -> int:
"""Estimate token count using cl100k_base tokenizer."""
if not text:
return 0
return len(_get_encoder().encode(text))
def _group_by_tokens(
texts: List[str], children: np.ndarray, target_tokens: int
) -> List[List[str]]:
"""Group texts into batches that respect the clustering hierarchy and token budget.
Traverses the UPGMA merge history (children_) from AgglomerativeClustering to form
the largest possible clusters whose combined token count does not exceed
target_tokens, preserving similarity-ordered batching for the synthesis layer.
Args:
texts: List of text chunks (e.g. sub-agent findings) to batch.
children: (N-1, 2) array of merge indices from sklearn AgglomerativeClustering.children_.
target_tokens: Maximum tokens per batch (typically from synthesizer context limit).
Returns:
List of batches; each batch is a list of strings to be summarized together.
"""
n_samples = len(texts)
node_tokens = {i: _estimate_tokens(texts[i]) for i in range(n_samples)}
node_indices = {i: [i] for i in range(n_samples)}
valid_roots = set(range(n_samples))
valid_nodes = set(range(n_samples))
for idx, (c1, c2) in enumerate(children):
new_node = n_samples + idx
c1, c2 = int(c1), int(c2)
is_merge_possible = (c1 in valid_nodes) and (c2 in valid_nodes)
if is_merge_possible:
combined_tokens = node_tokens[c1] + node_tokens[c2]
if combined_tokens <= target_tokens:
valid_nodes.add(new_node)
node_tokens[new_node] = combined_tokens
node_indices[new_node] = node_indices[c1] + node_indices[c2]
if c1 in valid_roots:
valid_roots.remove(c1)
if c2 in valid_roots:
valid_roots.remove(c2)
valid_roots.add(new_node)
batches = []
for root in valid_roots:
indices = node_indices[root]
batches.append([texts[i] for i in indices])
return batches
async def _summarize_batch_findings(
findings_batch: List[str],
root_query: str,
synthesis_directive: str = "",
) -> str:
"""Summarize one findings batch into a merged summary."""
batch_text = "\n\n---\n\n".join(findings_batch)
prompt_content = SYNTHESIS_PROMPT.format(
findings=batch_text,
query=root_query,
synthesis_directive=synthesis_directive,
)
response = await RESEARCH_LLM_REASONING.ainvoke([HumanMessage(content=prompt_content)])
return getattr(response, "content", str(response))
async def recursive_summarize_findings(
raw_findings: List[str],
root_query: str,
target_batch_tokens: Optional[int] = None,
synthesis_directive: str = "",
) -> str:
"""Recursively summarize findings using agglomerative clustering."""
if target_batch_tokens is None:
target_batch_tokens = get_synthesizer_token_limit_for_fast()
current_level: List[str] = list(raw_findings)
iteration = 0
while len(current_level) > 1:
iteration += 1
n = len(current_level)
logger.info(
"Recursive summarization iteration %s, %s chunk(s) remaining",
iteration,
n,
)
embeddings = np.array(await generate_embeddings(current_level), dtype=np.float32)
sim_matrix = cosine_similarity(embeddings)
dist_matrix = 1.0 - sim_matrix
np.fill_diagonal(dist_matrix, 0)
dist_matrix[dist_matrix < 0] = 0
clustering = AgglomerativeClustering(
n_clusters=1, linkage="average", metric="precomputed"
).fit(dist_matrix)
batches = _group_by_tokens(
current_level, clustering.children_, target_batch_tokens
)
# Ensure convergence even if clustering did not reduce batch count.
if len(batches) >= n:
batches = [current_level]
logger.info("Formed %s batch(es) for LLM synthesis", len(batches))
tasks = [
_summarize_batch_findings(
batch,
root_query=root_query,
synthesis_directive=synthesis_directive,
)
for batch in batches
if batch
]
current_level = list(await asyncio.gather(*tasks))
return current_level[0]
class WriteTodos(BaseModel):
"""Structured output from coordination layer to drive retrieval+synthesis."""
sub_agent_todos: List[TodoItem] = Field(
description=(
"Shared Instruction Set for all sub-agents. Each todo must include task "
"and status."
)
)
synthesis_directive: str = Field(
description="Guidance for synthesizer goal, priorities, and output structure."
)
async def orchestrator_node(state: AgentState, config: RunnableConfig) -> Dict[str, Any]:
"""Coordination layer: decompose query into sub-agent todos and directive."""
del config
messages = state["messages"]
todo_writer = RESEARCH_LLM_REASONING.with_structured_output(
WriteTodos, method="function_calling", include_raw=False
)
result = await todo_writer.ainvoke(
[{"role": "system", "content": LEAD_RESEARCHER_PROMPT}] + messages
)
logger.info("Synthesis directive created: %s", result.synthesis_directive)
logger.info("Generated %s sub-agent todo(s)", len(result.sub_agent_todos))
return {
"messages": [
AIMessage(
content=f"Prepared {len(result.sub_agent_todos)} research task(s) for sub-agents."
)
],
"sub_agent_todos": result.sub_agent_todos,
"synthesis_directive": result.synthesis_directive,
}
async def document_sub_agent_node(input_data: SubAgentInput) -> Dict[str, Any]:
"""Parallel retrieval layer: iterative document-scoped retrieval loop."""
safety_limit = 5
doc_name = input_data["document_name"]
todos_list = input_data.get("todos", [])
todos_str = (
"\n".join(f"{i + 1}. {t.task}" for i, t in enumerate(todos_list))
if todos_list
else "No specific sub-tasks provided."
)
system_prompt = RESEARCH_SYSTEM_PROMPT.format(file_name=doc_name)
messages: List = [
SystemMessage(content=system_prompt),
HumanMessage(
content=(
f"**Orchestrator Assigned Tasks**:\n{todos_str}\n\n"
"Begin your investigation. Issue SEARCH actions to retrieve "
"information, then FINALIZE once all tasks are covered."
)
),
]
action_extractor = RESEARCH_LLM_FAST.with_structured_output(
AgentAction, method="function_calling", include_raw=False
)
iteration = 0
while True:
iteration += 1
if iteration > safety_limit:
logger.error(
"[%s] Safety ceiling of %s iterations reached; forcing extraction.",
doc_name,
safety_limit,
)
messages.append(
HumanMessage(
content="You have used the maximum number of searches. You MUST finalize your findings now."
)
)
try:
action = await action_extractor.ainvoke(messages)
findings = action.findings or "Safety limit reached; partial findings only."
except Exception as exc:
logger.error("[%s] Forced finalization failed: %s", doc_name, exc)
findings = "Extraction failed after safety limit."
return {"global_context": [Summary(document_name=doc_name, findings=findings)]}
try:
action: AgentAction = await action_extractor.ainvoke(messages)
except Exception as exc:
logger.error(
"[%s] AgentAction parsing failed (iter %s): %s; aborting loop.",
doc_name,
iteration,
exc,
)
return {
"global_context": [
Summary(
document_name=doc_name,
findings="Action parsing failed; no findings extracted.",
)
]
}
logger.info(
"[%s] Iter %s action=%s reasoning=%s",
doc_name,
iteration,
action.action,
action.reasoning,
)
if action.action == "finalize":
if not action.findings:
logger.warning(
"[%s] Sub-agent finalized with empty findings (iter %s).",
doc_name,
iteration,
)
return {
"global_context": [
Summary(
document_name=doc_name,
findings=action.findings or "No findings extracted.",
)
]
}
if not action.query:
logger.warning(
"[%s] Sub-agent issued search with no query (iter %s).",
doc_name,
iteration,
)
messages.append(
HumanMessage(
content=(
"Your last action was 'search' but the `query` field was empty. "
"Please provide a specific search string, or finalize if you have gathered enough information."
)
)
)
continue
search_results = await search_specific_document_for_research.ainvoke(
{"query": action.query, "file_name": doc_name}
)
logger.info("[%s] Search query: %s", doc_name, action.query)
messages.append(
AIMessage(content=f"[SEARCH] Reasoning: {action.reasoning}\nQuery: {action.query}")
)
messages.append(
HumanMessage(content=f"Search results for '{action.query}':\n\n{search_results}")
)
async def synthesis_node(
state: AgentState, config: RunnableConfig
) -> Command[Literal[END]]:
"""Synthesis layer: merge all document summaries into final response."""
del config
global_context = state.get("global_context", [])
synthesis_directive = state.get("synthesis_directive", "")
root_query = ""
for msg in state.get("messages", []):
if isinstance(msg, HumanMessage):
root_query = getattr(msg, "content", "")
break
if global_context:
raw_findings_chunks: List[str] = [
f"Document: {summary.document_name}\nFindings:\n{summary.findings}"
for summary in global_context
]
merged_findings = await recursive_summarize_findings(
raw_findings_chunks,
root_query=root_query,
synthesis_directive=synthesis_directive,
)
else:
merged_findings = "No document findings available."
return Command(goto=END, update={"messages": [AIMessage(content=merged_findings)]})