diff --git a/applications/rag/frontend/container/main.py b/applications/rag/frontend/container/main.py index 7f433b841..b0f811b83 100644 --- a/applications/rag/frontend/container/main.py +++ b/applications/rag/frontend/container/main.py @@ -18,6 +18,7 @@ import google.cloud.logging as logging import sqlalchemy import pymysql +import traceback import json from flask import Flask, render_template, request, jsonify @@ -173,7 +174,7 @@ def handlePrompt(): "user_prompt": user_prompt }) if 'nlpFilterLevel' in data: - if nlp_filter.is_content_inappropriate(response['text'], data.get['nlpFilterLevel']): + if nlp_filter.is_content_inappropriate(response['text'], data['nlpFilterLevel']): response['text'] = 'The response is deemed inappropriate for display.' elif 'inspectTemplate' in data and 'deidentifyTemplate' in data: inspect_template_path = data['inspectTemplate'] @@ -185,6 +186,7 @@ def handlePrompt(): return {'response': response} except Exception as err: log.info(f"exception from llm: {err}") + traceback.print_exc() return str(err), 500 diff --git a/applications/rag/frontend/container/rai/nlp_filter.py b/applications/rag/frontend/container/rai/nlp_filter.py index 2092293a2..1dc907e6a 100644 --- a/applications/rag/frontend/container/rai/nlp_filter.py +++ b/applications/rag/frontend/container/rai/nlp_filter.py @@ -13,7 +13,7 @@ # limitations under the License. import os -import google.cloud.language as language +import google.cloud.language_v1 as language from . import retry # Convert the project id into a full resource id. @@ -45,14 +45,13 @@ def sum_moderation_confidences(text): response = nature_language_client.moderate_text( request=request, retry=retry.retry_policy ) - + print(f'get response: {response}') # 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 + print(f'total confidence is: {total_confidence}') + return total_confidence def is_content_inappropriate(text, nlp_filter_level): # Define thresholds @@ -63,9 +62,9 @@ def is_content_inappropriate(text, nlp_filter_level): } # Map the filter level to a threshold - if nlp_filter_level <= 20: + if int(nlp_filter_level) <= 20: threshold = thresholds['high'] - elif nlp_filter_level <= 50: + elif int(nlp_filter_level) <= 50: threshold = thresholds['mid'] else: threshold = thresholds['low'] diff --git a/applications/rag/frontend/container/static/script.js b/applications/rag/frontend/container/static/script.js index 09846f34e..341f261c9 100644 --- a/applications/rag/frontend/container/static/script.js +++ b/applications/rag/frontend/container/static/script.js @@ -84,8 +84,8 @@ function onReady() { // Handle templates document.getElementById("toggle-filters").addEventListener("change", function() { - var filterOptions = document.getElementById("template-section"); - filterOptions.style.display = this.checked ? "block" : "none"; + fetchNLPEnabled(); + fetchDLPEnabled(); }); } if (document.readyState != "loading") onReady(); @@ -113,8 +113,9 @@ function autoResizeTextarea() { // Function to handle the visibility of filter section function toggleNlpFilterSection(nlpEnabled) { var filterOptions = document.getElementById("nlp-filter-section"); + var checkbox = document.getElementById('toggle-filters'); - if (nlpEnabled) { + if (nlpEnabled && checkbox.checked) { filterOptions.style.display = "block"; } else { filterOptions.style.display = "none"; @@ -136,8 +137,8 @@ function fetchNLPEnabled() { // Function to handle the visibility of filter section function toggleFilterSection(dlpEnabled) { var filterOptions = document.getElementById("template-section"); - - if (dlpEnabled) { + var checkbox = document.getElementById('toggle-filters'); + if (dlpEnabled && checkbox.checked) { filterOptions.style.display = "block"; } else { filterOptions.style.display = "none"; diff --git a/applications/rag/frontend/container/templates/index.html b/applications/rag/frontend/container/templates/index.html index 40732528d..cddb446b2 100644 --- a/applications/rag/frontend/container/templates/index.html +++ b/applications/rag/frontend/container/templates/index.html @@ -38,7 +38,7 @@ - +