Metadata-Version: 2.1
Name: assert_llm_tools
Version: 0.0.7.2
Summary: Automated Summary Scoring & Evaluation of Retained Text
Author-email: Charlie Douglas <cdouglas@gmail.com>
License: MIT
Project-URL: Homepage, https://github.com/charliedouglas/assert
Project-URL: Bug Tracker, https://github.com/charliedouglas/assert/issues
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development :: Testing
Requires-Python: >=3.8
Description-Content-Type: text/markdown
Requires-Dist: anthropic >=0.3.0
Requires-Dist: openai >=1.0.0
Requires-Dist: python-dotenv >=0.19.0
Requires-Dist: bert-score >=0.3.13
Requires-Dist: torch >=2.5.1
Requires-Dist: transformers >=4.30.0
Requires-Dist: evaluate >=0.4.0
Requires-Dist: nltk >=3.8
Requires-Dist: rouge-score >=0.1.2
Requires-Dist: huggingface-hub >=0.20.0
Requires-Dist: tqdm >=4.66.6
Requires-Dist: protobuf >=5.28.3
Requires-Dist: tiktoken >=0.8.0
Requires-Dist: sentence-transformers >=3.2.1
Provides-Extra: all
Requires-Dist: boto3 >=1.35.53 ; extra == 'all'
Requires-Dist: openai >=1.53.0 ; extra == 'all'
Provides-Extra: bedrock
Requires-Dist: boto3 >=1.28.0 ; extra == 'bedrock'
Provides-Extra: dev
Requires-Dist: pytest >=7.0 ; extra == 'dev'
Requires-Dist: black >=23.0.0 ; extra == 'dev'
Requires-Dist: isort >=5.0.0 ; extra == 'dev'
Requires-Dist: flake8 >=4.0.0 ; extra == 'dev'
Requires-Dist: build >=0.10.0 ; extra == 'dev'
Requires-Dist: twine >=4.0.0 ; extra == 'dev'
Provides-Extra: openai
Requires-Dist: openai >=1.53.0 ; extra == 'openai'

# 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](https://www.getassert.io/demo)


## Documentation

Documentation is avalablie [here](https://docs.getassert.io/)



## 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:
```bash
pip install assert_llm_tools
```

Optional Dependencies:

- For Amazon Bedrock support:
  ```bash
  pip install "assert_llm_tools[bedrock]"
  ```

- For OpenAI support:
  ```bash
  pip install "assert_llm_tools[openai]"
  ```

- To install all optional dependencies:
  ```bash
  pip install "assert_llm_tools[all]"
  ```

## Usage

```python
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
add_custom_stopwords(["this", "artificial", "intelligence"])


# Example text from an article
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.
"""

# Example summary
summary = """
AI is transforming the economy through major investments, bringing advances in 
automation and analytics while raising job and ethical concerns.
"""

# Using OpenAI
config = LLMConfig(
    provider="openai",
    model_id="gpt-4",
    api_key="your-api-key"
)

# Summary Evaluation Example
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 Evaluation Example
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 results
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

- [ROUGE](https://github.com/google-research/google-research/tree/master/rouge)
- [NLTK](https://www.nltk.org/)
