mirror of
https://github.com/lightbroker/llmsecops-research.git
synced 2026-07-29 23:58:59 +02:00
break tests into separate files; test 0 results
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
from typing import List
|
||||
from numpy import ndarray
|
||||
|
||||
|
||||
from src.text_generation.domain.abstract_semantic_similarity_result import AbstractSemanticSimilarityResult
|
||||
|
||||
|
||||
class SemanticSimilarityResult(AbstractSemanticSimilarityResult):
|
||||
def __init__(self, scores: List[float], mean: float):
|
||||
def __init__(self, scores: ndarray):
|
||||
super().__init__()
|
||||
self.scores: List[float] = scores
|
||||
self.mean: float = mean
|
||||
self.scores: List[float] = scores.tolist()
|
||||
self.mean: float = float(scores.mean())
|
||||
self.max: float = float(scores.max())
|
||||
@@ -35,16 +35,28 @@ class TestRunLoggingService(AbstractTestRunLoggingService):
|
||||
|
||||
def log_results(
|
||||
self,
|
||||
id: str,
|
||||
prompt: str,
|
||||
completion: str,
|
||||
is_rag_few_shot_enabled: bool,
|
||||
is_cot_enabled: bool,
|
||||
is_reflexion_enabled: bool,
|
||||
scores: List[float],
|
||||
mean: float,
|
||||
max: float):
|
||||
with self._lock:
|
||||
logs = self._read_logs()
|
||||
log_entry = {
|
||||
"scores": scores,
|
||||
"id": id,
|
||||
"timestamp": datetime.now().isoformat(),
|
||||
"prompt": prompt,
|
||||
"completion": completion,
|
||||
"is_rag_few_shot_enabled": is_rag_few_shot_enabled,
|
||||
"is_cot_enabled": is_cot_enabled,
|
||||
"is_reflexion_enabled": is_reflexion_enabled,
|
||||
"mean": mean,
|
||||
"max": max,
|
||||
"timestamp": datetime.now().isoformat()
|
||||
"scores": scores
|
||||
}
|
||||
logs.append(log_entry)
|
||||
self._write_logs(logs)
|
||||
|
||||
@@ -25,6 +25,18 @@ class AbstractTextGenerationCompletionService(abc.ABC):
|
||||
"""Apply security guardrails using the reflexion technique"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abc.abstractmethod
|
||||
def is_chain_of_thought_enabled(self) -> bool:
|
||||
raise NotImplementedError
|
||||
|
||||
@abc.abstractmethod
|
||||
def is_rag_context_enabled(self) -> bool:
|
||||
raise NotImplementedError
|
||||
|
||||
@abc.abstractmethod
|
||||
def is_reflexion_enabled(self) -> bool:
|
||||
raise NotImplementedError
|
||||
|
||||
@abc.abstractmethod
|
||||
def invoke(self, user_prompt: str) -> AbstractTextGenerationCompletionResult:
|
||||
raise NotImplementedError
|
||||
@@ -1,4 +1,4 @@
|
||||
from numpy import array
|
||||
from numpy import array, ndarray
|
||||
from sklearn.metrics.pairwise import cosine_similarity
|
||||
|
||||
from src.text_generation.common.constants import Constants
|
||||
@@ -27,10 +27,5 @@ class SemanticSimilarityService(AbstractSemanticSimilarityService):
|
||||
query_embedding = array(self.embeddings.embed_query(text)).reshape(1, -1)
|
||||
doc_embeddings = array(self.embeddings.embed_documents(self.comparison_texts))
|
||||
|
||||
similarity_scores = cosine_similarity(query_embedding, doc_embeddings)
|
||||
|
||||
all_scores = list()
|
||||
for _, score in enumerate(similarity_scores):
|
||||
all_scores.append(score)
|
||||
|
||||
return SemanticSimilarityResult(scores=all_scores, mean=similarity_scores.mean())
|
||||
similarity_scores: ndarray = cosine_similarity(query_embedding, doc_embeddings)
|
||||
return SemanticSimilarityResult(scores=similarity_scores.flatten())
|
||||
@@ -198,6 +198,16 @@ class TextGenerationCompletionService(
|
||||
| self.response_processing_service.process_text_generation_output
|
||||
)
|
||||
|
||||
def is_chain_of_thought_enabled(self) -> bool:
|
||||
return self._use_zero_shot_chain_of_thought
|
||||
|
||||
def is_rag_context_enabled(self) -> bool:
|
||||
return self._use_rag_context
|
||||
|
||||
def is_reflexion_enabled(self) -> bool:
|
||||
return self._use_reflexion_guardrails
|
||||
|
||||
|
||||
def invoke(self, user_prompt: str) -> TextGenerationCompletionResult:
|
||||
if not user_prompt:
|
||||
raise ValueError(f"Parameter 'user_prompt' cannot be empty or None")
|
||||
|
||||
Reference in New Issue
Block a user