-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_budget.py
More file actions
62 lines (45 loc) · 1.76 KB
/
Copy pathtest_budget.py
File metadata and controls
62 lines (45 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from __future__ import annotations
from deepread.budget import (
CONTEXT_LIMIT,
SAFE_LIMIT,
TOKENS_PER_IMAGE,
budget,
estimate,
)
from deepread.ingest import Shard
def _img_shard(name: str = "doc.pdf#p1") -> Shard:
return Shard(cite_id=name, source="doc.pdf", page=1, png_bytes=b"\x89PNG..", extracted_text="")
def _text_shard(text: str, name: str = "notes.md") -> Shard:
return Shard(cite_id=name, source=name, page=None, png_bytes=b"", extracted_text=text)
def test_empty_shard_list_uses_zero_tokens():
b = budget([])
assert b["used"] == 0
assert b["pct"] == 0
assert not b["over_safe"]
assert not b["over_limit"]
def test_image_shard_costs_fixed_amount():
assert estimate(_img_shard()) == TOKENS_PER_IMAGE
def test_pct_monotonically_increases_with_shards():
b1 = budget([_img_shard("a#p1")])
b2 = budget([_img_shard("a#p1"), _img_shard("a#p2")])
b3 = budget([_img_shard(f"a#p{i}") for i in range(10)])
assert b1["pct"] < b2["pct"] < b3["pct"]
def test_over_safe_flips_at_threshold():
# Enough image shards to cross SAFE_LIMIT but stay under CONTEXT_LIMIT
n = (SAFE_LIMIT // TOKENS_PER_IMAGE) + 2
shards = [_img_shard(f"p{i}") for i in range(n)]
b = budget(shards)
assert b["used"] > SAFE_LIMIT
assert b["used"] <= CONTEXT_LIMIT
assert b["over_safe"]
assert not b["over_limit"]
def test_over_limit_flips_when_truly_over():
n = (CONTEXT_LIMIT // TOKENS_PER_IMAGE) + 2
shards = [_img_shard(f"p{i}") for i in range(n)]
b = budget(shards)
assert b["over_limit"]
assert b["pct"] == 100 # capped at 100
def test_text_shard_token_count_scales_with_length():
short = _text_shard("x" * 100)
long = _text_shard("x" * 10_000)
assert estimate(long) > estimate(short)