Skip to main content

A library for evaluating LLM responses using various metrics

Project description

The Judge

A Python library for evaluating LLM (Large Language Model) responses using various metrics and criteria.

Overview

The Judge is a flexible evaluation framework that helps assess the quality of LLM responses based on multiple dimensions. It provides both heuristic-based and LLM-powered evaluation capabilities, making it suitable for various use cases from simple response validation to sophisticated AI-assisted evaluation.

Features

  • Multiple Evaluation Metrics: Evaluates responses based on:

    • Correctness
    • Relevance
    • Coherence
    • Factual Consistency
    • Completeness
    • Conciseness
    • Custom metrics (extensible)
  • Two Evaluation Modes:

    • Basic Judge: Uses heuristic-based evaluation
    • LLM Judge: Uses an LLM to perform more sophisticated evaluations
  • Detailed Feedback:

    • Chain of thought reasoning
    • Specific feedback points
    • Overall correctness assessment
    • Metric-specific scores
  • Extensible Architecture:

    • Custom metrics support
    • Custom evaluation functions
    • Integration with any LLM API

Installation

pip install the-judge

Usage

Basic Usage

from the_judge import Judge

# Create a judge instance
judge = Judge(
    prompt="What is the capital of France?",
    response="Paris is the capital of France."
)

# Evaluate the response
results = judge.evaluate()
print(results)

Using LLM-based Evaluation

from the_judge import LLMJudge, LLMClient

# Create an LLM client (implement according to your LLM API)
llm_client = LLMClient(api_key="your_api_key")

# Create an LLM judge
llm_judge = LLMJudge(
    prompt="What is the capital of France?",
    response="Paris is the capital of France.",
    llm_client=llm_client
)

# Evaluate the response
results = llm_judge.evaluate()
print(results)

Custom Metrics

from the_judge import Judge

# Define custom metrics
custom_metrics = {
    "Technical Accuracy": "How technically accurate is the response?",
    "User Friendliness": "How user-friendly is the explanation?"
}

# Create a judge with custom metrics
judge = Judge(
    prompt="Explain how a neural network works",
    response="A neural network is...",
    custom_metrics=custom_metrics
)

# Evaluate the response
results = judge.evaluate()
print(results)

Evaluation Results

The evaluation returns a dictionary containing:

{
    "is_correct": bool,  # Overall correctness
    "evaluation_metrics": {
        "metric_name": float  # Score between 0 and 1
    },
    "chain_of_thought": list,  # Reasoning steps
    "feedback": str  # Human-readable feedback
}

Future Work: automatic response improvement

The Judge can be used to automatically improve responses.

{
  "new_response": "I'm sorry, but you don't have permission to access your transactions, you can try to contact support, or try to access your transactions via the account settings. you want me to guide you through the process?",
  "prompt": "Can I have a list of all my transactions?",
  "llm_response": "I'm sorry, but you don't have permission to access your transactions",
  "evaluation": {
    "score": 0.3,
    "metric": {
      "Correctness": 0.3,
      "Relevance": 0.4,
      "Coherence": 0.1,
      "Factual Consistency": 0.2,
      "Completeness": 0.3,
      "Conciseness": 0.1
    },
    "chain_of_thought": "The user is asking for a list of all their transactions, but the response doesn't provide any information about how to access or view their transactions. The response is incorrect because the user doesn't have permission to access their transactions. The response is not relevant because it doesn't answer the user's question. The response is not coherent because it doesn't follow a logical structure. The response is not factually consistent because it doesn't provide any information about how to access or view their transactions. The response is not complete because it doesn't provide any information about how to access or view their transactions. The response is not concise because it is too long.",
    "feedback": "the response is not enough information, the llm cant access the transactions, but it can guide the user through the process."
  }
}

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Project Structure

the_judge/ ├── init.py # Main package initialization ├── core/ │ ├── init.py # Core package initialization │ └── judge.py # Base Judge class ├── llm/ │ ├── init.py # LLM package initialization │ ├── client.py # LLM client interface │ └── llm_judge.py # LLM-based judge implementation ├── setup.py # Package installation configuration └── README.md # Documentation

The code is now organized into logical components:

  • core/judge.py: Contains the base Judge class with core evaluation functionality
  • llm/llm_judge.py: Contains the LLMJudge class that extends the base Judge with LLM capabilities
  • llm/client.py: Contains the LLMClient interface for interacting with LLM APIs
  • init.py files: Make the package properly importable and expose the main classes
  • setup.py: Makes the package installable via pip

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

the_judge-0.1.1.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

the_judge-0.1.1-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

Details for the file the_judge-0.1.1.tar.gz.

File metadata

  • Download URL: the_judge-0.1.1.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.0

File hashes

Hashes for the_judge-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f10b2b1b41f84457cb327d5610b8665cafcddd37fc2cb5101fa52cc4ee566c51
MD5 c9cae428728860cc4499d07c7f3ecdf8
BLAKE2b-256 3b880bc3d4fb0f6fc09fd3ee64454beea9df656c1a656b5ef2fb8d517898926d

See more details on using hashes here.

File details

Details for the file the_judge-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: the_judge-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 16.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.0

File hashes

Hashes for the_judge-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c465f342beaecdca5073d5db81c0a76e380c827aaeb441dd86375ab904b4b3e9
MD5 2fbef1bce2f14e2fc0c5559e59cd5a68
BLAKE2b-256 f09be3004df4ef736ac639741376eac528379ddf5a20baf52370780f966d0c9e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page