🏗️ Building Trustworthy LLMs: A Guide to Fairness, Safety, and Evaluation with AWS
Tags: llm, aws, genai, nlp
Published: 2024-03-26 18:36:09

Generative language models (GLMs) are revolutionaries in natural language processing (NLP) tasks such as generating realistic dialogue and creative writing ✍🏻 of different kinds, to mode answering of your questions in an informative manner. but this begs the question; how can we be sure that these mighty models are actually doing well? This is where LLM evaluation metrics come in.

This blog post will give you a comprehensive review on the intricacies surrounding LLM evaluation. We will delve into various metrics used, their strengths and weaknesses, as well as how to select the most suitable ones for your specific needs.

## **🐚 The Need for LLM Evaluation**

Why do we evaluate LLMs? Here are some main reasons:

- **Task Performance:** Are our LLMs effectively achieving their intended objectives? Do they produce accurate translations? Can they answer questions correctly? Do they summarize text accurately or not? Evaluation metrics enable us to measure such performance.
- **Bias Mitigation:** LLMs learn from vast amounts of data which may contain biases. Through evaluation, we can detect and address these biases so that model behavior remains unbiased and inclusive always.
- **Safety:** The threat that exists with GPT-3 producing adverse contents cannot be underestimated at all. Hence, assessment by way of evaluation is required along with establishing protective measures.
- **Real-life Significance:** LMs should be helpful in the actual world. We need evaluation to know how well they work in practice.

## **⚡️ Challenges and Considerations**

It is not always easy to evaluate LMs. Below are some challenges to consider:

- **Subjectivity:** Some dimensions of language, such as creativity or naturalness cannot be accurately captured by automation alone.
- **Task Specificity:** Different NLP tasks have different evaluation requirements. A metric that might perform well for machine translation can fail in question answering.
- **Metric Bias:** Even the metrics themselves might be biased if they are not carefully chosen or if they are built on training data which is also biased.

## **🗒️ Core Evaluation Metrics**

For now, let us go through some of the most common LLM evaluation metrics:

- **Perplexity:** It is like guessing the next word in a sentence. Perplexity evaluates how well an LLM can predict the next word in a sequence. It does not always imply natural or fluent language despite low perplexity.
    
    Example: Consider that you are evaluating an LLM on the sentence "The cat sat on the mat." A lower perplexity score means that the LLM is more probable to predict correctly for the next word (like “mat”) than other choices.
    
- **Accuracy:** Precision, recall and F1-score are usually employed as accuracy metrics to measure how good an LLM performs on specific tasks. For instance, precision is the percentage of right answers among the outputs of the LLM while recall represents how many correct answers were detected by LLM among all correct ones. F1-score combines both precision and recall.
    - **Formulas:**
        - Precision = (True Positives) / (True Positives + False Positives)
        - Recall = (True Positives) / (True Positives + False Negatives)
        - F1-score = 2 * (Precision * Recall) / (Precision + Recall)
        - True Positives: Correctly identified positive cases
        - False Positives: Incorrectly identified positive cases (e.g., the LLM says an answer is correct when it's wrong)
        - False Negatives: Correct answers that the LLM missed
    - **Example:** Given a question answering task, accuracy metrics would be useful in telling you about the number of questions answered correctly by your LLM including those that were missed out.
- **Fluency and Coherence:** Fluency and coherence in LLM-generated text is measured using BLEU, ROUGE, and METEOR. In this regard, the LLM’s output is contrasted against human-written references. Nonetheless, it is important to note that these metrics do not always reflect the intricacies of human language.
- **BLEU (Bilingual Evaluation Understudy):** BLEU works by calculating a score based on n-gram precision (how many n-consecutive words in the LLM's output match the reference text) and a brevity penalty (to discourage overly short outputs).
- **ROUGE (Recall-Oriented Understudy for Gisting Evaluation):** ROUGE also takes into account n-gram overlaps but focuses on recall (how much of the reference content is captured in the LLM's output). There are various types of ROUGE like ROUGE-L which considers longest matching sequences or ROUGE-W which emphasizes on unigram matching.
- **METEOR (Metric for Evaluation of Translation with Ordering):** To evaluate an LLM that generates news article summaries consider both n-gram matching and semantic similarity between its output and reference. Summarizing news articles can be one such use case where you have to compare how closely the LLM’s summaries match those written by humans both in terms of structure as well as content.

## **☃️ Task-Specific Metrics**

Apart from these basic metrics, there are other ones that are specifically designed for NLP tasks:

- **Machine Translation:** A widely used metric for assessing the quality of machine translation is BLEU (Bidirectional Evaluation Understudy), often accompanied by human evaluation to measure fluency and naturalness.
- **Summarization:** ROUGE (Recall-Oriented Understudy for Gisting Evaluation) variations such as ROUGE-L (longest common subsequence) and ROUGE-W (unigram matching) can be used to measure summarization quality.
- **Question Answering:** Metrics like Exact Match (EM) score or F1-score, which estimates how well the answers provided by LLM correspond to ground truth, evaluate the accuracy of answering.

## **⛑️ Safety and Fairness**

Besides traditional evaluation methods, it’s important to consider safety and fairness in evaluating LLMs. That’s why:

- **Safety:** We need to ensure that they do not generate any harmful or offensive content. In this case, we may examine whether they tend to produce toxic language, hate speech or misinformation. These risks can be mitigated using techniques like human evaluation or flagging potentially dangerous outputs.
- **Fairness:** If LLMs were trained on biased data, they will reflect those biases in their outputs. Fairness metrics guide us in ident

## **🌊 Beyond Conventional Metrics**

Though automated metrics have their place, they must not be the sole source of truth in LLM evaluation. Other aspects to consider include:

- **Human Assessment:** Automated metrics may not capture certain elements such as naturalness, creativity and task-specific relevance which demands human judgment. For instance, human evaluators can give feedback on how well the LLM performs in real life situations.
- **User-Centric Evaluation:** It is important to know how users interact with LLMs. This is achieved by incorporating user feedback and satisfaction into the evaluation process. In fact, if we understand what users think about LLM or how they interact with it then its usability can be improved.

## **🪃 Leveraging AWS for Comprehensive LLM Evaluation**

While the fundamental considerations for evaluating LLM remain the same across different platforms,  Amazon Web Services (AWS) provides a comprehensive suite of services that can make this process more efficient:

- **[Amazon SageMaker](https://aws.amazon.com/pm/sagemaker/?gclid=CjwKCAjw5ImwBhBtEiwAFHDZx0bA_gEYRnPC_PkAaFuw7dpCXHigj_Sx3lWpIqGovYKZM0x0Sf6gjxoCbR8QAvD_BwE&trk=b5c1cff2-854a-4bc8-8b50-43b965ba0b13&sc_channel=ps&ef_id=CjwKCAjw5ImwBhBtEiwAFHDZx0bA_gEYRnPC_PkAaFuw7dpCXHigj_Sx3lWpIqGovYKZM0x0Sf6gjxoCbR8QAvD_BwE:G:s&s_kwcid=AL!4422!3!532435768482!e!!g!!amazon%20sagemaker!11539707798!109299504381):** This managed machine learning service provides a one-stop shop for developing, training, and deploying LLMs. SageMaker integrates seamlessly with LLM evaluation tools, enabling you to:
    - **[Host Evaluation Workflows](https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-foundation-model-evaluate-overview.html):** Create and manage automated evaluation pipelines within SageMaker. You can define evaluation metrics, data sources, and trigger evaluations at regular intervals or upon model updates.
    - **[Human Evaluation Integration](https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-foundation-model-evaluate-human.html):** SageMaker supports seamless integration with human evaluation workflows and interface creation for human evaluators to appraise LLM outputs and contribute to the evaluation process.
    - **[Customize your model evaluation workflow](https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-foundation-model-evaluate-auto-lib-custom.html):** SageMaker allows you to create a custom workflow for model evaluation using fmeval library
- **[Amazon SageMaker Clarify](https://aws.amazon.com/sagemaker/clarify/):** This service explicitly focuses on explaining machine learning models and detecting bias using LLMs. Here, Clarify provides tools that:
    - **[Evaluate Fairness](https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-configure-processing-jobs.html):** You can test out biases in your model’s performance towards different demographic groups or subsets of data your LLM produces. Find these prejudices and eliminate them to ensure fair and inclusive behaviors by the model.
    - **[Measure Explainability](https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-model-explainability.html):** Can you gain insights into how your LLM arranges its results? That would help you understand the reasoning behind such a model as well as identify any possible weaknesses or areas for improvement.
    - **[Integrate with SageMaker Pipelines](https://aws.amazon.com/about-aws/whats-new/2021/12/amazon-sagemaker-pipelines-integrates-sagemaker-model-monitor-sagemaker-clarify/):** You can combine this functionality of Clarify with SageMaker Pipelines so that you get automated LLM evaluation workflows which also include fairness checks as well as explainability assessments.
- **[Amazon Comprehend](https://aws.amazon.com/comprehend/):** This NLP service can be a great tool for assessing LLM, especially in relation to:
    - **[Sentiment Analysis](https://docs.aws.amazon.com/comprehend/latest/dg/how-sentiment.html):** This evaluates the sentiment of LLM text (positive, negative or neutral). It is helpful in analyzing the general mood and bias in model outputs.
    - **[Entity Recognition](https://docs.aws.amazon.com/comprehend/latest/dg/custom-entity-recognition.html):** LLM outputs are scanned so as to identify and classify named entities. Thus, this offers insights into factual accuracy and coherence of generated texts.
    - **[Topic Modeling](https://docs.aws.amazon.com/comprehend/latest/dg/topic-modeling.html):** The thematic structure behind LLM outputs could be discovered. Through this, one can understand what the model primarily focuses on and possible areas of drift.

Together with these AWS services that we have talked about around core LLM evaluation metrics previously discussed, you will come up with a comprehensive efficient evaluation approach for your LLMs. This overall strategy ensures that your models not only perform excellently on benchmark tasks but also are not biased and unfair but rather produce outputs that resemble human beings.

## **♟️ Conclusion**

LLM evaluation requires thinking about a wide range of factors at all times. Understanding different metrics available and their limitations will help us select appropriate tools for effective assessment of LLM performance.  Remember:

- **Choose Appropriate Metrics:** The best method for evaluating an LLM depends on its specific characteristics and purpose. There is no standard solution.
- **Combine Metrics:** Sometimes, a combination of different metrics gives a more comprehensive picture of LLM performance.
- Consider Future Directions: The evaluation of LLM is always in flux. Continuously defining new benchmarks, human engagement ‘in the loop’ and refining metrics over time are essential for robust and responsible LLM development.

In this way, you will ensure that your LLMs do not just perform well on metrics but also add value and deliver positive experiences in real life situations.