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=, for 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=, for 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 mapping a system and user prompt to a 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
MultiImageVisionProvider ¶
Bases: Protocol
Optional native-region batching, preserving labels and image identities.
Source code in src/linexcel/aidoc.py
OpenAICompatProvider ¶
A client for anything exposing an OpenAI-compatible chat API.
The wire format is the only thing assumed. Which vendor, gateway or local
runtime answers at base_url is the caller's business.
base_url may contain {model} in its hostname or path. The model
is also sent in the JSON body. Pass this object as provider= to node,
workbook or image documentation. client_kwargs configures the SDK;
http_client_kwargs configures its default HTTPX transport (HTTPX2 in
newer SDKs); request_kwargs configures chat completions, including
extra_body, extra_headers and extra_query.
streaming=True explicitly assembles content deltas and terminal usage.
A stream must finish with stop; interrupted streams are never accepted
as documentation. SDK retries do not restart an interrupted stream.
Use a context manager to close connections created here. Injected SDK clients and HTTP clients remain caller-owned and are never closed here.
Source code in src/linexcel/aidoc.py
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 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 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 | |
request_parameters
property
¶
Actual wire sampling/token fields, including rejected attempts.
receipt
property
¶
Last completion receipt in this thread, excluding credentials.
vision_receipt
property
¶
Last image completion identity in this thread, excluding credentials.
close ¶
generate_with_image ¶
generate_with_image(
system_prompt: str,
user_prompt: str,
image: bytes,
*,
media_type: str = "image/png",
temperature: float = 0.2,
max_tokens: int | None = None,
) -> tuple[str, TokenUsage]
The same call, with the image inlined as a data URI.
No upload and no file id: the picture travels in the request body, so nothing is left behind on the endpoint's side.
Source code in src/linexcel/aidoc.py
generate_with_images ¶
generate_with_images(
system_prompt: str,
user_prompt: str,
images: list[tuple[str, bytes, str]],
*,
temperature: float = 0.2,
max_tokens: int | None = None,
) -> tuple[str, TokenUsage]
Send labelled native crops; the prompt supplies their exact manifest.
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 times 4/3 (the usual 1 token to
roughly 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
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 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 | |
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.
This is 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,
specialized_review: bool = True,
) -> str
Generate a Markdown overview grounded in the workbook dossier.
Provider resolution (first match wins; no implicit default):
1. provider, a custom LLMProvider instance or callable
2. base_url + model (or LINEXCEL_AI_BASE_URL + LINEXCEL_AI_MODEL),
for any OpenAI-compatible endpoint
context is the workbook presentation context, including 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.
By default, specialized_review makes two requests: draft, then review
against the same source dossier. Both count toward usage. A budget exhausted
after the draft prevents the review and no draft is published as reviewed.
A final request already in flight may finish above the approximate ceiling;
its complete response and cost are retained. max_tokens caps each
response, not the two-request document's combined output.
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
2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 | |
describe_images ¶
describe_images(
images: Mapping[
str,
bytes | str | Path | Iterable[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,
validation_results: dict[str, Any] | None = None,
evidence_dir: str | Path | None = None,
) -> dict[str, str]
Describe every native image region with bounded visual observations.
Large screenshots are cropped without resampling into overlapping 1024px regions. Each request contains exactly one native crop, including for multi-image providers. Each response must identify the requested region and short contiguous quotations. Their scope is the exact crop computed by code; the model supplies no geometry. Rendering is deterministic, with no further model-generated summary. This validates scope, not visual truth. Exact uniform or fully transparent decoded crops are resolved locally: their empty text observation is not a model response and has no provider receipt. Nonuniform pixels always require the model.
validation_results retains original/crop hashes, coordinates, raw
responses and request identities, including one optional complete retry
for invalid regional JSON. Both attempts retain their full raw response
and consumption; no reply is truncated or repaired. evidence_dir saves
the exact sent crops. Multiple pages belonging to one sheet are processed. A sheet
is returned only if every page and region completes; partial evidence is
retained, but cannot silently become a complete screenshot description.
Source code in src/linexcel/aidoc.py
2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 | |
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,
specialized_review: bool = True,
) -> 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.
specialized_review=True makes two requests per node: draft, then bounded
editorial review by the same provider. Both requests count toward usage.
An incomplete or unaffordable review never publishes the draft as reviewed.
Set it to False explicitly to request only one draft per node.
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. Each active node has its own remaining-budget snapshot, so the final
tally may include both phases of up to max_workers active nodes. Set the
ceiling as an order of magnitude, not to the token. Use max_tokens to
bound each individual response, not a document's combined output.
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
2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 | |