Tools to extract, visualize, and quantify latent knowledge in large language models
Project description
🧐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:
- Sampling outputs from a model given a set of prompts
- Partitioning these outputs into decomposed claims of equivalent meaning
- 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 rowfactoid: The claim in this rowchunk: The original chunk from which this claim was decomposedmodel_id: The model that generated the claimsetting: 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}
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file llm_knowledge-0.1.3.tar.gz.
File metadata
- Download URL: llm_knowledge-0.1.3.tar.gz
- Upload date:
- Size: 1.4 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b3114ddf37171e53aec7420a8de3ad6530e3afee38afdcb699d550c25a5b00e7
|
|
| MD5 |
dbef88cd18269e64ac02c926ee1d9252
|
|
| BLAKE2b-256 |
443205be98d30924578c641badbcb8b63d748929e9e42836073ca258636f0711
|
File details
Details for the file llm_knowledge-0.1.3-py3-none-any.whl.
File metadata
- Download URL: llm_knowledge-0.1.3-py3-none-any.whl
- Upload date:
- Size: 25.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0361cbb07f525a685efe0561d442b8a2739d875b8e6f72ed3d6f4c8fe2fb22dc
|
|
| MD5 |
50ca2c8f86288336af6b088d386770f7
|
|
| BLAKE2b-256 |
1a1e4f799ad93a8942e467763fc61fc86a0da5e5244f28671d0edea34bcfb859
|