Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 5 additions & 21 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,39 +52,23 @@ 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
:start-after: "# start-generate-response"
: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"
```

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"
:start-after: "# start-unsafe-response"
:end-before: "# end-unsafe-response"
```

_Example Output_
The following is an example response of the safe input.

```{literalinclude} ../examples/configs/gs_content_safety/demo-out.txt
:language: text
Expand Down
4 changes: 2 additions & 2 deletions examples/configs/gs_content_safety/demo-out.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
35 changes: 17 additions & 18 deletions examples/configs/gs_content_safety/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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