Git Product home page Git Product logo

extractive_summary_validator's Introduction

Overview

Developed by Aryn
Date of development Feb 15, 2024
Validator type Summarization
Blog
License Apache 2
Input/Output Output

Description

This validator checks if a summary generated by an LLM is an extractive summary of the original document. An extractive summary contains words and phrases selected from an original document to create a summary.

This validator works by performing a fuzzy match between the sentences in the summary and the sentences in the document. Each sentence in the summary must be similar to at least one sentence in the document. After the validation, the summary is updated to include the sentences from the document that were matched, and the citations for those sentences are added to the end of the summary.

Intended use

This validator is only useful when performing extractive summarization. If the summary is correct but is an abstractive summary, this validator will give false negatives.

Resources required

  • Dependencies: thefuzz

Installation

$ guardrails hub install hub://aryn-ai/extractive_summary

Usage Examples

Validating string output via Python

In this example, we apply the validator to a string output generated by an LLM.

# Import Guard and Validator
from guardrails.hub import ExtractiveSummary
from guardrails import Guard

val = ExtractiveSummary(
    threshold=90,
    filepaths="/path/to/original/documents"
)

# Create Guard with Validator
guard = Guard.from_string(validators=[val, ...])

guard.parse("Summarized text")  # Validator passes
guard.parse("Incorrect summary")  # Validator fails

Validating JSON output via Python

In this example, we apply the validator to a string field of a JSON output generated by an LLM.

# Import Guard and Validator
from pydantic import BaseModel
from guardrails.hub import ExtractiveSummary
from guardrails import Guard

val = ExtractiveSummary(
    threshold=90,
    filepaths="/path/to/original/documents"
)

# Create Pydantic BaseModel
class ArticleSummary(BaseModel):
    title: str
    summary: str = Field(
        description="Summary of text", validators=[val]
    )

# Create a Guard to check for valid Pydantic output
guard = Guard.from_pydantic(output_class=ArticleSummary)

# Run LLM output generating JSON through guard
guard.parse("""
{
    "pet_name": "Using Guardrails Hub",
    "pet_type": "To use Guardrails Hub, download individual validators using the CLI and compose them together into guards."
}
""")

API Reference

__init__(self, on_fail="noop")

    Initializes a new instance of the Validator class.

    Parameters:

    • threshold (int): The minimum fuzz ratio to be considered summarized. Defaults to 85.
    • on_fail (str, Callable): The policy to enact when a validator fails. If str, must be one of reask, fix, filter, refrain, noop, exception or fix_reask. Otherwise, must be a function that is called when the validator fails.

__call__(self, value, metadata={}) โ†’ ValidationOutcome

    Validates the given value using the rules defined in this validator, relying on the metadata provided to customize the validation process. This method is automatically invoked by guard.parse(...), ensuring the validation logic is applied to the input data.

    Note:

    1. This method should not be called directly by the user. Instead, invoke guard.parse(...) where this method will be called internally for each associated Validator.
    2. When invoking guard.parse(...), ensure to pass the appropriate metadata dictionary that includes keys and values required by this validator. If guard is associated with multiple validators, combine all necessary metadata into a single dictionary.

    Parameters:

    • value (Any): The input value to validate.

    • metadata (dict): A dictionary containing metadata required for validation. Keys and values must match the expectations of this validator.

      Key Type Description Default
      filepaths list[str] A list of strings that specifies the filepaths for any documents that should be used for asserting the summary's similarity. N/A

extractive_summary_validator's People

Contributors

baitsguy avatar smohiuddin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.