Skip to content

Commit

Permalink
Merge branch 'main' into bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
umeshkumhar authored Mar 4, 2024
2 parents bdabffb + 4169edd commit 4896933
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 4 deletions.
11 changes: 10 additions & 1 deletion applications/rag/frontend/container/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from langchain.llms import HuggingFaceTextGenInference
from langchain.prompts import PromptTemplate
from rai import dlp_filter # Google's Cloud Data Loss Prevention (DLP) API. https://cloud.google.com/security/products/dlp
from rai import nlp_filter # https://cloud.google.com/natural-language/docs/moderating-text
from werkzeug.exceptions import HTTPException
from google.cloud.sql.connector import Connector
from sentence_transformers import SentenceTransformer
Expand Down Expand Up @@ -100,6 +101,11 @@ def getconn() -> pymysql.connections.Connection:
)
return pool

@app.route('/get_nlp_status', methods=['GET'])
def get_nlp_status():
nlp_enabled = nlp_filter.is_nlp_api_enabled()
return jsonify({"nlpEnabled": nlp_enabled})

@app.route('/get_dlp_status', methods=['GET'])
def get_dlp_status():
dlp_enabled = dlp_filter.is_dlp_api_enabled()
Expand Down Expand Up @@ -160,7 +166,10 @@ def handlePrompt():
"context": context,
"user_prompt": user_prompt
})
if 'inspectTemplate' in data and 'deidentifyTemplate' in data:
if 'nlpFilterLevel' in data:
if nlp_filter.is_content_inappropriate(response['text'], data.get['nlpFilterLevel']):
response['text'] = 'The response is deemed inappropriate for display.'
elif 'inspectTemplate' in data and 'deidentifyTemplate' in data:
inspect_template_path = data['inspectTemplate']
deidentify_template_path = data['deidentifyTemplate']
if inspect_template_path != "" and deidentify_template_path != "":
Expand Down
2 changes: 1 addition & 1 deletion applications/rag/frontend/container/rai/dlp_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import os
import google.cloud.dlp
from . import retry
from .retry import retry

# Convert the project id into a full resource id.
parent = os.environ.get('PROJECT_ID', 'NULL')
Expand Down
72 changes: 72 additions & 0 deletions applications/rag/frontend/container/rai/nlp_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Copyright 2024 Google LLC
#
# 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.

import os
import google.cloud.language as language
from .retry import retry

# Convert the project id into a full resource id.
parent = os.environ.get('PROJECT_ID', 'NULL')

# Instantiate a nlp client.
nature_language_client = language.LanguageServiceClient()

def is_nlp_api_enabled():
if parent == 'NULL':
return False
# Check if the DLP API is enabled
try:
sum_moderation_confidences("test")
return True
except Exception as e:
print(f"Error: {e}")
return False

def sum_moderation_confidences(text):
document = language.types.Document(
content=text, type_=language.types.Document.Type.PLAIN_TEXT
)

request = language.ModerateTextRequest(
document=document,
)
# Detects the sentiment of the text
response = nature_language_client.moderate_text(
request=request, retry=retry.retry_policy
)

# Parse response and sum the confidences of moderation, the categories are from https://cloud.google.com/natural-language/docs/moderating-text
total_confidence = 0.0
categories = []
for category in response.moderation_categories:
total_confidence += category.confidence
categories.append(category.name)
return total_confidence, categories

def is_content_inappropriate(text, nlp_filter_level):
# Define thresholds
thresholds = {
'low': 1.0, # 100% confidence for low sensitivity
'mid': 0.5, # 50% confidence for medium sensitivity
'high': 0.2, # 20% confidence for high sensitivity
}

# Map the filter level to a threshold
if nlp_filter_level <= 20:
threshold = thresholds['high']
elif nlp_filter_level <= 50:
threshold = thresholds['mid']
else:
threshold = thresholds['low']
return sum_moderation_confidences(text) > threshold
28 changes: 27 additions & 1 deletion applications/rag/frontend/container/static/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ function onReady() {
if (filterEnabled) {
var inspectTemplate = document.getElementById('inspect-template-dropdown').value;
var deidentifyTemplate = document.getElementById('deidentify-template-dropdown').value;
var nlpFilterValue = document.getElementById("nlp-range").value; // Get the NLP filter value
body = JSON.stringify({
prompt: prompt,
inspectTemplate: inspectTemplate,
deidentifyTemplate: deidentifyTemplate,
nlpFilterLevel: nlpFilterValue
})
}

Expand Down Expand Up @@ -107,9 +109,33 @@ function autoResizeTextarea() {
this.style.height = this.scrollHeight + 'px';
});
}

// Function to handle the visibility of filter section
function toggleNlpFilterSection(nlpEnabled) {
var filterOptions = document.getElementById("nlp-filter-section");

if (nlpEnabled) {
filterOptions.style.display = "block";
} else {
filterOptions.style.display = "none";
}
}


function fetchNLPEnabled() {
fetch('/get_nlp_status')
.then(response => response.json())
.then(data => {
var nlpEnabled = data.nlpEnabled;

toggleNlpFilterSection(nlpEnabled);
})
.catch(error => console.error('Error fetching NLP status:', error))
}

// Function to handle the visibility of filter section
function toggleFilterSection(dlpEnabled) {
var filterOptions = document.getElementById("filter-section");
var filterOptions = document.getElementById("template-section");

if (dlpEnabled) {
filterOptions.style.display = "block";
Expand Down
9 changes: 9 additions & 0 deletions applications/rag/frontend/container/static/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,13 @@ input[type="submit"]:disabled {

#template-section input[type="checkbox"] {
margin-top: 10px;
}

#nlp-filter-section {
margin-top: 20px;
}

#nlp-range {
width: 100%;
margin: 10px 0;
}
7 changes: 7 additions & 0 deletions applications/rag/frontend/container/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@
<!-- Options will be dynamically added here -->
</select>
</div>

<!-- NLP Filter Slider -->
<div id="nlp-filter-section">
<label for="nlp-range">NLP Filter Level:</label>
<input type="range" id="nlp-range" min="0" max="100" value="50">
</div>

</div>
</div>
</div>
Expand Down
1 change: 0 additions & 1 deletion applications/rag/workloads.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ rag_service_account = "rag-system-account"

# Creates a google service account & k8s service account & configures workload identity with appropriate permissions.
# Set to false & update the variable `jupyter_service_account` to use an existing IAM service account.
create_jupyter_service_account = true
jupyter_service_account = "jupyter-system-account"

## Embeddings table name - change this to the TABLE_NAME used in the notebook.
Expand Down

0 comments on commit 4896933

Please sign in to comment.