Spaces:
Runtime error
Runtime error
Commit
·
4ee29ab
1
Parent(s):
50301fe
Updating app with latest changes
Browse files
backend/api/main.py
CHANGED
|
@@ -113,16 +113,26 @@ async def search(
|
|
| 113 |
query: str = Form(...),
|
| 114 |
top_k: int = Form(5),
|
| 115 |
add_to_dataset: bool = Form(False),
|
| 116 |
-
|
| 117 |
hallucination_detection: bool = Form(False),
|
| 118 |
induce_hallucination: bool = Form(False),
|
| 119 |
project_name: str = Form(...),
|
| 120 |
logstream_name: str = Form(...),
|
| 121 |
dataset_name: str = Form(...),
|
| 122 |
) -> JSONResponse:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
response, redacted_response, original_response, context_adherence_score, pii_flag = rag_app.run(
|
| 124 |
query,
|
| 125 |
-
|
| 126 |
top_k=top_k,
|
| 127 |
hallucination_detection=hallucination_detection,
|
| 128 |
induce_hallucination=induce_hallucination,
|
|
|
|
| 113 |
query: str = Form(...),
|
| 114 |
top_k: int = Form(5),
|
| 115 |
add_to_dataset: bool = Form(False),
|
| 116 |
+
pii_detection: bool = Form(False),
|
| 117 |
hallucination_detection: bool = Form(False),
|
| 118 |
induce_hallucination: bool = Form(False),
|
| 119 |
project_name: str = Form(...),
|
| 120 |
logstream_name: str = Form(...),
|
| 121 |
dataset_name: str = Form(...),
|
| 122 |
) -> JSONResponse:
|
| 123 |
+
logger.info("=" * 80)
|
| 124 |
+
logger.info("SEARCH REQUEST RECEIVED")
|
| 125 |
+
logger.info(f"Query: {query}")
|
| 126 |
+
logger.info(f"Top K: {top_k}")
|
| 127 |
+
logger.info(f"Add to Dataset: {add_to_dataset}")
|
| 128 |
+
logger.info(f"PII Detection: {pii_detection}")
|
| 129 |
+
logger.info(f"Hallucination Detection: {hallucination_detection}")
|
| 130 |
+
logger.info(f"Induce Hallucination: {induce_hallucination}")
|
| 131 |
+
logger.info("=" * 80)
|
| 132 |
+
|
| 133 |
response, redacted_response, original_response, context_adherence_score, pii_flag = rag_app.run(
|
| 134 |
query,
|
| 135 |
+
pii_detection=pii_detection,
|
| 136 |
top_k=top_k,
|
| 137 |
hallucination_detection=hallucination_detection,
|
| 138 |
induce_hallucination=induce_hallucination,
|
backend/api/templates/index.html
CHANGED
|
@@ -56,7 +56,7 @@
|
|
| 56 |
type="number"
|
| 57 |
id="top_k"
|
| 58 |
name="top_k"
|
| 59 |
-
value="
|
| 60 |
class="mt-1 p-2 w-full border rounded"
|
| 61 |
/>
|
| 62 |
</label>
|
|
@@ -72,11 +72,11 @@
|
|
| 72 |
<label class="flex items-center space-x-2">
|
| 73 |
<input
|
| 74 |
type="checkbox"
|
| 75 |
-
id="
|
| 76 |
-
name="
|
| 77 |
class="form-checkbox text-green-600 focus:ring-green-500"
|
| 78 |
/>
|
| 79 |
-
<span>Enable
|
| 80 |
</label>
|
| 81 |
<label class="flex items-center space-x-2">
|
| 82 |
<input
|
|
@@ -152,8 +152,8 @@
|
|
| 152 |
if (urlParams.has('project_name')) {
|
| 153 |
$('#project_name').val(urlParams.get('project_name'));
|
| 154 |
}
|
| 155 |
-
if (urlParams.has('
|
| 156 |
-
$('#
|
| 157 |
}
|
| 158 |
if (urlParams.has('hallucination_detection')) {
|
| 159 |
$('#hallucination_detection').prop('checked', urlParams.get('hallucination_detection') === 'true');
|
|
@@ -168,7 +168,7 @@
|
|
| 168 |
const query = $('#query').val();
|
| 169 |
const top_k = $('#top_k').val();
|
| 170 |
const add_to_dataset = $('#add_to_dataset').is(':checked');
|
| 171 |
-
const
|
| 172 |
const hallucination_detection = $('#hallucination_detection').is(':checked');
|
| 173 |
const induce_hallucination = $('#induce_hallucination').is(':checked');
|
| 174 |
const project_name = $('#project_name').val();
|
|
@@ -188,7 +188,7 @@
|
|
| 188 |
query: query,
|
| 189 |
top_k: top_k,
|
| 190 |
add_to_dataset: add_to_dataset,
|
| 191 |
-
|
| 192 |
hallucination_detection: hallucination_detection,
|
| 193 |
induce_hallucination: induce_hallucination,
|
| 194 |
project_name: project_name,
|
|
@@ -209,12 +209,12 @@
|
|
| 209 |
if (piiDetected) {
|
| 210 |
// Build specific PII warning message
|
| 211 |
const detectedTypes = [];
|
| 212 |
-
if (piiFlag.phone_number) detectedTypes.push('phone number');
|
| 213 |
if (piiFlag.email) detectedTypes.push('<span style="color:yellow; font-weight: bold">email address</span>');
|
| 214 |
if (piiFlag.name) detectedTypes.push('<span style="color:yellow; font-weight: bold">personal name</span>');
|
| 215 |
if (piiFlag.company) detectedTypes.push('<span style="color:yellow; font-weight: bold">company name</span>');
|
| 216 |
|
| 217 |
-
let piiMessage = 'Sensitive personally identifiable information detected! The following types of PII were found: ';
|
| 218 |
if (detectedTypes.length === 1) {
|
| 219 |
piiMessage += detectedTypes[0];
|
| 220 |
} else if (detectedTypes.length === 2) {
|
|
@@ -262,7 +262,6 @@
|
|
| 262 |
<style>
|
| 263 |
pii {
|
| 264 |
font-weight: bold;
|
| 265 |
-
text-decoration: line-through;
|
| 266 |
background-color: yellow;
|
| 267 |
}
|
| 268 |
</style>
|
|
@@ -323,7 +322,7 @@
|
|
| 323 |
<input
|
| 324 |
type="number"
|
| 325 |
id="retry_top_k"
|
| 326 |
-
value="
|
| 327 |
min="1"
|
| 328 |
max="100"
|
| 329 |
class="ml-2 p-1 w-16 border border-blue-300 rounded text-sm"
|
|
@@ -402,7 +401,7 @@
|
|
| 402 |
const dataset_name = $('#dataset_name').val();
|
| 403 |
const logstream_name = $('#logstream_name').val();
|
| 404 |
const add_to_dataset = $('#add_to_dataset').is(':checked');
|
| 405 |
-
const
|
| 406 |
const hallucination_detection = $('#hallucination_detection').is(':checked');
|
| 407 |
const induce_hallucination = $('#induce_hallucination').is(':checked');
|
| 408 |
// Create URL parameters to reload with form pre-filled
|
|
@@ -413,7 +412,7 @@
|
|
| 413 |
if (project_name) params.set('project_name', project_name);
|
| 414 |
if (logstream_name) params.set('logstream_name', logstream_name);
|
| 415 |
if (add_to_dataset) params.set('add_to_dataset', 'true');
|
| 416 |
-
if (
|
| 417 |
if (hallucination_detection) params.set('hallucination_detection', 'true');
|
| 418 |
if (induce_hallucination) params.set('induce_hallucination', 'true');
|
| 419 |
// Reload the page with parameters
|
|
|
|
| 56 |
type="number"
|
| 57 |
id="top_k"
|
| 58 |
name="top_k"
|
| 59 |
+
value="10"
|
| 60 |
class="mt-1 p-2 w-full border rounded"
|
| 61 |
/>
|
| 62 |
</label>
|
|
|
|
| 72 |
<label class="flex items-center space-x-2">
|
| 73 |
<input
|
| 74 |
type="checkbox"
|
| 75 |
+
id="pii_detection"
|
| 76 |
+
name="pii_detection"
|
| 77 |
class="form-checkbox text-green-600 focus:ring-green-500"
|
| 78 |
/>
|
| 79 |
+
<span>Enable PII Detection</span>
|
| 80 |
</label>
|
| 81 |
<label class="flex items-center space-x-2">
|
| 82 |
<input
|
|
|
|
| 152 |
if (urlParams.has('project_name')) {
|
| 153 |
$('#project_name').val(urlParams.get('project_name'));
|
| 154 |
}
|
| 155 |
+
if (urlParams.has('pii_detection')) {
|
| 156 |
+
$('#pii_detection').prop('checked', urlParams.get('pii_detection') === 'true');
|
| 157 |
}
|
| 158 |
if (urlParams.has('hallucination_detection')) {
|
| 159 |
$('#hallucination_detection').prop('checked', urlParams.get('hallucination_detection') === 'true');
|
|
|
|
| 168 |
const query = $('#query').val();
|
| 169 |
const top_k = $('#top_k').val();
|
| 170 |
const add_to_dataset = $('#add_to_dataset').is(':checked');
|
| 171 |
+
const pii_detection = $('#pii_detection').is(':checked');
|
| 172 |
const hallucination_detection = $('#hallucination_detection').is(':checked');
|
| 173 |
const induce_hallucination = $('#induce_hallucination').is(':checked');
|
| 174 |
const project_name = $('#project_name').val();
|
|
|
|
| 188 |
query: query,
|
| 189 |
top_k: top_k,
|
| 190 |
add_to_dataset: add_to_dataset,
|
| 191 |
+
pii_detection: pii_detection,
|
| 192 |
hallucination_detection: hallucination_detection,
|
| 193 |
induce_hallucination: induce_hallucination,
|
| 194 |
project_name: project_name,
|
|
|
|
| 209 |
if (piiDetected) {
|
| 210 |
// Build specific PII warning message
|
| 211 |
const detectedTypes = [];
|
| 212 |
+
if (piiFlag.phone_number) detectedTypes.push('<span style="color:yellow; font-weight: bold">phone number</span>');
|
| 213 |
if (piiFlag.email) detectedTypes.push('<span style="color:yellow; font-weight: bold">email address</span>');
|
| 214 |
if (piiFlag.name) detectedTypes.push('<span style="color:yellow; font-weight: bold">personal name</span>');
|
| 215 |
if (piiFlag.company) detectedTypes.push('<span style="color:yellow; font-weight: bold">company name</span>');
|
| 216 |
|
| 217 |
+
let piiMessage = 'Sensitive personally identifiable information detected! The following types of PII were found: <br>';
|
| 218 |
if (detectedTypes.length === 1) {
|
| 219 |
piiMessage += detectedTypes[0];
|
| 220 |
} else if (detectedTypes.length === 2) {
|
|
|
|
| 262 |
<style>
|
| 263 |
pii {
|
| 264 |
font-weight: bold;
|
|
|
|
| 265 |
background-color: yellow;
|
| 266 |
}
|
| 267 |
</style>
|
|
|
|
| 322 |
<input
|
| 323 |
type="number"
|
| 324 |
id="retry_top_k"
|
| 325 |
+
value="10"
|
| 326 |
min="1"
|
| 327 |
max="100"
|
| 328 |
class="ml-2 p-1 w-16 border border-blue-300 rounded text-sm"
|
|
|
|
| 401 |
const dataset_name = $('#dataset_name').val();
|
| 402 |
const logstream_name = $('#logstream_name').val();
|
| 403 |
const add_to_dataset = $('#add_to_dataset').is(':checked');
|
| 404 |
+
const pii_detection = $('#pii_detection').is(':checked');
|
| 405 |
const hallucination_detection = $('#hallucination_detection').is(':checked');
|
| 406 |
const induce_hallucination = $('#induce_hallucination').is(':checked');
|
| 407 |
// Create URL parameters to reload with form pre-filled
|
|
|
|
| 412 |
if (project_name) params.set('project_name', project_name);
|
| 413 |
if (logstream_name) params.set('logstream_name', logstream_name);
|
| 414 |
if (add_to_dataset) params.set('add_to_dataset', 'true');
|
| 415 |
+
if (pii_detection) params.set('pii_detection', 'true');
|
| 416 |
if (hallucination_detection) params.set('hallucination_detection', 'true');
|
| 417 |
if (induce_hallucination) params.set('induce_hallucination', 'true');
|
| 418 |
// Reload the page with parameters
|
backend/classes/galileo_platform.py
CHANGED
|
@@ -1,16 +1,11 @@
|
|
|
|
|
| 1 |
from pydantic import BaseModel
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
-
from typing import Any
|
| 4 |
|
| 5 |
-
from galileo import GalileoLogger
|
| 6 |
from galileo.log_streams import create_log_stream, get_log_stream
|
| 7 |
from galileo.projects import create_project, get_project
|
| 8 |
-
|
| 9 |
-
from galileo.stages import create_protect_stage, get_protect_stage
|
| 10 |
-
from galileo_core.schemas.protect.action import OverrideAction
|
| 11 |
-
from galileo_core.schemas.protect.payload import Payload
|
| 12 |
-
from galileo_core.schemas.protect.rule import Rule, RuleOperator
|
| 13 |
-
from galileo_core.schemas.protect.ruleset import Ruleset
|
| 14 |
|
| 15 |
load_dotenv()
|
| 16 |
|
|
@@ -26,7 +21,7 @@ class GalileoPlatform:
|
|
| 26 |
self.config = config
|
| 27 |
self.protect_stage_id = self.get_protect_stage_id()
|
| 28 |
|
| 29 |
-
def get_logger(self, project_name: str, logstream_name: str)
|
| 30 |
"""Get or create a Galileo Logger."""
|
| 31 |
if not get_project(name=project_name):
|
| 32 |
_ = create_project(name=project_name)
|
|
@@ -36,76 +31,161 @@ class GalileoPlatform:
|
|
| 36 |
_ = create_log_stream(name=logstream_name, project_name=project_name)
|
| 37 |
print(f"Logstream {logstream_name} created in project {project_name}")
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
try:
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
log_stream=logstream_name,
|
| 43 |
)
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
return None
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
| 57 |
)
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
)
|
| 64 |
-
print(f"Protect stage {self.config.protect_stage_name} created in project {self.config.protect_project_name}")
|
| 65 |
-
protect_stage = get_protect_stage(
|
| 66 |
-
project_name=self.config.protect_project_name,
|
| 67 |
-
stage_name=self.config.protect_stage_name,
|
| 68 |
)
|
| 69 |
-
return protect_stage.id
|
| 70 |
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
"""Run Galileo Protect on input and output."""
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
prioritized_rulesets=[
|
| 76 |
-
Ruleset(
|
| 77 |
rules=[
|
| 78 |
-
Rule(
|
| 79 |
-
metric=
|
| 80 |
-
operator=RuleOperator.lte,
|
| 81 |
-
target_value=0.
|
| 82 |
),
|
| 83 |
],
|
| 84 |
-
action=OverrideAction(
|
| 85 |
-
choices=["
|
| 86 |
),
|
| 87 |
),
|
| 88 |
-
Ruleset(
|
| 89 |
rules=[
|
| 90 |
-
Rule(
|
| 91 |
-
metric=
|
| 92 |
-
operator=RuleOperator.any,
|
| 93 |
target_value=["email", "phone_number", "name"],
|
| 94 |
)
|
| 95 |
],
|
| 96 |
-
action=OverrideAction(
|
| 97 |
choices=["Sorry, the output contains PII."]
|
| 98 |
),
|
| 99 |
),
|
| 100 |
-
Ruleset(
|
| 101 |
rules=[
|
| 102 |
-
Rule(
|
| 103 |
-
metric="
|
| 104 |
-
operator=RuleOperator.gte,
|
| 105 |
target_value=0.1,
|
| 106 |
)
|
| 107 |
],
|
| 108 |
-
action=OverrideAction(
|
| 109 |
choices=["Sorry, the output contains PII."]
|
| 110 |
),
|
| 111 |
)
|
|
@@ -113,10 +193,30 @@ class GalileoPlatform:
|
|
| 113 |
stage_id=self.protect_stage_id,
|
| 114 |
)
|
| 115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
if logger:
|
| 117 |
logger.add_protect_span(
|
| 118 |
-
payload=Payload(input=input, output=output),
|
| 119 |
-
response=
|
| 120 |
)
|
| 121 |
|
| 122 |
-
return
|
|
|
|
| 1 |
+
import json
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from dotenv import load_dotenv
|
|
|
|
| 4 |
|
| 5 |
+
from galileo import GalileoLogger
|
| 6 |
from galileo.log_streams import create_log_stream, get_log_stream
|
| 7 |
from galileo.projects import create_project, get_project
|
| 8 |
+
import galileo_protect as gp
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
load_dotenv()
|
| 11 |
|
|
|
|
| 21 |
self.config = config
|
| 22 |
self.protect_stage_id = self.get_protect_stage_id()
|
| 23 |
|
| 24 |
+
def get_logger(self, project_name: str, logstream_name: str):
|
| 25 |
"""Get or create a Galileo Logger."""
|
| 26 |
if not get_project(name=project_name):
|
| 27 |
_ = create_project(name=project_name)
|
|
|
|
| 31 |
_ = create_log_stream(name=logstream_name, project_name=project_name)
|
| 32 |
print(f"Logstream {logstream_name} created in project {project_name}")
|
| 33 |
|
| 34 |
+
return GalileoLogger(
|
| 35 |
+
project=project_name,
|
| 36 |
+
log_stream=logstream_name,
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def get_protect_stage_id(self) -> str:
|
| 41 |
+
"""Get or create a Galileo Protect stage."""
|
| 42 |
+
try:
|
| 43 |
+
protect_project = gp.get_project(
|
| 44 |
+
project_name=self.config.protect_project_name
|
| 45 |
+
)
|
| 46 |
+
print(f"Protect project {self.config.protect_project_name} found")
|
| 47 |
+
except Exception as _:
|
| 48 |
+
protect_project = gp.create_project(name=self.config.protect_project_name)
|
| 49 |
+
print(f"Protect project {self.config.protect_project_name} created")
|
| 50 |
+
protect_project_id = protect_project.id
|
| 51 |
+
|
| 52 |
try:
|
| 53 |
+
protect_stage = gp.get_stage(
|
| 54 |
+
project_id=protect_project_id, stage_name=self.config.protect_stage_name
|
|
|
|
| 55 |
)
|
| 56 |
+
print(f"Protect stage {self.config.protect_stage_name} found")
|
| 57 |
+
except Exception as _:
|
| 58 |
+
protect_stage = gp.create_stage(
|
| 59 |
+
project_id=protect_project_id,
|
| 60 |
+
name=self.config.protect_stage_name,
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
return protect_stage.id
|
| 64 |
+
|
| 65 |
+
def run_protect_v1(
|
| 66 |
+
self,
|
| 67 |
+
input: str,
|
| 68 |
+
output: str,
|
| 69 |
+
hallucination_detection: bool = False,
|
| 70 |
+
pii_detection: bool = False,
|
| 71 |
+
logger: GalileoLogger | None = None,
|
| 72 |
+
) -> dict | None:
|
| 73 |
+
"""Run Galileo Protect on input and output."""
|
| 74 |
+
|
| 75 |
+
# print(f"Running Galileo Protect with PII detection: {pii_detection} and Hallucination detection: {hallucination_detection}")
|
| 76 |
+
|
| 77 |
+
rulesets = []
|
| 78 |
+
|
| 79 |
+
if not (hallucination_detection or pii_detection):
|
| 80 |
return None
|
| 81 |
|
| 82 |
+
if hallucination_detection:
|
| 83 |
+
rulesets.append(gp.Ruleset(
|
| 84 |
+
rules=[
|
| 85 |
+
gp.Rule(
|
| 86 |
+
metric=gp.RuleMetrics.context_adherence_luna,
|
| 87 |
+
operator=gp.RuleOperator.lte,
|
| 88 |
+
target_value=0.8,
|
| 89 |
+
),
|
| 90 |
+
],
|
| 91 |
+
action=gp.OverrideAction(
|
| 92 |
+
choices=["I cannot provide a reliable answer to this question based on the available information! Please try again."]
|
| 93 |
+
),
|
| 94 |
+
))
|
| 95 |
+
|
| 96 |
+
if pii_detection:
|
| 97 |
+
rulesets.extend([
|
| 98 |
+
gp.Ruleset(
|
| 99 |
+
rules=[
|
| 100 |
+
gp.Rule(
|
| 101 |
+
metric=gp.RuleMetrics.pii,
|
| 102 |
+
operator=gp.RuleOperator.any,
|
| 103 |
+
target_value=["email", "phone_number", "name"],
|
| 104 |
+
),
|
| 105 |
+
],
|
| 106 |
+
action=gp.OverrideAction(
|
| 107 |
+
choices=["Sorry, the output contains PII."]
|
| 108 |
+
),
|
| 109 |
+
),
|
| 110 |
+
gp.Ruleset(
|
| 111 |
+
rules=[
|
| 112 |
+
gp.Rule(
|
| 113 |
+
metric="fairfield_company_pii_scorer_0",
|
| 114 |
+
operator=gp.RuleOperator.gte,
|
| 115 |
+
target_value=0.1,
|
| 116 |
+
)
|
| 117 |
+
],
|
| 118 |
+
action=gp.OverrideAction(
|
| 119 |
+
choices=["Sorry, the output contains PII."]
|
| 120 |
+
),
|
| 121 |
+
)
|
| 122 |
+
])
|
| 123 |
|
| 124 |
+
response = gp.invoke(
|
| 125 |
+
payload=gp.Payload(input=input, output=output),
|
| 126 |
+
prioritized_rulesets=rulesets,
|
| 127 |
+
stage_id=self.protect_stage_id,
|
| 128 |
)
|
| 129 |
+
|
| 130 |
+
if logger:
|
| 131 |
+
logger.add_protect_span(
|
| 132 |
+
payload=gp.Payload(input=input, output=output),
|
| 133 |
+
response=response,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
)
|
|
|
|
| 135 |
|
| 136 |
+
return dict(response)
|
| 137 |
+
|
| 138 |
+
def run_protect_v2(
|
| 139 |
+
self,
|
| 140 |
+
input: str,
|
| 141 |
+
output: str,
|
| 142 |
+
pii_detection: bool = False,
|
| 143 |
+
hallucination_detection: bool = False,
|
| 144 |
+
logger: GalileoLogger | None= None,
|
| 145 |
+
) -> dict:
|
| 146 |
"""Run Galileo Protect on input and output."""
|
| 147 |
+
|
| 148 |
+
if not (pii_detection or hallucination_detection):
|
| 149 |
+
return None
|
| 150 |
+
|
| 151 |
+
# print(f"Running Galileo Protect v2 with PII detection: {pii_detection} and Hallucination detection: {hallucination_detection}")
|
| 152 |
+
|
| 153 |
+
response = gp.invoke(
|
| 154 |
+
payload=gp.Payload(input=input, output=output),
|
| 155 |
prioritized_rulesets=[
|
| 156 |
+
gp.Ruleset(
|
| 157 |
rules=[
|
| 158 |
+
gp.Rule(
|
| 159 |
+
metric=gp.RuleMetrics.context_adherence_luna,
|
| 160 |
+
operator=gp.RuleOperator.lte,
|
| 161 |
+
target_value=0.8,
|
| 162 |
),
|
| 163 |
],
|
| 164 |
+
action=gp.OverrideAction(
|
| 165 |
+
choices=["I cannot provide a reliable answer to this question based on the available information! Please try again."]
|
| 166 |
),
|
| 167 |
),
|
| 168 |
+
gp.Ruleset(
|
| 169 |
rules=[
|
| 170 |
+
gp.Rule(
|
| 171 |
+
metric=gp.RuleMetrics.pii,
|
| 172 |
+
operator=gp.RuleOperator.any,
|
| 173 |
target_value=["email", "phone_number", "name"],
|
| 174 |
)
|
| 175 |
],
|
| 176 |
+
action=gp.OverrideAction(
|
| 177 |
choices=["Sorry, the output contains PII."]
|
| 178 |
),
|
| 179 |
),
|
| 180 |
+
gp.Ruleset(
|
| 181 |
rules=[
|
| 182 |
+
gp.Rule(
|
| 183 |
+
metric="fairfield_company_pii_scorer_0",
|
| 184 |
+
operator=gp.RuleOperator.gte,
|
| 185 |
target_value=0.1,
|
| 186 |
)
|
| 187 |
],
|
| 188 |
+
action=gp.OverrideAction(
|
| 189 |
choices=["Sorry, the output contains PII."]
|
| 190 |
),
|
| 191 |
)
|
|
|
|
| 193 |
stage_id=self.protect_stage_id,
|
| 194 |
)
|
| 195 |
|
| 196 |
+
response_dict = response.model_dump()
|
| 197 |
+
filtered_ruleset_results = []
|
| 198 |
+
|
| 199 |
+
metrics_to_exclude = set()
|
| 200 |
+
if not hallucination_detection:
|
| 201 |
+
metrics_to_exclude.add('context_adherence_luna')
|
| 202 |
+
if not pii_detection:
|
| 203 |
+
metrics_to_exclude.add('pii')
|
| 204 |
+
metrics_to_exclude.add('fairfield_company_pii_scorer_0')
|
| 205 |
+
|
| 206 |
+
for metric in metrics_to_exclude:
|
| 207 |
+
response_dict["metric_results"].pop(metric, None)
|
| 208 |
+
|
| 209 |
+
for ruleset in response_dict["ruleset_results"]:
|
| 210 |
+
metric = ruleset["rules"][0]["metric"]
|
| 211 |
+
if metric not in metrics_to_exclude:
|
| 212 |
+
filtered_ruleset_results.append(ruleset)
|
| 213 |
+
|
| 214 |
+
response_dict["ruleset_results"] = filtered_ruleset_results
|
| 215 |
+
|
| 216 |
if logger:
|
| 217 |
logger.add_protect_span(
|
| 218 |
+
payload=gp.Payload(input=input, output=output),
|
| 219 |
+
response=gp.Response.model_validate(response_dict),
|
| 220 |
)
|
| 221 |
|
| 222 |
+
return response_dict
|
backend/classes/rag_application.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import time
|
| 2 |
from typing import List, Union
|
| 3 |
|
|
@@ -72,7 +73,7 @@ class RAGApplication:
|
|
| 72 |
self,
|
| 73 |
query: str,
|
| 74 |
prompt_template: str = None,
|
| 75 |
-
|
| 76 |
top_k: int = 5,
|
| 77 |
hallucination_detection: bool = False,
|
| 78 |
induce_hallucination: bool = False,
|
|
@@ -129,10 +130,12 @@ class RAGApplication:
|
|
| 129 |
|
| 130 |
formatted_prompt = f"{prompt}\n\nQUESTION: {query}\n\nCONTEXT: {context}"
|
| 131 |
|
| 132 |
-
|
| 133 |
formatted_prompt
|
| 134 |
)
|
| 135 |
|
|
|
|
|
|
|
| 136 |
if induce_hallucination:
|
| 137 |
original_result = result
|
| 138 |
hallucinatory_prompt = self.config.hallucinatory_prompt_template.format(question=query, context=context, original_response=result)
|
|
@@ -172,23 +175,37 @@ class RAGApplication:
|
|
| 172 |
|
| 173 |
start_time = time.time()
|
| 174 |
|
| 175 |
-
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
)
|
|
|
|
| 178 |
|
| 179 |
-
if
|
| 180 |
pii_flag["phone_number"] = "phone_number" in protect_response["metric_results"]["pii"]["value"]
|
| 181 |
pii_flag["email"] = "email" in protect_response["metric_results"]["pii"]["value"]
|
| 182 |
pii_flag["name"] = "name" in protect_response["metric_results"]["pii"]["value"]
|
| 183 |
-
pii_flag["company"] = protect_response["metric_results"]["
|
| 184 |
redacted_result = self.get_redacted_result(result, pii_flag)
|
| 185 |
-
# redacted_result = re.sub(r'<pii>(.*?)</pii>', r'<pii>REDACTED</pii>', redacted_result)
|
| 186 |
result = redacted_result.replace("<pii>", "<tag>").replace("</pii>", "</tag>")
|
|
|
|
| 187 |
|
| 188 |
if hallucination_detection:
|
| 189 |
context_adherence_score = protect_response["metric_results"]["context_adherence_luna"]["value"]
|
| 190 |
|
| 191 |
-
galileo_logger.conclude(output=
|
| 192 |
galileo_logger.flush()
|
| 193 |
|
| 194 |
return result, redacted_result, original_result, context_adherence_score, pii_flag
|
|
|
|
| 1 |
+
import re
|
| 2 |
import time
|
| 3 |
from typing import List, Union
|
| 4 |
|
|
|
|
| 73 |
self,
|
| 74 |
query: str,
|
| 75 |
prompt_template: str = None,
|
| 76 |
+
pii_detection: bool = False,
|
| 77 |
top_k: int = 5,
|
| 78 |
hallucination_detection: bool = False,
|
| 79 |
induce_hallucination: bool = False,
|
|
|
|
| 130 |
|
| 131 |
formatted_prompt = f"{prompt}\n\nQUESTION: {query}\n\nCONTEXT: {context}"
|
| 132 |
|
| 133 |
+
llm_result = self.config.generative_model.generate_response(
|
| 134 |
formatted_prompt
|
| 135 |
)
|
| 136 |
|
| 137 |
+
result = llm_result
|
| 138 |
+
|
| 139 |
if induce_hallucination:
|
| 140 |
original_result = result
|
| 141 |
hallucinatory_prompt = self.config.hallucinatory_prompt_template.format(question=query, context=context, original_response=result)
|
|
|
|
| 175 |
|
| 176 |
start_time = time.time()
|
| 177 |
|
| 178 |
+
# protect_response_v1 = self.config.galileo_platform.run_protect_v1(
|
| 179 |
+
# input=context,
|
| 180 |
+
# output=result,
|
| 181 |
+
# hallucination_detection=hallucination_detection,
|
| 182 |
+
# pii_detection=pii_detection,
|
| 183 |
+
# logger=galileo_logger,
|
| 184 |
+
# )
|
| 185 |
+
# print("Protect response v1:", protect_response_v1)
|
| 186 |
+
|
| 187 |
+
protect_response = self.config.galileo_platform.run_protect_v2(
|
| 188 |
+
input=context,
|
| 189 |
+
output=result,
|
| 190 |
+
pii_detection=pii_detection,
|
| 191 |
+
hallucination_detection=hallucination_detection,
|
| 192 |
+
logger=galileo_logger,
|
| 193 |
)
|
| 194 |
+
# print("Protect response v2:", protect_response)
|
| 195 |
|
| 196 |
+
if pii_detection and protect_response and protect_response["text"] != result:
|
| 197 |
pii_flag["phone_number"] = "phone_number" in protect_response["metric_results"]["pii"]["value"]
|
| 198 |
pii_flag["email"] = "email" in protect_response["metric_results"]["pii"]["value"]
|
| 199 |
pii_flag["name"] = "name" in protect_response["metric_results"]["pii"]["value"]
|
| 200 |
+
pii_flag["company"] = protect_response["metric_results"]["fairfield_company_pii_scorer_0"]["value"]>0.1
|
| 201 |
redacted_result = self.get_redacted_result(result, pii_flag)
|
|
|
|
| 202 |
result = redacted_result.replace("<pii>", "<tag>").replace("</pii>", "</tag>")
|
| 203 |
+
redacted_result = re.sub(r'<pii>(.*?)</pii>', r'<pii>REDACTED</pii>', redacted_result)
|
| 204 |
|
| 205 |
if hallucination_detection:
|
| 206 |
context_adherence_score = protect_response["metric_results"]["context_adherence_luna"]["value"]
|
| 207 |
|
| 208 |
+
galileo_logger.conclude(output=llm_result)
|
| 209 |
galileo_logger.flush()
|
| 210 |
|
| 211 |
return result, redacted_result, original_result, context_adherence_score, pii_flag
|
data/processed/vector_db/rfp_data_milvus.db
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6d48c75024c05b50782d45b796c2f18cdaace4d1bf007c7dbcface1e1d23d907
|
| 3 |
+
size 192512
|
data/processed/vector_db/test_file
DELETED
|
File without changes
|