Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
9303f39
Move model wrapper definitions to separate modules
rvashurin Sep 16, 2025
fc898d1
Fix openai tests
rvashurin Sep 16, 2025
84aa0e4
Fix lint issues
rvashurin Sep 16, 2025
4a88776
Only api adapter knows whether particular api provider supports log p…
rvashurin Sep 18, 2025
ec24e1e
Use credentials from environment, remove provider-specific constructo…
rvashurin Sep 19, 2025
b28fc8c
Prepare hf inference for moving
rvashurin Sep 19, 2025
c5763c8
Move inference logic to corresponding adapters
rvashurin Sep 19, 2025
f4ae52d
Rename api key for together in tests, don't set openai client to bb m…
rvashurin Sep 19, 2025
9737f47
Intermediate lint
rvashurin Sep 19, 2025
c1dd504
Simplify openai adapter
rvashurin Sep 22, 2025
5b8e7e0
Make huggingface adapter work and support logprobs, fix and simplify …
rvashurin Sep 23, 2025
f45be0b
Remove support of GreedyAlternativesNLI calculator for bb models
rvashurin Sep 23, 2025
39a1be9
Add alternative tokens to the api response:
rvashurin Sep 23, 2025
16f3819
Simplify blackbox greedy calc, add test config for greybox models
rvashurin Sep 23, 2025
c027fd3
Ensure greedy generation for greedy probs, fix together adapter, add …
rvashurin Sep 24, 2025
8a08ea9
Make sampling with probabilities work for greybox models, add relevan…
rvashurin Sep 24, 2025
d57b161
Lint
rvashurin Sep 24, 2025
1b5d835
Align generate/generate texts behavior of blackbox models with whiteb…
rvashurin Sep 25, 2025
ec9619f
Add tests for estimate_uncertainty with blackbox openai model
rvashurin Sep 25, 2025
fdd917a
Remove redundant code, unnecessary functions
rvashurin Sep 25, 2025
4a91fbd
Remove bad tests for openai adapter, fix some bugs
rvashurin Sep 26, 2025
6d62491
Streamline checking if model supports logprobs
rvashurin Sep 29, 2025
04d4d06
Some blackbox models can have tokenizers
rvashurin Nov 20, 2025
f18d08d
Use together-specific key
rvashurin Nov 20, 2025
6c25d9c
Add estimators lists for greybox and blackbox models
rvashurin Nov 26, 2025
c0042ce
Fix default estimator lists
rvashurin Nov 26, 2025
2b6d8bb
Handle alternative logprobs return value for together
rvashurin Dec 4, 2025
aafd9c8
Use together's own library
rvashurin Dec 4, 2025
a151a61
Use correct client
rvashurin Dec 4, 2025
cf43c29
Fix some minor issues
rvashurin Dec 4, 2025
3f33642
Handle inconsistent number of alternative tokens presented by API
rvashurin Dec 4, 2025
1f3e4e9
Merge branch 'blackbox-adapters-to-main' of github.com:IINemo/lm-poly…
rvashurin Dec 4, 2025
e803640
Fix import
rvashurin Dec 4, 2025
ddb6641
Add visual modality to greybox estimators
rvashurin Mar 3, 2026
11080b4
Fix insonsistent generation calls in blackbox calculator, added rando…
rvashurin Mar 12, 2026
684c9a8
Allow to explicitly set supports_logprobs
rvashurin Mar 12, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pip install lm-polygraph
1. Initialize the base model (encoder-decoder or decoder-only) and tokenizer from HuggingFace or a local file, and use them to initialize the WhiteboxModel for evaluation:
```python
from transformers import AutoModelForCausalLM, AutoTokenizer
from lm_polygraph.utils.model import WhiteboxModel
from lm_polygraph.model_adapters.whitebox_model import WhiteboxModel

model_path = "Qwen/Qwen2.5-0.5B-Instruct"
base_model = AutoModelForCausalLM.from_pretrained(model_path, device_map="cuda:0")
Expand Down Expand Up @@ -77,10 +77,9 @@ LM-Polygraph can work with any OpenAI-compatible API services:
from lm_polygraph import BlackboxModel
from lm_polygraph.estimators import Perplexity, MaximumSequenceProbability

model = BlackboxModel.from_openai(
openai_api_key='YOUR_API_KEY',
model = BlackboxModel(
model_path='gpt-4o',
supports_logprobs=True # Enable for deployments
api_provider_name='openai'
)

ue_method = Perplexity() # or DetMat(), MeanTokenEntropy(), EigValLaplacian(), etc.
Expand Down
4 changes: 2 additions & 2 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Uncertainty for single input
.. code-block:: python

from transformers import AutoModelForCausalLM, AutoTokenizer
from lm_polygraph.utils.model import WhiteboxModel
from lm_polygraph.model_adapters.whitebox_model import WhiteboxModel

model_path = "bigscience/bloomz-560m"
base_model = AutoModelForCausalLM.from_pretrained(model_path, device_map="cuda:0")
Expand All @@ -74,7 +74,7 @@ Uncertainty for single input

.. code-block:: python

from lm_polygraph.utils.model import WhiteboxModel
from lm_polygraph.model_adapters.whitebox_model import WhiteboxModel

model = WhiteboxModel.from_pretrained(
"bigscience/bloomz-3b",
Expand Down
8 changes: 6 additions & 2 deletions examples/basic_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@
"%autoreload 2\n",
"\n",
"from transformers import AutoModelForCausalLM, AutoTokenizer\n",
"from lm_polygraph.utils.model import WhiteboxModel, BlackboxModel\n",
"from lm_polygraph.model_adapters.whitebox_model import WhiteboxModel\n",
"from lm_polygraph.model_adapters.blackbox_model import BlackboxModel\n",
"from lm_polygraph import estimate_uncertainty\n",
"from lm_polygraph.estimators import MaximumTokenProbability, MaximumSequenceProbability, SemanticEntropy, EigValLaplacian\n",
"from lm_polygraph.utils.generation_parameters import GenerationParameters"
Expand Down Expand Up @@ -381,7 +382,10 @@
"\n",
"HUGGINGFACE_API_TOKEN = '<Your HuggingFace API token>'\n",
"MODEL_ID = 'meta-llama/Llama-3.3-70B-Instruct'\n",
"model = BlackboxModel.from_huggingface(hf_api_token=HUGGINGFACE_API_TOKEN, hf_model_id=MODEL_ID, openai_api_key=None, openai_model_path=None)"
"model = BlackboxModel(\n",
" model_path=MODEL_ID,\n",
" api_provider_name='huggingface'\n",
")\n"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion examples/configs/estimators/default_claim_estimators.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@
idf_dataset_size: -1
spacy_path: "en_core_web_sm"
- name: FrequencyScoringClaim
- name: TokenSARClaim
- name: TokenSARClaim
16 changes: 16 additions & 0 deletions examples/configs/estimators/default_estimators_blackbox.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
- name: LexicalSimilarity
cfg:
metric: "rougeL"
- name: NumSemSets
- name: EigValLaplacian
cfg:
similarity_score: "NLI_score"
affinity: "entail"
- name: DegMat
cfg:
similarity_score: "NLI_score"
affinity: "entail"
- name: Eccentricity
cfg:
similarity_score: "NLI_score"
affinity: "entail"
27 changes: 27 additions & 0 deletions examples/configs/estimators/default_estimators_greybox.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
- name: MaximumSequenceProbability
- name: Perplexity
- name: MeanTokenEntropy
- name: MonteCarloSequenceEntropy
- name: MonteCarloNormalizedSequenceEntropy
- name: LexicalSimilarity
cfg:
metric: "rougeL"
- name: NumSemSets
- name: EigValLaplacian
cfg:
similarity_score: "NLI_score"
affinity: "entail"
- name: DegMat
cfg:
similarity_score: "NLI_score"
affinity: "entail"
- name: Eccentricity
cfg:
similarity_score: "NLI_score"
affinity: "entail"
- name: SemanticEntropy
- name: SentenceSAR
- name: CocoaMSP
- name: CocoaPPL
- name: CocoaMTE
- name: SemanticDensity
3 changes: 3 additions & 0 deletions examples/configs/model/gpt_4.1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
path: gpt-4.1
type: Blackbox
provider: openai
3 changes: 3 additions & 0 deletions examples/configs/model/llama_huggingface.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
path: meta-llama/Meta-Llama-3.1-8B-Instruct
type: Blackbox
provider: huggingface
3 changes: 3 additions & 0 deletions examples/configs/model/llama_together.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
path: meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo
type: Blackbox
provider: together_ai
2 changes: 1 addition & 1 deletion examples/other/ats_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"source": [
"from transformers import AutoModelForSeq2SeqLM, AutoTokenizer\n",
"from lm_polygraph.estimators import *\n",
"from lm_polygraph.utils.model import WhiteboxModel\n",
"from lm_polygraph.model_adapters.whitebox_model import WhiteboxModel\n",
"from lm_polygraph.utils.dataset import Dataset\n",
"from lm_polygraph.utils.processor import Logger\n",
"from lm_polygraph.utils.manager import UEManager\n",
Expand Down
2 changes: 1 addition & 1 deletion examples/other/claim_level_example_ar.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
"source": [
"import os\n",
"\n",
"from lm_polygraph.utils.model import WhiteboxModel\n",
"from lm_polygraph.model_adapters.whitebox_model import WhiteboxModel\n",
"from lm_polygraph.estimators import *\n",
"from lm_polygraph.stat_calculators import *\n",
"from lm_polygraph.utils.openai_chat import OpenAIChat\n",
Expand Down
2 changes: 1 addition & 1 deletion examples/other/claim_level_example_en.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"source": [
"import os\n",
"\n",
"from lm_polygraph.utils.model import WhiteboxModel\n",
"from lm_polygraph.model_adapters.whitebox_model import WhiteboxModel\n",
"from lm_polygraph.estimators import MaximumClaimProbability, ClaimConditionedProbabilityClaim\n",
"from lm_polygraph.stat_calculators import *\n",
"from lm_polygraph.utils.openai_chat import OpenAIChat\n",
Expand Down
2 changes: 1 addition & 1 deletion examples/other/claim_level_example_ru.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
"source": [
"import os\n",
"\n",
"from lm_polygraph.utils.model import WhiteboxModel\n",
"from lm_polygraph.model_adapters.whitebox_model import WhiteboxModel\n",
"from lm_polygraph.estimators import *\n",
"from lm_polygraph.stat_calculators import *\n",
"from lm_polygraph.utils.openai_chat import OpenAIChat\n",
Expand Down
2 changes: 1 addition & 1 deletion examples/other/claim_level_example_zh.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"import sys\n",
"sys.path.append('../src')\n",
"\n",
"from lm_polygraph.utils.model import WhiteboxModel\n",
"from lm_polygraph.model_adapters.whitebox_model import WhiteboxModel\n",
"from lm_polygraph.estimators import *\n",
"from lm_polygraph.stat_calculators import *\n",
"from lm_polygraph.utils.openai_chat import OpenAIChat\n",
Expand Down
2 changes: 1 addition & 1 deletion examples/other/mt_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"from datasets import load_dataset\n",
"from transformers import FSMTForConditionalGeneration, FSMTTokenizer\n",
"from lm_polygraph.estimators import *\n",
"from lm_polygraph.utils.model import WhiteboxModel\n",
"from lm_polygraph.model_adapters.whitebox_model import WhiteboxModel\n",
"from lm_polygraph.utils.dataset import Dataset\n",
"from lm_polygraph.utils.processor import Logger\n",
"from lm_polygraph.utils.manager import UEManager\n",
Expand Down
2 changes: 1 addition & 1 deletion examples/other/qa_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"\n",
"from transformers import AutoModelForCausalLM, AutoTokenizer\n",
"from lm_polygraph.estimators import *\n",
"from lm_polygraph.utils.model import WhiteboxModel\n",
"from lm_polygraph.model_adapters.whitebox_model import WhiteboxModel\n",
"from lm_polygraph.utils.dataset import Dataset\n",
"from lm_polygraph.utils.processor import Logger\n",
"from lm_polygraph.utils.manager import UEManager\n",
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ evaluate>=0.4.2
spacy>=3.4.0,<3.8.0
fastchat
diskcache>=5.6.3
together>=1.5
45 changes: 13 additions & 32 deletions scripts/polygraph_eval
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ logging.getLogger("httpx").setLevel(logging.WARNING)

from lm_polygraph.utils.manager import UEManager
from lm_polygraph.utils.dataset import Dataset
from lm_polygraph.utils.model import WhiteboxModel, BlackboxModel
from lm_polygraph.model_adapters import WhiteboxModelvLLM
from lm_polygraph.model_adapters import *
from lm_polygraph.utils.processor import Logger
from lm_polygraph.generation_metrics import *
from lm_polygraph.estimators import *
Expand Down Expand Up @@ -120,7 +119,7 @@ def main(args):
ue_metrics = get_ue_metrics(args)

builder_env_stat_calc = BuilderEnvironmentStatCalculator(model=model)
available_stat_calculators = get_stat_calculator_names(args)
available_stat_calculators = get_stat_calculator_names(args, model)

man = UEManager(
data=dataset,
Expand Down Expand Up @@ -171,16 +170,13 @@ def get_ue_metrics(args):
return ue_metrics


def get_stat_calculator_names(config):
def get_stat_calculator_names(config, model):
model_type = "Whitebox" if getattr(config.model, "type", "Whitebox") != "Blackbox" else "Blackbox"
language = getattr(config, "language", "en")
output_attentions = getattr(config, "output_attentions", True) and (getattr(config.model, "type", "Whitebox") != "vLLMCausalLM")
output_hidden_states = False if getattr(config.model, "type", "Whitebox") == "vLLMCausalLM" else True
hf_cache = getattr(config, "hf_cache", None)
deberta_batch_size = getattr(config, "deberta_batch_size", 10)
blackbox_supports_logprobs = model_type == "Blackbox" and getattr(
config.model, "supports_logprobs", False
)

all_stat_calculators = []
if "auto" in config.stat_calculators:
Expand All @@ -190,8 +186,8 @@ def get_stat_calculator_names(config):
hf_cache,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
blackbox_supports_logprobs=blackbox_supports_logprobs,
deberta_batch_size=deberta_batch_size,
model=model,
)

for stat_calculator in config.stat_calculators:
Expand Down Expand Up @@ -231,7 +227,7 @@ def get_generation_metrics(args):
RougeMetric("rouge2"),
RougeMetric("rougeL"),
BLEUMetric(),
BertScoreMetric("rh"),
BertScoreMetric(),
SbertMetric(),
AccuracyMetric(
target_ignore_regex=getattr(args, "target_ignore_regex", None),
Expand Down Expand Up @@ -316,32 +312,17 @@ def get_blackbox_model(args):
provider = getattr(args.model, "provider", "")
if provider is None or provider.strip() == "":
raise ValueError(
"Blackbox model provider cannot be empty or None. Please specify a valid provider."
)

supports_logprobs = getattr(args.model, "supports_logprobs", False)

if provider == "openai":
openai_api_key = os.environ.get("OPENAI_API_KEY")
if openai_api_key is None:
raise ValueError("OpenAI API key is not set in the environment variables.")
return BlackboxModel.from_openai(
openai_api_key=openai_api_key,
model_path=args.model.path,
supports_logprobs=supports_logprobs,
"Blackbox model provider cannot be empty or None. Please specify a valid provider (e.g. 'openai', 'huggingface', 'together_ai')."
)
elif provider == "huggingface":
hf_api_key = os.environ.get("HUGGINGFACE_API_KEY")
if hf_api_key is None:
raise ValueError(
"HuggingFace API key is not set in the environment variables."
)
return BlackboxModel.from_huggingface(
hf_api_token=hf_api_key, hf_model_id=args.model.path
)
else:
elif provider not in ["openai", "huggingface", "together_ai"]:
raise ValueError(f"Unsupported black-box model provider: {provider}")

return BlackboxModel(
model_path=args.model.path,
api_provider_name=provider,
supports_logprobs=getattr(args.model, "supports_logprobs", None),
)


def get_whitebox_model(args, cache_kwargs={}):
if not "path_to_load_script" in args.model or not args.model.path_to_load_script:
Expand Down
6 changes: 5 additions & 1 deletion src/lm_polygraph/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from .utils.model import WhiteboxModel, BlackboxModel
from .model_adapters.whitebox_model import WhiteboxModel
from .model_adapters.blackbox_model import BlackboxModel
from .utils.manager import UEManager
from .utils.estimate_uncertainty import estimate_uncertainty
from .utils.dataset import Dataset

# Import model adapters to ensure API provider adapters are registered
from . import model_adapters
Loading