Skip to content

feat(model): validate model name configuration #1084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 17, 2025
Merged

Conversation

Pouyanpi
Copy link
Collaborator

@Pouyanpi Pouyanpi commented Apr 2, 2025

Description

Implement proper model name validation in the Model class to ensure that a model name is specified exactly once, either directly via the 'model' field or through parameters. This also normalizes the configuration by moving model names from parameters to the dedicated model field.

  • Add tests to verify all validation scenarios

  • Add log.info to warn the user about this change per Mike's suggestion OR do not pop it from Model

@mikemckiernan
Copy link
Member

Unsolicited suggestion: WDYT about warning with a deprecation message if the model is specified in the parameters field? If we could gradually direct folks to use the dedicated model field, that might be simpler in the long run. Sometimes, the straightjacket is the right fit. My $0.02.

@Pouyanpi Pouyanpi requested a review from mikemckiernan April 2, 2025 15:29
@Pouyanpi
Copy link
Collaborator Author

Pouyanpi commented Apr 2, 2025

Unsolicited suggestion: WDYT about warning with a deprecation message if the model is specified in the parameters field? If we could gradually direct folks to use the dedicated model field, that might be simpler in the long run. Sometimes, the straightjacket is the right fit. My $0.02.

It is still ok to set model or model_name in parameters field. We get validation error if both model field and any of model or model_name in parameters are set.

But I agree, If someone is using something like model.parameters["model_name"] somewhere in their code this change is problematic because we are popping it from parameters and set it to model, in this case I think adding a warning is useful. Thank you for pointing it out 👍🏻

@Pouyanpi Pouyanpi added the enhancement New feature or request label Apr 2, 2025
Copy link
Collaborator

@tgasser-nv tgasser-nv left a comment

Choose a reason for hiding this comment

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

The code looks good, and the tests cover all the permutations of config in main model / parameters with model and model_name. I have a couple of questions though:

  1. Why do we need to support models in both the yaml config and parameters?
  2. Why not standardize on just model rather than both model and model_name?

@Pouyanpi
Copy link
Collaborator Author

The code looks good, and the tests cover all the permutations of config in main model / parameters with model and model_name. I have a couple of questions though:

  1. Why do we need to support models in both the yaml config and parameters?

in the parameters user can add any field that the underlying langchain llm accepts. Sadly, not alll the langchain third party integrations (e.g., community models) support a unified signature, some accepts model_name and not model

  1. Why not standardize on just model rather than both model and model_name?

it depends on the langchain model and provider used, and in llmrails.py we are checking this explicitly:

        if model_config.model:
            # Some LLM providers use `model_name` instead of `model`.
            # Use the `__fields__` attribute which is computed dynamically by pydantic.
            if "model" in provider_cls.__fields__:
                kwargs["model"] = model_config.model
            if "model_name" in provider_cls.__fields__:
                kwargs["model_name"] = model_config.model

Copy link
Collaborator

@trebedea trebedea left a comment

Choose a reason for hiding this comment

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

This is useful, kudos for the tests.

@mikemckiernan , maybe we should make more explicit that we accept both model and model_name in parameters, depending on how langchain providers are naming this key / attribute?

@mikemckiernan
Copy link
Member

This is useful, kudos for the tests.

@mikemckiernan , maybe we should make more explicit that we accept both model and model_name in parameters, depending on how langchain providers are naming this key / attribute?

Isn't it that what we really want to emphasize is that the top-level model: field is never wrong? Won't spending words on the flexibility just pin us down longer on supporting too flexible an API?

@Pouyanpi
Copy link
Collaborator Author

Pouyanpi commented Apr 16, 2025

The model field's value is checked against the Langchain class:

        if model_config.model:
            # Some LLM providers use `model_name` instead of `model`.
            # Use the `__fields__` attribute which is computed dynamically by pydantic.
            if "model" in provider_cls.__fields__:
                kwargs["model"] = model_config.model
            if "model_name" in provider_cls.__fields__:
                kwargs["model_name"] = model_config.model                

so at the end model "is never wrong" and is the only required field. "model_name" makes it more explicit and also current implementation makes it backward compatible.

Please let me know if there are further objections that must be addressed in this PR. I'd like to merge it tomorrow as it is blocking two other PRs. We can follow up on it in separate PRs.

cc @tgasser-nv @mikemckiernan

Copy link
Member

@mikemckiernan mikemckiernan left a comment

Choose a reason for hiding this comment

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

my feedback is never blocking. I appreciate the heads up and the chance to peek at the change. TY!

@trebedea
Copy link
Collaborator

My comment here was that here the parameters are actually meant for the Langchain classes that we are using to actually interact with various LLM providers. This way we are able to set any of the attributes different LLM providers support, model and model_name are just two of them, and we cannot control the names of these attributes:

kwargs = model_config.parameters

We can merge this, but maybe we should highlight in the docs in another MR that the parameters key in our config.yml file are actually the attributes that can be passed to different Langchain LLM (or anything that extends BaseLanguageModel) providers. Right now in the config documentation we just mention that parameters: any additional parameters, e.g., temperature, top_k, etc. (but there is no mention how to understand which are the actual supported additional parameters, or maybe there is this information somewhere else and I cannot find it now) - @mikemckiernan any thoughts?

https://docs.nvidia.com/nemo/guardrails/latest/user-guides/configuration-guide.html

Implement proper model name validation in the Model class to ensure that
a model name is specified exactly once, either directly via the 'model'
field or through parameters. This also normalizes the configuration by
moving model names from parameters to the dedicated model field.

Add tests to verify all validation scenarios

fix: ensure custom_model config has model
@Pouyanpi Pouyanpi force-pushed the refactor/config-model branch from 25a353b to 457c532 Compare April 17, 2025 07:24
@Pouyanpi Pouyanpi merged commit ac87e57 into develop Apr 17, 2025
9 checks passed
@Pouyanpi Pouyanpi deleted the refactor/config-model branch April 17, 2025 07:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants