Marcio Cunha

How to use RAG with local embeddings without data leaks

Learn how to implement RAG and embeddings on local servers to ensure complete data privacy while eliminating reliance on external APIs.

Marcio Cunha•4 min
Also available in:EspañolPortuguês
Summary
  • Processing documents on local servers prevents sensitive information from traveling across public networks.
  • Using local embeddings converts texts into numerical representations without sending data to the cloud.
  • Open-source tools ensure sovereign control over artificial intelligence infrastructure.
  • Proper configuration of volumes and isolated networks prevents leaks from misconfigurations.
  • Keeping the pipeline offline reduces operational costs and ensures compliance with privacy laws.

Why protect your data when using Artificial Intelligence?

When deciding to implement artificial intelligence in corporate or personal projects, the first impulse is usually to contract a major cloud API. In practice, this means sending confidential documents, financial reports, and proprietary code to third-party servers. For anyone dealing with trade secrets or data protected by strict laws, this exposure is an unacceptable risk that can result in heavy fines and a loss of competitive advantage. The good news is that the current open-source ecosystem allows you to run this entire infrastructure in-house.

To achieve this level of isolation, we need to understand the concept of RAG, which stands for Retrieval-Augmented Generation. In practice, RAG acts like an ultra-fast librarian: when you ask a question, the system searches an internal database for relevant documents and delivers them along with the question for the language model to formulate the answer. Thus, the model doesn't need to guess facts; it simply reads what our internal base provided. When we combine this technique with tools we discussed when integrating local LLMs with RAG and embeddings in the backend, we create a fully sovereign and secure ecosystem.

What are embeddings and why process them locally?

For a computer to search texts by similarity, we need to turn words into numbers. This is where embeddings come in, which are numerical vectors capable of capturing the meaning of a sentence. If two sentences mean similar things, their generated numbers end up close together in a large mathematical map. If we use external services to generate these vectors, every word you type is sent and stored on outside servers, opening a massive security loophole in your workflow.

Processing embeddings locally means running small, specialized models directly on your own hardware, whether it is a robust desktop computer or a dedicated server in your company. Modern tools run compact models extremely efficiently, consuming few processing and memory resources. This precaution prevents any search pattern or snippet of your private documents from leaking to the internet, keeping the complete reading and vectorization cycle locked tight inside your network perimeter.

Setting up the isolated environment with containers

To ensure the system works without relying on external connections and remains easy to update, the best approach is to use containerization. With Docker, we package the language model, the vector database, and the application into isolated boxes that only talk to each other through a private internal network. Below, see a practical configuration example using a compose file to spin up a secure environment:

version: '3.8'
services:
  vector-db:
    image: chromadb/chroma:latest
    container_path: /chroma/chroma
    ports:
      - '8000:8000'
    networks:
      - ai-internal
  local-llm:
    image: ollama/ollama:latest
    ports:
      - '11434:11434'
    volumes:
      - ollama_data:/root/.ollama
    networks:
      - ai-internal
networks:
  ai-internal:
    internal: true
volumes:
  ollama_data:

This setup prevents any container from accessing the internet directly unless you explicitly configure an outbound route. To maintain long-term stability in this environment, it is also worth applying the infrastructure best practices we discussed when using Ansible for secure agentless configuration management, ensuring that package updates and security patches are applied without invasion risks.

Step-by-step guide to indexing documents offline

With the infrastructure running on your server, the next step is to feed the system with your text files, PDFs, or technical manuals. The process should be automated by a Python script that reads the files, breaks the content into smaller chunks, and sends them to the local embedding generator.

  1. Instantiate the client library pointing to your local server and the desired embedding model.
  2. Walk through the corporate document directory, securely extracting the text from each file.
  3. Generate vectors for each chunk using the internal API and save everything to the vector database without external traffic.

Executing these steps ensures your documents never leave the hard drive during the vectorization process, maintaining absolute confidentiality of the processed information. To explore these architectural concepts further in production, see our related technical analysis on How to migrate a monolith to microservices smoothly.

Final considerations on privacy and autonomy in AI

Adopting a RAG strategy with local embeddings requires an initial investment of time in hardware and software configuration, but the payoff in legal security and confidentiality makes every minute worthwhile. By eliminating dependence on major cloud providers to process confidential data, your organization gains complete autonomy over intellectual property and easily meets data protection regulatory requirements. Ultimately, artificial intelligence stops being an external risk factor and becomes a reliable, secure internal engine.