Preload tiktoken encoding cache in Docker images#528
Conversation
Set TIKTOKEN_CACHE_DIR and warm cl100k_base during image build so runtime token counting does not need to download encoding data in offline environments.
There was a problem hiding this comment.
Code Review
This pull request configures a tiktoken cache directory and preloads the cl100k_base encoding in Dockerfile, Dockerfile-litellm, and Dockerfile-ollama-local. The reviewer suggested setting directory permissions to 777 to prevent permission issues for non-root users and preloading the o200k_base encoding to support newer models like GPT-4o in offline environments.
| RUN mkdir -p "$TIKTOKEN_CACHE_DIR" && \ | ||
| python -c "import tiktoken; tiktoken.get_encoding('cl100k_base')" |
There was a problem hiding this comment.
To ensure complete offline support and compatibility with non-root container execution, two improvements should be made here:
- Permissions: If the container runs as a non-root user (a security best practice), they may encounter a
PermissionErrorwhen accessing or writing to the cache directory (e.g., if they need to cache other encodings at runtime). Setting the directory permissions to777ensures compatibility with any runtime user. - GPT-4o Support (
o200k_base): Modern OpenAI models likegpt-4oandgpt-4o-miniuse theo200k_baseencoding. Preloading bothcl100k_baseando200k_baseensures that offline environments do not attempt to download encoding data at runtime for these newer models.
RUN mkdir -p "$TIKTOKEN_CACHE_DIR" && \
chmod -R 777 "$TIKTOKEN_CACHE_DIR" && \
python -c "import tiktoken; tiktoken.get_encoding('cl100k_base'); tiktoken.get_encoding('o200k_base')"
| RUN mkdir -p "$TIKTOKEN_CACHE_DIR" && \ | ||
| python -c "import tiktoken; tiktoken.get_encoding('cl100k_base')" |
There was a problem hiding this comment.
To ensure complete offline support and compatibility with non-root container execution, two improvements should be made here:
- Permissions: If the container runs as a non-root user (a security best practice), they may encounter a
PermissionErrorwhen accessing or writing to the cache directory (e.g., if they need to cache other encodings at runtime). Setting the directory permissions to777ensures compatibility with any runtime user. - GPT-4o Support (
o200k_base): Modern OpenAI models likegpt-4oandgpt-4o-miniuse theo200k_baseencoding. Preloading bothcl100k_baseando200k_baseensures that offline environments do not attempt to download encoding data at runtime for these newer models.
RUN mkdir -p "$TIKTOKEN_CACHE_DIR" && \
chmod -R 777 "$TIKTOKEN_CACHE_DIR" && \
python -c "import tiktoken; tiktoken.get_encoding('cl100k_base'); tiktoken.get_encoding('o200k_base')"
| RUN mkdir -p "$TIKTOKEN_CACHE_DIR" && \ | ||
| python -c "import tiktoken; tiktoken.get_encoding('cl100k_base')" |
There was a problem hiding this comment.
To ensure complete offline support and compatibility with non-root container execution, two improvements should be made here:
- Permissions: If the container runs as a non-root user (a security best practice), they may encounter a
PermissionErrorwhen accessing or writing to the cache directory (e.g., if they need to cache other encodings at runtime). Setting the directory permissions to777ensures compatibility with any runtime user. - GPT-4o Support (
o200k_base): Modern OpenAI models likegpt-4oandgpt-4o-miniuse theo200k_baseencoding. Preloading bothcl100k_baseando200k_baseensures that offline environments do not attempt to download encoding data at runtime for these newer models.
RUN mkdir -p "$TIKTOKEN_CACHE_DIR" && \
chmod -R 777 "$TIKTOKEN_CACHE_DIR" && \
python -c "import tiktoken; tiktoken.get_encoding('cl100k_base'); tiktoken.get_encoding('o200k_base')"
Set TIKTOKEN_CACHE_DIR and warm cl100k_base during image build so runtime token counting does not need to download encoding data in offline environments.
fix #228 #259