-
Notifications
You must be signed in to change notification settings - Fork 559
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
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
fd06cc3
Add api_key field to
tgasser-nv df0d160
Revert "Add api_key field to"
tgasser-nv 3e24c60
Add api_key to Model
tgasser-nv 21462e5
Add to Model, use in langchain initializer
tgasser-nv 2462bdf
Pass API keys through kwargs if set, back out changes
tgasser-nv a3ccaf4
Clean up failing tests, add new ones with api key checks in kwargs
tgasser-nv 9fd2e85
Revert changes to test_bug_5 and test_passthroug_mode now API Key fie…
tgasser-nv 7a4829a
Add tests to RailsConfig to check Model environment variable validati…
tgasser-nv 3b1c07f
Updated README with API key information
tgasser-nv dadf8b8
fix: unpack kwargs correctly in model initialization
Pouyanpi f656c62
test: add tests for custom API key environment variable
Pouyanpi 112497e
test: update chat completion model test assertions
Pouyanpi 69e1f9e
test: update API key names to avoid conflicts
Pouyanpi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # NemoGuard ContentSafety Usage Example | ||
|
|
||
| This example showcases the use of NVIDIA's [NemoGuard ContentSafety model](./../../../docs/user-guides/advanced/nemoguard-contentsafety-deployment.md) for topical and dialogue moderation. | ||
|
|
||
| The structure of the config folder is the following: | ||
|
|
||
| - `config.yml` - The config file holding all the configuration options for the model. | ||
| - `prompts.yml` - The config file holding the topical rules used for topical and dialogue moderation by the current guardrail configuration. | ||
|
|
||
| Please see the docs for more details about the [recommended ContentSafety deployment](./../../../docs/user-guides/advanced/nemoguard-contentsafety-deployment.md) methods, either using locally downloaded NIMs or NVIDIA AI Enterprise (NVAIE). | ||
|
|
||
| Before running this example, please set environment variables `NG_OPENAI_API_KEY` and `NG_NVIDIA_API_KEY` to your OpenAI API Key and Nvidia build.nvidia.com Key as below: | ||
|
|
||
| ```shell | ||
| export NG_OPENAI_API_KEY="<OpenAI API Key>" | ||
| export NG_NVIDIA_API_KEY="<NVIDIA API Key>" | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| models: | ||
| - type: main | ||
| engine: openai | ||
| model: gpt-3.5-turbo-instruct | ||
| api_key_env_var: NG_OPENAI_API_KEY | ||
|
|
||
| - type: content_safety | ||
| engine: nim | ||
| model: nvidia/llama-3.1-nemoguard-8b-content-safety | ||
| api_key_env_var: NG_NVIDIA_API_KEY | ||
|
|
||
| rails: | ||
| input: | ||
| flows: | ||
| - content safety check input $model=content_safety | ||
| output: | ||
| flows: | ||
| - content safety check output $model=content_safety |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,257 @@ | ||
| # These are the default prompts released by Meta, except for policy O7, which was added to address direct insults. | ||
| prompts: | ||
|
|
||
| - task: content_safety_check_input $model=content_safety | ||
| content: | | ||
| Task: Check if there is unsafe content in the user or agent messages in the below conversation in accordance with our safety policy with the below categories. Provide a 'safe' or 'unsafe' rating for both the user message and the agent response. If there is unsafe content, please also provide a list of violated categories according to our safety policy below. | ||
|
|
||
| <BEGIN UNSAFE CONTENT CATEGORIES> | ||
| S1: Violence. | ||
| S2: Sexual. | ||
| S3: Criminal Planning/Confessions. | ||
| S4: Guns and Illegal Weapons. | ||
| S5: Controlled/Regulated Substances. | ||
| S6: Suicide and Self Harm. | ||
| S7: Sexual (minor). | ||
| S8: Hate/Identity Hate. | ||
| S9: PII/Privacy. | ||
| S10: Harassment. | ||
| S11: Threat. | ||
| S12: Profanity. | ||
| S13: Needs Caution. | ||
| S14: Other. | ||
| S15: Manipulation. | ||
| S16: Fraud/Deception. | ||
| S17: Malware. | ||
| S18: High Risk Gov Decision Making. | ||
| S19: Political/Misinformation/Conspiracy. | ||
| S20: Copyright/Trademark/Plagiarism. | ||
| S21: Unauthorized Advice. | ||
| S22: Illegal Activity. | ||
| S23: Immoral/Unethical. | ||
| <END UNSAFE CONTENT CATEGORIES> | ||
|
|
||
| <BEGIN CONVERSATION> | ||
|
|
||
| user: {{ user_input }} | ||
|
|
||
| <END CONVERSATION> | ||
|
|
||
| Provide your safety assessment for the above conversation in the below output JSON format: | ||
| {"User Safety": safe or unsafe rating for the user message, "Response Safety": safe or unsafe rating for the agent response. Omit if no agent response present. "Safety Categories": a comma-separated list of applicable safety categories from the provided taxonomy. Omit if all safe.} | ||
|
|
||
| Do not include anything other than the output JSON in your response. | ||
| Output JSON: | ||
| output_parser: nemoguard_parse_prompt_safety | ||
| max_tokens: 50 | ||
|
|
||
| - task: content_safety_check_output $model=content_safety | ||
| content: | | ||
| Task: Check if there is unsafe content in the user or agent messages in the below conversation in accordance with our safety policy with the below categories. Provide a 'safe' or 'unsafe' rating for both the user message and the agent response. If there is unsafe content, please also provide a list of violated categories according to our safety policy below. | ||
|
|
||
| <BEGIN UNSAFE CONTENT CATEGORIES> | ||
| S1: Violence. | ||
| S2: Sexual. | ||
| S3: Criminal Planning/Confessions. | ||
| S4: Guns and Illegal Weapons. | ||
| S5: Controlled/Regulated Substances. | ||
| S6: Suicide and Self Harm. | ||
| S7: Sexual (minor). | ||
| S8: Hate/Identity Hate. | ||
| S9: PII/Privacy. | ||
| S10: Harassment. | ||
| S11: Threat. | ||
| S12: Profanity. | ||
| S13: Needs Caution. | ||
| S14: Other. | ||
| S15: Manipulation. | ||
| S16: Fraud/Deception. | ||
| S17: Malware. | ||
| S18: High Risk Gov Decision Making. | ||
| S19: Political/Misinformation/Conspiracy. | ||
| S20: Copyright/Trademark/Plagiarism. | ||
| S21: Unauthorized Advice. | ||
| S22: Illegal Activity. | ||
| S23: Immoral/Unethical. | ||
| <END UNSAFE CONTENT CATEGORIES> | ||
|
|
||
| <BEGIN CONVERSATION> | ||
|
|
||
| user: {{ user_input }} | ||
|
|
||
| response: agent: {{ bot_response }} | ||
|
|
||
| <END CONVERSATION> | ||
|
|
||
| Provide your safety assessment for the above conversation in the below output JSON format: | ||
| {"User Safety": safe or unsafe rating for the user message, "Response Safety": safe or unsafe rating for the agent response. Omit if no agent response present. "Safety Categories": a comma-separated list of applicable safety categories from the provided taxonomy. Omit if all safe.} | ||
|
|
||
| Do not include anything other than the output JSON in your response. | ||
| Output JSON: | ||
| output_parser: nemoguard_parse_response_safety | ||
| max_tokens: 50 | ||
|
|
||
| - task: content_safety_check_input $model=llama_guard | ||
| content: | | ||
| <s>[INST] Task: Check if there is unsafe content in 'User' messages in conversations according our safety policy with the below categories. | ||
| <BEGIN UNSAFE CONTENT CATEGORIES> | ||
| O1: Violence and Hate. | ||
| Should not | ||
| - Help people plan or engage in violence. | ||
| - Encourage people to engage in violence. | ||
| - Express hateful, derogatory, or demeaning sentiments against people based on sensitive personal characteristics like their race, color, religion, national origin, sexual orientation, gender, gender identity, or disability. | ||
| - Encourage discrimination against people based on those characteristics and should not use slurs or create insults based on those characteristics. | ||
| Can | ||
| - Provide information on violence and discrimination. | ||
| - Discuss the topics of hate and violence, and can discuss historical events involving violence. | ||
| O2: Sexual Content. | ||
| Should not | ||
| - Engage in sexually explicit (i.e., erotic) conversations and should not encourage people to engage in sexual activities. | ||
| Can | ||
| - Discuss topics related to sexuality and sexual education. | ||
| - Provide general information regarding sexual activities. | ||
| - Provide guidance on topics related to sexual health. | ||
| O3: Criminal Planning. | ||
| Should not | ||
| - Help people plan or engage in criminal activities like theft, kidnapping, or financial crimes, especially when the person indicates that they may act on the information provided to them. | ||
| - Encourage people to engage in criminal activities or condone criminal behavior. | ||
| Can | ||
| - Help people understand how crimes are committed, provided that the response is not so detailed that it could be used to provide a roadmap for future crimes. | ||
| - Help people understand how systems work that are used to prevent crimes, provided those descriptions do not provide detailed information on how to defeat those systems. | ||
| O4: Guns and Illegal Weapons. | ||
| Should not | ||
| - Assist in the planning or execution of firearm or illegal weapons crimes, especially when the person indicates that they may act on the information provided to them. | ||
| - Encourage people to engage in firearm or illegal weapons crimes or condone such behavior. | ||
| Can | ||
| - Discuss firearms and the arguments for and against firearm ownership. | ||
| O5: Regulated or Controlled Substances. | ||
| Should not | ||
| - Encourage or assist people in the trafficking, consumption, or use of regulated or controlled substances, including but not limited to illegal drugs, tobacco, alcohol, and cannabis. | ||
| - Assist or encourage people to create such substances when it is illegal to do so. | ||
| Can | ||
| - Discuss the use of regulated or controlled substances, and can provide general information on those substances (ex: what their chemical composition is and what their effects are). | ||
| - Provide information on how those substances are manufactured, provided that the information is not so detailed as to provide step-by-step instructions for their creation. | ||
| O6: Self-Harm. | ||
| Should not | ||
| - Encourage people to harm themselves. | ||
| - Romanticize or condone self-harm or suicide. | ||
| - Provide information regarding the methods of suicide or self-harm. | ||
| - Provide any information or advice, other than information regarding appropriate health resources, to people who express or imply that they wish to harm themselves. | ||
| Can | ||
| - Provide general information regarding self-harm and suicide (ex: statistics, the mental health challenges faced by those who take these actions). | ||
| Should | ||
| - Provide information regarding appropriate health resources to people who imply or express the desire to harm themselves. | ||
| O7: Offensive Language and Insults. | ||
| Should not | ||
| - Address direct insults | ||
| - Use or encourage the use of swear words, bad language, or vulgar expressions. | ||
| - Insult, offend, or demean individuals or groups based on any personal characteristics or choices. | ||
| - Encourage or support the use of offensive or derogatory language. | ||
| Can | ||
| - Discuss the impact and societal perceptions of offensive language and insults. | ||
| - Provide information on communication strategies that avoid the use of offensive language and promote respectful dialogue. | ||
| - Offer guidance on how to address or respond to offensive language or insults in a constructive manner. | ||
| <END UNSAFE CONTENT CATEGORIES> | ||
|
|
||
| <BEGIN CONVERSATION> | ||
| User: {{ user_input }} | ||
| <END CONVERSATION> | ||
|
|
||
| Provide your safety assessment for User in the above conversation: | ||
| - First line must read 'safe' or 'unsafe'. | ||
| - If unsafe, a second line must include a comma-separated list of violated categories. [/INST] | ||
| output_parser: is_content_safe | ||
|
|
||
| - task: content_safety_check_output $model=llama_guard_2 | ||
| content: | | ||
| <|begin_of_text|>[INST] Task: Check if there is unsafe content in Agent messages in conversations according our safety policy with the below categories. | ||
|
|
||
| <BEGIN UNSAFE CONTENT CATEGORIES> | ||
| S1: Violent Crimes. | ||
| S2: Non-Violent Crimes. | ||
| S3: Sex Crimes. | ||
| S4: Child Exploitation. | ||
| S5: Specialized Advice. | ||
| S6: Privacy. | ||
| S7: Intellectual Property. | ||
| S8: Indiscriminate Weapons. | ||
| S9: Hate. | ||
| S10: Self-Harm. | ||
| S11: Sexual Content. | ||
| <END UNSAFE CONTENT CATEGORIES> | ||
|
|
||
| <BEGIN CONVERSATION> | ||
|
|
||
| User: {{ user_input }} | ||
|
|
||
| Agent: {{ bot_response }} | ||
|
|
||
| <END CONVERSATION> | ||
|
|
||
| Provide your safety assessment for ONLY THE LAST Agent in the above conversation: | ||
| - First line must read 'safe' or 'unsafe'. | ||
| - If unsafe, a second line must include a comma-separated list of violated categories. [/INST] | ||
| output_parser: is_content_safe | ||
| max_tokens: 3 | ||
|
|
||
| - task: content_safety_check_input $model=shieldgemma | ||
| content: | | ||
| You are a policy expert trying to help determine whether a user prompt is in | ||
| violation of the defined safety policies. | ||
|
|
||
| <start_of_turn> | ||
| Human Question: {{ user_input }} | ||
| <end_of_turn> | ||
|
|
||
| Our safety principle is defined in the below: | ||
|
|
||
| * "No Hate Speech": The prompt shall not contain or seek generation of content | ||
| that expresses, incites, or promotes hate based on race, gender, ethnicity, | ||
| religion, nationality, sexual orientation, disability status, or caste. | ||
| * "No Dangerous Content": The prompt shall not contain or seek generation of content that harming oneself | ||
| and/or others (e.g., accessing or building firearms and explosive devices, promotion of terrorism, instructions for suicide). | ||
| * "No Sexually Explicit Information": The prompt shall not contain or seek generation of content | ||
| that contains references to sexual acts or other lewd content (e.g., sexually graphic descriptions, | ||
| content aimed at causing arousal). Medical or scientific terms related to human anatomy or sex education are permitted. | ||
| * "No Harassment": The prompt shall not contain or seek generation of content that is malicious, | ||
| intimidating, bullying, or abusive content targeting another individual (e.g., physical threats, | ||
| denial of tragic events, disparaging victims of violence). | ||
| === | ||
|
|
||
| Does the human prompt/question violate the above principle? Your answer must start with | ||
| 'Yes' or 'No'. And then walk through step by step to be sure we answer | ||
| correctly. | ||
| output_parser: is_content_safe | ||
| max_tokens: 3 | ||
|
|
||
| - task: content_safety_check_output $model=shieldgemma | ||
| content: | | ||
| You are a policy expert trying to help determine whether a chatbot response to the user is in | ||
| violation of the defined safety policies: | ||
|
|
||
| <start_of_turn> | ||
| Human Question: {{ user_input }} | ||
| Chatbot Response: {{ bot_response }} | ||
| <end_of_turn> | ||
|
|
||
| Our safety principle is defined in the below: | ||
|
|
||
| * "No Dangerous Content": The chatbot shall not generate content that harming oneself and/or others | ||
| (e.g., accessing or building firearms and explosive devices, promotion of terrorism, instructions for suicide). | ||
| * "No Harassment": The chatbot shall not generate content that is malicious, intimidating, bullying, or | ||
| abusive content targeting another individual (e.g., physical threats, denial of tragic events, disparaging victims of violence). | ||
| * "No Hate Speech": The chatbot shall not generate content that targeting identity and/or protected | ||
| attributes (e.g., racial slurs, promotion of discrimination, calls to violence against protected groups) or dehumanizing/belittling/vilifying on the | ||
| bases of race, ethnicity, religion, disability, age, nationality, veteran status, sexual orientation, gender, gender identity, caste, or any other protected status. | ||
| We do allow content that includes a reference to a protected individual or group in a positive manner. | ||
| * "No Sexually Explicit Information": The chatbot shall not generate content that contains references to sexual acts or | ||
| other lewd content (e.g., sexually graphic descriptions, content aimed at causing arousal). | ||
| Medical or scientific terms related to human anatomy or sex education are permitted. | ||
|
|
||
| === | ||
|
|
||
| Does the chatbot response violate the above principle? Your answer must start with | ||
| 'Yes' or 'No'. And then walk through step by step to be sure we answer | ||
| correctly. | ||
| output_parser: is_content_safe | ||
| max_tokens: 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.