File size: 22,407 Bytes
d520909 |
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 |
"""
DocumentAgent for SPARKNET
A ReAct-style agent for document intelligence tasks:
- Document parsing and extraction
- Field extraction with grounding
- Table and chart analysis
- Document classification
- Question answering over documents
"""
from typing import List, Dict, Any, Optional, Tuple
from dataclasses import dataclass
from enum import Enum
import json
import time
from loguru import logger
from .base_agent import BaseAgent, Task, Message
from ..llm.langchain_ollama_client import LangChainOllamaClient
from ..document.schemas.core import (
ProcessedDocument,
DocumentChunk,
EvidenceRef,
ExtractionResult,
)
from ..document.schemas.extraction import ExtractionSchema, ExtractedField
from ..document.schemas.classification import DocumentClassification, DocumentType
class AgentAction(str, Enum):
"""Actions the DocumentAgent can take."""
THINK = "think"
USE_TOOL = "use_tool"
ANSWER = "answer"
ABSTAIN = "abstain"
@dataclass
class ThoughtAction:
"""A thought-action pair in the ReAct loop."""
thought: str
action: AgentAction
tool_name: Optional[str] = None
tool_args: Optional[Dict[str, Any]] = None
observation: Optional[str] = None
evidence: Optional[List[EvidenceRef]] = None
@dataclass
class AgentTrace:
"""Full trace of agent execution for inspection."""
task: str
steps: List[ThoughtAction]
final_answer: Optional[Any] = None
confidence: float = 0.0
total_time_ms: float = 0.0
success: bool = True
error: Optional[str] = None
class DocumentAgent:
"""
ReAct-style agent for document intelligence tasks.
Implements the Think -> Tool -> Observe -> Refine loop
with inspectable traces and grounded outputs.
"""
# System prompt for ReAct reasoning
SYSTEM_PROMPT = """You are a document intelligence agent that analyzes documents
and extracts information with evidence.
You operate in a Think-Act-Observe loop:
1. THINK: Analyze what you need to do and what information you have
2. ACT: Choose a tool to use or provide an answer
3. OBSERVE: Review the tool output and update your understanding
Available tools:
{tool_descriptions}
CRITICAL RULES:
- Every extraction MUST include evidence (page, bbox, text snippet)
- If you cannot find evidence for a value, ABSTAIN rather than guess
- Always cite the source of information with page numbers
- For tables, analyze structure before extracting data
- For charts, describe what you see before extracting values
Output format for each step:
THOUGHT: <your reasoning>
ACTION: <tool_name or ANSWER or ABSTAIN>
ACTION_INPUT: <JSON arguments for tool, or final answer>
"""
# Available tools
TOOLS = {
"extract_text": {
"description": "Extract text from specific pages or regions",
"args": ["page_numbers", "region_bbox"],
},
"analyze_table": {
"description": "Analyze and extract structured data from a table region",
"args": ["page", "bbox", "expected_columns"],
},
"analyze_chart": {
"description": "Analyze a chart/graph and extract insights",
"args": ["page", "bbox"],
},
"extract_fields": {
"description": "Extract specific fields using a schema",
"args": ["schema", "context_chunks"],
},
"classify_document": {
"description": "Classify the document type",
"args": ["first_page_chunks"],
},
"search_text": {
"description": "Search for text patterns in the document",
"args": ["query", "page_range"],
},
}
def __init__(
self,
llm_client: LangChainOllamaClient,
memory_agent: Optional[Any] = None,
max_iterations: int = 10,
temperature: float = 0.3,
):
"""
Initialize DocumentAgent.
Args:
llm_client: LangChain Ollama client
memory_agent: Optional memory agent for context retrieval
max_iterations: Maximum ReAct iterations
temperature: LLM temperature for reasoning
"""
self.llm_client = llm_client
self.memory_agent = memory_agent
self.max_iterations = max_iterations
self.temperature = temperature
# Current document context
self._current_document: Optional[ProcessedDocument] = None
self._page_images: Dict[int, Any] = {}
logger.info(f"Initialized DocumentAgent (max_iterations={max_iterations})")
def set_document(
self,
document: ProcessedDocument,
page_images: Optional[Dict[int, Any]] = None,
):
"""
Set the current document context.
Args:
document: Processed document
page_images: Optional dict of page number -> image array
"""
self._current_document = document
self._page_images = page_images or {}
logger.info(f"Set document context: {document.metadata.document_id}")
async def run(
self,
task_description: str,
extraction_schema: Optional[ExtractionSchema] = None,
) -> Tuple[Any, AgentTrace]:
"""
Run the agent on a task.
Args:
task_description: Natural language task description
extraction_schema: Optional schema for structured extraction
Returns:
Tuple of (result, trace)
"""
start_time = time.time()
if not self._current_document:
raise ValueError("No document set. Call set_document() first.")
trace = AgentTrace(task=task_description, steps=[])
try:
# Build initial context
context = self._build_context(extraction_schema)
# ReAct loop
result = None
for iteration in range(self.max_iterations):
logger.debug(f"ReAct iteration {iteration + 1}")
# Generate thought and action
step = await self._generate_step(task_description, context, trace.steps)
trace.steps.append(step)
# Check for terminal actions
if step.action == AgentAction.ANSWER:
result = self._parse_answer(step.tool_args)
trace.final_answer = result
trace.confidence = self._calculate_confidence(trace.steps)
break
elif step.action == AgentAction.ABSTAIN:
trace.final_answer = {
"abstained": True,
"reason": step.thought,
}
trace.confidence = 0.0
break
elif step.action == AgentAction.USE_TOOL:
# Execute tool and get observation
observation, evidence = await self._execute_tool(
step.tool_name, step.tool_args
)
step.observation = observation
step.evidence = evidence
# Update context with observation
context += f"\n\nObservation from {step.tool_name}:\n{observation}"
trace.success = True
except Exception as e:
logger.error(f"Agent execution failed: {e}")
trace.success = False
trace.error = str(e)
trace.total_time_ms = (time.time() - start_time) * 1000
return trace.final_answer, trace
async def extract_fields(
self,
schema: ExtractionSchema,
) -> ExtractionResult:
"""
Extract fields from the document using a schema.
Args:
schema: Extraction schema defining fields
Returns:
ExtractionResult with extracted data and evidence
"""
task = f"Extract the following fields from this document: {', '.join(f.name for f in schema.fields)}"
result, trace = await self.run(task, schema)
# Build extraction result
data = {}
evidence = []
warnings = []
abstained = []
if isinstance(result, dict):
data = result.get("data", result)
# Collect evidence from trace
for step in trace.steps:
if step.evidence:
evidence.extend(step.evidence)
# Check for abstained fields
for field in schema.fields:
if field.name not in data and field.required:
abstained.append(field.name)
warnings.append(
f"Required field '{field.name}' not found with sufficient confidence"
)
return ExtractionResult(
data=data,
evidence=evidence,
warnings=warnings,
confidence=trace.confidence,
abstained_fields=abstained,
)
async def classify(self) -> DocumentClassification:
"""
Classify the document type.
Returns:
DocumentClassification with type and confidence
"""
task = "Classify this document into one of the standard document types (contract, invoice, patent, research_paper, report, letter, form, etc.)"
result, trace = await self.run(task)
# Parse classification result
doc_type = DocumentType.UNKNOWN
confidence = 0.0
if isinstance(result, dict):
type_str = result.get("document_type", "unknown")
try:
doc_type = DocumentType(type_str.lower())
except ValueError:
doc_type = DocumentType.OTHER
confidence = result.get("confidence", trace.confidence)
return DocumentClassification(
document_id=self._current_document.metadata.document_id,
primary_type=doc_type,
primary_confidence=confidence,
evidence=[e for step in trace.steps if step.evidence for e in step.evidence],
method="llm",
is_confident=confidence >= 0.7,
)
async def answer_question(self, question: str) -> Tuple[str, List[EvidenceRef]]:
"""
Answer a question about the document.
Args:
question: Natural language question
Returns:
Tuple of (answer, evidence)
"""
task = f"Answer this question about the document: {question}"
result, trace = await self.run(task)
answer = ""
evidence = []
if isinstance(result, dict):
answer = result.get("answer", str(result))
elif isinstance(result, str):
answer = result
# Collect evidence
for step in trace.steps:
if step.evidence:
evidence.extend(step.evidence)
return answer, evidence
def _build_context(self, schema: Optional[ExtractionSchema] = None) -> str:
"""Build initial context from document."""
doc = self._current_document
context_parts = [
f"Document: {doc.metadata.filename}",
f"Type: {doc.metadata.file_type}",
f"Pages: {doc.metadata.num_pages}",
f"Chunks: {len(doc.chunks)}",
"",
"Document content summary:",
]
# Add first few chunks as context
for chunk in doc.chunks[:10]:
context_parts.append(
f"[Page {chunk.page + 1}, {chunk.chunk_type.value}]: {chunk.text[:200]}..."
)
if schema:
context_parts.append("")
context_parts.append("Extraction schema:")
for field in schema.fields:
req = "required" if field.required else "optional"
context_parts.append(f"- {field.name} ({field.type.value}, {req}): {field.description}")
return "\n".join(context_parts)
async def _generate_step(
self,
task: str,
context: str,
previous_steps: List[ThoughtAction],
) -> ThoughtAction:
"""Generate the next thought-action step."""
# Build prompt
tool_descriptions = "\n".join(
f"- {name}: {info['description']}"
for name, info in self.TOOLS.items()
)
system_prompt = self.SYSTEM_PROMPT.format(tool_descriptions=tool_descriptions)
messages = [{"role": "system", "content": system_prompt}]
# Add task and context
user_content = f"TASK: {task}\n\nCONTEXT:\n{context}"
# Add previous steps
if previous_steps:
user_content += "\n\nPREVIOUS STEPS:"
for i, step in enumerate(previous_steps, 1):
user_content += f"\n\nStep {i}:"
user_content += f"\nTHOUGHT: {step.thought}"
user_content += f"\nACTION: {step.action.value}"
if step.tool_name:
user_content += f"\nTOOL: {step.tool_name}"
if step.observation:
user_content += f"\nOBSERVATION: {step.observation[:500]}..."
user_content += "\n\nNow generate your next step:"
messages.append({"role": "user", "content": user_content})
# Generate response
llm = self.llm_client.get_llm(complexity="complex", temperature=self.temperature)
from langchain_core.messages import HumanMessage, SystemMessage
lc_messages = [
SystemMessage(content=system_prompt),
HumanMessage(content=user_content),
]
response = await llm.ainvoke(lc_messages)
response_text = response.content
# Parse response
return self._parse_step(response_text)
def _parse_step(self, response: str) -> ThoughtAction:
"""Parse LLM response into ThoughtAction."""
thought = ""
action = AgentAction.THINK
tool_name = None
tool_args = None
lines = response.strip().split("\n")
current_section = None
for line in lines:
line = line.strip()
if line.startswith("THOUGHT:"):
current_section = "thought"
thought = line[8:].strip()
elif line.startswith("ACTION:"):
current_section = "action"
action_str = line[7:].strip().lower()
if action_str == "answer":
action = AgentAction.ANSWER
elif action_str == "abstain":
action = AgentAction.ABSTAIN
elif action_str in self.TOOLS:
action = AgentAction.USE_TOOL
tool_name = action_str
else:
action = AgentAction.USE_TOOL
tool_name = action_str
elif line.startswith("ACTION_INPUT:"):
current_section = "input"
input_str = line[13:].strip()
try:
tool_args = json.loads(input_str)
except json.JSONDecodeError:
tool_args = {"raw": input_str}
elif current_section == "thought":
thought += " " + line
elif current_section == "input":
try:
tool_args = json.loads(line)
except:
pass
return ThoughtAction(
thought=thought,
action=action,
tool_name=tool_name,
tool_args=tool_args,
)
async def _execute_tool(
self,
tool_name: str,
tool_args: Optional[Dict[str, Any]],
) -> Tuple[str, List[EvidenceRef]]:
"""Execute a tool and return observation."""
if not tool_args:
tool_args = {}
doc = self._current_document
evidence = []
try:
if tool_name == "extract_text":
return self._tool_extract_text(tool_args)
elif tool_name == "analyze_table":
return await self._tool_analyze_table(tool_args)
elif tool_name == "analyze_chart":
return await self._tool_analyze_chart(tool_args)
elif tool_name == "extract_fields":
return await self._tool_extract_fields(tool_args)
elif tool_name == "classify_document":
return self._tool_classify_document(tool_args)
elif tool_name == "search_text":
return self._tool_search_text(tool_args)
else:
return f"Unknown tool: {tool_name}", []
except Exception as e:
logger.error(f"Tool {tool_name} failed: {e}")
return f"Error executing {tool_name}: {e}", []
def _tool_extract_text(self, args: Dict[str, Any]) -> Tuple[str, List[EvidenceRef]]:
"""Extract text from pages or regions."""
doc = self._current_document
page_numbers = args.get("page_numbers", list(range(doc.metadata.num_pages)))
if isinstance(page_numbers, int):
page_numbers = [page_numbers]
texts = []
evidence = []
for page in page_numbers:
page_chunks = doc.get_page_chunks(page)
for chunk in page_chunks:
texts.append(f"[Page {page + 1}]: {chunk.text}")
evidence.append(EvidenceRef(
chunk_id=chunk.chunk_id,
page=chunk.page,
bbox=chunk.bbox,
source_type="text",
snippet=chunk.text[:100],
confidence=chunk.confidence,
))
return "\n".join(texts[:20]), evidence[:10]
async def _tool_analyze_table(self, args: Dict[str, Any]) -> Tuple[str, List[EvidenceRef]]:
"""Analyze a table region."""
page = args.get("page", 0)
doc = self._current_document
# Find table chunks
table_chunks = [c for c in doc.chunks if c.chunk_type.value == "table" and c.page == page]
if not table_chunks:
return "No table found on this page", []
# Use LLM to analyze table
table_text = table_chunks[0].text
llm = self.llm_client.get_llm(complexity="standard")
from langchain_core.messages import HumanMessage
prompt = f"Analyze this table and extract structured data as JSON:\n\n{table_text}"
response = await llm.ainvoke([HumanMessage(content=prompt)])
evidence = [EvidenceRef(
chunk_id=table_chunks[0].chunk_id,
page=page,
bbox=table_chunks[0].bbox,
source_type="table",
snippet=table_text[:200],
confidence=table_chunks[0].confidence,
)]
return response.content, evidence
async def _tool_analyze_chart(self, args: Dict[str, Any]) -> Tuple[str, List[EvidenceRef]]:
"""Analyze a chart region."""
page = args.get("page", 0)
doc = self._current_document
# Find chart/figure chunks
chart_chunks = [
c for c in doc.chunks
if c.chunk_type.value in ("chart", "figure") and c.page == page
]
if not chart_chunks:
return "No chart/figure found on this page", []
# If we have the image, use vision model
if page in self._page_images:
# TODO: Use vision model for chart analysis
pass
return f"Chart found on page {page + 1}: {chart_chunks[0].caption or 'No caption'}", []
async def _tool_extract_fields(self, args: Dict[str, Any]) -> Tuple[str, List[EvidenceRef]]:
"""Extract specific fields."""
schema_dict = args.get("schema", {})
doc = self._current_document
# Build context from chunks
context = "\n".join(c.text for c in doc.chunks[:20])
# Use LLM to extract
llm = self.llm_client.get_llm(complexity="complex")
from langchain_core.messages import HumanMessage, SystemMessage
system = "Extract the requested fields from the document. Output JSON with field names as keys."
user = f"Fields to extract: {json.dumps(schema_dict)}\n\nDocument content:\n{context}"
response = await llm.ainvoke([
SystemMessage(content=system),
HumanMessage(content=user),
])
return response.content, []
def _tool_classify_document(self, args: Dict[str, Any]) -> Tuple[str, List[EvidenceRef]]:
"""Classify document type based on first page."""
doc = self._current_document
first_page_chunks = doc.get_page_chunks(0)
text = " ".join(c.text for c in first_page_chunks[:5])
return f"First page content for classification:\n{text[:500]}", []
def _tool_search_text(self, args: Dict[str, Any]) -> Tuple[str, List[EvidenceRef]]:
"""Search for text in document."""
query = args.get("query", "").lower()
doc = self._current_document
matches = []
evidence = []
for chunk in doc.chunks:
if query in chunk.text.lower():
matches.append(f"[Page {chunk.page + 1}]: ...{chunk.text}...")
evidence.append(EvidenceRef(
chunk_id=chunk.chunk_id,
page=chunk.page,
bbox=chunk.bbox,
source_type="text",
snippet=chunk.text[:100],
confidence=chunk.confidence,
))
if not matches:
return f"No matches found for '{query}'", []
return f"Found {len(matches)} matches:\n" + "\n".join(matches[:10]), evidence[:10]
def _parse_answer(self, answer_input: Optional[Dict[str, Any]]) -> Any:
"""Parse the final answer from tool args."""
if not answer_input:
return None
if isinstance(answer_input, dict):
return answer_input
return {"answer": answer_input}
def _calculate_confidence(self, steps: List[ThoughtAction]) -> float:
"""Calculate overall confidence from trace."""
if not steps:
return 0.0
# Average evidence confidence
all_evidence = [e for s in steps if s.evidence for e in s.evidence]
if all_evidence:
return sum(e.confidence for e in all_evidence) / len(all_evidence)
return 0.5 # Default moderate confidence
|