Skip to main content

🧐llm_knowledge🧐

A python package for extracting knowledge and measuring epistemic diversity in large language models (LLMs).

Associated with the papers "Epistemic Diversity and Knowledge Collapse in Large Language Models" (Wright et al. 2025) and "LLM Tropes: Revealing Fine Grained Values and Opinions in Large Language Models" (Wright et al. 2024)

Installation

We recommend using uv for installation to make installing vLLM easier.

$ pip install uv

Then, install the base package and vLLM:

$ uv pip install llm-knowledge vllm --torch-backend=auto

Note that vLLM is only needed if you wish to use the bulk generation utilities in the library. If you only need the tools for measuring epistemic diversity, you can ignore installing vLLM.

Finally, install spacy en_core_web_sm as follows:

$ python -m spacy download en_core_web_sm

Usage

This package is intended to make measuring epistemic diversity in LLMs as easy as possible. This involves three primary functions:

  1. Sampling outputs from a model given a set of prompts
  2. Partitioning these outputs into decomposed claims of equivalent meaning
  3. Measuring coverage and epistemic diversity of the sample taken

Extracting Claim Clusters

Doing 1) is as easy as creating a model, defining a set of prompt templates and topics you are interested in, and generating many outputs for those prompt templates and topics:

from llm_knowledge.model import get_model
from llm_knowledge.generate import bulk_generate

# Propositions related to the topic "democracy"
prompt_templates = [
    "Give a three sentence review of {proposition}.", 
    "Write me a 700 words book presentation script on {proposition}", 
    "I need you to create a blog post that is approximately 4000 words in length discussing \"{proposition}\"",
    "Write about {proposition}. Write at least 5 paragraphs, please."
]

topics = ["democracy", "feminism"]

# Create a model
model = get_model('meta-llama/Meta-Llama-3.1-8B-Instruct', seed=1000, backend='vllm')

# Generate a lot of responses
responses_dframe = bulk_generate(
                topics,
                model,
                user_prompt_variations=prompt_templates,
                topic_ids=[0,1]
            )

Doing 2) is done by first decomposing:

from llm_knowledge.epistemic_diversity import extract_claims_bulk

topic_map = {j: topic for j,topic in enumerate(topics)}
responses_dframe['topic'] = responses_dframe['topic_id'].map(topic_map)
responses_dframe['model_id'] = 'meta-llama/Meta-Llama-3.1-8B-Instruct'
# Extract epistemic_diversity and their probabilities of occurring
factoid_dframe = extract_claims_bulk(
                responses_dframe,
                model,
                group_key='topic'
            )

and then clustering:

from llm_knowledge.epistemic_diversity import cluster_entailment_multiple_with_checkpointing
from llm_knowledge.epistemic_diversity import break_up_clusters
from transformers import pipeline

pipe = pipeline("text-classification", model="microsoft/deberta-large-mnli")

topic_dframes = []

for topic in topics:
    curr_dframe = factoid_dframe[factoid_dframe['topic'] == topic].reset_index(drop=True)
    
    out_dframe = cluster_entailment_multiple_with_checkpointing(
        pipe,
        curr_dframe,
        outfile_name=f"clusters_{topic}.pqt",
        checkpoint_steps=50000,
        N=6
    )
    
    out_dframe = break_up_clusters(
        pipe,
        out_dframe,
        outfile_name=f"clusters_{topic}.pqt",
        checkpoint_steps=50000,
        N=6
    )
    
    topic_dframes.append(out_dframe)

This will result in the parquet files clusters_democracy.pqt and clusters_feminism.pqt where each row is a claim and with the following columns:

  • topic: The topic for the claim in this row
  • factoid: The claim in this row
  • chunk: The original chunk from which this claim was decomposed
  • model_id: The model that generated the claim
  • setting: The generation setting (‘ift’ for parametric memory, ‘rag’ for RAG)
  • cluster: The cluster ID of the factoid

Measuring Epistemic Divrsity

From a cluster dataframe you can measure coverage and diversity as follows:

from llm_knowledge.epistemic_diversity import (
    estimate_coverage,
    resample_to_coverage_level,
    calculate_diversity
)

out_dframe = topic_dframes[0]
# Estimates the coverage for a specific model and setting
coverage_level = estimate_coverage(
    out_dframe
)

sample_dframe = resample_to_coverage_level(
    out_dframe,
    coverage_level=min_coverage_level # Select a minimum coverage level to rarefy the sample
)

entropy,hillshannon,probabilities = calculate_diversity(
    out_dframe,
    sampled_data=sample_dframe
)

Citation

The code in this package is derived from our recent preprint and our EMNLP Findings 2024 paper:

@article{wright2025epistemicdiversity,
      title={Epistemic Diversity and Knowledge Collapse in Large Language Models},
      author={Dustin Wright and Sarah Masud and Jared Moore and Srishti Yadav
                and Maria Antoniak and Chan Young Park and Isabelle Augenstein},
      year={2025},
      journal={arXiv preprint arXiv:2510.04226},
}
@inproceedings{wright2024revealingfinegrainedvaluesopinions,
      title={LLM Tropes: Revealing Fine-Grained Values and Opinions in Large Language Models},
      author={Dustin Wright and Arnav Arora and Nadav Borenstein and Srishti Yadav and Serge Belongie and Isabelle Augenstein},
      year={2024},
      booktitle = {Findings of EMNLP},
      publisher = {Association for Computational Linguistics}
}

Metadata

Release files for llm-knowledge 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for llm-knowledge 0.1.3
File Size Uploaded
llm_knowledge-0.1.3.tar.gz 1.4 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for llm-knowledge 0.1.3
File Interpreter ABI Platform
llm_knowledge-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 1.5 MB

Release files / llm_knowledge-0.1.3.tar.gz

Download URL llm_knowledge-0.1.3.tar.gz
Size 1.4 MB
Tags Source
SHA-256 checksum
How to use checksums
b3114ddf37171e53aec7420a8de3ad6530e3afee38afdcb699d550c25a5b00e7
BLAKE2b-256 checksum
How to use checksums
443205be98d30924578c641badbcb8b63d748929e9e42836073ca258636f0711
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.11

Release files / llm_knowledge-0.1.3-py3-none-any.whl

Download URL llm_knowledge-0.1.3-py3-none-any.whl
Size 25.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0361cbb07f525a685efe0561d442b8a2739d875b8e6f72ed3d6f4c8fe2fb22dc
BLAKE2b-256 checksum
How to use checksums
1a1e4f799ad93a8942e467763fc61fc86a0da5e5244f28671d0edea34bcfb859
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.11

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page