ASSERT LLM TOOLS
Automated Summary Scoring & Evaluation of Retained Text
This repository contains tools for evaluating the quality of summaries generated by LLMs.
Demo
View a live demo of the library here
Documentation
Documentation is available here
Metrics
Summary Evaluation Metrics
Currently Supported Non-LLM Metrics
- ROUGE Score: Measures overlap of n-grams between the reference text and generated summary
- BLEU Score: Evaluates translation quality by comparing n-gram matches, with custom weights emphasizing unigrams and bigrams
- BERT Score: Leverages contextual embeddings to better capture semantic similarity
- BART Score: Uses BART's sequence-to-sequence model to evaluate semantic similarity and generation quality
Currently Supported LLM Metrics
- Faithfulness: Measures factual consistency between summary and source text (requires an LLM provider)
- Topic Preservation: Will verify that the most important topics from the source are retained in the summary (requires an LLM provider)
- Redundancy Detection: Will identify and flag repeated information within summaries (requires an LLM provider)
- Conciseness Assessment: Will evaluate if the summary effectively condenses information without unnecessary verbosity (requires an LLM provider)
RAG Evaluation Metrics
Currently Supported Metrics
- Answer Attribution: Evaluates if the answer's claims are properly supported by the provided context
- Answer Relevance: Measures how well the answer addresses the specific query intent
- Completeness: Evaluates whether the answer addresses all aspects of the query comprehensively
- Context Relevance: Assesses how well the retrieved context aligns with and is applicable to the query
- Faithfulness: Measures how accurately the answer reflects the information contained in the context without introducing external or contradictory information
Planned Features
- Coherence Evaluation: Will assess the logical flow and readability of the generated summary
- Style Consistency: Will evaluate if the summary maintains a consistent writing style and tone
- Information Density: Will measure the ratio of meaningful content to length in summaries
Features
- Remove Common Stopwords: Allows for adding custom stopwords to the evaluation process
- This is useful for removing common words that are often included in summaries but do not contribute to the overall meaning
- evaluate_summary(full_text, summary, remove_stopwords=True)
- Custom Stopwords: Allows for adding custom stopwords to the evaluation process
- Usage: from assert_llm_tools.utils import add_custom_stopwords
- Example: add_custom_stopwords(["your", "custom", "stopwords", "here"])
- remove_stopwords=True must be enabled
- Select Summary Metrics: Allows for selecting which summary evaluation metrics to calculate
- Usage: evaluate_summary(full_text, summary, metrics=["rouge", "bleu"])
- Defaults to all metrics if not included
- Available metrics: ["rouge", "bleu", "bert_score", "bart_score", "faithfulness", "topic_preservation", "redundancy", "conciseness"]
- Faithfulness, topic preservation, redundancy, and conciseness require an LLM provider via llm_config parameter
- Select RAG Metrics: Allows for selecting which RAG evaluation metrics to calculate
- Usage: evaluate_rag(query, context, answer, metrics=["context_relevance", "answer_accuracy"])
- Defaults to all metrics if not included
- Available metrics: ["context_relevance", "answer_accuracy", "context_utilization", "completeness", "faithfulness"]
- All metrics require an LLM provider via llm_config parameter
- LLM Provider: Allows for specifying the LLM provider and model to use for the faithfulness metric
- Usage: evaluate_summary(full_text, summary, llm_config=LLMConfig(provider="bedrock", model_id="anthropic.claude-v2", region="us-east-1", api_key="your-api-key", api_secret="your-api-secret"))
- Available providers: ["bedrock", "openai"]
- Show Progress: Allows for showing a progress bar during metric calculation
- Usage: evaluate_summary(full_text, summary, show_progress=True)
- Defaults to showing progress bar if not included.
Understanding Scores
Summary Evaluation Scores
All metrics are normalized to return scores between 0 and 1, where higher scores indicate better performance:
- ROUGE Score: Higher means better overlap with reference
- BLEU Score: Higher means better translation quality
- BERT Score: Higher means better semantic similarity
- Note that running BERT score for the first time will require a download of the model weights, which may take a while.
- Use the
bert_model
parameter to specify the model to use for BERTScore calculation. - Default model is "microsoft/deberta-base-mnli". (~500mb download on first use.)
- Other options is "microsoft/deberta-xlarge-mnli". (~3gb download on first use.)
- BART Score: Higher means better semantic similarity and generation quality
- Returns log-likelihood scores normalized to be interpretable, therefore results are likely to be negative. Closer to 0 is better.
- Calculates bidirectional scores (reference→summary and summary→reference)
- Uses the BART-large-CNN model by default (~1.6GB download on first use)
- Faithfulness: Higher means better factual consistency
- Topic Preservation: Higher means better retention of key topics
- Redundancy: Higher means less redundant content (1.0 = no redundancy)
- Conciseness: Higher means less verbose content (1.0 = optimal conciseness)
RAG Evaluation Scores
All RAG metrics return scores between 0 and 1:
- Answer Attribution: Higher means better support from context for answer claims
- Answer Relevance: Higher means better alignment with query intent
- Completeness: Higher means the answer addresses all aspects of the query comprehensively
- Context Relevance: Higher means better match between query and retrieved context
- Faithfulness: Higher means better alignment between the answer and the provided context
Installation
Basic installation:
pip install assert_llm_tools
Optional Dependencies:
-
For Amazon Bedrock support:
pip install "assert_llm_tools[bedrock]"
-
For OpenAI support:
pip install "assert_llm_tools[openai]"
-
To install all optional dependencies:
pip install "assert_llm_tools[all]"
Usage
from assert_llm_tools.core import evaluate_summary, evaluate_rag
from assert_llm_tools.utils import add_custom_stopwords
from assert_llm_tools.llm.config import LLMConfig
add_custom_stopwords(["this", "artificial", "intelligence"])
full_text = """
Artificial intelligence is rapidly transforming the world economy. Companies
are investing billions in AI research and development, leading to breakthroughs
in automation, data analysis, and decision-making processes. While this
technology offers immense benefits, it also raises concerns about job
displacement and ethical considerations.
"""
summary = """
AI is transforming the economy through major investments, bringing advances in
automation and analytics while raising job and ethical concerns.
"""
config = LLMConfig(
provider="openai",
model_id="gpt-4",
api_key="your-api-key"
)
metrics = evaluate_summary(full_text, summary,
remove_stopwords=True,
metrics=["rouge", "bleu", "bert_score", "bart_score", "faithfulness", "topic_preservation", "redundancy", "conciseness"],
llm_config=config)
rag_metrics = evaluate_rag(query="What is the capital of France?",
context="Paris is the capital and largest city of France.",
answer="The capital of France is Paris.",
metrics=["context_relevance", "answer_accuracy", "completeness", "faithfulness","answer_attribution"],
llm_config=config)
print(metrics)
print(rag_metrics)
LICENSE
MIT License
Copyright (c) 2024
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Acknowledgements