Skip to content

refactor: lazy-init ChatBot at LRCer level, share across files#117

Merged
MaleicAcid merged 1 commit into
zh-plus:masterfrom
iautolab:refactor/lrcer-chatbot-lazy-init
Apr 28, 2026
Merged

refactor: lazy-init ChatBot at LRCer level, share across files#117
MaleicAcid merged 1 commit into
zh-plus:masterfrom
iautolab:refactor/lrcer-chatbot-lazy-init

Conversation

@MaleicAcid
Copy link
Copy Markdown
Collaborator

Motivation

After PR #113 moved ChatBot creation from LLMTranslator to LRCer._translate(), each file translation still creates and destroys its own ChatBot (HTTP connection pool). Translating 10 files means 10 TLS handshakes and 10 connection teardowns to the same API endpoint.

This PR lifts ChatBot lifecycle to the LRCer instance level using lazy initialization — the same pattern already used for Transcriber. ChatBot instances are created on first access and shared across all files processed by the same LRCer.

Changes

  • Added chatbot and retry_chatbot as lazy-initialized properties on LRCer, with thread-safe double-checked locking (consistent with the existing transcriber property).
  • Added close() method and context manager support (__enter__/__exit__) to LRCer for explicit resource cleanup.
  • Simplified _translate(): removed ExitStack / create_chatbot / _open_bot helper, now uses self.chatbot and self.retry_chatbot directly.
  • Updated README.md: fixed outdated LLMTranslator example to use the new create_chatbot() + chatbot= API, and documented the new with LRCer() as lrcer: usage pattern.

Benefits

  • Connection reuse: 10 files = 1 connection pool instead of 10. TLS handshake and connection setup happen once.
  • Simpler _translate(): no more ExitStack, create_chatbot, or _open_bot helper — just self.chatbot.
  • Explicit cleanup: with LRCer() as lrcer: guarantees connections are closed when done.
  • Lazy creation: ChatBot is only created when translation is actually needed. Users who only call transcribe() never pay the cost of establishing an LLM connection.

@MaleicAcid MaleicAcid merged commit 7ce5f6c into zh-plus:master Apr 28, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant