NVIDIA / NeMo-Guardrails

NeMo Guardrails is an open-source toolkit for easily adding programmable guardrails to LLM-based conversational systems.
Other
3.95k stars 359 forks source link

Value Error for GPT 3.5 #489

Closed Drewwb closed 2 weeks ago

Drewwb commented 4 months ago

Issue: Value Error with NeMo Guardrails Config

I'm encountering a ValueError in my .ipynb file when trying to set up NeMo Guardrails. The error appears to be related to NeMo not accessing GPT correctly.

Code Snippet:


colang_content = """
define user ask about criminal activity
  "How can I rob a bank?"

define flow
  user ask about criminal activity
  bot refuse to respond about criminal activity
"""

yaml_content = """
models:
 - type: main
   engine: openai
   model: gpt-3.5-turbo-instruct

instructions:
  - type: general
    content: |
        (content here)

sample_conversation: |
        (sample convo here)

rails:
  input:
    flows:
      - self check input

prompts:
  - task: self check input
    content: >
          (user {input} should not include xyz goes here)
          Question: Should the user message be blocked?
          Answer:
"""

from nemoguardrails import LLMRails, RailsConfig
import os

os.environ["OPENAI_API_KEY"] = ""

async def check_guardrails(query, course):
    # Initialize rails config
    config = RailsConfig.from_path("./config")
    rails = LLMRails(config)

    # Use the async version of the generate method
    response = await rails.generate_async(messages=[{
        "role": "user",
        "content": query
    }])

    if (prompt_blocks_the_user_input):
        return False

    return True

os.environ["OPENAI_API_KEY"] = ""
course = "Ethics"
user_query = "Can you help me study for Ethics"
result = await check_guardrails(user_query, course)
print(result)

ValidationError                           Traceback (most recent call last)
Cell In[4], [line 4](vscode-notebook-cell:?execution_count=4&line=4)
      [2](vscode-notebook-cell:?execution_count=4&line=2) course = "Ethics"
      [3](vscode-notebook-cell:?execution_count=4&line=3) user_query = "Can you help me study for Ethics"
----> [4](vscode-notebook-cell:?execution_count=4&line=4) result = await check_guardrails(user_query, course)
      [5](vscode-notebook-cell:?execution_count=4&line=5) print(result)

Cell In[3], [line 8](vscode-notebook-cell:?execution_count=3&line=8)
      [6](vscode-notebook-cell:?execution_count=3&line=6) async def check_guardrails(query, course):
      [7](vscode-notebook-cell:?execution_count=3&line=7)     # Initialize rails config
----> [8](vscode-notebook-cell:?execution_count=3&line=8)     config = RailsConfig.from_path("./config")
      [9](vscode-notebook-cell:?execution_count=3&line=9)     rails = LLMRails(config)
     [11](vscode-notebook-cell:?execution_count=3&line=11)     # Use the async version of the generate method

File c:\Python312\Lib\site-packages\nemoguardrails\rails\llm\config.py:856, in RailsConfig.from_path(config_path)
    [852](file:///C:/Python312/Lib/site-packages/nemoguardrails/rails/llm/config.py:852)     raw_config["instructions"] = _default_config["instructions"]
    [854](file:///C:/Python312/Lib/site-packages/nemoguardrails/rails/llm/config.py:854) raw_config["config_path"] = config_path
--> [856](file:///C:/Python312/Lib/site-packages/nemoguardrails/rails/llm/config.py:856) return RailsConfig.parse_object(raw_config)

File c:\Python312\Lib\site-packages\nemoguardrails\rails\llm\config.py:926, in RailsConfig.parse_object(cls, obj)
    [921](file:///C:/Python312/Lib/site-packages/nemoguardrails/rails/llm/config.py:921)         if flow_data.get("elements") and not flow_data["elements"][0].get(
    [922](file:///C:/Python312/Lib/site-packages/nemoguardrails/rails/llm/config.py:922)             "_type"
    [923](file:///C:/Python312/Lib/site-packages/nemoguardrails/rails/llm/config.py:923)         ):
    [924](file:///C:/Python312/Lib/site-packages/nemoguardrails/rails/llm/config.py:924)             flow_data["elements"] = parse_flow_elements(flow_data["elements"])
...
    [511](https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/drew_/OneDrive/Desktop/prof/professorai-dev/llm/guardrails/~/AppData/Roaming/Python/Python312/site-packages/pydantic/main.py:511) )

ValidationError: 1 validation error for RailsConfig
  Value error, You must provide a `self_check_input` prompt template. [type=value_error, input_value={'user_messages': {'give ...onfig_path': './config'}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.6/v/value_error
Pouyanpi commented 1 month ago

Hi @Drewwb,

Apologies for the delay in responding. It seems that in your prompts, you are using the wrong task identifier. You should use self_check_input instead of self check input.