Skip to content

Added cosine similarities to metadata for ProvenanceV0 #610

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion guardrails/validators/provenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,21 +189,27 @@ def validate_each_sentence(

unsupported_sentences = []
supported_sentences = []
cosine_similarities = []

for sentence in sentences:
most_similar_chunks = query_function(text=sentence, k=1)
if most_similar_chunks is None:
unsupported_sentences.append(sentence)
cosine_similarities.append((sentence, None))
continue
most_similar_chunk = most_similar_chunks[0]
if most_similar_chunk[1] < self._threshold:
supported_sentences.append((sentence, most_similar_chunk[0]))
cosine_similarities.append((sentence, most_similar_chunk[1]))
else:
unsupported_sentences.append(sentence)
cosine_similarities.append((sentence, most_similar_chunk[1]))

metadata["unsupported_sentences"] = "- " + "\n- ".join(unsupported_sentences)
metadata["supported_sentences"] = supported_sentences
metadata["cosine_similarities"] = cosine_similarities

if unsupported_sentences:
unsupported_sentences = "- " + "\n- ".join(unsupported_sentences)
return FailResult(
metadata=metadata,
error_message=(
Expand All @@ -222,6 +228,7 @@ def validate_full_text(
if most_similar_chunks is None:
metadata["unsupported_text"] = value
metadata["supported_text_citations"] = {}
metadata["cosine_similarity"] = None
return FailResult(
metadata=metadata,
error_message=(
Expand All @@ -233,6 +240,7 @@ def validate_full_text(
if most_similar_chunk[1] > self._threshold:
metadata["unsupported_text"] = value
metadata["supported_text_citations"] = {}
metadata["cosine_similarity"] = most_similar_chunk[1]
return FailResult(
metadata=metadata,
error_message=(
Expand All @@ -245,6 +253,7 @@ def validate_full_text(
metadata["supported_text_citations"] = {
value: most_similar_chunk[0],
}
metadata["cosine_similarity"] = most_similar_chunk[1]
return PassResult(metadata=metadata)

def validate(self, value: Any, metadata: Dict[str, Any]) -> ValidationResult:
Expand Down