langchain-cellprose 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.
- langchain_cellprose-0.1.0/.gitignore +37 -0
- langchain_cellprose-0.1.0/PKG-INFO +35 -0
- langchain_cellprose-0.1.0/README.md +20 -0
- langchain_cellprose-0.1.0/pyproject.toml +22 -0
- langchain_cellprose-0.1.0/src/langchain_cellprose/__init__.py +74 -0
- langchain_cellprose-0.1.0/tests/conftest.py +19 -0
- langchain_cellprose-0.1.0/tests/test_loader.py +39 -0
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
node_modules/
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.pyc
|
|
4
|
+
.venv/
|
|
5
|
+
.pytest_cache/
|
|
6
|
+
*.egg-info/
|
|
7
|
+
dist/
|
|
8
|
+
build/
|
|
9
|
+
benchmarks/results/*.log
|
|
10
|
+
.DS_Store
|
|
11
|
+
benchmarks/external/.cache/
|
|
12
|
+
benchmarks/external/spreadsheetbench/
|
|
13
|
+
benchmarks/results/headline.json
|
|
14
|
+
benchmarks/results/verdicts.json
|
|
15
|
+
benchmarks/external/realworld/workbooks/
|
|
16
|
+
benchmarks/external/realworld/encoded/
|
|
17
|
+
benchmarks/external/realworld2/workbooks/
|
|
18
|
+
benchmarks/external/realworld2/encoded/
|
|
19
|
+
benchmarks/external/realworld3/workbooks/
|
|
20
|
+
benchmarks/external/realworld3/encoded/
|
|
21
|
+
benchmarks/external/devlarge/files/
|
|
22
|
+
benchmarks/external/devlarge/workbooks/
|
|
23
|
+
benchmarks/external/devlarge/encoded/
|
|
24
|
+
benchmarks/external/devdata/files/
|
|
25
|
+
benchmarks/external/devdata/workbooks/
|
|
26
|
+
benchmarks/external/devdata/encoded/
|
|
27
|
+
benchmarks/external/devdata2/files/
|
|
28
|
+
benchmarks/external/devdata2/workbooks/
|
|
29
|
+
benchmarks/external/devdata2/encoded/
|
|
30
|
+
# SpreadsheetBench-derived raw responses and per-answer scores are not committed (share-alike licence); checksums are
|
|
31
|
+
benchmarks/results/run_sb/raw/
|
|
32
|
+
benchmarks/results/run_sb/scores.csv
|
|
33
|
+
benchmarks/results/preflight_lean/sb/raw/
|
|
34
|
+
benchmarks/results/preflight_lean/sb/scores.csv
|
|
35
|
+
benchmarks/results/addendum_sb/raw/
|
|
36
|
+
benchmarks/results/addendum_sb/scores.csv
|
|
37
|
+
benchmarks/results/addendum_rw2_noimpact/raw
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: langchain-cellprose
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: LangChain document loader for Excel workbooks as Cellprose text (formulas, every sheet, cell addresses)
|
|
5
|
+
Project-URL: Homepage, https://cellprose.com
|
|
6
|
+
Project-URL: Documentation, https://cellprose.com/guide/
|
|
7
|
+
Project-URL: Repository, https://github.com/cellprose/cellprose
|
|
8
|
+
Project-URL: Issues, https://github.com/cellprose/cellprose/issues
|
|
9
|
+
Author: Utkarsh Gaikwad
|
|
10
|
+
License-Expression: Apache-2.0
|
|
11
|
+
Requires-Python: >=3.10
|
|
12
|
+
Requires-Dist: cellprose>=0.3
|
|
13
|
+
Requires-Dist: langchain-core>=0.3
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
|
|
16
|
+
# langchain-cellprose
|
|
17
|
+
|
|
18
|
+
A LangChain document loader that reads `.xlsx` and `.xlsm` workbooks as [Cellprose](https://cellprose.com) text,
|
|
19
|
+
keeping formulas, cell addresses and every sheet in valid Markdown.
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pip install langchain-cellprose # not yet on PyPI: pip install -e integrations/langchain-cellprose from a clone
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
from langchain_cellprose import CellproseLoader
|
|
27
|
+
|
|
28
|
+
docs = CellproseLoader("book.xlsx").load() # one Document for the whole workbook
|
|
29
|
+
docs = CellproseLoader("book.xlsx", mode="sheet").load() # one per sheet, metadata["sheet"] set
|
|
30
|
+
docs = CellproseLoader("book.xlsx", max_rows=200).load() # options pass to cellprose.dumps
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
For question answering over a workbook, passing the whole workbook in one Document usually beats chunking: the
|
|
34
|
+
benchmark answers came from reading every sheet at once. Use `mode="sheet"` when the workbook is too large for
|
|
35
|
+
the context window.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# langchain-cellprose
|
|
2
|
+
|
|
3
|
+
A LangChain document loader that reads `.xlsx` and `.xlsm` workbooks as [Cellprose](https://cellprose.com) text,
|
|
4
|
+
keeping formulas, cell addresses and every sheet in valid Markdown.
|
|
5
|
+
|
|
6
|
+
```bash
|
|
7
|
+
pip install langchain-cellprose # not yet on PyPI: pip install -e integrations/langchain-cellprose from a clone
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
```python
|
|
11
|
+
from langchain_cellprose import CellproseLoader
|
|
12
|
+
|
|
13
|
+
docs = CellproseLoader("book.xlsx").load() # one Document for the whole workbook
|
|
14
|
+
docs = CellproseLoader("book.xlsx", mode="sheet").load() # one per sheet, metadata["sheet"] set
|
|
15
|
+
docs = CellproseLoader("book.xlsx", max_rows=200).load() # options pass to cellprose.dumps
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
For question answering over a workbook, passing the whole workbook in one Document usually beats chunking: the
|
|
19
|
+
benchmark answers came from reading every sheet at once. Use `mode="sheet"` when the workbook is too large for
|
|
20
|
+
the context window.
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "langchain-cellprose"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "LangChain document loader for Excel workbooks as Cellprose text (formulas, every sheet, cell addresses)"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
authors = [{ name = "Utkarsh Gaikwad" }]
|
|
11
|
+
license = "Apache-2.0"
|
|
12
|
+
requires-python = ">=3.10"
|
|
13
|
+
dependencies = ["cellprose>=0.3", "langchain-core>=0.3"]
|
|
14
|
+
|
|
15
|
+
[project.urls]
|
|
16
|
+
Homepage = "https://cellprose.com"
|
|
17
|
+
Documentation = "https://cellprose.com/guide/"
|
|
18
|
+
Repository = "https://github.com/cellprose/cellprose"
|
|
19
|
+
Issues = "https://github.com/cellprose/cellprose/issues"
|
|
20
|
+
|
|
21
|
+
[tool.hatch.build.targets.wheel]
|
|
22
|
+
packages = ["src/langchain_cellprose"]
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"""LangChain loader: Excel workbooks as Cellprose documents.
|
|
2
|
+
|
|
3
|
+
from langchain_cellprose import CellproseLoader
|
|
4
|
+
docs = CellproseLoader("book.xlsx").load() # one Document for the workbook
|
|
5
|
+
docs = CellproseLoader("book.xlsx", mode="sheet").load() # one per sheet, for splitting and retrieval
|
|
6
|
+
"""
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Any, Iterator, Literal
|
|
11
|
+
|
|
12
|
+
from langchain_core.document_loaders import BaseLoader
|
|
13
|
+
from langchain_core.documents import Document
|
|
14
|
+
|
|
15
|
+
__all__ = ["CellproseLoader", "split_sheets"]
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class CellproseLoader(BaseLoader):
|
|
19
|
+
"""Load a .xlsx/.xlsm (or an existing .sheet.md) as Cellprose text.
|
|
20
|
+
|
|
21
|
+
mode: "workbook" gives one Document; "sheet" gives one per sheet (plus one for defined names, if any), each
|
|
22
|
+
with the front matter so it reads alone. Extra keyword arguments go to `cellprose.dumps` (sheets, cell_range,
|
|
23
|
+
max_rows, recalc, legend). recalc defaults to "never": formulas without cached values print as `?`.
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
def __init__(self, file_path: str | Path, mode: Literal["workbook", "sheet"] = "workbook", **options: Any):
|
|
27
|
+
if mode not in ("workbook", "sheet"):
|
|
28
|
+
raise ValueError("mode must be 'workbook' or 'sheet'")
|
|
29
|
+
self.file_path = Path(file_path)
|
|
30
|
+
self.mode = mode
|
|
31
|
+
self.options = {"recalc": "never", **options}
|
|
32
|
+
|
|
33
|
+
def _text(self) -> str:
|
|
34
|
+
if self.file_path.name.endswith((".sheet.md", ".cellprose.md")):
|
|
35
|
+
return self.file_path.read_text(encoding="utf-8")
|
|
36
|
+
import cellprose
|
|
37
|
+
return cellprose.dumps(self.file_path, **self.options)
|
|
38
|
+
|
|
39
|
+
def lazy_load(self) -> Iterator[Document]:
|
|
40
|
+
text = self._text()
|
|
41
|
+
meta = {"source": str(self.file_path), "format": "cellprose"}
|
|
42
|
+
if self.mode == "workbook":
|
|
43
|
+
yield Document(page_content=text, metadata=meta)
|
|
44
|
+
return
|
|
45
|
+
for sheet, part in split_sheets(text):
|
|
46
|
+
yield Document(page_content=part, metadata={**meta, "sheet": sheet} if sheet is not None
|
|
47
|
+
else {**meta, "section": "names"})
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def split_sheets(text: str) -> list[tuple[str | None, str]]:
|
|
51
|
+
"""Split Cellprose text into (sheet name, text) parts, one per `# Sheet:` section. Each part keeps the front
|
|
52
|
+
matter so it reads on its own; the `# Names` section, when present, is the last part, with sheet name None."""
|
|
53
|
+
lines = text.split("\n")
|
|
54
|
+
start = 0
|
|
55
|
+
if lines and lines[0] == "---" and "---" in lines[1:]:
|
|
56
|
+
start = lines.index("---", 1) + 1
|
|
57
|
+
head = "\n".join(lines[:start]).strip("\n")
|
|
58
|
+
parts: list[tuple[str | None, list[str]]] = []
|
|
59
|
+
fenced = False
|
|
60
|
+
for line in lines[start:]:
|
|
61
|
+
if line.startswith("```"):
|
|
62
|
+
fenced = not fenced
|
|
63
|
+
if not fenced and line.startswith("# Sheet: "):
|
|
64
|
+
name = line[len("# Sheet: "):].strip()
|
|
65
|
+
if len(name) >= 2 and name[0] == name[-1] == '"':
|
|
66
|
+
name = name[1:-1]
|
|
67
|
+
parts.append((name, [line]))
|
|
68
|
+
elif not fenced and line == "# Names":
|
|
69
|
+
parts.append((None, [line]))
|
|
70
|
+
elif parts:
|
|
71
|
+
parts[-1][1].append(line)
|
|
72
|
+
if not parts:
|
|
73
|
+
return [(None, text)]
|
|
74
|
+
return [(n, (head + "\n\n" if head else "") + "\n".join(b).strip("\n") + "\n") for n, b in parts]
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import openpyxl
|
|
2
|
+
import pytest
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
@pytest.fixture
|
|
6
|
+
def book(tmp_path):
|
|
7
|
+
wb = openpyxl.Workbook()
|
|
8
|
+
ws = wb.active
|
|
9
|
+
ws.title = "Plan"
|
|
10
|
+
ws.append(["Item", "Units", "Price", "Total"])
|
|
11
|
+
ws.append(["Tea", 3, 2.5, "=B2*C2"])
|
|
12
|
+
ws.append(["Cake", 2, 4, "=B3*C3"])
|
|
13
|
+
s = wb.create_sheet("Q#2")
|
|
14
|
+
s.append(["Total", "=SUM(Plan!D2:D3)"])
|
|
15
|
+
from openpyxl.workbook.defined_name import DefinedName
|
|
16
|
+
wb.defined_names["Units"] = DefinedName("Units", attr_text="Plan!$B$2:$B$3")
|
|
17
|
+
p = tmp_path / "plan.xlsx"
|
|
18
|
+
wb.save(p)
|
|
19
|
+
return p
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
|
|
3
|
+
from langchain_cellprose import CellproseLoader, split_sheets
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def test_workbook_mode(book):
|
|
7
|
+
docs = CellproseLoader(book).load()
|
|
8
|
+
assert len(docs) == 1
|
|
9
|
+
assert docs[0].page_content.startswith("---\ncellprose: 0.3")
|
|
10
|
+
assert "D2:D3 = B2*C2" in docs[0].page_content
|
|
11
|
+
assert docs[0].metadata == {"source": str(book), "format": "cellprose"}
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def test_sheet_mode(book):
|
|
15
|
+
docs = CellproseLoader(book, mode="sheet").load()
|
|
16
|
+
assert [d.metadata.get("sheet") for d in docs] == ["Plan", "Q#2", None]
|
|
17
|
+
assert docs[-1].metadata["section"] == "names"
|
|
18
|
+
for d in docs:
|
|
19
|
+
assert d.page_content.startswith("---\ncellprose: 0.3")
|
|
20
|
+
assert "# Sheet: Plan" in docs[0].page_content and "Q#2" not in docs[0].page_content
|
|
21
|
+
assert docs[-1].page_content.split("---\n")[-1].lstrip().startswith("# Names")
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def test_reads_sheet_md(tmp_path, book):
|
|
25
|
+
import cellprose
|
|
26
|
+
p = tmp_path / "plan.sheet.md"
|
|
27
|
+
p.write_text(cellprose.dumps(book, recalc="never"), encoding="utf-8")
|
|
28
|
+
assert CellproseLoader(p).load()[0].page_content == p.read_text(encoding="utf-8")
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def test_split_ignores_fenced_headings():
|
|
32
|
+
text = "---\ncellprose: 0.3\n---\n\n# Sheet: A\n\n```formulas\n# Sheet: not a sheet\n```\n"
|
|
33
|
+
parts = split_sheets(text)
|
|
34
|
+
assert [n for n, _ in parts] == ["A"]
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def test_bad_mode():
|
|
38
|
+
with pytest.raises(ValueError):
|
|
39
|
+
CellproseLoader("x.xlsx", mode="rows")
|