uqlm 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
uqlm-0.1.0/LICENSE ADDED
@@ -0,0 +1,176 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
uqlm-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,244 @@
1
+ Metadata-Version: 2.1
2
+ Name: uqlm
3
+ Version: 0.1.0
4
+ Summary: UQLM (Uncertainty Quantification for Language Models) is a Python package for UQ-based LLM hallucination detection.
5
+ Author: Dylan Bouchard
6
+ Author-email: dylan.bouchard@cvshealth.com
7
+ Maintainer: Dylan Bouchard
8
+ Maintainer-email: dbouchard92@gmail.com
9
+ Requires-Python: >=3.9,<3.13
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 3.9
12
+ Classifier: Programming Language :: Python :: 3.10
13
+ Classifier: Programming Language :: Python :: 3.11
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Requires-Dist: bert-score (>=0.3.0,<0.4.0)
16
+ Requires-Dist: datasets (>=3.3.2,<4.0.0)
17
+ Requires-Dist: langchain (>=0.3.7,<0.4.0)
18
+ Requires-Dist: matplotlib (>=3.9.2,<4.0.0)
19
+ Requires-Dist: numpy (>=1.26.4,<2.0.0)
20
+ Requires-Dist: optuna (>=4.0.0,<5.0.0)
21
+ Requires-Dist: pandas (>=2.2.3,<3.0.0)
22
+ Requires-Dist: scikit-learn (>=1.5.2,<2.0.0)
23
+ Requires-Dist: sentence-transformers (>=3.4.0,<4.0.0)
24
+ Requires-Dist: transformers (>=4.45.2,<5.0.0)
25
+ Description-Content-Type: text/markdown
26
+
27
+ <p align="center">
28
+ <img src="https://raw.githubusercontent.com/cvs-health/uqlm/develop/assets/images/uqlm_flow_ds.png" />
29
+ </p>
30
+
31
+
32
+ # uqlm: Uncertainty Quantification for Language Models
33
+
34
+ [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
35
+ [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
36
+ [![Build Status](https://github.com/cvs-health/uqlm/actions/workflows/ci.yaml/badge.svg)](https://github.com/cvs-health/uqlm/actions)
37
+ [![](https://img.shields.io/badge/arXiv-2504.19254-B31B1B.svg)](https://arxiv.org/abs/2504.19254)
38
+
39
+ UQLM is a Python library for Large Language Model (LLM) hallucination detection using state-of-the-art uncertainty quantification techniques.
40
+
41
+ ## Installation
42
+ The latest version can be installed from PyPI:
43
+
44
+ ```bash
45
+ pip install uqlm
46
+ ```
47
+
48
+ ## Hallucination Detection
49
+ UQLM provides a suite of response-level scorers for quantifying the uncertainty of Large Language Model (LLM) outputs. Each scorer returns a confidence score between 0 and 1, where higher scores indicate a lower likelihood of errors or hallucinations. We categorize these scorers into four main types:
50
+
51
+
52
+
53
+ | Scorer Type | Added Latency | Added Cost | Compatibility | Off-the-Shelf / Effort |
54
+ |------------------------|----------------------------------------------------|------------------------------------------|-----------------------------------------------------------|---------------------------------------------------------|
55
+ | [Black-Box Scorers](#black-box-scorers-consistency-based) | ⏱️ Medium-High (multiple generations & comparisons) | 💸 High (multiple LLM calls) | 🌍 Universal (works with any LLM) | ✅ Off-the-shelf |
56
+ | [White-Box Scorers](#white-box-scorers-token-probability-based) | ⚡ Minimal (token probabilities already returned) | ✔️ None (no extra LLM calls) | 🔒 Limited (requires access to token probabilities) | ✅ Off-the-shelf |
57
+ | [LLM-as-a-Judge Scorers](#llm-as-a-judge-scorers) | ⏳ Low-Medium (additional judge calls add latency) | 💵 Low-High (depends on number of judges)| 🌍 Universal (any LLM can serve as judge) |✅ Off-the-shelf |
58
+ | [Ensemble Scorers](#ensemble-scorers) | 🔀 Flexible (combines various scorers) | 🔀 Flexible (combines various scorers) | 🔀 Flexible (combines various scorers) | ✅ Off-the-shelf (beginner-friendly); 🛠️ Can be tuned (best for advanced users) |
59
+
60
+
61
+ Below we provide illustrative code snippets and details about available scorers for each type.
62
+
63
+ ### Black-Box Scorers (Consistency-Based)
64
+
65
+ These scorers assess uncertainty by measuring the consistency of multiple responses generated from the same prompt. They are compatible with any LLM, intuitive to use, and don't require access to internal model states or token probabilities.
66
+
67
+ <p align="center">
68
+ <img src="https://raw.githubusercontent.com/cvs-health/uqlm/develop/assets/images/black_box_graphic.png" />
69
+ </p>
70
+
71
+ **Example Usage:**
72
+ Below is a sample of code illustrating how to use the `BlackBoxUQ` class to conduct hallucination detection.
73
+
74
+ ```python
75
+ from langchain_google_vertexai import ChatVertexAI
76
+ llm = ChatVertexAI(model='gemini-pro')
77
+
78
+ from uqlm import BlackBoxUQ
79
+ bbuq = BlackBoxUQ(llm=llm, scorers=["semantic_negentropy"], use_best=True)
80
+
81
+ results = await bbuq.generate_and_score(prompts=prompts, num_responses=5)
82
+ results.to_df()
83
+ ```
84
+ <p align="center">
85
+ <img src="https://raw.githubusercontent.com/cvs-health/uqlm/develop/assets/images/black_box_output4.png" />
86
+ </p>
87
+
88
+ Above, `use_best=True` implements mitigation so that the uncertainty-minimized responses is selected. Note that although we use `ChatVertexAI` in this example, any [LangChain Chat Model](https://js.langchain.com/docs/integrations/chat/) may be used. For a more detailed demo, refer to our [Black-Box UQ Demo](./examples/black_box_demo.ipynb).
89
+
90
+
91
+ **Available Scorers:**
92
+
93
+ * Non-Contradiction Probability ([Chen & Mueller, 2023](https://arxiv.org/abs/2308.16175); [Lin et al., 2025](https://arxiv.org/abs/2305.19187); [Manakul et al., 2023](https://arxiv.org/abs/2303.08896))
94
+ * Semantic Entropy ([Farquhar et al., 2024](https://www.nature.com/articles/s41586-024-07421-0); [Kuhn et al., 2023](https://arxiv.org/abs/2302.09664))
95
+ * Exact Match ([Cole et al., 2023](https://arxiv.org/abs/2305.14613); [Chen & Mueller, 2023](https://arxiv.org/abs/2308.16175))
96
+ * BERT-score ([Manakul et al., 2023](https://arxiv.org/abs/2303.08896); [Zheng et al., 2020](https://arxiv.org/abs/1904.09675))
97
+ * BLUERT-score ([Sellam et al., 2020](https://arxiv.org/abs/2004.04696))
98
+ * Cosine Similarity ([Shorinwa et al., 2024](https://arxiv.org/abs/2412.05563); [HuggingFace](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2))
99
+
100
+ ### White-Box Scorers (Token-Probability-Based)
101
+
102
+ These scorers leverage token probabilities to estimate uncertainty. They are significantly faster and cheaper than black-box methods, but require access to the LLM's internal probabilities, meaning they are not necessarily compatible with all LLMs/APIs.
103
+
104
+ <p align="center">
105
+ <img src="https://raw.githubusercontent.com/cvs-health/uqlm/develop/assets/images/white_box_graphic.png" />
106
+ </p>
107
+
108
+ **Example Usage:**
109
+ Below is a sample of code illustrating how to use the `WhiteBoxUQ` class to conduct hallucination detection.
110
+
111
+ ```python
112
+ from langchain_google_vertexai import ChatVertexAI
113
+ llm = ChatVertexAI(model='gemini-pro')
114
+
115
+ from uqlm import WhiteBoxUQ
116
+ wbuq = WhiteBoxUQ(llm=llm, scorers=["min_probability"])
117
+
118
+ results = await wbuq.generate_and_score(prompts=prompts)
119
+ results.to_df()
120
+ ```
121
+ <p align="center">
122
+ <img src="https://raw.githubusercontent.com/cvs-health/uqlm/develop/assets/images/white_box_output2.png" />
123
+ </p>
124
+
125
+ Again, any [LangChain Chat Model](https://js.langchain.com/docs/integrations/chat/) may be used in place of `ChatVertexAI`. For a more detailed demo, refer to our [White-Box UQ Demo](./examples/white_box_demo.ipynb).
126
+
127
+
128
+ **Available Scorers:**
129
+
130
+ * Minimum token probability ([Manakul et al., 2023](https://arxiv.org/abs/2303.08896))
131
+ * Length-Normalized Joint Token Probability ([Malinin & Gales, 2021](https://arxiv.org/abs/2002.07650))
132
+
133
+ ### LLM-as-a-Judge Scorers
134
+
135
+ These scorers use one or more LLMs to evaluate the reliability of the original LLM's response. They offer high customizability through prompt engineering and the choice of judge LLM(s).
136
+
137
+ <p align="center">
138
+ <img src="https://raw.githubusercontent.com/cvs-health/uqlm/develop/assets/images/judges_graphic.png" />
139
+ </p>
140
+
141
+ **Example Usage:**
142
+ Below is a sample of code illustrating how to use the `LLMPanel` class to conduct hallucination detection using a panel of LLM judges.
143
+
144
+ ```python
145
+ from langchain_google_vertexai import ChatVertexAI
146
+ llm1 = ChatVertexAI(model='gemini-1.0-pro')
147
+ llm2 = ChatVertexAI(model='gemini-1.5-flash-001')
148
+ llm3 = ChatVertexAI(model='gemini-1.5-pro-001')
149
+
150
+ from uqlm import LLMPanel
151
+ panel = LLMPanel(llm=llm1, judges=[llm1, llm2, llm3])
152
+
153
+ results = await panel.generate_and_score(prompts=prompts)
154
+ results.to_df()
155
+ ```
156
+ <p align="center">
157
+ <img src="https://raw.githubusercontent.com/cvs-health/uqlm/develop/assets/images/panel_output2.png" />
158
+ </p>
159
+
160
+ Note that although we use `ChatVertexAI` in this example, we can use any [LangChain Chat Model](https://js.langchain.com/docs/integrations/chat/) as judges. For a more detailed demo illustrating how to customize a panel of LLM judges, refer to our [LLM-as-a-Judge Demo](./examples/judges_demo.ipynb).
161
+
162
+
163
+ **Available Scorers:**
164
+
165
+ * Categorical LLM-as-a-Judge ([Manakul et al., 2023](https://arxiv.org/abs/2303.08896); [Chen & Mueller, 2023](https://arxiv.org/abs/2308.16175); [Luo et al., 2023](https://arxiv.org/abs/2303.15621))
166
+ * Continuous LLM-as-a-Judge ([Xiong et al., 2024](https://arxiv.org/abs/2306.13063))
167
+ * Panel of LLM Judges ([Verga et al., 2024](https://arxiv.org/abs/2404.18796))
168
+
169
+ ### Ensemble Scorers
170
+
171
+ These scorers leverage a weighted average of multiple individual scorers to provide a more robust uncertainty/confidence estimate. They offer high flexibility and customizability, allowing you to tailor the ensemble to specific use cases.
172
+
173
+ <p align="center">
174
+ <img src="https://raw.githubusercontent.com/cvs-health/uqlm/develop/assets/images/uqensemble_generate_score.png" />
175
+ </p>
176
+
177
+ **Example Usage:**
178
+ Below is a sample of code illustrating how to use the `UQEnsemble` class to conduct hallucination detection.
179
+
180
+ ```python
181
+ from langchain_google_vertexai import ChatVertexAI
182
+ llm = ChatVertexAI(model='gemini-pro')
183
+
184
+ from uqlm import UQEnsemble
185
+ ## ---Option 1: Off-the-Shelf Ensemble---
186
+ # uqe = UQEnsemble(llm=llm)
187
+ # results = await uqe.generate_and_score(prompts=prompts, num_responses=5)
188
+
189
+ ## ---Option 2: Tuned Ensemble---
190
+ scorers = [ # specify which scorers to include
191
+ "exact_match", "noncontradiction", # black-box scorers
192
+ "min_probability", # white-box scorer
193
+ llm # use same LLM as a judge
194
+ ]
195
+ uqe = UQEnsemble(llm=llm, scorers=scorers)
196
+
197
+ # Tune on tuning prompts with provided ground truth answers
198
+ tune_results = await uqe.tune(
199
+ prompts=tuning_prompts, ground_truth_answers=ground_truth_answers
200
+ )
201
+ # ensemble is now tuned - generate responses on new prompts
202
+ results = await uqe.generate_and_score(prompts=prompts)
203
+ results.to_df()
204
+ ```
205
+ <p align="center">
206
+ <img src="https://raw.githubusercontent.com/cvs-health/uqlm/develop/assets/images/uqensemble_output2.png" />
207
+ </p>
208
+
209
+ As with the other examples, any [LangChain Chat Model](https://js.langchain.com/docs/integrations/chat/) may be used in place of `ChatVertexAI`. For more detailed demos, refer to our [Off-the-Shelf Ensemble Demo](./examples/ensemble_off_the_shelf_demo.ipynb) (quick start) or our [Ensemble Tuning Demo](./examples/ensemble_tuning_demo.ipynb) (advanced).
210
+
211
+
212
+ **Available Scorers:**
213
+
214
+ * BS Detector ([Chen & Mueller, 2023](https://arxiv.org/abs/2308.16175))
215
+ * Generalized UQ Ensemble ([Bouchard & Chauhan, 2025](https://arxiv.org/abs/2504.19254))
216
+
217
+ ## Documentation
218
+ Check out our [documentation site](https://cvs-health.github.io/uqlm/latest/index.html) for detailed instructions on using this package, including API reference and more.
219
+
220
+ ## Example notebooks
221
+ Explore the following demo notebooks to see how to use UQLM for various hallucination detection methods:
222
+
223
+ - [Black-Box Uncertainty Quantification](https://github.com/cvs-health/uqlm/blob/develop/examples/black_box_demo.ipynb): A notebook demonstrating hallucination detection with black-box (consistency) scorers.
224
+ - [White-Box Uncertainty Quantification](https://github.com/cvs-health/uqlm/blob/develop/examples/white_box_demo.ipynb): A notebook demonstrating hallucination detection with white-box (token probability-based) scorers.
225
+ - [LLM-as-a-Judge](https://github.com/cvs-health/uqlm/blob/develop/examples/judges_demo.ipynb): A notebook demonstrating hallucination detection with LLM-as-a-Judge.
226
+ - [Tunable UQ Ensemble](https://github.com/cvs-health/uqlm/blob/develop/examples/ensemble_tuning_demo.ipynb): A notebook demonstrating hallucination detection with a tunable ensemble of UQ scorers ([Bouchard & Chauhan, 2023](https://arxiv.org/abs/2504.19254)).
227
+ - [Off-the-Shelf UQ Ensemble](https://github.com/cvs-health/uqlm/blob/develop/examples/ensemble_off_the_shelf_demo.ipynb): A notebook demonstrating hallucination detection using BS Detector ([Chen & Mueller, 2023](https://arxiv.org/abs/2308.16175)) off-the-shelf ensemble.
228
+
229
+
230
+ ## Associated Research
231
+ A technical description of the `uqlm` scorers and extensive experiment results are contained in this **[this paper](https://arxiv.org/abs/2504.19254)**. If you use our framework or toolkit, we would appreciate citations to the following paper:
232
+
233
+ ```bibtex
234
+ @misc{bouchard2025uncertaintyquantificationlanguagemodels,
235
+ title={Uncertainty Quantification for Language Models: A Suite of Black-Box, White-Box, LLM Judge, and Ensemble Scorers},
236
+ author={Dylan Bouchard and Mohit Singh Chauhan},
237
+ year={2025},
238
+ eprint={2504.19254},
239
+ archivePrefix={arXiv},
240
+ primaryClass={cs.CL},
241
+ url={https://arxiv.org/abs/2504.19254},
242
+ }
243
+ ```
244
+
uqlm-0.1.0/README.md ADDED
@@ -0,0 +1,217 @@
1
+ <p align="center">
2
+ <img src="https://raw.githubusercontent.com/cvs-health/uqlm/develop/assets/images/uqlm_flow_ds.png" />
3
+ </p>
4
+
5
+
6
+ # uqlm: Uncertainty Quantification for Language Models
7
+
8
+ [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
9
+ [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
10
+ [![Build Status](https://github.com/cvs-health/uqlm/actions/workflows/ci.yaml/badge.svg)](https://github.com/cvs-health/uqlm/actions)
11
+ [![](https://img.shields.io/badge/arXiv-2504.19254-B31B1B.svg)](https://arxiv.org/abs/2504.19254)
12
+
13
+ UQLM is a Python library for Large Language Model (LLM) hallucination detection using state-of-the-art uncertainty quantification techniques.
14
+
15
+ ## Installation
16
+ The latest version can be installed from PyPI:
17
+
18
+ ```bash
19
+ pip install uqlm
20
+ ```
21
+
22
+ ## Hallucination Detection
23
+ UQLM provides a suite of response-level scorers for quantifying the uncertainty of Large Language Model (LLM) outputs. Each scorer returns a confidence score between 0 and 1, where higher scores indicate a lower likelihood of errors or hallucinations. We categorize these scorers into four main types:
24
+
25
+
26
+
27
+ | Scorer Type | Added Latency | Added Cost | Compatibility | Off-the-Shelf / Effort |
28
+ |------------------------|----------------------------------------------------|------------------------------------------|-----------------------------------------------------------|---------------------------------------------------------|
29
+ | [Black-Box Scorers](#black-box-scorers-consistency-based) | ⏱️ Medium-High (multiple generations & comparisons) | 💸 High (multiple LLM calls) | 🌍 Universal (works with any LLM) | ✅ Off-the-shelf |
30
+ | [White-Box Scorers](#white-box-scorers-token-probability-based) | ⚡ Minimal (token probabilities already returned) | ✔️ None (no extra LLM calls) | 🔒 Limited (requires access to token probabilities) | ✅ Off-the-shelf |
31
+ | [LLM-as-a-Judge Scorers](#llm-as-a-judge-scorers) | ⏳ Low-Medium (additional judge calls add latency) | 💵 Low-High (depends on number of judges)| 🌍 Universal (any LLM can serve as judge) |✅ Off-the-shelf |
32
+ | [Ensemble Scorers](#ensemble-scorers) | 🔀 Flexible (combines various scorers) | 🔀 Flexible (combines various scorers) | 🔀 Flexible (combines various scorers) | ✅ Off-the-shelf (beginner-friendly); 🛠️ Can be tuned (best for advanced users) |
33
+
34
+
35
+ Below we provide illustrative code snippets and details about available scorers for each type.
36
+
37
+ ### Black-Box Scorers (Consistency-Based)
38
+
39
+ These scorers assess uncertainty by measuring the consistency of multiple responses generated from the same prompt. They are compatible with any LLM, intuitive to use, and don't require access to internal model states or token probabilities.
40
+
41
+ <p align="center">
42
+ <img src="https://raw.githubusercontent.com/cvs-health/uqlm/develop/assets/images/black_box_graphic.png" />
43
+ </p>
44
+
45
+ **Example Usage:**
46
+ Below is a sample of code illustrating how to use the `BlackBoxUQ` class to conduct hallucination detection.
47
+
48
+ ```python
49
+ from langchain_google_vertexai import ChatVertexAI
50
+ llm = ChatVertexAI(model='gemini-pro')
51
+
52
+ from uqlm import BlackBoxUQ
53
+ bbuq = BlackBoxUQ(llm=llm, scorers=["semantic_negentropy"], use_best=True)
54
+
55
+ results = await bbuq.generate_and_score(prompts=prompts, num_responses=5)
56
+ results.to_df()
57
+ ```
58
+ <p align="center">
59
+ <img src="https://raw.githubusercontent.com/cvs-health/uqlm/develop/assets/images/black_box_output4.png" />
60
+ </p>
61
+
62
+ Above, `use_best=True` implements mitigation so that the uncertainty-minimized responses is selected. Note that although we use `ChatVertexAI` in this example, any [LangChain Chat Model](https://js.langchain.com/docs/integrations/chat/) may be used. For a more detailed demo, refer to our [Black-Box UQ Demo](./examples/black_box_demo.ipynb).
63
+
64
+
65
+ **Available Scorers:**
66
+
67
+ * Non-Contradiction Probability ([Chen & Mueller, 2023](https://arxiv.org/abs/2308.16175); [Lin et al., 2025](https://arxiv.org/abs/2305.19187); [Manakul et al., 2023](https://arxiv.org/abs/2303.08896))
68
+ * Semantic Entropy ([Farquhar et al., 2024](https://www.nature.com/articles/s41586-024-07421-0); [Kuhn et al., 2023](https://arxiv.org/abs/2302.09664))
69
+ * Exact Match ([Cole et al., 2023](https://arxiv.org/abs/2305.14613); [Chen & Mueller, 2023](https://arxiv.org/abs/2308.16175))
70
+ * BERT-score ([Manakul et al., 2023](https://arxiv.org/abs/2303.08896); [Zheng et al., 2020](https://arxiv.org/abs/1904.09675))
71
+ * BLUERT-score ([Sellam et al., 2020](https://arxiv.org/abs/2004.04696))
72
+ * Cosine Similarity ([Shorinwa et al., 2024](https://arxiv.org/abs/2412.05563); [HuggingFace](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2))
73
+
74
+ ### White-Box Scorers (Token-Probability-Based)
75
+
76
+ These scorers leverage token probabilities to estimate uncertainty. They are significantly faster and cheaper than black-box methods, but require access to the LLM's internal probabilities, meaning they are not necessarily compatible with all LLMs/APIs.
77
+
78
+ <p align="center">
79
+ <img src="https://raw.githubusercontent.com/cvs-health/uqlm/develop/assets/images/white_box_graphic.png" />
80
+ </p>
81
+
82
+ **Example Usage:**
83
+ Below is a sample of code illustrating how to use the `WhiteBoxUQ` class to conduct hallucination detection.
84
+
85
+ ```python
86
+ from langchain_google_vertexai import ChatVertexAI
87
+ llm = ChatVertexAI(model='gemini-pro')
88
+
89
+ from uqlm import WhiteBoxUQ
90
+ wbuq = WhiteBoxUQ(llm=llm, scorers=["min_probability"])
91
+
92
+ results = await wbuq.generate_and_score(prompts=prompts)
93
+ results.to_df()
94
+ ```
95
+ <p align="center">
96
+ <img src="https://raw.githubusercontent.com/cvs-health/uqlm/develop/assets/images/white_box_output2.png" />
97
+ </p>
98
+
99
+ Again, any [LangChain Chat Model](https://js.langchain.com/docs/integrations/chat/) may be used in place of `ChatVertexAI`. For a more detailed demo, refer to our [White-Box UQ Demo](./examples/white_box_demo.ipynb).
100
+
101
+
102
+ **Available Scorers:**
103
+
104
+ * Minimum token probability ([Manakul et al., 2023](https://arxiv.org/abs/2303.08896))
105
+ * Length-Normalized Joint Token Probability ([Malinin & Gales, 2021](https://arxiv.org/abs/2002.07650))
106
+
107
+ ### LLM-as-a-Judge Scorers
108
+
109
+ These scorers use one or more LLMs to evaluate the reliability of the original LLM's response. They offer high customizability through prompt engineering and the choice of judge LLM(s).
110
+
111
+ <p align="center">
112
+ <img src="https://raw.githubusercontent.com/cvs-health/uqlm/develop/assets/images/judges_graphic.png" />
113
+ </p>
114
+
115
+ **Example Usage:**
116
+ Below is a sample of code illustrating how to use the `LLMPanel` class to conduct hallucination detection using a panel of LLM judges.
117
+
118
+ ```python
119
+ from langchain_google_vertexai import ChatVertexAI
120
+ llm1 = ChatVertexAI(model='gemini-1.0-pro')
121
+ llm2 = ChatVertexAI(model='gemini-1.5-flash-001')
122
+ llm3 = ChatVertexAI(model='gemini-1.5-pro-001')
123
+
124
+ from uqlm import LLMPanel
125
+ panel = LLMPanel(llm=llm1, judges=[llm1, llm2, llm3])
126
+
127
+ results = await panel.generate_and_score(prompts=prompts)
128
+ results.to_df()
129
+ ```
130
+ <p align="center">
131
+ <img src="https://raw.githubusercontent.com/cvs-health/uqlm/develop/assets/images/panel_output2.png" />
132
+ </p>
133
+
134
+ Note that although we use `ChatVertexAI` in this example, we can use any [LangChain Chat Model](https://js.langchain.com/docs/integrations/chat/) as judges. For a more detailed demo illustrating how to customize a panel of LLM judges, refer to our [LLM-as-a-Judge Demo](./examples/judges_demo.ipynb).
135
+
136
+
137
+ **Available Scorers:**
138
+
139
+ * Categorical LLM-as-a-Judge ([Manakul et al., 2023](https://arxiv.org/abs/2303.08896); [Chen & Mueller, 2023](https://arxiv.org/abs/2308.16175); [Luo et al., 2023](https://arxiv.org/abs/2303.15621))
140
+ * Continuous LLM-as-a-Judge ([Xiong et al., 2024](https://arxiv.org/abs/2306.13063))
141
+ * Panel of LLM Judges ([Verga et al., 2024](https://arxiv.org/abs/2404.18796))
142
+
143
+ ### Ensemble Scorers
144
+
145
+ These scorers leverage a weighted average of multiple individual scorers to provide a more robust uncertainty/confidence estimate. They offer high flexibility and customizability, allowing you to tailor the ensemble to specific use cases.
146
+
147
+ <p align="center">
148
+ <img src="https://raw.githubusercontent.com/cvs-health/uqlm/develop/assets/images/uqensemble_generate_score.png" />
149
+ </p>
150
+
151
+ **Example Usage:**
152
+ Below is a sample of code illustrating how to use the `UQEnsemble` class to conduct hallucination detection.
153
+
154
+ ```python
155
+ from langchain_google_vertexai import ChatVertexAI
156
+ llm = ChatVertexAI(model='gemini-pro')
157
+
158
+ from uqlm import UQEnsemble
159
+ ## ---Option 1: Off-the-Shelf Ensemble---
160
+ # uqe = UQEnsemble(llm=llm)
161
+ # results = await uqe.generate_and_score(prompts=prompts, num_responses=5)
162
+
163
+ ## ---Option 2: Tuned Ensemble---
164
+ scorers = [ # specify which scorers to include
165
+ "exact_match", "noncontradiction", # black-box scorers
166
+ "min_probability", # white-box scorer
167
+ llm # use same LLM as a judge
168
+ ]
169
+ uqe = UQEnsemble(llm=llm, scorers=scorers)
170
+
171
+ # Tune on tuning prompts with provided ground truth answers
172
+ tune_results = await uqe.tune(
173
+ prompts=tuning_prompts, ground_truth_answers=ground_truth_answers
174
+ )
175
+ # ensemble is now tuned - generate responses on new prompts
176
+ results = await uqe.generate_and_score(prompts=prompts)
177
+ results.to_df()
178
+ ```
179
+ <p align="center">
180
+ <img src="https://raw.githubusercontent.com/cvs-health/uqlm/develop/assets/images/uqensemble_output2.png" />
181
+ </p>
182
+
183
+ As with the other examples, any [LangChain Chat Model](https://js.langchain.com/docs/integrations/chat/) may be used in place of `ChatVertexAI`. For more detailed demos, refer to our [Off-the-Shelf Ensemble Demo](./examples/ensemble_off_the_shelf_demo.ipynb) (quick start) or our [Ensemble Tuning Demo](./examples/ensemble_tuning_demo.ipynb) (advanced).
184
+
185
+
186
+ **Available Scorers:**
187
+
188
+ * BS Detector ([Chen & Mueller, 2023](https://arxiv.org/abs/2308.16175))
189
+ * Generalized UQ Ensemble ([Bouchard & Chauhan, 2025](https://arxiv.org/abs/2504.19254))
190
+
191
+ ## Documentation
192
+ Check out our [documentation site](https://cvs-health.github.io/uqlm/latest/index.html) for detailed instructions on using this package, including API reference and more.
193
+
194
+ ## Example notebooks
195
+ Explore the following demo notebooks to see how to use UQLM for various hallucination detection methods:
196
+
197
+ - [Black-Box Uncertainty Quantification](https://github.com/cvs-health/uqlm/blob/develop/examples/black_box_demo.ipynb): A notebook demonstrating hallucination detection with black-box (consistency) scorers.
198
+ - [White-Box Uncertainty Quantification](https://github.com/cvs-health/uqlm/blob/develop/examples/white_box_demo.ipynb): A notebook demonstrating hallucination detection with white-box (token probability-based) scorers.
199
+ - [LLM-as-a-Judge](https://github.com/cvs-health/uqlm/blob/develop/examples/judges_demo.ipynb): A notebook demonstrating hallucination detection with LLM-as-a-Judge.
200
+ - [Tunable UQ Ensemble](https://github.com/cvs-health/uqlm/blob/develop/examples/ensemble_tuning_demo.ipynb): A notebook demonstrating hallucination detection with a tunable ensemble of UQ scorers ([Bouchard & Chauhan, 2023](https://arxiv.org/abs/2504.19254)).
201
+ - [Off-the-Shelf UQ Ensemble](https://github.com/cvs-health/uqlm/blob/develop/examples/ensemble_off_the_shelf_demo.ipynb): A notebook demonstrating hallucination detection using BS Detector ([Chen & Mueller, 2023](https://arxiv.org/abs/2308.16175)) off-the-shelf ensemble.
202
+
203
+
204
+ ## Associated Research
205
+ A technical description of the `uqlm` scorers and extensive experiment results are contained in this **[this paper](https://arxiv.org/abs/2504.19254)**. If you use our framework or toolkit, we would appreciate citations to the following paper:
206
+
207
+ ```bibtex
208
+ @misc{bouchard2025uncertaintyquantificationlanguagemodels,
209
+ title={Uncertainty Quantification for Language Models: A Suite of Black-Box, White-Box, LLM Judge, and Ensemble Scorers},
210
+ author={Dylan Bouchard and Mohit Singh Chauhan},
211
+ year={2025},
212
+ eprint={2504.19254},
213
+ archivePrefix={arXiv},
214
+ primaryClass={cs.CL},
215
+ url={https://arxiv.org/abs/2504.19254},
216
+ }
217
+ ```