late-queen-51746
11/19/2025, 2:36 AMlimited-student-10747
11/20/2025, 2:00 PMelegant-branch-54423
11/20/2025, 4:35 PMlate-queen-51746
11/21/2025, 7:24 AMTOOL_DESCRIPTION = """Powerful tool for navigating and searching large engineering or scientific reference documents (PDFs, datasheets, textbooks, research reports, etc.).
Designed as an intelligent extension of the Unix `cat` command.
* Converts documents into structured, metadata-rich pages for reliable AI consumption.
* **VIEW MODE:** Retrieve overall document structure (Table of Contents, page count, metadata).
* **SEARCH MODE:** Find keywords or regex patterns and return structured page-level data. **Search is CASE SENSITIVE**
* **PAGE RETRIEVAL:** Directly fetch specific pages or ranges without performing a search.
* Accepts single pages (e.g., 5), lists (e.g., [1, 3, 7]), or ranges (e.g., '2-5').
* Mixed inputs are supported, such as ['1-3', 5, 8].
* Ignored if a search_pattern is provided.
* **Search Levels:**
* surface (default): Returns a concise summary of page and section metadata where matches occur โ ideal for quick scoping.
* deep: Returns the full, rich dictionary data for each matched page โ ideal for reasoning or downstream AI processing.
* Use this tool to efficiently extract precise information (e.g., register definitions, thermal limits, or circuit parameters) without scanning entire documents manually.
**CRITICAL PAGE LIMIT RESTRICTIONS:**
!! NEVER request results that span more than 10 pages in a single invocation. This causes context overload and system failure.
* **PAGE RETRIEVAL MODE:** Request maximum 10 pages total (e.g., pages [1,2,3,4,5,6,7,8,9,10] or range '1-10' is acceptable; '1-15' or [1,5,8,12,15,18,20,22,25,30] is NOT).
* **SEARCH LEVEL:** If search results exceed 10 pages:
- Use "surface" level first to scope matches
- Narrow your search pattern to be more specific
- Split into multiple targeted queries focusing on different sections/keywords
- Retrieve only the most relevant 10 pages maximum per call
* **Best Practice:** Always start with VIEW MODE or "surface" level search to understand document structure, then make focused queries for specific pages.
* **If you need information across >10 pages:** Make multiple sequential tool calls, each retrieving โค10 pages.
"""
I found this tool extremely useful for the agent to collect information from structured documents like datasheets and user manuals.late-queen-51746
11/21/2025, 9:49 AMmelodic-engine-73602
11/21/2025, 11:51 AMlate-queen-51746
11/21/2025, 4:00 PMdef extract_page_data(doc) -> List[Dict[str, Any]]:
"""
Extracts plain text, structured blocks, and metadata for each page.
Args:
doc: PyMuPDF Document object.
Returns:
List of dictionaries containing page details.
"""
# def sanitize_utf8(text: str) -> str:
# """
# Ensures text contains only valid UTF-8 characters.
# Replaces invalid characters with a placeholder.
# """
# if isinstance(text, bytes):
# # If we receive bytes, decode with error handling
# return text.decode('utf-8', errors='replace')
# # If it's already a string, encode and decode to catch any issues
# try:
# # This will catch any surrogate pairs or invalid Unicode
# text.encode('utf-8')
# return text
# except UnicodeEncodeError:
# # Replace invalid characters with the Unicode replacement character
# return text.encode('utf-8', errors='replace').decode('utf-8')
pages = []
full_text = ""
for i in range(doc.page_count):
page = doc.load_page(i)
page_number = i + 1
rect = page.rect
page_meta = {
"width": rect.width,
"height": rect.height,
"rotation": page.rotation,
"number": i + 1,
}
# Extract and sanitize page content
page_content_raw = page.get_text("text")
page_content = sanitize_utf8(page_content_raw)
marker = f"\n\n<<PAGE_BREAK:{page_number}>>\n\n"
start_index = len(full_text)
full_text += marker + page_content
end_index = len(full_text)
page_dict_text = page.get_text("dict")
pages.append({
"page_number": page_number,
"page_indices":{"start_index":start_index,"end_index":end_index},
"page_meta": page_meta,
"page_content": page_content,
"page_blocks": page_dict_text,
"toc_details": None,
})
return pages,full_text
Above code convert pymupdf document into the dictionary format of the COS tool. By replacing this single function, while keeping the output interface(the dictionary format) intact would enable us to integrate any other text extraction tool like deepseek-ocr.
TLDR; I think it's relatively easy to migrate to deepseek-ocr, if it provides enough details from the source document.late-queen-51746
11/27/2025, 1:52 AMelegant-branch-54423
11/27/2025, 10:02 AMwooden-umbrella-84098
11/27/2025, 6:29 PMlate-queen-51746
11/27/2025, 6:56 PM