nemoguardrails.integrations.langchain.langchain_initializer

View as Markdown

Module for initializing LangChain models with proper error handling.

Module Contents

Classes

NameDescription
ModelInitializerA method for initializing a model with its supported modes.

Functions

NameDescription
_handle_model_special_casesHandle model initialization for special cases that need custom logic.
_init_chat_completion_modelInitialize a chat completion model.
_init_community_chat_modelsInitialize community chat models.
_init_gpt35_turbo_instructInitialize GPT-3.5 Turbo Instruct model.
_init_nvidia_modelInitialize NVIDIA AI Endpoints model.
_init_text_completion_modelInitialize a text completion model.
_update_model_kwargsUpdate kwargs with the model name based on the provider’s expected fields.
init_langchain_modelInitialize a LangChain model using a series of initialization methods.
try_initialization_methodWrap an initialization method execution with a try/except to capture errors.

Data

ModelInitMethod

_PROVIDER_INITIALIZERS

_SPECIAL_MODEL_INITIALIZERS

__all__

log

API

class nemoguardrails.integrations.langchain.langchain_initializer.ModelInitializer(
init_method: nemoguardrails.integrations.langchain.langchain_initializer.ModelInitMethod,
supported_modes: list[typing.Literal['chat', 'text']]
)

A method for initializing a model with its supported modes.

nemoguardrails.integrations.langchain.langchain_initializer.ModelInitializer.__str__() -> str
nemoguardrails.integrations.langchain.langchain_initializer.ModelInitializer.execute(
model_name: str,
provider_name: str,
kwargs: typing.Dict[str, typing.Any]
) -> typing.Optional[typing.Union[langchain_core.language_models.BaseChatModel, langchain_core.language_models.BaseLLM]]

Execute this initializer to initialize a model.

nemoguardrails.integrations.langchain.langchain_initializer.ModelInitializer.supports_mode(
mode: typing.Literal['chat', 'text']
) -> bool

Check if this initializer supports the given mode.

nemoguardrails.integrations.langchain.langchain_initializer._handle_model_special_cases(
model_name: str,
provider_name: str,
kwargs: typing.Dict[str, typing.Any]
) -> typing.Optional[typing.Union[langchain_core.language_models.BaseChatModel, langchain_core.language_models.BaseLLM]]

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:

provider_name
str

Name of the provider to use

model_name
str

Name of the model to initialize

kwargs
Dict[str, Any]

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

nemoguardrails.integrations.langchain.langchain_initializer._init_chat_completion_model(
model_name: str,
provider_name: str,
kwargs: typing.Dict[str, typing.Any]
) -> langchain_core.language_models.BaseChatModel

Initialize a chat completion model.

Parameters:

model_name
str

Name of the model to initialize

provider_name
str

Name of the provider to use

kwargs
Dict[str, Any]

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
nemoguardrails.integrations.langchain.langchain_initializer._init_community_chat_models(
model_name: str,
provider_name: str,
kwargs: typing.Dict[str, typing.Any]
) -> langchain_core.language_models.BaseChatModel | None

Initialize community chat models.

Parameters:

provider_name
str

Name of the provider to use

model_name
str

Name of the model to initialize

kwargs
Dict[str, Any]

Additional arguments to pass to the model initialization

Returns: BaseChatModel | None

An initialized chat model

Raises:

  • ImportError: If langchain_community is not installed
  • ModelInitializationError: If model initialization fails
nemoguardrails.integrations.langchain.langchain_initializer._init_gpt35_turbo_instruct(
model_name: str,
provider_name: str,
kwargs: typing.Dict[str, typing.Any]
) -> langchain_core.language_models.BaseLLM | None

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:

model_name
str

Name of the model to initialize

provider_name
str

Name of the provider to use

kwargs
Dict[str, Any]

Additional arguments to pass to the model initialization

Returns: BaseLLM | None

An initialized text completion model

Raises:

  • ModelInitializationError: If model initialization fails
nemoguardrails.integrations.langchain.langchain_initializer._init_nvidia_model(
model_name: str,
provider_name: str,
kwargs: typing.Dict[str, typing.Any]
) -> langchain_core.language_models.BaseChatModel

Initialize NVIDIA AI Endpoints model.

Parameters:

model_name
str

Name of the model to initialize

provider_name
str

Name of the provider to use

**kwargs
Dict[str, Any]

Additional arguments to pass to the model initialization

Returns: BaseChatModel

An initialized chat model

Raises:

  • ImportError: If langchain_nvidia_ai_endpoints is not installed
  • ModelInitializationError: If model initialization fails
nemoguardrails.integrations.langchain.langchain_initializer._init_text_completion_model(
model_name: str,
provider_name: str,
kwargs: typing.Dict[str, typing.Any]
) -> langchain_core.language_models.BaseLLM | None

Initialize a text completion model.

Parameters:

model_name
str

Name of the model to initialize

provider_name
str

Name of the provider to use

kwargs
Dict[str, Any]

Additional arguments to pass to the model initialization

Returns: BaseLLM | None

An initialized text completion model, or None if the provider is not found

nemoguardrails.integrations.langchain.langchain_initializer._update_model_kwargs(
provider_cls: type,
model_name: str,
kwargs: dict
) -> typing.Dict

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.

nemoguardrails.integrations.langchain.langchain_initializer.init_langchain_model(
model_name: str,
provider_name: str,
mode: typing.Literal['chat', 'text'],
kwargs: typing.Dict[str, typing.Any]
) -> typing.Union[langchain_core.language_models.BaseChatModel, langchain_core.language_models.BaseLLM]

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.

nemoguardrails.integrations.langchain.langchain_initializer.try_initialization_method(
initializer: nemoguardrails.integrations.langchain.langchain_initializer.ModelInitializer,
model_name: str,
provider_name: str,
mode: typing.Literal['chat', 'text'],
kwargs: typing.Dict[str, typing.Any]
)

Wrap an initialization method execution with a try/except to capture errors.

  1. Wraps the call to try_initialization_method in a try/except block
  2. Catches any exceptions that might be thrown
  3. Logs them and continues with the next initializer
  4. Only fails at the end if all initializers have been tried
nemoguardrails.integrations.langchain.langchain_initializer.ModelInitMethod = Callable[[str, str, Dict[str, Any]], Optional[Union[BaseChatModel, BaseLLM]]]
nemoguardrails.integrations.langchain.langchain_initializer._PROVIDER_INITIALIZERS: Dict[str, Any] = {'nvidia_ai_endpoints': _init_nvidia_model, 'nim': _init_nvidia_model}
nemoguardrails.integrations.langchain.langchain_initializer._SPECIAL_MODEL_INITIALIZERS = {'gpt-3.5-turbo-instruct': _init_gpt35_turbo_instruct}
nemoguardrails.integrations.langchain.langchain_initializer.__all__ = ['ModelInitializationError', 'ModelInitMethod', 'ModelInitializer', 'init_langch...
nemoguardrails.integrations.langchain.langchain_initializer.log = logging.getLogger(__name__)