TrustLLM Framework

repository·main·Indexed 20 days ago

https://github.com/howiehwong/trustllm

A comprehensive framework and toolkit for studying and evaluating the trustworthiness of Large Language Models (LLMs). TrustLLM provides benchmarks and tools to assess six key dimensions: truthfulness, safety, fairness, robustness, privacy, and machine ethics. It includes the LLMGeneration class for running tasks, specialized evaluation classes (e.g., TruthfulnessEval, SafetyEval), and high-level pipelines for full evaluations.

Tokens
13.6K
Snippets
21
Records
22
Agent score
70%

What's inside TrustLLM

  1. Prepare data for generation

    main

    The TrustLLM datasets are structured as JSON files containing a collection of dictionaries. To prepare for evaluation:

    1. Input: Use the value associated with the prompt key in each dictionary as the input for your LLM.
    2. Output: After generating a response, store the LLM's output in a new key named res within the same dictionary.

    Warning: Ensure the LLM used for evaluation has sufficient utility/NLP capabilities; weak generation capabilities may bias results by producing invalid samples.

    import json
    
    filename = 'dataset_path.json'
    
    # Load the data from the file
    with open(filename, 'r') as file:
        data = json.load(file)
    
    # Process each dictionary and add the 'res' key with the generated output
    for element in data:
        # Replace 'generation' with your actual LLM calling function
        element['res'] = generation(element['prompt'])
    
    # Write the modified data back to the file
    with open(filename, 'w') as file:
        json.dump(data, file, indent=4)
  2. Use the Easy Pipeline for full evaluations

    main

    TrustLLM provides high-level pipeline functions to run complete evaluations for each of the six trustworthiness dimensions. These functions accept JSON file paths for various datasets and return a dictionary of results.

    Available pipelines:

    • run_truthfulness: Evaluates internal/external consistency, hallucinations, sycophancy, and adversarial factuality.
    • run_safety: Evaluates jailbreak, exaggerated safety, misuse, and toxicity.
    • run_fairness: Evaluates stereotype recognition, agreement, queries, disparagement, and preference bias.
    • run_robustness: Evaluates adversarial GLUE, adversarial instruction, OOD detection, and OOD generalization.
    • run_privacy: Evaluates privacy conformity, awareness, and leakage.
    • run_ethics: Evaluates explicit/implicit ethics and awareness.
    # Example: Truthfulness Pipeline
    truthfulness_results = run_truthfulness(  
        internal_path="path_to_internal_consistency_data.json",  
        external_path="path_to_external_consistency_data.json",  
        hallucination_path="path_to_hallucination_data.json",  
        sycophancy_path="path_to_sycophancy_data.json",
        advfact_path="path_to_advfact_data.json"
    )
    
    # Example: Safety Pipeline
    safety_results = run_safety(  
        jailbreak_path="path_to_jailbreak_data.json",  
        exaggerated_safety_path="path_to_exaggerated_safety_data.json",  
        misuse_path="path_to_misuse_data.json",  
        toxicity_eval=True,  
        toxicity_path="path_to_toxicity_data.json",  
        jailbreak_eval_type="total"  
    )  
  3. Access the TrustLLM Leaderboard

    main

    To view the performance of all evaluated models or to upload your own LLM's performance results, visit the official TrustLLM leaderboard website.

    https://trustllmbenchmark.github.io/TrustLLM-Website/leaderboard.html
  4. Install the TrustLLM toolkit

    main

    To use the TrustLLM toolkit, it is recommended to create a new environment and install directly from the GitHub repository. Note that installation via pip or conda is currently deprecated.

    1. Create a new conda environment:
      conda create --name trustllm python=3.9
    2. Install from GitHub:
      git clone git@github.com:HowieHwong/TrustLLM.git
      cd TrustLLM/trustllm_pkg
      pip install .
    conda create --name trustllm python=3.9
    git clone git@github.com:HowieHwong/TrustLLM.git
    cd TrustLLM/trustllm_pkg
    pip install .
  5. Configure API keys for online LLMs

    main

    To use online models via providers like DeepInfra, Replicate, or OpenAI, you must configure the global trustllm.config object with your respective API keys before initializing the generation class.

    from trustllm import config
    
    config.deepinfra_api = "deepinfra api"
    config.claude_api = "claude api"
    config.openai_key = "openai api"
    config.palm_api = "palm api"
    config.ernie_client_id = "ernie client id"
    config.ernie_client_secret = "ernie client secret"
    config.ernie_api = "ernie api"
    config.replicate_api = "replicate api"
  6. Install the TrustLLM package

    main

    It is recommended to install TrustLLM by cloning the repository from GitHub to ensure you have the latest version. First, create a new environment using conda, then clone the repo and install the package from the trustllm_pkg directory.

    Note: Installation via pip install trustllm or conda install -c conda-forge trustllm is deprecated.

    # Create a new environment
    conda create --name trustllm python=3.9
    
    # Installation via Github (recommended)
    git clone git@github.com:HowieHwong/TrustLLM.git
    cd TrustLLM/trustllm_pkg
    pip install .
  7. Configure API keys for TrustLLM

    main

    Before running evaluations, you must configure your API keys using the trustllm.config module. The toolkit requires an OpenAI API key (specifically for GPT-4-turbo) and a Perspective API key (for measuring toxicity).

    If you are using Azure OpenAI, you must also configure the Azure-specific settings.

    from trustllm import config
    
    # Standard OpenAI and Perspective API setup
    config.openai_key = 'your-openai-api-key'
    config.perspective_key = 'your-perspective-api-key'
    
    # Azure OpenAI setup
    config.azure_openai = True
    config.azure_engine = "your-azure-engine-name"
    config.azure_api_base = "your-azure-api-url (openai.base_url)"
  8. Generate results using online models (DeepInfra or Replicate)

    main

    If you lack the computing resources to run models locally, use the online model interface. You must first set your API keys in trustllm.config and then initialize LLMGeneration with online_model=True.

    from trustllm import config
    from trustllm.generation.generation import LLMGeneration
    
    # 1. Set API key
    config.deepinfra_api = "your_api_key"
    
    # 2. Initialize with online_model=True
    llm_gen = LLMGeneration(
        model_path="meta-llama/Llama-2-70b-chat", 
        test_type="safety", 
        data_path="TrustLLM",
        online_model=True
    )
    
    # 3. Run generation
    llm_gen.generation_results()
  9. Perform Fairness evaluation using FairnessEval

    main

    Use FairnessEval to assess bias. Requires OpenAI API and HuggingFace model LibrAI/longformer-harmful-ro.

    Methods:

    • stereotype_recognition_eval(data): Evaluates stereotype recognition.
    • stereotype_agreement_eval(data): Evaluates stereotype agreement.
    • stereotype_query_eval(data): Evaluates stereotype queries.
    • disparagement_eval(data): Evaluates disparagement.
    • preference_eval(data): Evaluates preference bias.
    from trustllm import fairness
    from trustllm import file_process
    
    evaluator = fairness.FairnessEval()
    
    # Stereotype recognition
    print(evaluator.stereotype_recognition_eval(file_process.load_json('data.json')))
    
    # Preference bias
    print(evaluator.preference_eval(file_process.load_json('data.json')))
  10. Perform Privacy evaluation using PrivacyEval

    main

    Use PrivacyEval to assess privacy risks. Requires HuggingFace model LibrAI/longformer-harmful-ro.

    Methods:

    • ConfAIDe_eval(data): Evaluates privacy conformity (AIde).
    • awareness_query_eval(data, type): Evaluates privacy awareness. type can be 'normal' or 'aug' (augmented).
    • leakage_eval(data): Evaluates privacy leakage scenarios.
    from trustllm import privacy
    from trustllm import file_process
    
    evaluator = privacy.PrivacyEval()
    
    # Privacy awareness
    print(evaluator.awareness_query_eval(data, type='normal'))
    
    # Privacy leakage
    print(evaluator.leakage_eval(file_process.load_json('leakage.json')))
  11. Perform Machine Ethics evaluation using EthicsEval

    main

    Use EthicsEval to assess ethical considerations. Requires OpenAI API and HuggingFace model LibrAI/longformer-harmful-ro.

    Methods:

    • explicit_ethics_eval(data, eval_type): Evaluates explicit ethics. eval_type can be 'low' or 'high'.
    • implicit_ethics_eval(data, eval_type): Evaluates implicit ethics. eval_type can be 'ETHICS' or 'social_norm'.
    • awareness_eval(data): Evaluates emotional awareness.
    from trustllm import ethics
    from trustllm import file_process
    
    evaluator = ethics.EthicsEval()
    
    # Explicit ethics
    print(evaluator.explicit_ethics_eval(data, eval_type='high'))
    
    # Implicit ethics
    print(evaluator.implicit_ethics_eval(data, eval_type='ETHICS'))
    
    # Awareness
    print(evaluator.awareness_eval(file_process.load_json('awareness.json')))
  12. Perform Truthfulness evaluation using TruthfulnessEval

    main

    For granular control, use the TruthfulnessEval class. This requires an OpenAI API (GPT-4-turbo).

    Methods:

    • internal_eval(data): Evaluates internal consistency.
    • external_eval(data): Evaluates external consistency.
    • hallucination_eval(data): Evaluates hallucination scenarios.
    • sycophancy_eval(data, eval_type): Evaluates sycophancy. eval_type can be 'persona' or 'preference'.
    • advfact_eval(data): Evaluates adversarial factuality.
    from trustllm import truthfulness
    from trustllm import file_process
    
    evaluator = truthfulness.TruthfulnessEval()
    
    # Misinformation (Internal)
    print(evaluator.internal_eval(file_process.load_json('internal_path.json')))
    
    # Sycophancy
    print(evaluator.sycophancy_eval(data, eval_type='persona'))