-
Notifications
You must be signed in to change notification settings - Fork 469
/
Copy pathconfig.py
1478 lines (1185 loc) · 51.6 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Module for the configuration of rails."""
import logging
import os
import re
import warnings
from enum import Enum
from typing import Any, Dict, List, Literal, Optional, Set, Tuple, Union
import yaml
from pydantic import (
BaseModel,
ConfigDict,
ValidationError,
model_validator,
root_validator,
)
from pydantic.fields import Field
from nemoguardrails import utils
from nemoguardrails.colang import parse_colang_file, parse_flow_elements
from nemoguardrails.colang.v2_x.lang.colang_ast import Flow
from nemoguardrails.colang.v2_x.lang.utils import format_colang_parsing_error_message
from nemoguardrails.colang.v2_x.runtime.errors import ColangParsingError
log = logging.getLogger(__name__)
# Load the default config values from the file
with open(os.path.join(os.path.dirname(__file__), "default_config.yml")) as _fc:
_default_config = yaml.safe_load(_fc)
with open(os.path.join(os.path.dirname(__file__), "default_config_v2.yml")) as _fc:
_default_config_v2 = yaml.safe_load(_fc)
# Extract the COLANGPATH directories.
colang_path_dirs = [
_path.strip()
for _path in os.environ.get("COLANGPATH", "").split(os.pathsep)
if _path.strip() != ""
]
# We also make sure that the standard library is in the COLANGPATH.
standard_library_path = os.path.normpath(
os.path.join(os.path.dirname(__file__), "..", "..", "colang", "v2_x", "library")
)
# nemoguardrails/library
guardrails_stdlib_path = os.path.normpath(
os.path.join(os.path.dirname(__file__), "..", "..", "..")
)
colang_path_dirs.append(standard_library_path)
colang_path_dirs.append(guardrails_stdlib_path)
class ReasoningModelConfig(BaseModel):
"""Configuration for reasoning models/LLMs, including start and end tokens for reasoning traces."""
remove_thinking_traces: Optional[bool] = Field(
default=True,
description="For reasoning models (e.g. OpenAI o1, DeepSeek-r1), if the output parser should remove thinking traces.",
)
start_token: Optional[str] = Field(
default="<think>",
description="The start token used for reasoning traces.",
)
end_token: Optional[str] = Field(
default="</think>",
description="The end token used for reasoning traces.",
)
class Model(BaseModel):
"""Configuration of a model used by the rails engine.
Typically, the main model is configured e.g.:
{
"type": "main",
"engine": "openai",
"model": "gpt-3.5-turbo-instruct"
}
"""
type: str
engine: str
model: Optional[str] = Field(
default=None,
description="The name of the model. If not specified, it should be specified through the parameters attribute.",
)
reasoning_config: Optional[ReasoningModelConfig] = Field(
default_factory=ReasoningModelConfig,
description="Configuration parameters for reasoning LLMs.",
)
parameters: Dict[str, Any] = Field(default_factory=dict)
mode: Literal["chat", "text"] = Field(
default="chat",
description="Whether the mode is 'text' completion or 'chat' completion. Allowed values are 'chat' or 'text'.",
)
@model_validator(mode="before")
@classmethod
def set_and_validate_model(cls, data: Any) -> Any:
if isinstance(data, dict):
parameters = data.get("parameters")
if parameters is None:
return data
model_field = data.get("model")
model_from_params = parameters.get("model_name") or parameters.get("model")
if model_field and model_from_params:
raise ValueError(
"Model name must be specified in exactly one place: either in the 'model' field or in parameters, not both."
)
if not model_field and model_from_params:
data["model"] = model_from_params
if (
"model_name" in parameters
and parameters["model_name"] == model_from_params
):
parameters.pop("model_name")
elif "model" in parameters and parameters["model"] == model_from_params:
parameters.pop("model")
return data
@model_validator(mode="after")
def model_must_be_none_empty(self) -> "Model":
"""Validate that a model name is present either directly or in parameters."""
if not self.model or not self.model.strip():
raise ValueError(
"Model name must be specified either directly in the 'model' field or through 'model_name'/'model' in parameters"
)
return self
class Instruction(BaseModel):
"""Configuration for instructions in natural language that should be passed to the LLM."""
type: str
content: str
class Document(BaseModel):
"""Configuration for documents that should be used for question answering."""
format: str
content: str
class SensitiveDataDetectionOptions(BaseModel):
entities: List[str] = Field(
default_factory=list,
description="The list of entities that should be detected. "
"Check out https://microsoft.github.io/presidio/supported_entities/ for"
"the list of supported entities.",
)
# TODO: this is not currently in use.
mask_token: str = Field(
default="*",
description="The token that should be used to mask the sensitive data.",
)
score_threshold: float = Field(
default=0.2,
description="The score threshold that should be used to detect the sensitive data.",
)
class SensitiveDataDetection(BaseModel):
"""Configuration of what sensitive data should be detected."""
recognizers: List[dict] = Field(
default_factory=list,
description="Additional custom recognizers. "
"Check out https://microsoft.github.io/presidio/tutorial/08_no_code/ for more details.",
)
input: SensitiveDataDetectionOptions = Field(
default_factory=SensitiveDataDetectionOptions,
description="Configuration of the entities to be detected on the user input.",
)
output: SensitiveDataDetectionOptions = Field(
default_factory=SensitiveDataDetectionOptions,
description="Configuration of the entities to be detected on the bot output.",
)
retrieval: SensitiveDataDetectionOptions = Field(
default_factory=SensitiveDataDetectionOptions,
description="Configuration of the entities to be detected on retrieved relevant chunks.",
)
class PrivateAIDetectionOptions(BaseModel):
"""Configuration options for Private AI."""
entities: List[str] = Field(
default_factory=list,
description="The list of entities that should be detected.",
)
class PrivateAIDetection(BaseModel):
"""Configuration for Private AI."""
server_endpoint: str = Field(
default="http://localhost:8080/process/text",
description="The endpoint for the private AI detection server.",
)
input: PrivateAIDetectionOptions = Field(
default_factory=PrivateAIDetectionOptions,
description="Configuration of the entities to be detected on the user input.",
)
output: PrivateAIDetectionOptions = Field(
default_factory=PrivateAIDetectionOptions,
description="Configuration of the entities to be detected on the bot output.",
)
retrieval: PrivateAIDetectionOptions = Field(
default_factory=PrivateAIDetectionOptions,
description="Configuration of the entities to be detected on retrieved relevant chunks.",
)
class FiddlerGuardrails(BaseModel):
"""Configuration for Fiddler Guardrails."""
fiddler_endpoint: str = Field(
default="http://localhost:8080/process/text",
description="The global endpoint for Fiddler Guardrails requests.",
)
safety_threshold: float = Field(
default=0.1,
description="Fiddler Guardrails safety detection threshold.",
)
faithfulness_threshold: float = Field(
default=0.05,
description="Fiddler Guardrails faithfulness detection threshold.",
)
class MessageTemplate(BaseModel):
"""Template for a message structure."""
type: str = Field(
description="The type of message, e.g., 'assistant', 'user', 'system'."
)
content: str = Field(description="The content of the message.")
class TaskPrompt(BaseModel):
"""Configuration for prompts that will be used for a specific task."""
task: str = Field(description="The id of the task associated with this prompt.")
content: Optional[str] = Field(
default=None, description="The content of the prompt, if it's a string."
)
messages: Optional[List[Union[MessageTemplate, str]]] = Field(
default=None,
description="The list of messages included in the prompt. Used for chat models.",
)
models: Optional[List[str]] = Field(
default=None,
description="If specified, the prompt will be used only for the given LLM engines/models. "
"The format is a list of strings with the format: <engine> or <engine>/<model>.",
)
output_parser: Optional[str] = Field(
default=None,
description="The name of the output parser to use for this prompt.",
)
max_length: Optional[int] = Field(
default=16000,
description="The maximum length of the prompt in number of characters.",
)
mode: Optional[str] = Field(
default=_default_config["prompting_mode"],
description="Corresponds to the `prompting_mode` for which this prompt is fetched. Default is 'standard'.",
)
stop: Optional[List[str]] = Field(
default=None,
description="If specified, will be configure stop tokens for models that support this.",
)
max_tokens: Optional[int] = Field(
default=None,
description="The maximum number of tokens that can be generated in the chat completion.",
)
@root_validator(pre=True, allow_reuse=True)
def check_fields(cls, values):
if not values.get("content") and not values.get("messages"):
raise ValidationError("One of `content` or `messages` must be provided.")
if values.get("content") and values.get("messages"):
raise ValidationError(
"Only one of `content` or `messages` must be provided."
)
return values
class LogAdapterConfig(BaseModel):
name: str = Field(default="FileSystem", description="The name of the adapter.")
model_config = ConfigDict(extra="allow")
class TracingConfig(BaseModel):
enabled: bool = False
adapters: List[LogAdapterConfig] = Field(
default_factory=lambda: [LogAdapterConfig()],
description="The list of tracing adapters to use. If not specified, the default adapters are used.",
)
class EmbeddingsCacheConfig(BaseModel):
"""Configuration for the caching embeddings."""
enabled: bool = Field(
default=False,
description="Whether caching of the embeddings should be enabled or not.",
)
key_generator: str = Field(
default="sha256",
description="The method to use for generating the cache keys.",
)
store: str = Field(
default="filesystem",
description="What type of store to use for the cached embeddings.",
)
store_config: Dict[str, Any] = Field(
default_factory=dict,
description="Any additional configuration options required for the store. "
"For example, path for `filesystem` or `host`/`port`/`db` for redis.",
)
def to_dict(self):
return self.dict()
class EmbeddingSearchProvider(BaseModel):
"""Configuration of a embedding search provider."""
name: str = Field(
default="default",
description="The name of the embedding search provider. If not specified, default is used.",
)
parameters: Dict[str, Any] = Field(default_factory=dict)
cache: EmbeddingsCacheConfig = Field(default_factory=EmbeddingsCacheConfig)
class KnowledgeBaseConfig(BaseModel):
folder: str = Field(
default="kb",
description="The folder from which the documents should be loaded.",
)
embedding_search_provider: EmbeddingSearchProvider = Field(
default_factory=EmbeddingSearchProvider,
description="The search provider used to search the knowledge base.",
)
class CoreConfig(BaseModel):
"""Settings for core internal mechanics."""
embedding_search_provider: EmbeddingSearchProvider = Field(
default_factory=EmbeddingSearchProvider,
description="The search provider used to search the most similar canonical forms/flows.",
)
class InputRails(BaseModel):
"""Configuration of input rails."""
flows: List[str] = Field(
default_factory=list,
description="The names of all the flows that implement input rails.",
)
class OutputRailsStreamingConfig(BaseModel):
"""Configuration for managing streaming output of LLM tokens."""
enabled: bool = Field(
default=False, description="Enables streaming mode when True."
)
chunk_size: int = Field(
default=200,
description="The number of tokens in each processing chunk. This is the size of the token block on which output rails are applied.",
)
context_size: int = Field(
default=50,
description="The number of tokens carried over from the previous chunk to provide context for continuity in processing.",
)
stream_first: bool = Field(
default=True,
description="If True, token chunks are streamed immediately before output rails are applied.",
)
model_config = ConfigDict(extra="allow")
class OutputRails(BaseModel):
"""Configuration of output rails."""
flows: List[str] = Field(
default_factory=list,
description="The names of all the flows that implement output rails.",
)
streaming: Optional[OutputRailsStreamingConfig] = Field(
default_factory=OutputRailsStreamingConfig,
description="Configuration for streaming output rails.",
)
class RetrievalRails(BaseModel):
"""Configuration of retrieval rails."""
flows: List[str] = Field(
default_factory=list,
description="The names of all the flows that implement retrieval rails.",
)
class ActionRails(BaseModel):
"""Configuration of action rails.
Action rails control various options related to the execution of actions.
Currently, only
In the future multiple options will be added, e.g., what input validation should be
performed per action, output validation, throttling, disabling, etc.
"""
instant_actions: Optional[List[str]] = Field(
default=None,
description="The names of all actions which should finish instantly.",
)
class SingleCallConfig(BaseModel):
"""Configuration for the single LLM call option for topical rails."""
enabled: bool = False
fallback_to_multiple_calls: bool = Field(
default=True,
description="Whether to fall back to multiple calls if a single call is not possible.",
)
class UserMessagesConfig(BaseModel):
"""Configuration for how the user messages are interpreted."""
embeddings_only: bool = Field(
default=False,
description="Whether to use only embeddings for computing the user canonical form messages.",
)
embeddings_only_similarity_threshold: Optional[float] = Field(
default=None,
ge=0,
le=1,
description="The similarity threshold to use when using only embeddings for computing the user canonical form messages.",
)
embeddings_only_fallback_intent: Optional[str] = Field(
default=None,
description="Defines the fallback intent when the similarity is below the threshold. If set to None, the user intent is computed normally using the LLM. If set to a string value, that string is used as the intent.",
)
class DialogRails(BaseModel):
"""Configuration of topical rails."""
single_call: SingleCallConfig = Field(
default_factory=SingleCallConfig,
description="Configuration for the single LLM call option.",
)
user_messages: UserMessagesConfig = Field(
default_factory=UserMessagesConfig,
description="Configuration for how the user messages are interpreted.",
)
class FactCheckingRailConfig(BaseModel):
"""Configuration data for the fact-checking rail."""
parameters: Dict[str, Any] = Field(default_factory=dict)
fallback_to_self_check: bool = Field(
default=False,
description="Whether to fall back to self-check if another method fail.",
)
class JailbreakDetectionConfig(BaseModel):
"""Configuration data for jailbreak detection."""
server_endpoint: Optional[str] = Field(
default=None,
description="The endpoint for the jailbreak detection heuristics server.",
)
length_per_perplexity_threshold: float = Field(
default=89.79, description="The length/perplexity threshold."
)
prefix_suffix_perplexity_threshold: float = Field(
default=1845.65, description="The prefix/suffix perplexity threshold."
)
nim_url: Optional[str] = Field(
default=None,
description="Location of the NemoGuard JailbreakDetect NIM.",
)
nim_port: int = Field(
default=8000,
description="Port the NemoGuard JailbreakDetect NIM is listening on.",
)
embedding: Optional[str] = Field(
default="nvidia/nv-embedqa-e5-v5",
description="DEPRECATED: Model to use for embedding-based detections. Use NIM instead.",
deprecated=True,
)
class AutoAlignOptions(BaseModel):
"""List of guardrails that are activated"""
guardrails_config: Dict[str, Any] = Field(
default_factory=dict,
description="The guardrails configuration that is passed to the AutoAlign endpoint",
)
class AutoAlignRailConfig(BaseModel):
"""Configuration data for the AutoAlign API"""
parameters: Dict[str, Any] = Field(default_factory=dict)
input: AutoAlignOptions = Field(
default_factory=AutoAlignOptions,
description="Input configuration for AutoAlign guardrails",
)
output: AutoAlignOptions = Field(
default_factory=AutoAlignOptions,
description="Output configuration for AutoAlign guardrails",
)
class PatronusEvaluationSuccessStrategy(str, Enum):
"""
Strategy for determining whether a Patronus Evaluation API
request should pass, especially when multiple evaluators
are called in a single request.
ALL_PASS requires all evaluators to pass for success.
ANY_PASS requires only one evaluator to pass for success.
"""
ALL_PASS = "all_pass"
ANY_PASS = "any_pass"
class PatronusEvaluateApiParams(BaseModel):
"""Config to parameterize the Patronus Evaluate API call"""
success_strategy: Optional[PatronusEvaluationSuccessStrategy] = Field(
default=PatronusEvaluationSuccessStrategy.ALL_PASS,
description="Strategy to determine whether the Patronus Evaluate API Guardrail passes or not.",
)
params: Dict[str, Any] = Field(
default_factory=dict,
description="Parameters to the Patronus Evaluate API",
)
class PatronusEvaluateConfig(BaseModel):
"""Config for the Patronus Evaluate API call"""
evaluate_config: PatronusEvaluateApiParams = Field(
default_factory=PatronusEvaluateApiParams,
description="Configuration passed to the Patronus Evaluate API",
)
class PatronusRailConfig(BaseModel):
"""Configuration data for the Patronus Evaluate API"""
input: Optional[PatronusEvaluateConfig] = Field(
default_factory=PatronusEvaluateConfig,
description="Patronus Evaluate API configuration for an Input Guardrail",
)
output: Optional[PatronusEvaluateConfig] = Field(
default_factory=PatronusEvaluateConfig,
description="Patronus Evaluate API configuration for an Output Guardrail",
)
class RailsConfigData(BaseModel):
"""Configuration data for specific rails that are supported out-of-the-box."""
fact_checking: FactCheckingRailConfig = Field(
default_factory=FactCheckingRailConfig,
description="Configuration data for the fact-checking rail.",
)
autoalign: AutoAlignRailConfig = Field(
default_factory=AutoAlignRailConfig,
description="Configuration data for the AutoAlign guardrails API.",
)
patronus: Optional[PatronusRailConfig] = Field(
default_factory=PatronusRailConfig,
description="Configuration data for the Patronus Evaluate API.",
)
sensitive_data_detection: Optional[SensitiveDataDetection] = Field(
default_factory=SensitiveDataDetection,
description="Configuration for detecting sensitive data.",
)
jailbreak_detection: Optional[JailbreakDetectionConfig] = Field(
default_factory=JailbreakDetectionConfig,
description="Configuration for jailbreak detection.",
)
privateai: Optional[PrivateAIDetection] = Field(
default_factory=PrivateAIDetection,
description="Configuration for Private AI.",
)
fiddler: Optional[FiddlerGuardrails] = Field(
default_factory=FiddlerGuardrails,
description="Configuration for Fiddler Guardrails.",
)
class Rails(BaseModel):
"""Configuration of specific rails."""
config: RailsConfigData = Field(
default_factory=RailsConfigData,
description="Configuration data for specific rails that are supported out-of-the-box.",
)
input: InputRails = Field(
default_factory=InputRails, description="Configuration of the input rails."
)
output: OutputRails = Field(
default_factory=OutputRails, description="Configuration of the output rails."
)
retrieval: RetrievalRails = Field(
default_factory=RetrievalRails,
description="Configuration of the retrieval rails.",
)
dialog: DialogRails = Field(
default_factory=DialogRails, description="Configuration of the dialog rails."
)
actions: ActionRails = Field(
default_factory=ActionRails, description="Configuration of action rails."
)
def merge_two_dicts(dict_1: dict, dict_2: dict, ignore_keys: Set[str]) -> None:
"""Merges the fields of two dictionaries recursively."""
for key, value in dict_2.items():
if key not in ignore_keys:
if key in dict_1:
if isinstance(dict_1[key], dict) and isinstance(value, dict):
merge_two_dicts(dict_1[key], value, set())
elif dict_1[key] != value:
log.warning(
"Conflicting fields with same name '%s' in yaml config files detected!",
key,
)
else:
dict_1[key] = value
def _join_config(dest_config: dict, additional_config: dict):
"""Helper to join two configuration."""
dest_config["user_messages"] = {
**dest_config.get("user_messages", {}),
**additional_config.get("user_messages", {}),
}
dest_config["bot_messages"] = {
**dest_config.get("bot_messages", {}),
**additional_config.get("bot_messages", {}),
}
dest_config["instructions"] = dest_config.get(
"instructions", []
) + additional_config.get("instructions", [])
dest_config["flows"] = dest_config.get("flows", []) + additional_config.get(
"flows", []
)
dest_config["models"] = dest_config.get("models", []) + additional_config.get(
"models", []
)
dest_config["prompts"] = dest_config.get("prompts", []) + additional_config.get(
"prompts", []
)
dest_config["docs"] = dest_config.get("docs", []) + additional_config.get(
"docs", []
)
dest_config["actions_server_url"] = dest_config.get(
"actions_server_url", None
) or additional_config.get("actions_server_url", None)
dest_config["sensitive_data_detection"] = {
**dest_config.get("sensitive_data_detection", {}),
**additional_config.get("sensitive_data_detection", {}),
}
dest_config["embedding_search_provider"] = dest_config.get(
"embedding_search_provider", {}
) or additional_config.get("embedding_search_provider", {})
# We join the arrays and keep only unique elements for import paths.
dest_config["import_paths"] = dest_config.get("import_paths", [])
for import_path in additional_config.get("import_paths", []):
if import_path not in dest_config["import_paths"]:
dest_config["import_paths"].append(import_path)
additional_fields = [
"sample_conversation",
"lowest_temperature",
"enable_multi_step_generation",
"colang_version",
"event_source_uid",
"custom_data",
"prompting_mode",
"knowledge_base",
"core",
"rails",
"streaming",
"passthrough",
"raw_llm_call_action",
"enable_rails_exceptions",
"tracing",
]
for field in additional_fields:
if field in additional_config:
dest_config[field] = additional_config[field]
# TODO: Rethink the best way to parse and load yaml config files
ignore_fields = set(additional_fields).union(
{
"user_messages",
"bot_messages",
"instructions",
"flows",
"models",
"prompts",
"docs",
"actions_server_url",
"sensitive_data_detection",
"embedding_search_provider",
"import_paths",
}
)
# Reads all the other fields and merges them with the custom_data field
merge_two_dicts(
dest_config.get("custom_data", {}), additional_config, ignore_fields
)
def _load_path(
config_path: str,
) -> Tuple[dict, List[Tuple[str, str]]]:
"""Load a configuration object from the specified path.
Args:
config_path: The path from which to load.
Returns:
(raw_config, colang_files) The raw config object and the list of colang files.
"""
raw_config = {}
# The names of the colang files.
colang_files = []
if not os.path.exists(config_path):
raise ValueError(f"Could not find config path: {config_path}")
# the first .railsignore file found from cwd down to its subdirectories
railsignore_path = utils.get_railsignore_path(config_path)
ignore_patterns = utils.get_railsignore_patterns(railsignore_path)
if os.path.isdir(config_path):
for root, _, files in os.walk(config_path, followlinks=True):
# Followlinks to traverse symlinks instead of ignoring them.
for file in files:
# Verify railsignore to skip loading
ignored_by_railsignore = utils.is_ignored_by_railsignore(
file, ignore_patterns
)
if ignored_by_railsignore:
continue
# This is the raw configuration that will be loaded from the file.
_raw_config = {}
# Extract the full path for the file and compute relative path
full_path = os.path.join(root, file)
rel_path = os.path.relpath(full_path, config_path)
# If it's a file in the `kb` folder we need to append it to the docs
if rel_path.startswith("kb"):
_raw_config = {"docs": []}
if rel_path.endswith(".md"):
with open(full_path, encoding="utf-8") as f:
_raw_config["docs"].append(
{"format": "md", "content": f.read()}
)
elif file.endswith(".yml") or file.endswith(".yaml"):
with open(full_path, "r", encoding="utf-8") as f:
_raw_config = yaml.safe_load(f.read())
elif file.endswith(".co"):
colang_files.append((file, full_path))
_join_config(raw_config, _raw_config)
# If it's just a .co file, we append it as is to the config.
elif config_path.endswith(".co"):
colang_files.append((config_path, config_path))
return raw_config, colang_files
def _load_imported_paths(raw_config: dict, colang_files: List[Tuple[str, str]]):
"""Load recursively all the imported path in the specified raw_config.
Args:
raw_config: The starting raw configuration (i.e., a dict)
colang_files: The current set of colang files which will be extended as new
configurations are loaded.
"""
# We also keep a temporary array of all the paths that have been imported
if "imported_paths" not in raw_config:
raw_config["imported_paths"] = {}
while len(raw_config["imported_paths"]) != len(raw_config["import_paths"]):
for import_path in raw_config["import_paths"]:
if import_path in raw_config["imported_paths"]:
continue
log.info(f"Loading imported path: {import_path}")
# If the path does not exist, we try to resolve it using COLANGPATH
actual_path = None
if not os.path.exists(import_path):
for root in colang_path_dirs:
if os.path.exists(os.path.join(root, import_path)):
actual_path = os.path.join(root, import_path)
break
# We also check if we can load it as a file.
if not import_path.endswith(".co") and os.path.exists(
os.path.join(root, import_path + ".co")
):
actual_path = os.path.join(root, import_path + ".co")
break
else:
actual_path = import_path
if actual_path is None:
formated_import_path = import_path.replace("/", ".")
raise ValueError(
f"Import path '{formated_import_path}' could not be resolved.",
)
_raw_config, _colang_files = _load_path(actual_path)
# Join them.
_join_config(raw_config, _raw_config)
colang_files.extend(_colang_files)
# And mark the path as imported.
raw_config["imported_paths"][import_path] = actual_path
def _parse_colang_files_recursively(
raw_config: dict,
colang_files: List[Tuple[str, str]],
parsed_colang_files: List[dict],
):
"""Helper function to parse all the Colang files.
If there are imports, they will be imported recursively
"""
colang_version = raw_config.get("colang_version", "1.0")
_rails_parsed_config = None
# We start parsing the colang files one by one, and if we have
# new import paths, we continue to update
while len(parsed_colang_files) != len(colang_files):
current_file, current_path = colang_files[len(parsed_colang_files)]
with open(current_path, "r", encoding="utf-8") as f:
try:
content = f.read()
_parsed_config = parse_colang_file(
current_file, content=content, version=colang_version
)
except ValueError as e:
raise ColangParsingError(
f"Unsupported colang version {colang_version} for file: {current_path}"
) from e
except Exception as e:
raise ColangParsingError(
f"Error while parsing Colang file: {current_path}\n"
+ format_colang_parsing_error_message(e, content)
) from e
# We join only the "import_paths" field in the config for now
_join_config(
raw_config,
{"import_paths": _parsed_config.get("import_paths", [])},
)
parsed_colang_files.append(_parsed_config)
# If there are any new imports, we load them
if raw_config.get("import_paths"):
_load_imported_paths(raw_config, colang_files)
if colang_version == "2.x" and _has_input_output_config_rails(raw_config):
# raise deprecation warning
rails_flows = _get_rails_flows(raw_config)
flow_definitions = "\n".join(_generate_rails_flows(rails_flows))
current_file = "INTRINSIC_FLOW_GENERATION"
_rails_parsed_config = parse_colang_file(
current_file, content=flow_definitions, version=colang_version
)
_DOCUMENTATION_LINK = "https://docs.nvidia.com/nemo/guardrails/colang-2/getting-started/dialog-rails.html" # Replace with the actual documentation link
warnings.warn(
"Configuring input/output rails in config.yml is deprecated. "
"Please use the new flow-based configuration instead. "
f"For more information, please refer to the documentation at {_DOCUMENTATION_LINK}. "
f"Here is the expected usage:\n{flow_definitions}",
FutureWarning,
)
if _rails_parsed_config:
parsed_colang_files.append(_rails_parsed_config)
# To allow overriding of elements from imported paths, we need to merge the
# parsed data in reverse order.
for file_parsed_data in reversed(parsed_colang_files):
_join_config(raw_config, file_parsed_data)
class RailsConfig(BaseModel):
"""Configuration object for the models and the rails.
TODO: add typed config for user_messages, bot_messages, and flows.
"""
models: List[Model] = Field(
description="The list of models used by the rails configuration."
)
user_messages: Dict[str, List[str]] = Field(
default_factory=dict,
description="The list of user messages that should be used for the rails.",
)
bot_messages: Dict[str, List[str]] = Field(
default_factory=dict,
description="The list of bot messages that should be used for the rails.",
)
# NOTE: the Any below is used to get rid of a warning with pydantic 1.10.x;
# The correct typing should be List[Dict, Flow]. To be updated when
# support for pydantic 1.10.x is dropped.
flows: List[Union[Dict, Any]] = Field(
default_factory=list,
description="The list of flows that should be used for the rails.",