nemoguardrails.integrations.langchain.langchain_initializer
nemoguardrails.integrations.langchain.langchain_initializer
Module for initializing LangChain models with proper error handling.
Module Contents
Classes
Functions
Data
API
A method for initializing a model with its supported modes.
Execute this initializer to initialize a model.
Check if this initializer supports the given mode.
Handle model initialization for special cases that need custom logic.
This function handles edge cases where standard initialization methods don’t work properly. It looks up initializers in the registry and dispatches to the appropriate initialization function.
Parameters:
Name of the provider to use
Name of the model to initialize
Additional arguments to pass to the model initialization
Returns: Optional[Union[BaseChatModel, BaseLLM]]
An initialized model for special cases, or None if no special initializer exists
Initialize a chat completion model.
Parameters:
Name of the model to initialize
Name of the provider to use
Additional arguments to pass to the model initialization
Returns: BaseChatModel
An initialized chat completion model
Raises:
ValueError: If the model cannot be initialized as a chat model
Initialize community chat models.
Parameters:
Name of the provider to use
Name of the model to initialize
Additional arguments to pass to the model initialization
Returns: BaseChatModel | None
An initialized chat model
Raises:
ImportError: If langchain_community is not installedModelInitializationError: If model initialization fails
Initialize GPT-3.5 Turbo Instruct model.
Currently init_chat_model from langchain infers this as a chat model. This is a bug in langchain, and we need to handle it here.
This model requires text completion initialization.
Parameters:
Name of the model to initialize
Name of the provider to use
Additional arguments to pass to the model initialization
Returns: BaseLLM | None
An initialized text completion model
Raises:
ModelInitializationError: If model initialization fails
Initialize NVIDIA AI Endpoints model.
Parameters:
Name of the model to initialize
Name of the provider to use
Additional arguments to pass to the model initialization
Returns: BaseChatModel
An initialized chat model
Raises:
ImportError: If langchain_nvidia_ai_endpoints is not installedModelInitializationError: If model initialization fails
Initialize a text completion model.
Parameters:
Name of the model to initialize
Name of the provider to use
Additional arguments to pass to the model initialization
Returns: BaseLLM | None
An initialized text completion model, or None if the provider is not found
Update kwargs with the model name based on the provider’s expected fields.
If provider_cls.model_fields contains ‘model’ or ‘model_name’, sets the corresponding key in kwargs to model_name.
Initialize a LangChain model using a series of initialization methods.
This function tries multiple initialization methods in sequence until one succeeds. Each method is attempted only if it supports the requested mode.
Wrap an initialization method execution with a try/except to capture errors.
- Wraps the call to
try_initialization_methodin a try/except block - Catches any exceptions that might be thrown
- Logs them and continues with the next initializer
- Only fails at the end if all initializers have been tried