Overview

12 Using LLMs to Query Your Local Data

This chapter shows how to use large language models to query your own data while balancing privacy, cost, and capability. It frames two complementary strategies: retrieval-augmented question answering over local text documents, and prompting an LLM to generate executable code for analytics on structured data. The discussion emphasizes practical constraints such as context window limits, the need for chunking and semantic search to keep prompts compact and relevant, and the observation that LLMs excel at language tasks but are weaker at aggregations or full-dataset analysis without external tools.

The first approach builds a local retrieval pipeline with GPT4All and LangChain to keep sensitive content on-device. You load documents (for example, PDFs via PyPDFLoader, CSVs via CSVLoader, and JSON via JSONLoader with a jq schema), split them into chunks with RecursiveCharacterTextSplitter, and embed them locally using a Sentence-Transformers model (all-MiniLM-L6-v2) stored in a FAISS index that can be saved and reloaded. After downloading a GPT4All model such as mistral-7b-openorca, a prompt template and similarity_search over the index supply focused context for each question, supporting interactive Q&A across single or multiple files. Empirically, this works well for text extraction and entity-level questions, but it performs poorly for dataset-wide statistics because the LLM only sees the few retrieved chunks rather than the full table.

The second approach uses an LLM to write analysis code you then run locally, shifting heavy lifting to tools like pandas. Structured data is first normalized (for example, loading JSON and flattening it with json_normalize into a clear schema), then the prompt provides that schema and asks the model to return a single Python expression to evaluate or execute. In testing, a local Mistral 7B via GPT4All produced noisy or brittle snippets, whereas a hosted model like gpt-4o-mini reliably generated concise pandas expressions that handled substring searches, date filters, and even synonyms. The result is a practical split: rely on local RAG for privacy-preserving, text-centric Q&A, and use LLMs as code-writing assistants for analytics—executing their suggested queries in your own environment to maintain control over data and results.

The content of the PDF document contains an invoice of items purchased online
Posing a question and getting a response
The Titanic dataset (Titanic_train.csv)
Specifying the schema to load the JSON document
Directly loading the JSON file will result in a single-column dataframe
The dataframe now has multiple columns representing each of the keys in the JSON file
The LLM returned quite a fair bit of responses
The result returned by OpenAI is executed using the eval() function
LLM correctly returning the result
The result showing all the people born after 1980
The result shows the name of people born in the month of August

Summary

  • To load PDF documents, you can use the PyPDFLoader class from the document_loaders module in langchain.
  • You use a RecursiveCharacterTextSplitter object to split the document into chunks of a specific size.
  • For word embedding, you can use the sentence-transformers/all-MiniLM-L6-v2 model that is hosted on the HuggingFace Hub. You can then use the Faiss’s library to perform the embedding.
  • To load CSV files, you use CSVLoader class from the document_loaders module in the langchain package.
  • To load JSON files, you use the JSONLoader class from the document_loaders module in the langchain package. The JSONLoader class uses a specified jq schema to parse the JSON files.
  • LLMs are designed at understanding and generating human-like text and have limited ability in analyzing your data and summarizing them.
  • The primary obstacle to employing most local LLMs for analyzing large dataset lies in the limitation of context - most LLMs lack the necessary context size to process an entire document unless it is exceptionally short.
  • If your data originates from a text-based source, such as a PDF, you can employ word vector embedding on the data and subsequently utilize an LLM to directly interrogate it.
  • If your data is stored in a tabular format like CSV, JSON, Excel, etc., it is recommended to employ the LLM to generate Python-based queries for conducting analyses on your data, rather than directly querying the data with an LLM.

FAQ

How can I query my private documents without sending data to the cloud?Use a local RAG pipeline: load docs (PDF/CSV/JSON), split into chunks, embed locally with a sentence-transformers model, index with FAISS, and run a local GPT4All model (e.g., mistral-7b-openorca) to answer questions using only retrieved chunks. Embeddings and models stay on your machine.
Which packages do I need to set this up?- langchain (chains, loaders, prompts) - gpt4all (model + downloads) - faiss-cpu or faiss-gpu (vector index) - huggingface-hub + sentence-transformers (embeddings like all-MiniLM-L6-v2) - jq (for JSONLoader schemas) - pandas/json (for code-driven analytics) - openai (only if you choose a cloud model)
What is “chunking,” and how do I choose chunk size and overlap?Chunking splits long texts into smaller pieces that fit the model’s context window. A starting point is chunk_size ≈ 1024 characters and chunk_overlap ≈ 64. Increase size for more context per chunk; increase overlap to preserve continuity across boundaries. Tune based on accuracy and latency.
What are the high-level steps to build a PDF Q&A bot locally?- Load with PyPDFLoader and split with RecursiveCharacterTextSplitter - Create embeddings with HuggingFaceEmbeddings (all-MiniLM-L6-v2) - Build a FAISS index; optionally save it (index.faiss/index.pkl) - Download a GPT4All model (e.g., mistral-7b-openorca.Q4_0.gguf) - At query time: similarity_search(question, k) → build prompt with matched context → LLM generates answer
How do I scale to multiple PDFs?List files in your folder, create a PyPDFLoader per file, load_and_split each, aggregate all split Document objects, then embed and index them together in FAISS. Save the index once and reuse it for future queries.
Why do LLMs often give wrong answers on CSV/JSON analytics?RAG retrieves only the top-k rows similar to your query, so the LLM “sees” a small, possibly unrepresentative slice and then summarizes it. This works for text extraction but not for global counts/aggregations. Prefer asking the LLM to produce a Pandas query you execute on the full dataset.
What’s the difference between GPT4All from langchain vs the gpt4all package?The gpt4all package’s GPT4All class manages local model downloads/cache (~/.cache/gpt4all). LangChain’s GPT4All class wraps the local model for use in chains. Download the model first (gpt4all), then instantiate the LangChain LLM to avoid validation errors.
Which embedding model is used and why?sentence-transformers/all-MiniLM-L6-v2 (384-dim). It’s lightweight, fast, and provides good semantic search quality for retrieving relevant chunks from PDFs/CSV/JSON before prompting the LLM.
How do I load and query JSON with LangChain?Install jq, then use JSONLoader with a jq_schema (e.g., .laureates[]) to parse the JSON into Documents. Split, embed, and index just like PDFs. This is ideal for text-style lookups; for analytics, generate and run Pandas code instead.
When should I use a cloud model (OpenAI) versus a local model?- Local (GPT4All): maximum privacy, offline use, no API cost; may be weaker at code synthesis and complex reasoning. - Cloud (e.g., gpt-4o-mini): better code-writing and reasoning; incurs cost and requires sending prompts to the provider. Use cloud if you need stronger analytics/code generation; use local for sensitive data or offline scenarios.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Hugging Face in Action ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Hugging Face in Action ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Hugging Face in Action ebook for free