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.0.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.0-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: the_judge-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 2c58b6aa625d9599e584e2d8e60697bf097c23699cf9724fa040470e7a0044b7
MD5 273591800b7e9ca9e7fc26a51bcb2c4b
BLAKE2b-256 84495bb4d52dc0110edc1e3e1b2a3972ca12047e605556be7f36970947771fd2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: the_judge-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 da5d636beef2c878f10370b1fd033edfb6d256044768ecaa92b342c94d676a98
MD5 880483b2f9a30a18039a9a256bade7d6
BLAKE2b-256 177c94c2627f9b647dfcc2e129b6fac64ff4bad4cb5d81a0925e007a82f21630

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