|
| 1 | +# Running RAG-Anything in an Offline Environment |
| 2 | + |
| 3 | +This document explains a critical consideration for running the RAG-Anything project in an environment with no internet access. |
| 4 | + |
| 5 | +## The Network Dependency: `LightRAG` and `tiktoken` |
| 6 | + |
| 7 | +The `RAGAnything` core engine relies on the `LightRAG` library for its primary functionality. `LightRAG`, in turn, uses OpenAI's `tiktoken` library for text tokenization. |
| 8 | + |
| 9 | +By default, the `tiktoken` library has a network dependency. On its first use, it attempts to download tokenizer models from OpenAI's public servers (`openaipublic.blob.core.windows.net`). If the application is running in an offline or network-restricted environment, this download will fail, causing the `LightRAG` instance to fail to initialize. |
| 10 | + |
| 11 | +This results in an error similar to the following: |
| 12 | + |
| 13 | +``` |
| 14 | +Failed to initialize LightRAG instance: HTTPSConnectionPool(host='openaipublic.blob.core.windows.net', port=443): Max retries exceeded with url: /encodings/o200k_ba |
| 15 | +``` |
| 16 | + |
| 17 | +This dependency is indirect. The `RAG-Anything` codebase itself does not directly import or call `tiktoken`. The call is made from within the `lightrag` library. |
| 18 | + |
| 19 | +## The Solution: Using a Local `tiktoken` Cache |
| 20 | + |
| 21 | +To resolve this issue and enable fully offline operation, you must provide a local cache for the `tiktoken` models. This is achieved by setting the `TIKTOKEN_CACHE_DIR` environment variable. |
| 22 | + |
| 23 | +When this environment variable is set, `tiktoken` will look for its model files in the specified local directory instead of attempting to download them from the internet. |
| 24 | + |
| 25 | +### Steps to Implement the Solution: |
| 26 | + |
| 27 | +1. **Create a Model Cache:** In an environment *with* internet access, run a simple Python script to download and cache the necessary `tiktoken` models. |
| 28 | + |
| 29 | + ```python |
| 30 | + import tiktoken |
| 31 | + import os |
| 32 | + |
| 33 | + # Define the directory where you want to store the cache |
| 34 | + cache_dir = "./tiktoken_cache" |
| 35 | + if "TIKTOKEN_CACHE_DIR" not in os.environ: |
| 36 | + os.environ["TIKTOKEN_CACHE_DIR"] = cache_dir |
| 37 | + |
| 38 | + # Create the directory if it doesn't exist |
| 39 | + if not os.path.exists(cache_dir): |
| 40 | + os.makedirs(cache_dir) |
| 41 | + |
| 42 | + print("Downloading and caching tiktoken models...") |
| 43 | + tiktoken.get_encoding("cl100k_base") |
| 44 | + # tiktoken.get_encoding("p50k_base") |
| 45 | + |
| 46 | + print(f"tiktoken models have been cached in '{cache_dir}'") |
| 47 | + ``` |
| 48 | + |
| 49 | +2. **Deploy the Cache:** Copy the created `tiktoken_cache` directory to the machine where you will be running the `RAG-Anything` application. |
| 50 | + |
| 51 | +By following these steps, you can eliminate the network dependency and run the `RAG-Anything` project successfully in a fully offline environment. |
0 commit comments