Skip to content

Conversation

@amanjaiswal73892
Copy link
Collaborator

@amanjaiswal73892 amanjaiswal73892 commented Sep 5, 2025

Make transformer import lazy
This pull request refactors the way HuggingFace and Transformers dependencies are handled throughout the codebase, making these imports lazy and optional. This change reduces unnecessary import overhead and avoids requiring users to install heavy dependencies unless they are actually needed (for example, when using HuggingFace backends). The HuggingFaceURLChatModel class is moved and restructured to further support this lazy-loading strategy.

Dependency management and lazy imports:

  • All imports of transformers and huggingface_hub are now performed lazily and only when required, with clear error messages if the dependencies are missing. This affects functions and classes in chat_api.py, huggingface_utils.py, and llm_utils.py. [1] [2] [3] [4] [5] [6] [7]

  • The HuggingFaceURLChatModel class is moved from chat_api.py to huggingface_utils.py and is now only imported when explicitly needed, either via a lazy import in code or through a module-level __getattr__ hook. This keeps the main API lightweight for non-HuggingFace use cases. [1] [2] [3]

HuggingFace backend improvements:

  • The initialization of HuggingFace tokenizers and inference clients is now guarded with lazy imports and improved error messages, making it clear when additional dependencies are required. [1] [2]

General code quality improvements:

  • Minor bugfixes and code style improvements, such as correcting membership checks and string conversions, and clarifying log messages. [1] [2] [3] [4] [5]

These changes help make the codebase more modular, lightweight, and user-friendly for those not using HuggingFace or Transformers-based models.

Description by Korbit AI

What change is being made?

Make the import of Hugging Face and transformers libraries lazy to reduce startup overhead and improve performance when they are not needed.

Why are these changes being made?

The changes optimize the code by avoiding the import of heavy dependencies unless they are specifically required, thus minimizing resource usage and potential delays during the startup of systems that do not need these libraries. This approach also provides clearer error handling for missing packages when users attempt to use the optional functionalities that require these libraries.

Is this description stale? Ask me to generate a new description by commenting /korbit-generate-pr-description

Copy link

@korbit-ai korbit-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review by Korbit AI

Korbit automatically attempts to detect when you fix issues in new commits.
Category Issue Status
Functionality Missing Upper Bound Temperature Validation ▹ view
Readability Inconsistent temperature default values ▹ view
Files scanned
File Path Reviewed
src/agentlab/llm/huggingface_utils.py
src/agentlab/llm/chat_api.py
src/agentlab/llm/llm_utils.py

Explore our documentation to understand the languages and file types we support and the files we ignore.

Check out our docs on how you can make Korbit work best for you and your team.

Loving Korbit!? Share us on LinkedIn Reddit and X

Comment on lines +189 to +193
self.temperature = temperature

if token is None:
# support both env var names used elsewhere
token = os.environ.get("TGI_TOKEN") or os.environ.get("AGENTLAB_MODEL_TOKEN")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing Upper Bound Temperature Validation category Functionality

Tell me more
What is the issue?

The temperature parameter is set but never validated for the upper bound, while the lower bound is checked for < 1e-3.

Why this matters

High temperature values (> 1.0) can lead to extremely random outputs, potentially making the model's responses unusable.

Suggested change ∙ Feature Preview

Add an upper bound check for temperature:

if temperature is not None:
    if temperature < 1e-3:
        logging.warning("Models might behave weirdly when temperature is too low.")
    if temperature > 2.0:
        logging.warning("High temperature values (>2.0) may result in extremely random outputs.")
self.temperature = temperature
Provide feedback to improve future suggestions

Nice Catch Incorrect Not in Scope Not in coding standard Other

💬 Looking for more details? Reply to this comment to chat with Korbit.

Comment on lines +112 to +116
temperature = (
temperature
if temperature is not None
else getattr(self, "temperature", 0.1)
)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent temperature default values category Readability

Tell me more
What is the issue?

The temperature default value of 0.1 is hardcoded in multiple places - in the getattr() call and in the class initialization (1e-1).

Why this matters

Having the same magic number in different formats (0.1 vs 1e-1) in multiple places makes it harder to maintain consistent temperature defaults and increases the risk of inconsistency when updating values.

Suggested change ∙ Feature Preview

Define a class-level constant DEFAULT_TEMPERATURE = 0.1 and use it consistently throughout the code:

DEFAULT_TEMPERATURE = 0.1

def __init__(..., temperature: Optional[float] = DEFAULT_TEMPERATURE, ...):
    ...

def __call__(...)
    temperature = temperature if temperature is not None else getattr(self, "temperature", DEFAULT_TEMPERATURE)
Provide feedback to improve future suggestions

Nice Catch Incorrect Not in Scope Not in coding standard Other

💬 Looking for more details? Reply to this comment to chat with Korbit.

Copy link
Collaborator

@optimass optimass left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@amanjaiswal73892 amanjaiswal73892 merged commit 887cce4 into main Sep 5, 2025
6 checks passed
@amanjaiswal73892 amanjaiswal73892 deleted the lazy-import branch September 5, 2025 18:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants