Skip to main content

A Python package for extracting confidence scores from LLM models outputs, particularly using log probabilities

Project description

llm-confidence

llm-confidence is a Python package designed to extract and calculate confidence scores from outputs of large language models (LLMs), specifically focusing on log probabilities. The package helps you work with model responses, particularly when working with structured data such as JSON outputs.

Features

  • Extract token-level log probabilities.
  • Aggregate probabilities to calculate confidence scores for key-value pairs.
  • Handle nested keys to compute confidence scores for related fields.
  • Simple API for processing log probabilities from OpenAI GPT models.

Installation

You can install the package using pip:

pip install llm-confidence

Usage

Here’s an example of how to use the llm-confidence package to calculate confidence scores based on log probabilities from OpenAI GPT models:

from openai import OpenAI
import os
from llm_confidence.logprobs_handler import LogprobsHandler

# Initialize the LogprobsHandler
logprobs_handler = LogprobsHandler()

def get_completion(
        messages: list[dict[str, str]],
        model: str = "gpt-4o",
        max_tokens=500,
        temperature=0,
        stop=None,
        seed=42,
        response_format=None,
        logprobs=None,
        top_logprobs=None,
):
    params = {
        "model": model,
        "messages": messages,
        "max_tokens": max_tokens,
        "temperature": temperature,
        "stop": stop,
        "seed": seed,
        "logprobs": logprobs,
        "top_logprobs": top_logprobs,
    }
    if response_format:
        params["response_format"] = response_format

    completion = client.chat.completions.create(**params)
    return completion

# Set up your OpenAI client with your API key
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY", "<your OpenAI API key if not set as env var>"))

# Define a prompt for completion
response_raw = get_completion(
    [{'role': 'user', 'content': 'Tell me the name of a city and a few streets in this city, and return the response in JSON format.'}],
    logprobs=True,
    response_format={'type': 'json_object'}
)

# Print the output
print(response_raw.choices[0].message.content)

# Extract the log probabilities from the response
response_logprobs = response_raw.choices[0].logprobs.content if hasattr(response_raw.choices[0], 'logprobs') else []

# Format the logprobs
logprobs_formatted = logprobs_handler.format_logprobs(response_logprobs)

# Process the log probabilities to get confidence scores
confidence = logprobs_handler.process_logprobs(
    logprobs_formatted, 
    nested_keys_dct={'vat': ['vat_data', 'percent', 'vat_amount', 'exclude_vat_amount']}
)

# Print the confidence scores
print(confidence)

Example Breakdown

  1. Get Completion: Sends a prompt to the OpenAI GPT model and retrieves the completion, including log probabilities.
  2. Logprobs Formatting: Formats the raw log probabilities into a structured format that can be processed.
  3. Confidence Calculation: Aggregates the probabilities and returns confidence scores for key-value pairs in the model's JSON response.

Customization

You can customize the nested_keys_dct parameter to aggregate confidence scores for your specific fields. For example:

nested_keys_dct={'address': ['street', 'city', 'state']}

This will compute a combined confidence score for all fields related to addresses.

License

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

Contributing

We welcome contributions! Here's how you can help:

  1. Fork the project.
  2. Create a branch for your feature or bug fix.
  3. Open a pull request.

Make sure to include tests for any new features or bug fixes.


Feel free to use the package and improve upon it. If you encounter any issues, please open an issue in the repository.

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

llm_confidence-0.2.2.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

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

llm_confidence-0.2.2-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file llm_confidence-0.2.2.tar.gz.

File metadata

  • Download URL: llm_confidence-0.2.2.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for llm_confidence-0.2.2.tar.gz
Algorithm Hash digest
SHA256 59e703fa3c21b3a9ae7b9a3812b76d05d0736e3392f639a46738711ad4663ebe
MD5 de3e2fb1c37cb254d68e324421dcdf8c
BLAKE2b-256 87279ccfae96b5aa2863865c349fab9a1ec4c6432c7ef4ade2fbf299d4e355ed

See more details on using hashes here.

File details

Details for the file llm_confidence-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: llm_confidence-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for llm_confidence-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ea257a414656b5dc171e524b57eee5cf222122c7945067b33e0c04f22203b914
MD5 44c3055a42c6797faf1f5022b6cc3fe9
BLAKE2b-256 4cf39896c8738678fc0e28be1999aa21c7f5fbd78b0494c3871a58220dd5962a

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