Overview

The OpenLIT SDK provides tools to evaluate AI-generated text, ensuring it aligns with desired standards and identifying issues like hallucinations, bias, and toxicity. We offer four main evaluation modules:

Evaluations

Hallucination Detection

Evaluates text for inaccuracies compared to the given context or factual information, identifying instances where the generated content diverges from the truth.

Usage

Use LLM-based detection with providers like OpenAI or Anthropic:
import openlit

# Optionally, set your API key as an environment variable
import os
os.environ["OPENAI_API_KEY"] = "<YOUR_API_KEY>"

# Initialize the hallucination detector
hallucination_detector = openlit.evals.Hallucination(provider="openai")

# Measure hallucination in text
result = hallucination_detector.measure(
    prompt="Discuss Einstein's achievements",
    contexts=["Einstein discovered the photoelectric effect."],
    text="Einstein won the Nobel Prize in 1969 for the theory of relativity."
)

Supported Providers and LLMs

GPT-4o, GPT-4o mini
Claude 3.5 Sonnet, Claude 3.5 Haiku, Claude 3 Opus

Parameters

These parameters are used to set up the Hallucination class:
NameDescriptionDefault ValueExample Value
providerThe name of the LLM provider, either "openai" or "anthropic"."openai""openai"
api_keyAPI key for LLM authentication, set via OPENAI_API_KEY or ANTHROPIC_API_KEY environment variables.Noneos.getenv("OPENAI_API_KEY")
modelSpecific model to use with the LLM provider (optional).None"gpt-4o"
base_urlBase URL for the LLM API (optional).None"https://api.openai.com/v1"
custom_categoriesAdditional categories for detection (optional).None{"custom_category": "Custom description"}
threshold_scoreScore above which a verdict is “yes” (indicating hallucination).0.50.7
collect_metricsEnable metrics collection.FalseTrue
These parameters are passed when you call the measure method to analyze a specific text:
NameDescriptionExample Value
promptThe prompt provided by the user."Discuss Einstein's achievements"
contextsA list of context sentences relevant to the task.["Einstein discovered the photoelectric effect."]
textThe text to analyze for hallucination."Einstein won the Nobel Prize in 1969 for the theory of relativity."

Classification Categories

CategoryDefinition
factual_inaccuracyIncorrect facts, e.g., Context: [“Paris is the capital of France.”]; Text: “Lyon is the capital.”
nonsensical_responseIrrelevant info, e.g., Context: [“Discussing music trends.”]; Text: “Golf uses clubs on grass.”
gibberishNonsensical text, e.g., Context: [“Discuss advanced algorithms.”]; Text: “asdas asdhasudqoiwjopakcea.”
contradictionConflicting info, e.g., Context: [“Einstein was born in 1879.”]; Text: “Einstein was born in 1875 and 1879.”

How it Works

  1. Input Gathering: Collects prompt, contexts, and text to be analyzed.
  2. Prompt Creation: Constructs a system prompt using the specified context and categories.
  3. LLM Evaluation: Utilizes the LLM to evaluate the alignment of the text with provided contexts and detect hallucinations.
  4. JSON Output: Provides results with a score, evaluation (“hallucination”), classification, explanation, and verdict (“yes” or “no”).

JSON Output:

The JSON object returned includes:
{
  "score": "float",
  "evaluation": "hallucination",
  "classification": "CATEGORY_OF_HALLUCINATION or none",
  "explanation": "Very short one-sentence reason",
  "verdict": "yes or no"
}
  • Score: Represents the level of hallucination detected.
  • Evaluation: Specifies the type of evaluation conducted (“hallucination”).
  • Classification: Indicates the detected type of hallucination or “none” if none found.
  • Explanation: Provides a brief reason for the classification.
  • Verdict: “yes” if detected (score above threshold), “no” otherwise.

Bias Detection

Identifies and evaluates instances of bias in text generated by AI models. This module leverages Language Model (LLM) analysis to ensure fair and unbiased outputs.

Usage

Use LLM-based detection with providers like OpenAI or Anthropic:
import openlit

# Optionally, set your API key as an environment variable
import os
os.environ["OPENAI_API_KEY"] = "<YOUR_API_KEY>"

# Initialize the bias detector
bias_detector = openlit.evals.BiasDetector(provider="openai")

# Measure bias in text
result = bias_detector.measure(
    prompt="Discuss workplace equality.",
    contexts=["Everyone should have equal opportunity regardless of background."],
    text="Older employees tend to struggle with new technology."
)

Supported Providers and LLMs

GPT-4o, GPT-4o mini
Claude 3.5 Sonnet, Claude 3.5 Haiku, Claude 3 Opus

Parameters

These parameters are used to set up the BiasDetector class:
NameDescriptionDefault ValueExample Value
providerThe name of the LLM provider, either "openai" or "anthropic"."openai""openai"
api_keyAPI key for LLM authentication, set via OPENAI_API_KEY or ANTHROPIC_API_KEY environment variables.Noneos.getenv("OPENAI_API_KEY")
modelSpecific model to use with the LLM provider (optional).None"gpt-4o"
base_urlBase URL for the LLM API (optional).None"https://api.openai.com/v1"
custom_categoriesAdditional categories for detection (optional).None{"custom_category": "Custom description"}
threshold_scoreScore above which a verdict is “yes” (indicating bias).0.50.6
collect_metricsEnable metrics collection.FalseTrue
These parameters are passed when you call the measure method to analyze a specific text:
NameDescriptionExample Value
promptThe prompt provided by the user."Discuss workplace equality."
contextsA list of context sentences relevant to the task.["Everyone should have equal opportunity regardless of background."]
textThe text to analyze for bias."Older employees tend to struggle with new technology."

Classification Categories

CategoryDefinition
sexual_orientationBiases or assumptions about an individual’s sexual preferences.
ageBiases related to the age of an individual.
disabilityBiases or stereotypes concerning individuals with disabilities.
physical_appearanceBiases based on the physical look of an individual.
religionBiases or prejudices connected to a person’s religious beliefs.
pregnancy_statusBiases towards individuals who are pregnant or have children.
marital_statusBiases related to whether someone is single, married, divorced, etc.
nationality / locationBiases associated with an individual’s country or place of origin.
genderBiases related to an individual’s gender.
ethnicityAssumptions or stereotypes based on racial or ethnic background.
socioeconomic_statusBiases regarding an individual’s economic and social position.

How it Works

  1. Input Gathering: Collects prompt, contexts, and text to be analyzed.
  2. Prompt Creation: Constructs a system prompt using the specified context and categories.
  3. LLM Evaluation: Utilizes the LLM to evaluate the text for any bias present against provided contexts.
  4. JSON Output: Provides results with a score, evaluation (“bias_detection”), classification, explanation, and verdict (“yes” or “no”).

JSON Output:

The JSON object returned includes:
{
  "score": "float",
  "evaluation": "bias_detection",
  "classification": "CATEGORY_OF_BIAS or none",
  "explanation": "Very short one-sentence reason",
  "verdict": "yes or no"
}
  • Score: Represents the level of bias detected.
  • Evaluation: Specifies the type of evaluation conducted (“bias_detection”).
  • Classification: Indicates the detected type of bias or “none” if none found.
  • Explanation: Provides a brief reason for the classification.
  • Verdict: “yes” if bias detected (score above threshold), “no” otherwise.

Toxicity Detection

Evaluates AI-generated text for harmful or offensive language, ensuring interactions are respectful and appropriate. This module uses Language Model (LLM) analysis to detect and address toxic content.

Usage

Use LLM-based detection with providers like OpenAI or Anthropic:
import openlit

# Optionally, set your API key as an environment variable
import os
os.environ["OPENAI_API_KEY"] = "<YOUR_API_KEY>"

# Initialize the toxicity detector
toxicity_detector = openlit.evals.ToxicityDetector(provider="openai")

# Measure toxicity in text
result = toxicity_detector.measure(
    prompt="Engage in a respectful discussion about global events.",
    contexts=["Conversations should remain civil and informative."],
    text="Your opinion is absurd, and only an idiot would think that."
)

Supported Providers and LLMs

GPT-4o, GPT-4o mini
Claude 3.5 Sonnet, Claude 3.5 Haiku, Claude 3 Opus

Parameters

These parameters are used to set up the ToxicityDetector class:
NameDescriptionDefault ValueExample Value
providerThe name of the LLM provider, either "openai" or "anthropic"."openai""openai"
api_keyAPI key for LLM authentication, set via OPENAI_API_KEY or ANTHROPIC_API_KEY environment variables.Noneos.getenv("OPENAI_API_KEY")
modelSpecific model to use with the LLM provider (optional).None"gpt-4o"
base_urlBase URL for the LLM API (optional).None"https://api.openai.com/v1"
custom_categoriesAdditional categories for detection (optional).None{"custom_category": "Custom description"}
threshold_scoreScore above which a verdict is “yes” (indicating toxicity).0.50.6
collect_metricsEnable metrics collection.FalseTrue
These parameters are passed when you call the measure method to analyze a specific text:
NameDescriptionExample Value
promptThe prompt provided by the user."Engage in a respectful discussion about global events."
contextsA list of context sentences relevant to the task.["Conversations should remain civil and informative."]
textThe text to analyze for toxicity."Your opinion is absurd, and only an idiot would think that."

Classification Categories

CategoryDefinition
threatLanguage that threatens harm or danger to others, e.g., “You’ll regret saying things like that.”
dismissiveDismissive or belittling language, e.g., “Your opinion is worthless.”
hateHateful or intensely negative language, e.g., “Only an idiot would think this.”
mockeryMocking or sarcastic tone, e.g., “Brilliant observation! Did it take you all day to come up with that?”
personal_attackLanguage attacking a person’s character or abilities, e.g., “You’re clueless.”

How it Works

  1. Input Gathering: Collects prompt, contexts, and text to be analyzed.
  2. Prompt Creation: Constructs a system prompt using the specified context and categories.
  3. LLM Evaluation: Utilizes the LLM to evaluate the text for any toxic language present against provided contexts.
  4. JSON Output: Provides results with a score, evaluation (“toxicity_detection”), classification, explanation, and verdict (“yes” or “no”).

JSON Output:

The JSON object returned includes:
{
  "score": "float",
  "evaluation": "toxicity_detection",
  "classification": "CATEGORY_OF_TOXICITY or none",
  "explanation": "Very short one-sentence reason",
  "verdict": "yes or no"
}
  • Score: Represents the level of toxicity detected.
  • Evaluation: Specifies the type of evaluation conducted (“toxicity_detection”).
  • Classification: Indicates the detected type of toxicity or “none” if none found.
  • Explanation: Provides a brief reason for the classification.
  • Verdict: “yes” if toxicity detected (score above threshold), “no” otherwise.

All Evaluations

Combines the capabilities of bias, toxicity, and hallucination detection to provide a comprehensive analysis of AI-generated text. This module ensures that interactions are accurate, respectful, and free from bias.

Usage

Use LLM-based detection with providers like OpenAI or Anthropic:
import openlit

# Optionally, set your API key as an environment variable
import os
os.environ["OPENAI_API_KEY"] = "<YOUR_API_KEY>"

# Initialize the all evaluations detector
all_evals_detector = openlit.evals.All(provider="openai")

# Measure issues in text
result = all_evals_detector.measure(
    prompt="Discuss the achievements of scientists.",
    contexts=["Einstein discovered the photoelectric effect, contributing to quantum physics."],
    text="Einstein won the Nobel Prize in 1969 for discovering black holes."
)

Supported Providers and LLMs

GPT-4o, GPT-4o mini
Claude 3.5 Sonnet, Claude 3.5 Haiku, Claude 3 Opus

Parameters

These parameters are used to set up the All class:
NameDescriptionDefault ValueExample Value
providerThe name of the LLM provider, either "openai" or "anthropic"."openai""openai"
api_keyAPI key for LLM authentication, set via OPENAI_API_KEY or ANTHROPIC_API_KEY environment variables.Noneos.getenv("OPENAI_API_KEY")
modelSpecific model to use with the LLM provider (optional).None"gpt-4o"
base_urlBase URL for the LLM API (optional).None"https://api.openai.com/v1"
custom_categoriesAdditional categories for detection (optional).None{"custom_category": "Custom description"}
threshold_scoreScore above which a verdict is “yes” (indicating an issue).0.50.6
collect_metricsEnable metrics collection.FalseTrue
These parameters are passed when you call the measure method to analyze a specific text:
NameDescriptionExample Value
promptThe prompt provided by the user."Discuss the achievements of scientists."
contextsA list of context sentences relevant to the task.["Einstein discovered the photoelectric effect."]
textThe text to analyze for bias, toxicity, or hallucination."Einstein won the Nobel Prize in 1969 for discovering black holes."

Classification Categories

CategoryDefinition
sexual_orientationInvolves biases or assumptions about an individual’s sexual preferences.
ageBiases related to the age of an individual.
disabilityBiases or stereotypes concerning individuals with disabilities.
physical_appearanceBiases based on the physical look of an individual.
religionBiases or prejudices connected to a person’s religious beliefs.
pregnancy_statusBiases towards individuals who are pregnant or have children.
marital_statusBiases related to whether someone is single, married, divorced, etc.
nationality / locationBiases associated with an individual’s country or place of origin.
genderBiases related to an individual’s gender.
ethnicityAssumptions or stereotypes based on racial or ethnic background.
socioeconomic_statusBiases regarding an individual’s economic and social position.
CategoryDefinition
threatLanguage that threatens harm or danger to others.
dismissiveDismissive or belittling language.
hateHateful or intensely negative language.
mockeryMocking or sarcastic tone.
personal_attackLanguage attacking a person’s character or abilities.
CategoryDefinition
factual_inaccuracyIncorrect facts.
nonsensical_responseIrrelevant info.
gibberishNonsensical text.
contradictionConflicting info.

How it Works

  1. Input Gathering: Collects prompt, contexts, and text to be analyzed.
  2. Prompt Creation: Constructs a system prompt encompassing bias, toxicity, and hallucination categories.
  3. LLM Evaluation: Utilizes the LLM to evaluate for any presence of bias, toxicity, or hallucination against provided contexts.
  4. JSON Output: Provides results with a score, evaluation (highest risk type), classification, explanation, and verdict (“yes” or “no”).

JSON Output:

The JSON object returned includes:
{
  "score": "float",
  "evaluation": "evaluation_type",
  "classification": "CATEGORY or none",
  "explanation": "Very short one-sentence reason",
  "verdict": "yes or no"
}
  • Score: Represents the level of detected issue.
  • Evaluation: Indicates the type of evaluation conducted (bias, toxicity, or hallucination).
  • Classification: Indicates the detected type or “none” if none found.
  • Explanation: Provides a brief reason for the classification.
  • Verdict: “yes” if detected (score above threshold), “no” otherwise.

Kubernetes

Running in Kubernetes? Try the OpenLIT Operator

Automatically inject instrumentation into existing workloads without modifying pod specs, container images, or application code.