LLM Sherpa
repository·main·Indexed 23 days ago
https://github.com/nlmatics/llmsherpaA library providing APIs to process complex documents into structured formats for RAG and LLM workflows. It features LayoutPDFReader for parsing PDFs with layout information, supporting smart chunking that preserves document structure, table extraction to HTML, and hierarchical section navigation. The library integrates with LlamaIndex for vector search and allows for self-hosting the back end service via the nlm-ingestor repository.
What's inside llmsherpa
- LLM Sherpa provides strategic APIs designed to accelerate large language model (LLM) use cases. It is primarily used for processing documents to make them suitable for LLM workflows, such as RAG (Retrieval Augmented Generation).
How LayoutPDFReader provides smart chunking
mainUnlike standard parsers,
LayoutPDFReaderpreserves document structure during chunking. This ensures:- List items and their preceding paragraphs are kept together.
- Table items are chunked together.
- Contextual information from section headers and nested headers is included in the text chunks.
This is particularly useful for RAG (Retrieval Augmented Generation) applications to maintain context within limited LLM window sizes.
Install llmsherpa
mainInstall the
llmsherpalibrary using pip.pip install llmsherpaSelf-hosting the llmsherpa back end service
mainThellmsherpaback end service is open-sourced under the Apache 2.0 License. Users are encouraged to spawn their own servers using thenlm-ingestorrepository, as the free and paid hosted servers may not always contain the latest code updates. You can run your own server using a Docker image from thenlm-ingestorrepository.Perform RAG with Smart Chunking
mainLLM Sherpa provides 'smart chunking' that maintains the integrity of related text. When iterating through
doc.chunks(), usechunk.to_context_text()to get text that includes contextual information from section headers and nested headers. This is ideal for building vector indices for Retrieval Augmented Generation (RAG).from llama_index.core import Document, VectorStoreIndex index = VectorStoreIndex([]) for chunk in doc.chunks(): # to_context_text() includes contextual information from section headers index.insert(Document(text=chunk.to_context_text(), extra_info={})) query_engine = index.as_query_engine() response = query_engine.query("list all the tasks that work with bart") print(response)Implement Vector Search and RAG with LlamaIndex
mainYou can integrate
LayoutPDFReaderwith LlamaIndex by iterating through the document chunks and inserting them into aVectorStoreIndex. Usechunk.to_context_text()to ensure the chunk includes hierarchical context.from llama_index.core import Document from llama_index.core import VectorStoreIndex index = VectorStoreIndex([]) for chunk in doc.chunks(): index.insert(Document(text=chunk.to_context_text(), extra_info={})) query_engine = index.as_query_engine() response = query_engine.query("list all the tasks that work with bart") print(response)Extract and summarize specific document sections
mainYou can navigate the document hierarchy using
doc.sections()to find specific headers. To extract the full content of a section including all its nested subsections, usesection.to_html(include_children=True, recurse=True). This HTML can then be passed to an LLM for targeted summarization.from IPython.core.display import display, HTML selected_section = None # find a section in the document by title for section in doc.sections(): if section.title == '3 Fine-tuning BART': selected_section = section break # use include_children=True and recurse=True to fully expand the section. # include_children only returns at one sublevel of children whereas recurse goes through all the descendants HTML(section.to_html(include_children=True, recurse=True))Read a PDF file with LayoutPDFReader
mainTo parse a PDF with layout information, initialize
LayoutPDFReaderwith the LLMSherpa API URL and use theread_pdfmethod. The method accepts either a URL or a local file path.from llmsherpa.readers import LayoutPDFReader llmsherpa_api_url = "https://readers.llmsherpa.com/api/document/developer/parseDocument?renderFormat=all" pdf_url = "https://arxiv.org/pdf/1910.13461.pdf" # also allowed is a file path e.g. /home/downloads/xyz.pdf pdf_reader = LayoutPDFReader(llmsherpa_api_url) doc = pdf_reader.read_pdf(pdf_url)Access the raw JSON output
mainIf you need to process the full parsed structure manually, you can access the complete JSON response returned by the LLMSherpa service via the
.jsonattribute on the document object.doc.jsonAnalyze tables using LLM prompts
mainAccess tables in a document using
doc.tables(). You can convert a specific table to HTML usingtable.to_html()and pass that string to an LLM to perform complex analysis or answer questions about the tabular data.from llama_index.llms import OpenAI context = doc.tables()[5].to_html() resp = OpenAI().complete(f"read this table and answer question: which model has the best performance on squad 2.0:\n{context}") print(resp.text)Analyze tables with LLMs
mainAccess tables in a document usingdoc.tables(). You can convert specific tables to HTML to provide structured context to an LLM for analysis.LayoutPDFReadersupports tables with nested headers and header rows.Access the raw JSON document
mainTo access the complete raw JSON response returned by the LLM Sherpa service, use the
.jsonattribute on the document object.doc.json