linexcel.aidoc¶
linexcel.aidoc ¶
AI-generated documentation for Excel calculations.
Vendor-neutral by construction: no provider is named in the code and none is chosen for you. There are exactly two ways in:
base_url=— any OpenAI-compatible endpoint (a local Ollama, vLLM or LM Studio runtime; a hosted gateway such as OpenRouter; OpenAI itself; anything else that speaks/chat/completions)provider=— your own callable or :class:LLMProviderobject, for an API that speaks something else entirely
Each node is presented with deterministic evidence from the graph and source metadata. Prompts ask the model to cite this evidence; generated claims still require review and are not a proof of calculation correctness.
TokenUsage
dataclass
¶
Tokens consumed by one or more documentation requests.
estimated is True as soon as any request in the tally had to be
approximated by :func:estimate_tokens instead of being reported by the
provider — treat such a total as an order of magnitude, not a bill.
Source code in src/linexcel/aidoc.py
add ¶
Accumulate other in place, keeping the model/provider labels.
Source code in src/linexcel/aidoc.py
LLMProvider ¶
Bases: Protocol
Minimal protocol: system + user prompt → text response.
Source code in src/linexcel/aidoc.py
UsageReportingProvider ¶
Bases: Protocol
A provider that also reports what the call consumed.
Optional: the built-in OpenAI-compatible client implements it so that token
counts come from the API rather than from an approximation. Custom
providers only need :class:LLMProvider.
Source code in src/linexcel/aidoc.py
VisionProvider ¶
Bases: Protocol
A provider that can be handed an image alongside the prompts.
Optional, and separate from :class:LLMProvider on purpose: most models
served behind an OpenAI-compatible endpoint are text-only, and a caller
asking for screenshot descriptions should be told so rather than have an
image quietly dropped from the request.
Source code in src/linexcel/aidoc.py
estimate_tokens ¶
Approximate the token count of text.
Only a fallback: :class:TokenUsage prefers the counts the provider
reports. Latin script is counted as words × 4/3 (the usual 1 token ≈ 0.75
words ratio); CJK characters are counted individually, because a Japanese
or Chinese sentence carries no spaces and would otherwise register as a
single word.
estimate_tokens("the quick brown fox jumps") 6 estimate_tokens("") 0
Source code in src/linexcel/aidoc.py
build_dossier ¶
Deterministic dossier for a node: everything the AI is allowed to use.
build_workbook_dossier ¶
build_workbook_dossier(
graph: dict[str, Any],
*,
context: dict[str, Any] | None = None,
) -> dict[str, Any]
Return a compact, deterministic dossier for a whole-workbook overview.
context is a :attr:linexcel.LineageResult.workbook_context mapping.
The graph alone describes how a workbook computes; it says nothing about
what a reader sees on opening it — titles sitting above a table, the labels
in the first column, cell comments, hidden columns, frozen panes. Those cues
are exactly what the sheet screenshots show, and merging them into each
sheet entry lets a text-only model describe the file as it looks without any
image ever leaving the machine.
Both parts stay deterministic: every value is read from the workbook, so the "cite only the dossier" rule of the system prompt still holds.
Source code in src/linexcel/aidoc.py
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 | |
render_markdown_table ¶
render_markdown_table(
columns: list[Any],
rows: list[list[Any]],
*,
caption: str | None = None,
) -> str
Render a Markdown pipe table from structured data — the only place in linexcel where table syntax is written.
A column whose cells all parse as numbers is right-aligned (---:),
so amounts line up on their decimal separator. Rows shorter than the
header are padded, longer ones truncated: a ragged model row can no
longer shift every column after it.
Source code in src/linexcel/aidoc.py
document_workbook ¶
document_workbook(
graph: dict[str, Any],
*,
model: str | None = None,
api_key: str | None = None,
base_url: str | None = None,
provider: ProviderLike | None = None,
language: str = "en",
usage: TokenUsage | None = None,
max_tokens: int | None = None,
token_budget: int | None = None,
context: dict[str, Any] | None = None,
validation_results: dict[str, Any] | None = None,
) -> str
Generate a Markdown overview grounded in the workbook dossier.
Provider resolution (first match wins; no implicit default):
1. provider — custom LLMProvider instance or callable
2. base_url + model (or LINEXCEL_AI_BASE_URL + LINEXCEL_AI_MODEL) —
any OpenAI-compatible endpoint
context is the workbook presentation context — the sheet previews,
comments, merged cells, frozen panes and hidden columns a reader sees when
opening the file. Pass it to describe the workbook as it looks, not only as
it computes; see :func:build_workbook_dossier.
If a :class:TokenUsage is passed as usage, what the call consumed is
accumulated into it. token_budget caps cumulative spend across that
accumulator: an already-exhausted budget raises before anything is sent.
validation_results, when supplied, receives the bounded quotation-check
report under workbook, including the raw response. Unsupported comparable
quotations and processing limits add a visible qualification; ellipses and
numerical illustrations remain explicitly unverified without alerts.
Source code in src/linexcel/aidoc.py
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 | |
describe_images ¶
describe_images(
images: Mapping[str, bytes | str | Path],
*,
model: str | None = None,
api_key: str | None = None,
base_url: str | None = None,
provider: ProviderLike | None = None,
language: str = "en",
usage: TokenUsage | None = None,
max_tokens: int | None = None,
token_budget: int | None = None,
) -> dict[str, str]
Describe rendered images with a multimodal model, {name: markdown}.
images maps a name — a sheet name, in practice — to a PNG, either as
bytes or as a path to read. Each one is sent on its own, so a description
is grounded in a single picture and nothing else.
This is the one part of linexcel whose evidence is not the deterministic dossier: a screenshot shows what no extraction reaches — colour conventions, conditional formatting, charts, the shape of a layout — and the prompt confines the model to what is visible rather than letting it reason about the calculation.
The provider must accept an image (:class:VisionProvider); a text-only
one raises rather than having the picture dropped from the request. Model
resolution is otherwise :func:document_workbook's, so model= here is
where a vision model is named when it differs from the writing one.
Images are sent one at a time: they are large, and the local runtimes this
is most used against serialize them anyway. An image that fails is skipped
with a :class:UserWarning; :class:AiDocError is raised only when every
one failed. token_budget is checked before each call.
Source code in src/linexcel/aidoc.py
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 | |
document_nodes ¶
document_nodes(
graph: dict[str, Any],
node_ids: list[str],
*,
model: str | None = None,
api_key: str | None = None,
base_url: str | None = None,
provider: ProviderLike | None = None,
language: str = "en",
max_workers: int = 4,
usage: TokenUsage | None = None,
max_tokens: int | None = None,
token_budget: int | None = None,
validation_results: dict[str, Any] | None = None,
) -> dict[str, str]
Document the requested nodes, returns {node_id: markdown}.
Provider resolution is the same as :func:document_workbook (no implicit
default; see :func:_resolve_provider).
Nodes are documented concurrently (max_workers in-flight requests;
raise it if the provider's rate limits allow). Documenting a large
workbook is a long, often billed operation, so a node that fails does not
discard the ones that succeeded: the successful cards are returned and a
:class:UserWarning reports how many nodes were dropped.
:class:AiDocError is raised only when every node failed.
If a :class:TokenUsage is passed as usage, consumed tokens are
accumulated into it, including usage reported for rejected responses and
calls in a run that later fails. Tokens already spent are still billed.
token_budget is a ceiling on the total tokens the run may spend,
input and output together, counted against usage so several calls
sharing one accumulator share one ceiling. It is enforced between requests,
the only point at which a cost is known: nodes still queued when the tally
reaches the budget are never sent, and a :class:UserWarning reports how
many were left undocumented. Requests already in flight are allowed to
finish, so the final tally can exceed the budget by up to max_workers
responses — set it as an order of magnitude, not to the token. Use
max_tokens to bound each individual response instead.
validation_results, when supplied, receives a bounded quotation-check
report keyed by node ID, including raw responses. Formula quotations without
source support are visibly qualified, not silently corrected. General prose
remains unverified even when every inspected quotation has source support.
Source code in src/linexcel/aidoc.py
1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 | |