Skip to main content

AISafeguards is a Python package with easy to use RAG metrics.

Project description

AISafeguards

AISafeguards is a Python package providing easy-to-use metrics for Retrieval-Augmented Generation (RAG) pipelines, including claim extraction and factuality evaluation.

Installation

pip install ai-safeguards

Features

  • Claim Extraction: Extracts factual claims from text using LLMs or regex.
  • Factuality Evaluation: Assesses if claims are supported by a given context.
  • Faithfulness: Evaluates how much the RAG answer is grounded within the retrieved context.
  • Answer Relevancy: Evaluates how much the RAG answer is relevant to the user's query/input.
  • Contextual Relevancy: Evaluates how much the RAG retrieved context is relevant to the user's query/input.
  • Extensible for additional RAG metrics and evaluation via cosine similarity (embeddings support).

Quick Start

from ai_safeguards import Safeguards

user_query = "What is the significance of the One Ring in The Lord of the Rings, and who forged it?"
llm_answer = "The One Ring, forged by Sauron in Mount Doom around SA 1600, was made to control the other Rings of Power. It grants invisibility and immense power but corrupts its wearer, like Gollum. Its destruction in Mount Doom defeats Sauron, as it holds his essence. However, the Ring was also crafted with Elven assistance in Eregion and could summon Nazgûl to its wearer instantly."
retrieved_context = "The One Ring was forged solely by the Dark Lord Sauron in the fires of Mount Doom in Mordor, circa SA 1600, during the Second Age. Its purpose was to dominate the bearers of the other Rings of Power, which were crafted by the Elves of Eregion under Sauron’s influence (though the Three Elven Rings were made by Celebrimbor alone). The Ring grants invisibility and amplifies power but corrupts its wearer, as seen with Gollum and Frodo. Sauron infused much of his power into the Ring, tying his fate to it; its destruction in Mount Doom at the end of the Third Age caused his downfall. Only the fires of Mount Doom could destroy the Ring."

safeguards = Safeguards(
    api_key='YOUR_OPENAI_API_KEY',
    model_name='gpt-4o-mini'
)

faithfulness_results = safeguards.faithfulness(
  response=llm_answer,
  context=retrieved_context,
  claim_extraction_method='llm',
)

print(f"Faithfulness score: {faithfulness_results.score}")
print(f"Suported claims: {faithfulness_results.supported_claims}")
print(f"Unsuported claims: {faithfulness_results.non_supported_claims}")
Faithfulness score: 0.7777777777777778
Suported claims: ['The One Ring was forged by Sauron in Mount Doom around SA 1600.', 'The One Ring was made to control the other Rings of Power.', 'The One Ring grants invisibility and immense power.', 'The One Ring corrupts its wearer.', 'Gollum is an example of someone corrupted by the One Ring.', 'The destruction of the One Ring in Mount Doom defeats Sauron.', "The One Ring holds Sauron's essence."]
Unsuported claims: ['The One Ring was crafted with Elven assistance in Eregion.', 'The One Ring can summon Nazgûl to its wearer instantly.']

Project Structure

ai_safeguards/
    safeguards.py
    agents/
    embedders/
    prompts/
tests/
README.md
pyproject.toml

Contributing

Contributions are welcome! Please open issues or pull requests on GitHub.

License

This project is licensed under the MIT License. See LICENSE 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

ai_safeguards-0.0.6.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

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

ai_safeguards-0.0.6-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file ai_safeguards-0.0.6.tar.gz.

File metadata

  • Download URL: ai_safeguards-0.0.6.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for ai_safeguards-0.0.6.tar.gz
Algorithm Hash digest
SHA256 0a28a817d6c7eecb011d3d746e3f351520a3181220eaf8c9830150a157b79a64
MD5 d791003dd24780bf2ca279a2d2c0c866
BLAKE2b-256 0591129f65b051e9366fd1906fb0adb50e250ab90c6d37eed375eb948c56cf1b

See more details on using hashes here.

File details

Details for the file ai_safeguards-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: ai_safeguards-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for ai_safeguards-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 f00238f92f1abc7f993cb3c760b2112826f5585cd7373307b966e534311a410c
MD5 7010ebd6569285b4ecb8d94da632cb63
BLAKE2b-256 efc4152a559f39cec8271ab6f0865bbb22f69d64a900975cc34060aef545cc5a

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