LLM Sherpa

repository·main·Indexed 23 days ago

https://github.com/nlmatics/llmsherpa

A 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.

Tokens
2.1K
Snippets
10
Records
14
Agent score
33%

What's inside llmsherpa

  1. Overview of LLM Sherpa

    main
    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).
  2. How LayoutPDFReader provides smart chunking

    main

    Unlike standard parsers, LayoutPDFReader preserves 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.

  3. Self-hosting the llmsherpa back end service

    main
    The llmsherpa back end service is open-sourced under the Apache 2.0 License. Users are encouraged to spawn their own servers using the nlm-ingestor repository, 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 the nlm-ingestor repository.
  4. Perform RAG with Smart Chunking

    main

    LLM Sherpa provides 'smart chunking' that maintains the integrity of related text. When iterating through doc.chunks(), use chunk.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)
  5. Implement Vector Search and RAG with LlamaIndex

    main

    You can integrate LayoutPDFReader with LlamaIndex by iterating through the document chunks and inserting them into a VectorStoreIndex. Use chunk.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)
  6. Extract and summarize specific document sections

    main

    You 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, use section.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))
  7. Read a PDF file with LayoutPDFReader

    main

    To parse a PDF with layout information, initialize LayoutPDFReader with the LLMSherpa API URL and use the read_pdf method. 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)
  8. Analyze tables using LLM prompts

    main

    Access tables in a document using doc.tables(). You can convert a specific table to HTML using table.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)