Skip to content

Commit dc509f6

Browse files
authored
Merge pull request #689 from NVIDIA/feature/colang-2.0-library
Colang 2.0 - Guardrails Library
2 parents 1e38fbf + 4760e35 commit dc509f6

File tree

47 files changed

+2414
-460
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2414
-460
lines changed

docs/user_guides/cli.md

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
**NOTE: THIS SECTION IS WORK IN PROGRESS.**
44

55
## Guardrails CLI
6+
67
For testing purposes, the Guardrails toolkit provides a command line chat that can be used to interact with the LLM.
8+
79
```
810
> nemoguardrails chat --config examples/ [--verbose] [--verbose-llm-calls]
911
```
12+
1013
## Options
14+
1115
- `--config`: The configuration that should be used. Can be a folder or a .co/.yml file.
1216
- `--verbose`: In verbose mode, detailed debugging information is also shown.
1317
- `--verbose-llm-calls`: In verbose LLM calls mode, the debugging information includes the entire prompt that is sent to the LLM and the completion.
1418

15-
16-
7. You should now be able to invoke the `nemoguardrails` CLI.
19+
You should now be able to invoke the `nemoguardrails` CLI.
1720

1821
```bash
1922
> nemoguardrails --help
@@ -31,11 +34,14 @@ For testing purposes, the Guardrails toolkit provides a command line chat that c
3134
Commands:
3235
actions-server Starts a NeMo Guardrails actions server.
3336
chat Starts an interactive chat session.
37+
convert Convert a Colang 1.0 directory to Colang 2.0 format.
38+
evaluate Run an evaluation task.
3439
server Starts a NeMo Guardrails server.
3540
```
3641

3742
You can also use the `--help` flag to learn more about each of the `nemoguardrails` commands:
3843

44+
#### actions-server
3945
```bash
4046
> nemoguardrails actions-server --help
4147

@@ -48,6 +54,7 @@ For testing purposes, the Guardrails toolkit provides a command line chat that c
4854
--help Show this message and exit.
4955
```
5056

57+
#### chat
5158
```bash
5259
> nemoguardrails chat --help
5360

@@ -82,15 +89,54 @@ For testing purposes, the Guardrails toolkit provides a command line chat that c
8289
[default: None]
8390
--help Show this message and exit.
8491
```
92+
#### server
93+
```bash
94+
> nemoguardrails server --help
95+
96+
Usage: nemoguardrails server [OPTIONS]
97+
98+
Starts a NeMo Guardrails server.
99+
100+
Options:
101+
--port INTEGER The port that the server should listen on. [default: 8000]
102+
--config TEXT Path to a directory containing multiple configuration sub-folders.
103+
--verbose --no-verbose: If the server should be verbose and output detailed logs including prompts. [default: no-verbose]
104+
--disable-chat-ui --no-disable-chat-ui Weather the ChatUI should be disabled [default: no-disable-chat-ui]
105+
--auto-reload --no-auto-reload Enable auto reload option. [default: no-auto-reload]
106+
--prefix TEXT A prefix that should be added to all server paths. Should start with '/'.
107+
--help Show this message and exit.
108+
```
85109
86-
```bash
87-
> nemoguardrails server --help
110+
#### evaluate
111+
```bash
112+
> nemoguardrails evaluate --help
88113

89-
Usage: nemoguardrails server [OPTIONS]
114+
Usage: nemoguardrails evaluate [OPTIONS] COMMAND [ARGS]...
90115

91-
Starts a NeMo Guardrails server.
116+
Options:
117+
--help: Show this message and exit.
92118

93-
Options:
94-
--port INTEGER The port that the server should listen on. [default: 8000]
95-
--help Show this message and exit.
96-
```
119+
Commands:
120+
fact-checking: Evaluate the performance of the fact-checking rails defined in a Guardrails application.
121+
hallucination: Evaluate the performance of the hallucination rails defined in a Guardrails application.
122+
moderation: Evaluate the performance of the moderation rails defined in a Guardrails application.
123+
topical: Evaluates the performance of the topical rails defined in a Guardrails application. Computes accuracy for canonical form detection, next step generation, and next bot message generation. Only a single Guardrails application can be specified in the config option.
124+
```
125+
126+
#### convert
127+
```bash
128+
> nemoguardrails convert --help
129+
130+
Usage: nemoguardrails convert [OPTIONS] PATH
131+
132+
Convert a Colang 1.0 directory to Colang 2.0.
133+
134+
Arguments:
135+
path TEXT The path to the file or directory to migrate. [default: None] [required]
136+
137+
Options:
138+
--verbose --no-verbose If the migration should be verbose and output detailed logs. [default: no-verbose]
139+
--validate --no-validate If the migration should validate the output using Colang Parser. [default: no-validate]
140+
--use-active-decorator --no-use-active-decorator If the migration should use the active decorator. [default: use-active-decorator]
141+
--help Show this message and exit.
142+
```

docs/user_guides/migration_guide.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# Migrating from Colang 1 to Colang 2
2+
3+
The NeMo Guardrails CLI provides a tool (`nemoguardrails convert ...`) for converting guardrail configurations from Colang 1.0 format to Colang 2.x.
4+
It performs several syntax transformations on the content of the files, such as converting certain keywords and changing the case of certain identifiers.
5+
6+
```{warning}
7+
There are edge cases not convered by the conversion tool. You should always manually review a guardrail configuration after conversion.
8+
```
9+
10+
## How it Works
11+
12+
The tool walks through the directory specified by the `path` argument and applies the transformations to each file ending with `.co`. The transformations include:
13+
14+
- Converting `define flow` to `flow`.
15+
- Converting `define subflow` to `flow`.
16+
- Converting `define bot` to `flow bot`.
17+
- Converting `define user` to `flow user`.
18+
- Converting `execute` to `await`.
19+
- Converting snake_case identifiers after `await` to CamelCase and append it with key word `Action` while preserving any arguments used..
20+
- Converting `else when` to `or when`.
21+
- Converting `stop` to `abort`.
22+
- Converting quoted strings after `bot` to `bot say` or `or bot say`.
23+
- Converting quoted strings after `user` to `user said` or `or user said`.
24+
- Convert anonymous flows.
25+
- Use `active` decorator for root flows (flows that need activation).
26+
- Add `global` keyword to global variables.
27+
- Converting `...` to corresponding syntax in Colang 2.
28+
29+
### Generation operator
30+
31+
The syntax for the generation operation has changed slightly between Colang 1.0 and 2.0. The following Colang 1.0 snippet:
32+
33+
```colang
34+
# some instruction in natural language
35+
$var_name = ...
36+
```
37+
38+
It is translated to:
39+
40+
```colang
41+
$name = ... "some instruction in natural language"
42+
```
43+
44+
### Generic Matching
45+
46+
In Colang 1.0 it was possible to use generic matching using `user ...` and `bot ...`.
47+
These have a different equivalent in Colang 2.0. The `...` can no longer be used for matching, only for generation.
48+
49+
The following changes are applied during conversion:
50+
- `user ...` is translated to `user said something`
51+
- `bot ...` is translated to `bot said something`
52+
53+
### Rails Configuration
54+
55+
Colang 1.0 configuration can define certain rails in the `rails` field.
56+
- If rails are defined in `config.yml` a `_rails.co` file is generated with the rails defined in it.
57+
58+
### Example Flow conversion
59+
60+
As an example, the following flow:
61+
62+
```colang
63+
define flow
64+
user express greeting
65+
bot express greeting
66+
```
67+
68+
is converted to:
69+
70+
```colang
71+
@active
72+
flow express_greeting
73+
user express greeting
74+
bot express greeting
75+
```
76+
77+
```{note}
78+
It is a convention to use past tense for user flows and present tense for bot flows. However, the migration tool does not enforce this convention.
79+
```
80+
81+
The tool keeps track of the number of lines processed and the number of changes made in each file. It also counts the total number of files changed.
82+
83+
```{warning}
84+
The tool modifies the original files. It is recommended to use version control to track the changes made by the tool. It also enables you to see the differences between the original and modified files.
85+
```
86+
87+
## Usage
88+
89+
To use the conversion tool, use the following command:
90+
91+
```bash
92+
nemoguardrails convert /path/to/directory
93+
```
94+
95+
The `convert` command has several options:
96+
97+
- `--verbose` or `--no-verbose`: If the migration should be verbose and output detailed logs. Default is `no-verbose`.
98+
- `--validate` or `--no-validate`: If the migration should validate the output using Colang Parser. Default is `no-validate`.
99+
- `--use-active-decorator` or `--no-use-active-decorator`: If the migration should use the `active` decorator. Default is `use-active-decorator`.
100+
101+
## Assumptions and Limitations
102+
103+
- The tool assumes that the input files are correctly formatted. If a file is not correctly formatted, the tool may not work as expected.
104+
- The tool uses regular expressions to find and replace certain patterns in the text. If the input files contain text that matches these patterns but should not be replaced, the tool may produce incorrect results.
105+
- The tool renames the original files and writes the transformed content to new files with the original names. Use version control to track the changes made by the tool.
106+
- The tool does not handle errors that may occur during file reading and writing operations. If an error occurs, the tool logs the error and continues with the next file.
107+
- Using characters like `-`, `+` and tokens like `or`, `and`, etc. is not supported in flow definition, the migration tool does not handle this conversion.
108+
- It is a better practice to define global variables at the beginning of the flow. However, the migration tool does not enforce this.

examples/bots/abc/_rails.co

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import guardrails
2+
import nemoguardrails.library
3+
4+
flow input rails $input_text
5+
self check input
6+
7+
flow output rails $output_text
8+
self check output

examples/bots/abc/config.yml

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,24 @@ instructions:
77
If the bot does not know the answer to a question, it truthfully says it does not know.
88
99
sample_conversation: |
10-
user "Hi there. Can you help me with some questions I have about the company?"
11-
express greeting and ask for assistance
12-
bot express greeting and confirm and offer assistance
13-
"Hi there! I'm here to help answer any questions you may have about the ABC Company. What would you like to know?"
14-
user "What's the company policy on paid time off?"
15-
ask question about benefits
16-
bot respond to question about benefits
17-
"The ABC Company provides eligible employees with up to two weeks of paid vacation time per year, as well as five paid sick days per year. Please refer to the employee handbook for more information."
10+
user action: user said "Hi there. Can you help me with some questions I have about the company?"
11+
user intent: user express greeting and ask for assistance
12+
bot intent: bot express greeting and confirm and offer assistance
13+
bot action: bot say "Hi there! I'm here to help answer any questions you may have about the ABC Company. What would you like to know?"
14+
user action: user said "What's the company policy on paid time off?"
15+
user intent: user ask question about benefits
16+
bot intent: bot respond to question about benefits
17+
bot action: bot say "The ABC Company provides eligible employees with up to two weeks of paid vacation time per year, as well as five paid sick days per year. Please refer to the employee handbook for more information."
18+
1819
1920
models:
2021
- type: main
2122
engine: openai
2223
model: gpt-3.5-turbo-instruct
2324

2425
rails:
25-
input:
26-
flows:
27-
- self check input
28-
29-
output:
30-
flows:
31-
- self check output
32-
3326
dialog:
3427
single_call:
3528
enabled: False
29+
30+
colang_version: "2.x"

examples/bots/abc/main.co

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import core
2+
import llm
3+
4+
flow main
5+
activate llm continuation

0 commit comments

Comments
 (0)