feat: add support for custom HTTP client in encoding functions #470
+96
−32
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds support for passing a custom HTTP client instance to tiktoken's encoding functions, addressing the need for custom SSL certificates, proxies, and other HTTP client configurations.
Fixes #462
Changes
HttpClientProtocol intiktoken/load.pyto define the HTTP client interfaceread_file(),read_file_cached(),data_gym_to_mergeable_bpe_ranks(), andload_tiktoken_bpe()to accept optionalhttp_clientparametertiktoken_ext/openai_public.pyto accept and pass throughhttp_clientget_encoding()intiktoken/registry.pyto accepthttp_clientand pass it to constructorsencoding_for_model()intiktoken/model.pyto accepthttp_clienttest_custom_http_client()to verify the functionalityMotivation
In some environments, it's necessary to use a custom HTTP client, for example:
Example Usage
Testing
tests/test_simple_public.py::test_custom_http_clientget_encoding()andencoding_for_model()Breaking Changes
None. This is a backward-compatible change as all
http_clientparameters are optional and default toNone, which maintains the current behavior.