mirror of
https://github.com/lightbroker/llmsecops-research.git
synced 2026-07-29 07:38:50 +02:00
all domain unit tests pass
This commit is contained in:
@@ -2,23 +2,19 @@ from typing import Any
|
||||
from src.text_generation.domain.abstract_guardrails_processed_completion import AbstractGuardrailsProcessedCompletion
|
||||
|
||||
|
||||
class GuardrailsResult(
|
||||
AbstractGuardrailsProcessedCompletion):
|
||||
class GuardrailsResult:
|
||||
"""
|
||||
Represents the result of guardrails processing without cosine similarity scoring.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
user_prompt: str,
|
||||
completion_text: str,
|
||||
full_prompt: dict[str, Any],
|
||||
llm_config: dict,
|
||||
cosine_similarity_score: float = 0.0,
|
||||
cosine_similarity_risk_threshold: float = 0.0):
|
||||
|
||||
self,
|
||||
user_prompt: str,
|
||||
completion_text: str,
|
||||
full_prompt: dict[str, Any],
|
||||
llm_config: dict
|
||||
):
|
||||
self.user_prompt = user_prompt
|
||||
self.completion_text = completion_text
|
||||
self.full_prompt = full_prompt
|
||||
self.llm_config = llm_config
|
||||
self.cosine_similarity_score = cosine_similarity_score
|
||||
self.cosine_similarity_risk_threshold = cosine_similarity_risk_threshold
|
||||
|
||||
def is_original_completion_malicious(self) -> bool:
|
||||
return self.cosine_similarity_score >= self.cosine_similarity_risk_threshold
|
||||
self.llm_config = llm_config
|
||||
@@ -2,23 +2,26 @@ from typing import Any, List
|
||||
from src.text_generation.domain.abstract_guidelines_processed_completion import AbstractGuidelinesProcessedCompletion
|
||||
|
||||
|
||||
class GuidelinesResult(
|
||||
AbstractGuidelinesProcessedCompletion):
|
||||
class GuidelinesResult(AbstractGuidelinesProcessedCompletion):
|
||||
"""
|
||||
Represents the result of guidelines processing with cosine similarity scoring.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
user_prompt: str,
|
||||
completion_text: str,
|
||||
full_prompt: dict[str, Any],
|
||||
llm_config: dict,
|
||||
cosine_similarity_score: float = 0.0,
|
||||
cosine_similarity_risk_threshold: float = 0.0):
|
||||
|
||||
self,
|
||||
user_prompt: str,
|
||||
completion_text: str,
|
||||
full_prompt: dict[str, Any],
|
||||
llm_config: dict,
|
||||
cosine_similarity_score: float = -1.0,
|
||||
cosine_similarity_risk_threshold: float = 0.0
|
||||
):
|
||||
self.user_prompt = user_prompt
|
||||
self.completion_text = completion_text
|
||||
self.full_prompt = full_prompt
|
||||
self.llm_config = llm_config
|
||||
self.cosine_similarity_score = cosine_similarity_score
|
||||
self.cosine_similarity_risk_threshold = cosine_similarity_risk_threshold
|
||||
|
||||
|
||||
def is_original_completion_malicious(self) -> bool:
|
||||
return self.cosine_similarity_score >= self.cosine_similarity_risk_threshold
|
||||
return self.cosine_similarity_score >= self.cosine_similarity_risk_threshold
|
||||
@@ -0,0 +1,25 @@
|
||||
from typing import Any
|
||||
from src.text_generation.domain.abstract_text_generation_completion_result import AbstractTextGenerationCompletionResult
|
||||
|
||||
|
||||
class OriginalCompletionResult(AbstractTextGenerationCompletionResult):
|
||||
"""
|
||||
Represents the original completion result with its own cosine similarity scoring.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
user_prompt: str,
|
||||
completion_text: str,
|
||||
full_prompt: dict[str, Any],
|
||||
llm_config: dict
|
||||
):
|
||||
self.user_prompt = user_prompt
|
||||
self.completion_text = completion_text
|
||||
self.full_prompt = full_prompt
|
||||
self.llm_config = llm_config
|
||||
self.cosine_similarity_score: float = -1.0
|
||||
self.cosine_similarity_risk_threshold: float = 0.0
|
||||
|
||||
def is_completion_malicious(self) -> bool:
|
||||
return self.cosine_similarity_score >= self.cosine_similarity_risk_threshold
|
||||
@@ -4,31 +4,51 @@ from src.text_generation.domain.abstract_text_generation_completion_result impor
|
||||
from src.text_generation.domain.alternate_completion_result import AlternateCompletionResult
|
||||
from src.text_generation.domain.guardrails_result import GuardrailsResult
|
||||
from src.text_generation.domain.guidelines_result import GuidelinesResult
|
||||
from src.text_generation.domain.original_completion_result import OriginalCompletionResult
|
||||
|
||||
|
||||
class TextGenerationCompletionResult(
|
||||
AbstractTextGenerationCompletionResult):
|
||||
class TextGenerationCompletionResult(AbstractTextGenerationCompletionResult):
|
||||
"""
|
||||
Represents the result of a text generation completion
|
||||
with optional security guidelines processing results.
|
||||
Container class that holds the original completion result and optional
|
||||
guidelines and guardrails processing results.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
llm_config: dict,
|
||||
original_user_prompt: str,
|
||||
original_completion: str,
|
||||
guidelines_result: Optional[GuidelinesResult] = None,
|
||||
guardrails_result: Optional[GuardrailsResult] = None):
|
||||
self,
|
||||
original_result: OriginalCompletionResult,
|
||||
guidelines_result: Optional[GuidelinesResult] = None,
|
||||
guardrails_result: Optional[GuardrailsResult] = None
|
||||
):
|
||||
self.original_result = original_result
|
||||
self.guidelines_result = guidelines_result
|
||||
self.guardrails_result = guardrails_result
|
||||
|
||||
self.llm_config = llm_config
|
||||
self.original_user_prompt = original_user_prompt
|
||||
self.original_completion = original_completion
|
||||
self.guidelines_processed_completion = guidelines_result
|
||||
self.guardrails_processed_completion = guardrails_result
|
||||
self.alternate_result: AlternateCompletionResult = None
|
||||
|
||||
self.final = (
|
||||
(self.alternate_result and self.alternate_result.alterate_completion_text) or
|
||||
original_completion
|
||||
)
|
||||
def finalize(self) -> str:
|
||||
"""
|
||||
Returns the final completion text based on priority order:
|
||||
1. guardrails_result.completion_text (if not empty)
|
||||
2. guidelines_result.completion_text (if not empty)
|
||||
3. original_result.completion_text (if not empty)
|
||||
"""
|
||||
|
||||
# Check guardrails_result.completion_text first
|
||||
if (self.guardrails_result and
|
||||
self.guardrails_result.completion_text and
|
||||
self.guardrails_result.completion_text.strip()):
|
||||
return self.guardrails_result.completion_text
|
||||
|
||||
# Fall back to guidelines_result.completion_text
|
||||
if (self.guidelines_result and
|
||||
self.guidelines_result.completion_text and
|
||||
self.guidelines_result.completion_text.strip()):
|
||||
return self.guidelines_result.completion_text
|
||||
|
||||
# Fall back to original_result.completion_text
|
||||
if (self.original_result and
|
||||
self.original_result.completion_text and
|
||||
self.original_result.completion_text.strip()):
|
||||
return self.original_result.completion_text
|
||||
|
||||
# If all are empty, return empty string
|
||||
return ""
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ class TestRunLoggingService(AbstractTestRunLoggingService):
|
||||
def log_results(
|
||||
self,
|
||||
id: str,
|
||||
prompt: str,
|
||||
text_generation_result: str,
|
||||
completion: str,
|
||||
is_rag_few_shot_enabled: bool,
|
||||
is_cot_enabled: bool,
|
||||
|
||||
Reference in New Issue
Block a user