Package: lm-polygraph
LM-Polygraph: Uncertainty estimation for LLMsInstallation|Basic usage|Overview|Benchmark|Demo application|DocumentationLM-Polygraph provides a battery of state-of-the-art of uncertainty estimation (UE) methods for LMs in text generation tasks. High uncertainty can indicate the presence of hallucinations and knowing a score that estimates uncertinaty can help to make applications of LLMs safer.The framework also introduces an extendable benchmark for consistent evaluation of UE techniques by researchers and a demo web application that enriches the standard chat dialog with confidence scores, empowering end-users to discern unreliable responses.Installationgit clone https://github.com/IINemo/lm-polygraph.git && cd lm-polygraph && pip install .Basic usageInitialize the model (encoder-decoder or decoder-only) from HuggingFace or a local file. For example,bigscience/bloomz-3bfromlm_polygraph.utils.modelimportWhiteboxModelmodel=WhiteboxModel.from_pretrained("bigscience/bloomz-3b",device="cuda:0",)Specify UE methodfromlm_polygraph.estimatorsimport*ue_method=MeanPointwiseMutualInformation()Get predictions and their uncertainty scoresfromlm_polygraph.utils.managerimportestimate_uncertaintyinput_text="Who is George Bush?"estimate_uncertainty(model,ue_method,input_text=input_text)Other examples:example.ipynb: simple examples of scoring individual queries;qa_example.ipynb: an example of scoring thebigscience/bloomz-3bmodel on theTriviaQAdataset;mt_example.ipynb: an of scoring thefacebook/wmt19-en-demodel on theWMT14 En-Dedataset;ats_example.ipynb: an example of scoring thefacebook/bart-large-cnnmodel on theXSUMsummarization dataset;colab: demo web application in Colab (bloomz-560m,gpt-3.5-turbo, andgpt-4fit the default memory limit; other models require Colab-pro).Overview of methodsUncertainty Estimation MethodTypeCategoryComputeMemoryNeed Training Data?Maximum sequence probabilityWhite-boxInformation-basedLowLowNoPerplexity(Fomicheva et al., 2020a)White-boxInformation-basedLowLowNoMean token entropy(Fomicheva et al., 2020a)White-boxInformation-basedLowLowNoMonte Carlo sequence entropy(Kuhn et al., 2023)White-boxInformation-basedHighLowNoPointwise mutual information (PMI)(Takayama and Arase, 2019)White-boxInformation-basedMediumLowNoConditional PMI(van der Poel et al., 2022)White-boxInformation-basedMediumMediumNoSemantic entropy(Kuhn et al., 2023)White-boxMeaning diversityHighLowNoSentence-level ensemble-based measures(Malinin and Gales, 2020)White-boxEnsemblingHighHighYesToken-level ensemble-based measures(Malinin and Gales, 2020)White-boxEnsemblingHighHighYesMahalanobis distance (MD)(Lee et al., 2018)White-boxDensity-basedLowLowYesRobust density estimation (RDE)(Yoo et al., 2022)White-boxDensity-basedLowLowYesRelative Mahalanobis distance (RMD)(Ren et al., 2023)White-boxDensity-basedLowLowYesHybrid Uncertainty Quantification (HUQ)(Vazhentsev et al., 2023a)White-boxDensity-basedLowLowYesp(True)(Kadavath et al., 2022)White-boxReflexiveMediumLowNoNumber of semantic sets (NumSets)(Kuhn et al., 2023)Black-boxMeaning DiversityHighLowNoSum of eigenvalues of the graph Laplacian (EigV)(Lin et al., 2023)Black-boxMeaning DiversityHighLowNoDegree matrix (Deg)(Lin et al., 2023)Black-boxMeaning DiversityHighLowNoEccentricity (Ecc)(Lin et al., 2023)Black-boxMeaning DiversityHighLowNoLexical similarity (LexSim)(Fomicheva et al., 2020a)Black-boxMeaning DiversityHighLowNoBenchmarkTo evaluate the performance of uncertainty estimation methods consider a quick example:HYDRA_CONFIG=../configs/polygraph_eval/polygraph_eval.yaml python ./scripts/polygraph_eval \
    dataset="./workdir/data/triviaqa.csv" \
    model="databricks/dolly-v2-3b" \
    save_path="./workdir/output" \
    seed=[1,2,3,4,5]Usevisualization_tables.ipynbto generate the summarizing tables for an experiment.A detailed description of the benchmark is in thedocumentation.Demo web applicationStart with Dockerdockerrun-p3001:3001-it\-v$HOME/.cache/huggingface/hub:/root/.cache/huggingface/hub\--gpusallmephodybro/polygraph_demo:0.0.17polygraph_serverThe server should be available onhttp://localhost:3001A more detailed description of the demo is available in thedocumentation.AcknowledgementsThe chat GUI implementation is based on thechatgpt-web-applicationproject.