-
Notifications
You must be signed in to change notification settings - Fork 910
generator: NeMoGuardrails server support #1675
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
Changes from all commits
1b97042
5d0f4ed
20fb006
f2aa26d
d531028
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| from garak import _config | ||
| from garak.attempt import Message, Conversation | ||
| from garak.generators.base import Generator | ||
| from garak.generators.openai import OpenAICompatible | ||
|
|
||
|
|
||
| class NeMoGuardrails(Generator): | ||
|
|
@@ -51,4 +52,37 @@ def _call_model( | |
| return [None] | ||
|
|
||
|
|
||
| class NeMoGuardrailsServer(OpenAICompatible): | ||
| """Generator for NeMo Guardrails Server | ||
|
|
||
| To select specific rails in a multi rail deployment set `config_ids` to match the rail configuration names | ||
| as documented by the `NeMo guardrails SDK <https://docs.nvidia.com/nemo/guardrails/0.21.0/run-rails/using-fastapi-server/chat-with-guardrailed-model.html#using-the-openai-python-sdk>`_. | ||
| """ | ||
|
|
||
| ENV_VAR = None | ||
|
|
||
| supports_multiple_generations = False | ||
| generator_family_name = "Guardrails" | ||
|
|
||
| DEFAULT_PARAMS = OpenAICompatible.DEFAULT_PARAMS | { | ||
| "uri": "http://localhost:8000/v1/", | ||
| "config_ids": set(), | ||
|
jmartin-tech marked this conversation as resolved.
jmartin-tech marked this conversation as resolved.
|
||
| } | ||
|
|
||
| def __init__(self, name="", config_root=_config): | ||
| self.api_key = "not-used" # suppress any api_key from being sent as the server does not utilize one | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it worth setting
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting point, currently NeMo Guardrails server does not offer any built in authentication, leading to the suppression added here is matched to the public docs from the sdk. I agree there is some confusion created by keeping the ENV_VAR on the class. I will test other patterns for this suppression.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
A place holder value still needs to be set here to ensure the |
||
| super().__init__(name, config_root) | ||
| if self.extra_params and not self.extra_params.get("extra_body"): | ||
| self.extra_params.append("extra_body") | ||
|
|
||
| guardrails = None | ||
| if self.config_ids: | ||
| guardrails = {"config_ids": self.config_ids} | ||
| if guardrails: | ||
| if hasattr(self, "extra_body") and self.extra_body and self.config_ids: | ||
| self.extra_body["guardrails"] = guardrails | ||
| else: | ||
| self.extra_body = {"guardrails": guardrails} | ||
|
Comment on lines
+82
to
+85
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is mysterious to me. i accept it, and welcome the mystery. -- is something being worked around here? maybe a brief explanation in the docstring / a comment
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Official docs this was based on here. Will work out how to document this better on the class.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please validate update in d531028 meets the ask here. |
||
|
|
||
|
|
||
| DEFAULT_CLASS = "NeMoGuardrails" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this still a sensible default?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would be in favor of shifting if there is consensus. That would elevate this to a breaking change for this addition as configurations that did not specify |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import httpx | ||
| import pytest | ||
|
|
||
| from garak.attempt import Message, Turn, Conversation | ||
| from garak.generators.guardrails import NeMoGuardrailsServer | ||
|
|
||
|
|
||
| def guardrails_config(selected_rails): | ||
| """helper method to provide generator configuration""" | ||
| return { | ||
| "generators": { | ||
| "guardrails": { | ||
| "NeMoGuardrailsServer": { | ||
| "name": "UnknownModel", | ||
| "config_ids": selected_rails, | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "selected_rails", | ||
| [ | ||
| [], | ||
| ["rail1"], | ||
| ["rail1", "rail2"], | ||
| ], | ||
| ) | ||
| @pytest.mark.respx(base_url=NeMoGuardrailsServer.DEFAULT_PARAMS["uri"]) | ||
| def test_guardrail_selection(selected_rails, respx_mock, openai_compat_mocks): | ||
| """validate selected rails are passed as headers on the request""" | ||
| mock_response = openai_compat_mocks["chat"] | ||
| mock_request = respx_mock.post("chat/completions") | ||
| mock_request.mock( | ||
| return_value=httpx.Response( | ||
| mock_response["code"], | ||
| json=mock_response["json"], | ||
| ) | ||
| ) | ||
| config_root = guardrails_config(selected_rails) | ||
| g = NeMoGuardrailsServer(config_root=config_root) | ||
| conv = Conversation(turns=[Turn(role="user", content=Message("Testing text"))]) | ||
| g.generate(conv) | ||
| assert mock_request.called | ||
| for rail in selected_rails: | ||
| content = str(mock_request.calls.last.request.content) | ||
| assert "guardrails" in content | ||
| assert "config_ids" in content | ||
| assert rail in content |
Uh oh!
There was an error while loading. Please reload this page.