Feat/add litellm provider#1135
Open
RheagalFire wants to merge 4 commits into
Open
Conversation
Author
|
@reycn do you have any update on this PR? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
Motivation
PDFMathTranslate currently supports individual providers (OpenAI, Gemini, Groq, Deepseek, etc.), each requiring a separate translator class. LiteLLM is a lightweight Python SDK that provides a unified
completion()interface across 100+ providers. Users specify the provider via the model string (e.g.anthropic/claude-sonnet-4-5,bedrock/anthropic.claude-v2,vertex_ai/gemini-pro) and LiteLLM routes the call to the correct provider API. This eliminates the need to add individual translator classes for each new provider.Changes
pdf2zh/translator.py- newLiteLLMTranslatorclass extendingBaseTranslatorpdf2zh/converter.py- registered in import list and translator looppdf2zh/gui.py- registered in import,service_map,babeldoc_translate_fileloop, andisinstancecheck for rich text supportpdf2zh/pdf2zh.py- registered in import and translator looppyproject.toml- addedlitellm>=1.55.0,<1.85as optional dependency (pip install pdf2zh[litellm])Implementation details
litellmis an optional extra (likeargostranslate), so the base install is unaffected. Lazy-imported with a clear error message if not installed.drop_params=True: Silently drops provider-unsupported kwargs (e.g.strict,seed), preventing cross-provider failures.OpenAITranslatorbehavior.tenacitywith exponential backoff onlitellm.RateLimitError.<think>tags from reasoning model outputs (DeepSeek-R1, QwQ, etc.).isinstancecheck alongsideOpenAITranslator.ANTHROPIC_API_KEY,OPENAI_API_KEY,GEMINI_API_KEY, etc.).Service table entry
litellmLITELLM_MODEL+ provider-specific keys (e.g.ANTHROPIC_API_KEY)gpt-4o-miniExample usage
Use
-s litellm:modelto specify the provider and model:Or specify the model with environment variables:
GUI: Select "LiteLLM" from the service dropdown, set
LITELLM_MODELto your desired model string (e.g.anthropic/claude-sonnet-4-5). Set the provider's API key as an environment variable before launching the GUI.Tests
Live E2E test against Anthropic Claude (
anthropic/claude-sonnet-4-5):Formula placeholders (
{{v0}},{{v1}},{{v2}}) are preserved correctly in translated output.Risk / Compatibility
litellmis an optional extra. Base install unaffected.>=1.55.0,<1.85for stability.