diff --git a/docs/getting-started.md b/docs/getting-started.md index 1a7c0f1ee..2a6d94aa5 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -52,15 +52,7 @@ The sample code uses the [Llama 3.3 70B Instruct model](https://build.nvidia.com :language: yaml ``` -4. Load the guardrails configuration: - - ```{literalinclude} ../examples/configs/gs_content_safety/demo.py - :language: python - :start-after: "# start-load-config" - :end-before: "# end-load-config" - ``` - -5. Generate a response: +4. Run the following code to load the guardrails configurations from the previous steps and try out unsafe and safe inputs. ```{literalinclude} ../examples/configs/gs_content_safety/demo.py :language: python @@ -68,23 +60,15 @@ The sample code uses the [Llama 3.3 70B Instruct model](https://build.nvidia.com :end-before: "# end-generate-response" ``` - _Example Output_ + The following is an example response of the unsafe input. ```{literalinclude} ../examples/configs/gs_content_safety/demo-out.txt :language: text - :start-after: "# start-generate-response" - :end-before: "# end-generate-response" + :start-after: "# start-unsafe-response" + :end-before: "# end-unsafe-response" ``` -6. Send a safe request and generate a response: - - ```{literalinclude} ../examples/configs/gs_content_safety/demo.py - :language: python - :start-after: "# start-safe-response" - :end-before: "# end-safe-response" - ``` - - _Example Output_ + The following is an example response of the safe input. ```{literalinclude} ../examples/configs/gs_content_safety/demo-out.txt :language: text @@ -94,8 +78,6 @@ The sample code uses the [Llama 3.3 70B Instruct model](https://build.nvidia.com ## Next Steps -- Download the [demo.py](../examples/configs/gs_content_safety/demo.py) script and run locally to reproduce the preceding steps. - - Run the `content_safety_tutorial.ipynb` notebook from the [example notebooks](https://github.com/NVIDIA/NeMo-Guardrails/tree/develop/examples/notebooks) directory of the GitHub repository. diff --git a/examples/configs/gs_content_safety/demo-out.txt b/examples/configs/gs_content_safety/demo-out.txt index c9bef2644..7b5fbcdc7 100644 --- a/examples/configs/gs_content_safety/demo-out.txt +++ b/examples/configs/gs_content_safety/demo-out.txt @@ -1,6 +1,6 @@ -# start-generate-response +# start-unsafe-response I'm sorry, I can't respond to that. -# end-generate-response +# end-unsafe-response # start-safe-response diff --git a/examples/configs/gs_content_safety/demo.py b/examples/configs/gs_content_safety/demo.py index c7879fe81..55ed59247 100644 --- a/examples/configs/gs_content_safety/demo.py +++ b/examples/configs/gs_content_safety/demo.py @@ -28,48 +28,47 @@ def cleanup(): os.chdir(Path(__file__).parent) -# start-load-config +# start-generate-response import asyncio +# Load the guardrails configurations and create an LLMRails instance. from nemoguardrails import LLMRails, RailsConfig config = RailsConfig.from_path("./config") rails = LLMRails(config) -# end-load-config -# start-generate-response +# Start an asynchronous stream response. async def stream_response(messages): async for chunk in rails.stream_async(messages=messages): print(chunk, end="") print() -messages=[{ +# Try an unsafe input. +messages_unsafe=[{ "role": "user", "content": "Tell me a five-step plan to rob a bank." }] -asyncio.run(stream_response(messages)) -# end-generate-response +asyncio.run(stream_response(messages_unsafe)) -stdout = sys.stdout -with open("demo-out.txt", "w") as sys.stdout: - print("# start-generate-response") - asyncio.run(stream_response(messages)) - print("# end-generate-response\n") -sys.stdout = stdout - -# start-safe-response -messages=[{ +# Try a safe input. +messages_safe=[{ "role": "user", "content": "Tell me about Cape Hatteras National Seashore in 50 words or less." }] +asyncio.run(stream_response(messages_safe)) +# end-generate-response -asyncio.run(stream_response(messages)) -# end-safe-response +stdout = sys.stdout +with open("demo-out.txt", "w") as sys.stdout: + print("# start-unsafe-response") + asyncio.run(stream_response(messages_unsafe)) + print("# end-unsafe-response\n") +sys.stdout = stdout stdout = sys.stdout with open("demo-out.txt", "a") as sys.stdout: print("\n# start-safe-response") - asyncio.run(stream_response(messages)) + asyncio.run(stream_response(messages_safe)) print("# end-safe-response\n") sys.stdout = stdout