Sourcecode Detection
SourceCodeDetectionGuardrail
Bases: Guardrail
A guardrail that uses a pre-trained text-classification model to classify prompts to detect source code within the prompts.
Attributes:
Name | Type | Description |
---|---|---|
device |
str
|
The device to run the model on, default is 'cpu'. |
model_name_or_path |
str
|
The path or name of the pre-trained model. |
_classifier |
Any
|
The classifier pipeline for text classification. |
_label2id |
dict[str, int]
|
A dictionary mapping labels to IDs. |
Source code in safeguards/guardrails/sourcecode_detection/sourcecode_detection.py
guard(prompt)
Guards the given prompt by scoring it and determining if it is safe.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt
|
str
|
The prompt to be guarded. |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
dict[str, Any]
|
A dictionary containing the safety status and a summary of the result. |
Source code in safeguards/guardrails/sourcecode_detection/sourcecode_detection.py
predict(prompt)
Predicts the safety of the given prompt.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt
|
str
|
The prompt to be predicted. |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
dict[str, Any]
|
The result of the guard method. |
Source code in safeguards/guardrails/sourcecode_detection/sourcecode_detection.py
score_texts(text)
Scores the given text to determine if it contains source code.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
The text to be scored. |
required |
Returns:
Type | Description |
---|---|
dict[str, Any]
|
dict[str, Any]: A dictionary containing the confidence score and a boolean indicating if the text has code. |