Confidence parsing of LLM outputs
Project description
Certus: understanding LLM certainty
Certus allows you to estimate confidence in a LLM response, both as a whole and in each part. It does this by parsing the log-probabilities from your response into a tree of nodes.
We build this tree from an ordered collection of certus.nodes.core.Token instances and gathering them up recursively
into a tree matching the structure of the response. Each Token is considered a leaf node in the tree, and higher-up
nodes in the tree are of other types.
Installation
The most convenient way to install Certus is to do so from PyPI:
python -m pip install certus
Developers
If you are planning to do some development work on Certus, please install the package from source and use uv:
git clone https://github.com/daffidwilde/certus
cd certus
uv sync --dev
Usage
Extracting token nodes from a response
To map your LLM response to the collection of leaf nodes, use the certus.interface module:
>>> import certus as ct
>>> from google.genai import types
>>>
>>> data = "certus"
>>> logprobs = types.LogprobsResult( # taken from `response.candidates[0].logprobsResult`
... chosen_candidates=[
... types.LogprobsResultCandidate(log_probability=0.0, token='"', token_id=24),
... types.LogprobsResultCandidate(log_probability=-0.0123, token="certus", token_id=42),
... types.LogprobsResultCandidate(log_probability=0.0, token='"', token_id=24),
... ]
... )
>>> tokens = ct.interface.from_google(logprobs)
>>> tokens
[Token(value='"', logprob=0.0, start=0), Token(value='certus', logprob=-0.0123, start=1), Token(value='"', logprob=0.0, start=7)]
This list of token nodes is ready to be parsed into a tree.
Building a tree
Consider this piece of JSON-friendly data:
>>> import certus as ct
>>>
>>> data = {
... "name": "Henry Wilde",
... "age": 29,
... "longest_walk_km": 160.9,
... "pets": [
... {
... "name": "Billie",
... "species": "cat",
... "favourite_foods": [
... "fish",
... "oat milk",
... {
... "name": "chicken",
... "preparation": "boiled",
... "when_sick": True,
... },
... ],
... },
... ],
... }
>>>
Let's say this data came from a gpt-4o response. We can tokenise this dictionary using tiktoken and simulate some
log-probabilities to go with them. From there, we can create a collection of Token leaf nodes ready for parsing;
details to do this are hidden below.
Simulating data tokens
>>> import json
>>> import random
>>>
>>> import tiktoken
>>>
>>> def tokenise_string(string: str, encoder: tiktoken.Encoding) -> list[str]:
... encoded = encoder.encode(string)
... return [encoder.decode_single_token_bytes(e).decode() for e in encoded]
>>>
>>> encoder = tiktoken.encoding_for_model("gpt-4o")
>>> data_tokenised = tokenise_string(json.dumps(data), encoder)
>>>
>>> random.seed(0)
>>> tokens, position = [], 0
>>> for t in data_tokenised:
... tokens.append(ct.nodes.Token(t, -round(random.expovariate(1e4), 6), position))
... position += len(t)
>>>
>>> assert json.loads("".join(t.value for t in tokens)) == data
>>>
Now, we can parse this dictionary response and token nodes into a single Object node using the
certus.parsers.parse_json() function:
>>> parsed = ct.parsers.parse_json(data, tokens)
>>> parsed # doctest:+SKIP
Object(
fields={
'name': Composite(children=[Token(value=' "', logprob=-3e-05, start=8), Token(value='Henry', logprob=-7.2e-05, start=10), Token(value=' Wilde', logprob=-5.2e-05, start=15), Token(value='",', logprob=-0.000153, start=21)]),
'age': Token(value='29', logprob=-7e-05, start=31),
'longest_walk_km': Composite(children=[Token(value='160', logprob=-0.000131, start=54), Token(value='.', logprob=-0.000229, start=57), Token(value='9', logprob=-0.000115, start=58)]),
'pets': Array(
elements=[
Object(
fields={
'name': Composite(children=[Token(value=' "', logprob=-0.0002, start=78), Token(value='Bill', logprob=-3e-05, start=80), Token(value='ie', logprob=-0.000163, start=84), Token(value='",', logprob=-8e-05, start=86)]),
'species': Composite(children=[Token(value=' "', logprob=-0.000174, start=99), Token(value='cat', logprob=-0.00011, start=101), Token(value='",', logprob=-0.0, start=104)]),
'favourite_foods': Array(
elements=[
Composite(children=[Token(value=' ["', logprob=-8.4e-05, start=125), Token(value='fish', logprob=-2.7e-05, start=128), Token(value='",', logprob=-0.000343, start=132)]),
Composite(children=[Token(value=' "', logprob=-0.000163, start=134), Token(value='o', logprob=-5.9e-05, start=136), Token(value='at', logprob=-8e-06, start=137), Token(value=' milk', logprob=-3.9e-05, start=139), Token(value='",', logprob=-7.1e-05, start=144)]),
Object(
fields={
'name': Composite(children=[Token(value=' "', logprob=-0.000123, start=155), Token(value='ch', logprob=-7.9e-05, start=157), Token(value='icken', logprob=-0.000168, start=159), Token(value='",', logprob=-7.8e-05, start=164)]),
'preparation': Composite(children=[Token(value=' "', logprob=-9.1e-05, start=181), Token(value='bo', logprob=-4.9e-05, start=183), Token(value='iled', logprob=-8.6e-05, start=185), Token(value='",', logprob=-3.4e-05, start=189)]),
'when_sick': Token(value=' true', logprob=-9e-06, start=204)
}
)
]
)
}
)
]
)
}
)
That's a lot of information, but you should be able to see a few node types here:
certus.nodes.core.Composite: a collection ofTokennodescertus.nodes.struct.Array: a collection of node elements, which behaves like alistcertus.nodes.struct.Object: a mapping of keys to nodes, which behaves like adict
We can leverage the list/dict-like properties of our Object node to look at the confidence in its various
components:
>>> parsed.confidence # the whole response
0.9999025047529705
>>> for key, value in parsed.items():
... print(key.ljust(16), value.confidence)
name 0.9999232529452059
age 0.9999300024499428
longest_walk_km 0.9998416792007273
pets 0.9999055044649844
>>>
>>> parsed["pets"][0]["favourite_foods"][-1]["name"].confidence # Billie's last favourite food
0.9998880062717659
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 certus-0.0.2.tar.gz.
File metadata
- Download URL: certus-0.0.2.tar.gz
- Upload date:
- Size: 129.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0705ffa43819e42308c5f15168b7e1c9f1f3c45a46b79cd51233de658fbc969e
|
|
| MD5 |
c73ef713b2dfc384f337290bd464a923
|
|
| BLAKE2b-256 |
99f74520a38c67a3c68c9ec238f80185d83bfe4d5add245986bdb120af25838e
|
File details
Details for the file certus-0.0.2-py3-none-any.whl.
File metadata
- Download URL: certus-0.0.2-py3-none-any.whl
- Upload date:
- Size: 10.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
60cc4185afe26de511f2fd487fff1d7953a7f79ad49666dad8962f2e13675fe9
|
|
| MD5 |
00e4bef7e8f4682397b918bb75fb94bb
|
|
| BLAKE2b-256 |
bf02a3ec8b7dc9e1c8c6ae50f1591d4a7fad56692b6c2ce90958842cc5949363
|