8000 feat: Add api_key_env_var to Model, pass in kwargs to langchain initializer by tgasser-nv · Pull Request #1142 · NVIDIA/NeMo-Guardrails · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: Add api_key_env_var to Model, pass in kwargs to langchain initializer #1142

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 13 commits into from
May 5, 2025

Conversation

tgasser-nv
Copy link
Collaborator
@tgasser-nv tgasser-nv commented Apr 24, 2025

Description

Related Issue(s)

Checklist

  • I've read the CONTRIBUTING guidelines.
  • I've updated the documentation if applicable.
  • I've added tests if applicable.
  • @mentions of the person or team responsible for reviewing proposed changes.

Copy link

Documentation preview

https://nvidia.github.io/NeMo-Guardrails/review/pr-1142

@tgasser-nv tgasser-nv changed the title Add api_key to Model Add api_key_env_var to Model, use in langchain initializer Apr 29, 2025
@tgasser-nv tgasser-nv marked this pull request as draft April 29, 2025 02:08
Copy link
Collaborator
@Pouyanpi Pouyanpi left a comment

Choose a reason for hiding this comment

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

Thank you Tim,

I suggest that we do not change init_llm_model and update kwargs in llmrails.py to pick up the value of env var (if set). The only test that requires fix is test_llm_rails_config.py.

A soft reset and deleting the files except rails/llm/config.py and rails/llm/llmrails.py, and examples/configs/content_safety_api_key should do the trick:

git reset --soft 90760ab16a9dc659ad0580348af1a2eeda0a7149
git restore --staged .

git restore nemoguardrails/llm/models/initializer.py \
            nemoguardrails/llm/models/langchain_initializer.py \
            nemoguardrails/llm/providers/providers.py \
            tests/llm_providers/test_langchain_initialization_methods.py \
            tests/test_bug_5.py \
            tests/test_rails_llm_config.py \
            tests/v2_x/test_passthroug_mode.py

and then we should make the changes to llmrails.py and config.py 👍🏻

What do you think?

@tgasser-nv tgasser-nv force-pushed the feat/add-model-api-key branch from e2137b9 to 52d4ad9 Compare April 29, 2025 17:23
@tgasser-nv tgasser-nv changed the title Add api_key_env_var to Model, use in langchain initializer feat: Add api_key_env_var to Model, pass in kwargs to langchain initializer Apr 29, 2025
@tgasser-nv tgasser-nv marked this pull request as ready for review April 29, 2025 20:02
Copy link
Collaborator Author
@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.

I addressed the feedback and added a few tests to check RailsConfig error checking and imports the environment variable correctly. Let me know anything else that needs fixing

@tgasser-nv tgasser-nv self-assigned this Apr 29, 2025
@tgasser-nv tgasser-nv requested a review from Pouyanpi April 29, 2025 20:05
@tgasser-nv
Copy link
Collaborator Author

What do you think?

Sounds great, made the necessary changes and added tests to cover the environment variable checks in RailsConfig

@tgasser-nv tgasser-nv requested a review from cparisien April 30, 2025 13:59
@Pouyanpi Pouyanpi added this to the v0.14.0 milestone Apr 30, 2025
@Pouyanpi Pouyanpi added the enhancement New feature or request label Apr 30, 2025
Copy link
Collaborator
@Pouyanpi Pouyanpi left a comment

Choose a reason for hiding this comment

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

Thanks @tgasser-nv, the PR looks great overall! Thank you for adding comprehensive tests 👍🏻

I noticed two minor issues, so I went ahead and pushed some minor fixes, updated the tests, and added two integration tests. Please let me know if you are OK with those changes.

p.s. test_langchain_integration.py is skipped on CI

@Pouyanpi Pouyanpi added bug Something isn't working refactoring and removed bug Something isn't working refactoring labels May 1, 2025
tgasser-nv and others added 13 commits May 2, 2025 09:00
- Added tests for initializing chat and text models using a custom
  API key environment variable (`NG_OPENAI_API_KEY`).
Update test assertions to match actual init_chat_model behavior where kwargs are unpacked rather than passed as a named parameter.
- Changed `NG_OPENAI_API_KEY` to `DUMMY_OPENAI_API_KEY`.
- Changed `NVIDIA_API_KEY` to `DUMMY_NVIDIA_API_KEY`.
- Updated test cases to use dummy key names to prevent conflicts with
  user defined environment variables when running tests locally.
@Pouyanpi Pouyanpi force-pushed the feat/add-model-api-key branch from 6d0bc2b to 69e1f9e Compare May 2, 2025 07:01
@codecov-commenter
Copy link

Codecov Report

Attention: Patch coverage is 75.00000% with 3 lines in your changes missing coverage. Please review.

Project coverage is 68.01%. Comparing base (67b8b7a) to head (69e1f9e).

Files with missing lines Patch % Lines
nemoguardrails/rails/llm/llmrails.py 25.00% 3 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #1142   +/-   ##
========================================
  Coverage    68.00%   68.01%           
========================================
  Files          161      161           
  Lines        15793    15805   +12     
========================================
+ Hits         10740    10749    +9     
- Misses        5053     5056    +3     
Flag Coverage Δ
python 68.01% <75.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Pouyanpi Pouyanpi merged commit d82f0b9 into develop May 5, 2025
19 checks passed
@Pouyanpi Pouyanpi deleted the feat/add-model-api-key branch May 5, 2025 07:14
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
0