agent-context-packager 0.1.0__py3-none-any.whl
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.
- agent_context_packager-0.1.0.dist-info/METADATA +137 -0
- agent_context_packager-0.1.0.dist-info/RECORD +27 -0
- agent_context_packager-0.1.0.dist-info/WHEEL +5 -0
- agent_context_packager-0.1.0.dist-info/entry_points.txt +2 -0
- agent_context_packager-0.1.0.dist-info/top_level.txt +1 -0
- agentpack/__init__.py +1 -0
- agentpack/__main__.py +4 -0
- agentpack/audit.py +65 -0
- agentpack/chunker.py +80 -0
- agentpack/cli.py +148 -0
- agentpack/eval/__init__.py +1 -0
- agentpack/eval/baselines.py +134 -0
- agentpack/eval/benchmarks.py +94 -0
- agentpack/eval/generation.py +152 -0
- agentpack/eval/metrics.py +47 -0
- agentpack/eval/runner.py +93 -0
- agentpack/models.py +30 -0
- agentpack/pack.py +157 -0
- agentpack/parsers/__init__.py +1 -0
- agentpack/parsers/base.py +9 -0
- agentpack/parsers/csv_parser.py +52 -0
- agentpack/parsers/markdown_parser.py +82 -0
- agentpack/parsers/pdf_parser.py +57 -0
- agentpack/parsers/text_parser.py +43 -0
- agentpack/retrieve.py +260 -0
- agentpack/scanner.py +107 -0
- agentpack/validate.py +67 -0
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: agent-context-packager
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A document-to-agent-context compiler.
|
|
5
|
+
Author-email: Your Name <your.email@example.com>
|
|
6
|
+
Requires-Python: >=3.9
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
Requires-Dist: typer>=0.9.0
|
|
9
|
+
Requires-Dist: pydantic>=2.0.0
|
|
10
|
+
Requires-Dist: pymupdf>=1.23.0
|
|
11
|
+
Requires-Dist: pandas>=2.0.0
|
|
12
|
+
Requires-Dist: tiktoken>=0.5.0
|
|
13
|
+
Requires-Dist: pyyaml>=6.0
|
|
14
|
+
Requires-Dist: pyinstaller>=6.0.0
|
|
15
|
+
Requires-Dist: tabulate>=0.9.0
|
|
16
|
+
Requires-Dist: fastembed>=0.2.7
|
|
17
|
+
Requires-Dist: numpy>=1.24.0
|
|
18
|
+
Requires-Dist: datasets>=2.0.0
|
|
19
|
+
Requires-Dist: requests>=2.0.0
|
|
20
|
+
Requires-Dist: pathspec>=0.11.0
|
|
21
|
+
Requires-Dist: detect-secrets>=1.4.0
|
|
22
|
+
Provides-Extra: docs
|
|
23
|
+
Requires-Dist: mkdocs-material>=9.0.0; extra == "docs"
|
|
24
|
+
Requires-Dist: mkdocstrings[python]>=0.24.0; extra == "docs"
|
|
25
|
+
Provides-Extra: test
|
|
26
|
+
Requires-Dist: pytest>=7.0.0; extra == "test"
|
|
27
|
+
Requires-Dist: pytest-mock>=3.10.0; extra == "test"
|
|
28
|
+
|
|
29
|
+
# AgentPack
|
|
30
|
+
|
|
31
|
+
**AgentPack** improves the context pipeline for document-grounded agents.
|
|
32
|
+
|
|
33
|
+
Instead of forcing AI agents to parse messy, disparate file formats (PDFs, CSVs, Markdown, text) at runtime, AgentPack is an offline **document-to-agent-context compiler**. It takes unstructured knowledge bases, turns them into clean semantic chunks with citations, retrieves the right evidence, and sends only high-signal context to the model.
|
|
34
|
+
|
|
35
|
+
## The Benchmark
|
|
36
|
+
**Given the same LLM, AgentPack provides better context than raw document stuffing or naive RAG.**
|
|
37
|
+
|
|
38
|
+
I benchmarked AgentPack against standard RAG baselines on 42 complex financial queries from [Patronus AI FinanceBench](https://github.com/patronus-ai/financebench). The results prove that AgentPack reduces context bloat, improves evidence retrieval, preserves citations, and helps the exact same LLM produce more grounded answers.
|
|
39
|
+
|
|
40
|
+
**Benchmark Highlights:**
|
|
41
|
+
* **161x Reduction in Token Cost:** Cut context token usage from 424k to 2.6k, saving ~$0.10 per query.
|
|
42
|
+
* **2x Context Relevance:** Vastly outperformed naive chunking in retrieving semantically complete financial tables.
|
|
43
|
+
* **"Lost in the Middle" Prevention:** Outperformed raw document stuffing in correctness by preventing the LLM from drowning in noise.
|
|
44
|
+
|
|
45
|
+
Read the full scientific methodology and results in [BENCHMARK.md](https://github.com/Vedant1202/agentpack/blob/main/BENCHMARK.md).
|
|
46
|
+
|
|
47
|
+
## Installation
|
|
48
|
+
|
|
49
|
+
You can install AgentPack via pip or npm.
|
|
50
|
+
|
|
51
|
+
**Option 1: Using pip (Python)**
|
|
52
|
+
```bash
|
|
53
|
+
pip install agent-context-packager
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
**Option 2: Using npm (Node.js/CLI binary)**
|
|
57
|
+
```bash
|
|
58
|
+
npm install -g agent-context-packager
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
**Option 3: From Source**
|
|
62
|
+
```bash
|
|
63
|
+
git clone https://github.com/Vedant1202/agentpack.git
|
|
64
|
+
cd agentpack
|
|
65
|
+
python3 -m venv venv
|
|
66
|
+
source venv/bin/activate
|
|
67
|
+
pip install -e .
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Quick Start
|
|
71
|
+
|
|
72
|
+
### 1. Scan for Secrets (Recommended)
|
|
73
|
+
Before compiling a pack, ensure you aren't accidentally leaking API keys or secrets into the LLM context window. AgentPack automatically installs Yelp's `detect-secrets`.
|
|
74
|
+
```bash
|
|
75
|
+
detect-secrets scan > .secrets.baseline
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### 2. Compile a Pack
|
|
79
|
+
Point AgentPack at any folder containing your documents (`.txt`, `.md`, `.csv`, `.pdf`).
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
agentpack pack ./my_docs --out ./agentpack-output
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
**Key Compilation Options:**
|
|
86
|
+
- `--include "*.md,*.txt"`: Only pack specific files or extensions.
|
|
87
|
+
- `--ignore "tests/,drafts/"`: Exclude specific directories or files.
|
|
88
|
+
- `--remove-empty-lines`: Compress text files to save LLM tokens.
|
|
89
|
+
- `--no-gitignore`: Ignore `.gitignore` rules and pack everything.
|
|
90
|
+
|
|
91
|
+
### 2. Retrieve
|
|
92
|
+
AgentPack comes with a built-in hybrid search engine (SQLite FTS5 + FastEmbed vector search) to test your chunks instantly.
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
agentpack retrieve ./agentpack-output "eligibility criteria" --top-k 5
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### 3. V1 Deterministic Eval
|
|
99
|
+
Benchmark AgentPack against naive chunking using our offline evaluation harness.
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
agentpack eval ./benchmarks/my_dataset
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Comprehensive CLI Documentation
|
|
106
|
+
|
|
107
|
+
AgentPack provides a rich CLI for auditing, validating, and testing your context packs (including Generative QA evaluations).
|
|
108
|
+
|
|
109
|
+
**[📖 Read the full CLI Reference](https://github.com/Vedant1202/agentpack/blob/main/docs/cli-reference.md)**
|
|
110
|
+
|
|
111
|
+
## Supported Parsers
|
|
112
|
+
- **TXT**: Paragraph-aware splitting.
|
|
113
|
+
- **Markdown**: Semantic heading-aware section path tracking.
|
|
114
|
+
- **CSV**: Uses Pandas & Tabulate to convert tabular data into Markdown tables.
|
|
115
|
+
- **PDF**: Accurate page-by-page PyMuPDF extraction.
|
|
116
|
+
|
|
117
|
+
## Architecture Overview
|
|
118
|
+
|
|
119
|
+
```mermaid
|
|
120
|
+
flowchart LR
|
|
121
|
+
Docs[Raw Docs] --> Parsers[Parsers]
|
|
122
|
+
Parsers --> Chunker[Chunker]
|
|
123
|
+
Chunker --> Pack[Context Pack]
|
|
124
|
+
Pack --> Agent[LLM Agent]
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
For a deep dive into how AgentPack parses, chunks, and indexes data, see [Architecture & Internals](https://github.com/Vedant1202/agentpack/blob/main/docs/architecture.md).
|
|
128
|
+
|
|
129
|
+
## Current Limitations & Roadmap
|
|
130
|
+
AgentPack is currently focused on text-based semantic extraction. The following features are on the roadmap but **not yet implemented**:
|
|
131
|
+
- **Image Understanding / Vision**: AgentPack does not currently run OCR or vision models on images embedded within PDFs or Markdown files. Images are currently ignored during the parsing phase.
|
|
132
|
+
- **Complex Table Structures**: While basic CSVs are supported, highly nested or merged-cell tables within PDFs are not perfectly reconstructed yet.
|
|
133
|
+
- **Web Crawling**: You currently need to provide local files. Direct URL scraping is planned.
|
|
134
|
+
- **Cloud Vector DB Integration**: Retrieval currently runs locally using SQLite FTS5 and FastEmbed. Connectors for Pinecone, Weaviate, or Qdrant are planned.
|
|
135
|
+
|
|
136
|
+
---
|
|
137
|
+
*Built with ❤️ for Agents.*
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
agentpack/__init__.py,sha256=loQ6gSLeLIQi-sqFzcAff-FimPgv3F1bli-IvFLw-HQ,34
|
|
2
|
+
agentpack/__main__.py,sha256=1GwAogovmCk7NUCapkbVWitA8WkrIFPmHOXK0cMhxKg,68
|
|
3
|
+
agentpack/audit.py,sha256=ic5aZaF1seYyQJED3ubhFNn6Wf9YmUssbGc18ODIi3w,2318
|
|
4
|
+
agentpack/chunker.py,sha256=ynqdp7b--RQxwg63LN9aSWV7rvv_6mesLY1lAASp6yM,2897
|
|
5
|
+
agentpack/cli.py,sha256=kPBEPN_NQ47Mi5KFyylro7c-vySkuKwB022qTs0KVxk,5996
|
|
6
|
+
agentpack/models.py,sha256=qg7o-77dKW995VXdyBkke9SwLDLL49vk2q6Xt6EVEXc,945
|
|
7
|
+
agentpack/pack.py,sha256=Yu7Ti03cv_iwwtMq7vt1zGpkAKBFE9QG9sHgMzJOmX0,5700
|
|
8
|
+
agentpack/retrieve.py,sha256=FYkOTJzG-jTgcifVCKfGm5Y6-yiAmBT2rjHXHbAc1wk,8941
|
|
9
|
+
agentpack/scanner.py,sha256=hzuAJPzTP9OjfQMLkVL_3-5SPyXd9K47WETCcSG1ink,3857
|
|
10
|
+
agentpack/validate.py,sha256=NB-zhHHNO8WNvV2E8bos7cLmjbQ0TDGyco-pdEiw0bc,2411
|
|
11
|
+
agentpack/eval/__init__.py,sha256=F3Mg_nrhyGVdckambYD5gtGh5rUxz1J3ALJ7We3jd6c,52
|
|
12
|
+
agentpack/eval/baselines.py,sha256=JVBkEBfBK0mzDIFkObtECeR39bSIwwaU_XSxspoM_vs,4768
|
|
13
|
+
agentpack/eval/benchmarks.py,sha256=_OfBnQ5QJJMqMDRc_UcXZVE0QyGPEajA8uxzE5cJlBI,3158
|
|
14
|
+
agentpack/eval/generation.py,sha256=v1bieidtafr943WfxXQxX70GLnNUTIIwTc6X806OgAs,6926
|
|
15
|
+
agentpack/eval/metrics.py,sha256=d1YBt7Mv2AhZswCZ4NOUw36KkeUDIL--X3h4qsP9fHU,1762
|
|
16
|
+
agentpack/eval/runner.py,sha256=7Hsnmw9NRJaFt7q9rbqztep4h37ZutlFIrRWMikJTk0,4366
|
|
17
|
+
agentpack/parsers/__init__.py,sha256=MO8UQ8t-gK4SyIS8FXglAHxeDJ4MeCix_l_OOX8HjkM,44
|
|
18
|
+
agentpack/parsers/base.py,sha256=xE71FFm_m5tgAjHp9HfQOQIsYSAf1vQXWqBmm4CEQZw,298
|
|
19
|
+
agentpack/parsers/csv_parser.py,sha256=q-99N2VkSCaWqTLi_fqbj--0_6Hk29KvzfMOE_bWCvc,1917
|
|
20
|
+
agentpack/parsers/markdown_parser.py,sha256=_KcMeMUTEuEUzUuTH-AG0WtXV2EcnGuHvg8PKbxE6yY,2984
|
|
21
|
+
agentpack/parsers/pdf_parser.py,sha256=4XR1Q21G3Fnq9NQ_c23QNUuKlQifu4K6Y2FkDn16PEg,1989
|
|
22
|
+
agentpack/parsers/text_parser.py,sha256=BOcOLYu_izB4Ry0FomXJ_zNihf8b8LKC6WBKgGw4AKg,1439
|
|
23
|
+
agent_context_packager-0.1.0.dist-info/METADATA,sha256=to06R0gMTdb5--FuigRkYojjtC3c-TyhT4uUavnl4nI,5567
|
|
24
|
+
agent_context_packager-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
25
|
+
agent_context_packager-0.1.0.dist-info/entry_points.txt,sha256=QEmFmah-r2yma_MKbIvKAYxe0A9pKQGnsruvL8_g9oc,48
|
|
26
|
+
agent_context_packager-0.1.0.dist-info/top_level.txt,sha256=kKYX3EZl6xeaJl0FJUcQz3OKeqPjHkJCJCjUVcvXYTU,10
|
|
27
|
+
agent_context_packager-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
agentpack
|
agentpack/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""AgentPack compiler package."""
|
agentpack/__main__.py
ADDED
agentpack/audit.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import yaml
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
def audit_pack(pack_dir: str) -> str:
|
|
5
|
+
"""Generates an audit report for an agentpack output directory."""
|
|
6
|
+
base_path = Path(pack_dir)
|
|
7
|
+
manifest_path = base_path / "manifest.yml"
|
|
8
|
+
|
|
9
|
+
if not manifest_path.exists():
|
|
10
|
+
return f"Error: Manifest not found at {manifest_path}"
|
|
11
|
+
|
|
12
|
+
try:
|
|
13
|
+
with open(manifest_path, "r", encoding="utf-8") as f:
|
|
14
|
+
manifest = yaml.safe_load(f)
|
|
15
|
+
except Exception as e:
|
|
16
|
+
return f"Error: Failed to parse manifest YAML: {e}"
|
|
17
|
+
|
|
18
|
+
sources = manifest.get("sources", [])
|
|
19
|
+
chunks = manifest.get("chunks", [])
|
|
20
|
+
tables = manifest.get("tables", [])
|
|
21
|
+
|
|
22
|
+
files_processed = len(sources)
|
|
23
|
+
total_chunks = len(chunks)
|
|
24
|
+
total_tables = len(tables)
|
|
25
|
+
total_tokens = sum(chunk.get("token_count", 0) for chunk in chunks)
|
|
26
|
+
|
|
27
|
+
max_chunk_size = 0
|
|
28
|
+
largest_chunk_id = None
|
|
29
|
+
for chunk in chunks:
|
|
30
|
+
if chunk.get("token_count", 0) > max_chunk_size:
|
|
31
|
+
max_chunk_size = chunk.get("token_count", 0)
|
|
32
|
+
largest_chunk_id = chunk.get("id")
|
|
33
|
+
|
|
34
|
+
warnings = []
|
|
35
|
+
for source in sources:
|
|
36
|
+
for warning in source.get("warnings", []):
|
|
37
|
+
warnings.append(f"Source {source.get('id')}: [{warning.get('type')}] {warning.get('message')}")
|
|
38
|
+
|
|
39
|
+
# Format report
|
|
40
|
+
report = [
|
|
41
|
+
f"# AgentPack Audit Report for '{manifest.get('pack', {}).get('name', 'Unknown')}'",
|
|
42
|
+
f"Generated at: {manifest.get('pack', {}).get('generated_at', 'Unknown')}\n",
|
|
43
|
+
"## Statistics",
|
|
44
|
+
f"- **Files Processed:** {files_processed}",
|
|
45
|
+
f"- **Total Chunks:** {total_chunks}",
|
|
46
|
+
f"- **Total Tables:** {total_tables}",
|
|
47
|
+
f"- **Total Tokens:** {total_tokens}",
|
|
48
|
+
f"- **Largest Chunk:** {max_chunk_size} tokens (ID: {largest_chunk_id})\n",
|
|
49
|
+
"## Extraction Warnings",
|
|
50
|
+
]
|
|
51
|
+
|
|
52
|
+
if warnings:
|
|
53
|
+
for warning in warnings:
|
|
54
|
+
report.append(f"- {warning}")
|
|
55
|
+
else:
|
|
56
|
+
report.append("- No extraction warnings.")
|
|
57
|
+
|
|
58
|
+
# Write report
|
|
59
|
+
report_text = "\n".join(report)
|
|
60
|
+
report_path = base_path / "reports" / "validation_report.md"
|
|
61
|
+
report_path.parent.mkdir(exist_ok=True)
|
|
62
|
+
with open(report_path, "w", encoding="utf-8") as f:
|
|
63
|
+
f.write(report_text)
|
|
64
|
+
|
|
65
|
+
return report_text
|
agentpack/chunker.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import tiktoken
|
|
2
|
+
from typing import List
|
|
3
|
+
from agentpack.models import SourceDocument
|
|
4
|
+
|
|
5
|
+
class Chunk:
|
|
6
|
+
def __init__(self, chunk_id: str, source_id: str, path: str, token_count: int, content: str, metadata: dict):
|
|
7
|
+
self.chunk_id = chunk_id
|
|
8
|
+
self.source_id = source_id
|
|
9
|
+
self.path = path
|
|
10
|
+
self.token_count = token_count
|
|
11
|
+
self.content = content
|
|
12
|
+
self.metadata = metadata
|
|
13
|
+
|
|
14
|
+
def chunk_document(doc: SourceDocument, max_tokens: int = 800, overlap_percent: float = 0.15) -> List[Chunk]:
|
|
15
|
+
encoder = tiktoken.get_encoding("cl100k_base")
|
|
16
|
+
chunks = []
|
|
17
|
+
|
|
18
|
+
current_blocks = []
|
|
19
|
+
current_tokens = 0
|
|
20
|
+
current_metadata = {"source_path": doc.path}
|
|
21
|
+
chunk_index = 0
|
|
22
|
+
|
|
23
|
+
overlap_tokens_target = int(max_tokens * overlap_percent)
|
|
24
|
+
|
|
25
|
+
def create_chunk():
|
|
26
|
+
nonlocal current_blocks, current_tokens, chunk_index, current_metadata
|
|
27
|
+
if not current_blocks:
|
|
28
|
+
return
|
|
29
|
+
content_str = "\n\n".join([b["text"] for b in current_blocks])
|
|
30
|
+
chunk_id = f"{doc.source_id}_chunk_{chunk_index:03d}"
|
|
31
|
+
chunks.append(Chunk(
|
|
32
|
+
chunk_id=chunk_id,
|
|
33
|
+
source_id=doc.source_id,
|
|
34
|
+
path=f"chunks/{chunk_id}.md",
|
|
35
|
+
token_count=current_tokens,
|
|
36
|
+
content=content_str,
|
|
37
|
+
metadata=current_metadata.copy()
|
|
38
|
+
))
|
|
39
|
+
chunk_index += 1
|
|
40
|
+
|
|
41
|
+
# Keep blocks for overlap
|
|
42
|
+
overlap_blocks = []
|
|
43
|
+
overlap_toks = 0
|
|
44
|
+
for b in reversed(current_blocks):
|
|
45
|
+
if overlap_toks + b["tokens"] > overlap_tokens_target:
|
|
46
|
+
break
|
|
47
|
+
# don't overlap tables to avoid duplicating massive tables
|
|
48
|
+
if b["type"] == "table" and overlap_toks > 0:
|
|
49
|
+
break
|
|
50
|
+
overlap_blocks.insert(0, b)
|
|
51
|
+
overlap_toks += b["tokens"]
|
|
52
|
+
|
|
53
|
+
current_blocks = overlap_blocks
|
|
54
|
+
current_tokens = overlap_toks
|
|
55
|
+
|
|
56
|
+
for block in doc.blocks:
|
|
57
|
+
if not block.text:
|
|
58
|
+
continue
|
|
59
|
+
|
|
60
|
+
block_tokens = len(encoder.encode(block.text))
|
|
61
|
+
|
|
62
|
+
# Update metadata
|
|
63
|
+
if block.section_path:
|
|
64
|
+
current_metadata["section"] = block.section_path[-1]
|
|
65
|
+
if block.page:
|
|
66
|
+
current_metadata["page"] = block.page
|
|
67
|
+
|
|
68
|
+
if block.row_range:
|
|
69
|
+
current_metadata["row_range"] = list(block.row_range)
|
|
70
|
+
|
|
71
|
+
# Very large blocks might exceed max_tokens, in a full implementation we'd split the block itself.
|
|
72
|
+
# For this MVP, we will just start a new chunk if adding this block pushes us over, unless we are empty.
|
|
73
|
+
if current_tokens + block_tokens > max_tokens and current_tokens > 0:
|
|
74
|
+
create_chunk()
|
|
75
|
+
|
|
76
|
+
current_blocks.append({"text": block.text, "tokens": block_tokens, "type": block.type})
|
|
77
|
+
current_tokens += block_tokens
|
|
78
|
+
|
|
79
|
+
create_chunk()
|
|
80
|
+
return chunks
|
agentpack/cli.py
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import typer
|
|
2
|
+
from agentpack.pack import write_pack
|
|
3
|
+
from agentpack.validate import validate_pack
|
|
4
|
+
from agentpack.audit import audit_pack
|
|
5
|
+
from agentpack.retrieve import search_pack
|
|
6
|
+
from agentpack.eval.runner import run_eval
|
|
7
|
+
|
|
8
|
+
app = typer.Typer(help="AgentPack CLI", no_args_is_help=True)
|
|
9
|
+
|
|
10
|
+
@app.command()
|
|
11
|
+
def pack(
|
|
12
|
+
input_dir: str,
|
|
13
|
+
out: str = typer.Option(..., help="Output directory"),
|
|
14
|
+
include: str = typer.Option(None, help="Include only files matching these glob patterns (comma-separated)"),
|
|
15
|
+
exclude: str = typer.Option(None, "-i", "--ignore", help="Additional patterns to exclude (comma-separated)"),
|
|
16
|
+
no_gitignore: bool = typer.Option(False, help="Don't use .gitignore rules for filtering files"),
|
|
17
|
+
no_default_patterns: bool = typer.Option(False, help="Don't apply built-in ignore patterns"),
|
|
18
|
+
include_hidden: bool = typer.Option(False, help="Include hidden directories"),
|
|
19
|
+
verbose: bool = typer.Option(False, help="Enable detailed debug logging"),
|
|
20
|
+
quiet: bool = typer.Option(False, help="Suppress all console output except errors"),
|
|
21
|
+
remove_empty_lines: bool = typer.Option(False, help="Remove blank lines from all text files")
|
|
22
|
+
):
|
|
23
|
+
"""Pack documents into an agent-friendly context pack."""
|
|
24
|
+
if not quiet:
|
|
25
|
+
typer.echo(f"Packing {input_dir} into {out}...")
|
|
26
|
+
|
|
27
|
+
include_patterns = include.split(",") if include else None
|
|
28
|
+
exclude_patterns = exclude.split(",") if exclude else None
|
|
29
|
+
|
|
30
|
+
write_pack(
|
|
31
|
+
input_dir=input_dir,
|
|
32
|
+
output_dir=out,
|
|
33
|
+
include_patterns=include_patterns,
|
|
34
|
+
exclude_patterns=exclude_patterns,
|
|
35
|
+
no_gitignore=no_gitignore,
|
|
36
|
+
no_default_patterns=no_default_patterns,
|
|
37
|
+
include_hidden=include_hidden,
|
|
38
|
+
verbose=verbose,
|
|
39
|
+
quiet=quiet,
|
|
40
|
+
remove_empty_lines=remove_empty_lines
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
if not quiet:
|
|
44
|
+
typer.echo("Done.")
|
|
45
|
+
|
|
46
|
+
@app.command()
|
|
47
|
+
def validate(pack_dir: str):
|
|
48
|
+
"""Validates the structural integrity of a context pack."""
|
|
49
|
+
typer.echo(f"Validating pack at {pack_dir}...")
|
|
50
|
+
errors = validate_pack(pack_dir)
|
|
51
|
+
if errors:
|
|
52
|
+
typer.secho("Validation failed with errors:", fg=typer.colors.RED)
|
|
53
|
+
for err in errors:
|
|
54
|
+
typer.echo(f"- {err}")
|
|
55
|
+
raise typer.Exit(code=1)
|
|
56
|
+
else:
|
|
57
|
+
typer.secho("Pack validation successful.", fg=typer.colors.GREEN)
|
|
58
|
+
|
|
59
|
+
@app.command()
|
|
60
|
+
def audit(pack_dir: str):
|
|
61
|
+
"""Generates an audit report for a context pack."""
|
|
62
|
+
typer.echo(f"Auditing pack at {pack_dir}...")
|
|
63
|
+
report = audit_pack(pack_dir)
|
|
64
|
+
if report.startswith("Error:"):
|
|
65
|
+
typer.secho(report, fg=typer.colors.RED)
|
|
66
|
+
raise typer.Exit(code=1)
|
|
67
|
+
else:
|
|
68
|
+
typer.echo(report)
|
|
69
|
+
typer.secho("\nAudit report generated.", fg=typer.colors.GREEN)
|
|
70
|
+
|
|
71
|
+
@app.command()
|
|
72
|
+
def retrieve(
|
|
73
|
+
pack_dir: str,
|
|
74
|
+
query: str,
|
|
75
|
+
top_k: int = typer.Option(5, help="Number of results to return"),
|
|
76
|
+
mode: str = typer.Option("hybrid", help="Search mode: hybrid, vector, or fts")
|
|
77
|
+
):
|
|
78
|
+
"""Retrieves top-k evidence chunks from a pack."""
|
|
79
|
+
typer.echo(f"Searching for '{query}' in {pack_dir} using {mode} mode...")
|
|
80
|
+
results = search_pack(pack_dir, query, top_k, mode=mode)
|
|
81
|
+
|
|
82
|
+
if not results:
|
|
83
|
+
typer.secho("No results found.", fg=typer.colors.YELLOW)
|
|
84
|
+
return
|
|
85
|
+
|
|
86
|
+
for i, res in enumerate(results):
|
|
87
|
+
source = res['citation'].get('source_path', res['source_id'])
|
|
88
|
+
section = res['citation'].get('section', '')
|
|
89
|
+
page = res['citation'].get('page', '')
|
|
90
|
+
|
|
91
|
+
cite_str = source
|
|
92
|
+
if page:
|
|
93
|
+
cite_str += f", page {page}"
|
|
94
|
+
if section:
|
|
95
|
+
cite_str += f", {section}"
|
|
96
|
+
|
|
97
|
+
typer.secho(f"\n{i+1}. {cite_str}", fg=typer.colors.CYAN, bold=True)
|
|
98
|
+
typer.echo(f" chunk: {res['path']}")
|
|
99
|
+
typer.echo(f" tokens: {res['token_count']}")
|
|
100
|
+
typer.echo(f" score: {res['score']:.2f}")
|
|
101
|
+
|
|
102
|
+
@app.command(name="eval")
|
|
103
|
+
def evaluate(benchmark_dir: str):
|
|
104
|
+
"""Runs a deterministic evaluation benchmark."""
|
|
105
|
+
typer.echo(f"Running evaluation on {benchmark_dir}...")
|
|
106
|
+
report = run_eval(benchmark_dir)
|
|
107
|
+
if report.startswith("Error:"):
|
|
108
|
+
typer.secho(report, fg=typer.colors.RED)
|
|
109
|
+
raise typer.Exit(code=1)
|
|
110
|
+
else:
|
|
111
|
+
typer.echo(report)
|
|
112
|
+
typer.secho("\nEvaluation complete.", fg=typer.colors.GREEN)
|
|
113
|
+
|
|
114
|
+
@app.command(name="gen-eval")
|
|
115
|
+
def gen_eval(benchmark_dir: str, gen_model: str = "gemini-1.5-flash", judge_model: str = "gemini-1.5-pro", limit: int = typer.Option(None, help="Limit number of queries for a smoke test")):
|
|
116
|
+
"""Evaluate Generative QA using AgentPack"""
|
|
117
|
+
from agentpack.eval.generation import run_generation_eval
|
|
118
|
+
|
|
119
|
+
typer.echo(f"Running generative evaluation on {benchmark_dir}...")
|
|
120
|
+
report = run_generation_eval(benchmark_dir, gen_model, judge_model, limit)
|
|
121
|
+
if report.startswith("Error"):
|
|
122
|
+
typer.secho(report, fg=typer.colors.RED)
|
|
123
|
+
else:
|
|
124
|
+
typer.echo(report)
|
|
125
|
+
typer.secho("Generation evaluation complete.", fg=typer.colors.GREEN)
|
|
126
|
+
|
|
127
|
+
@app.command(name="prep-benchmark")
|
|
128
|
+
def prep_benchmark(dataset: str = typer.Option("financebench", help="Dataset to slice"), sample_size: int = typer.Option(10, help="Number of documents to sample")):
|
|
129
|
+
"""Downloads and slices a public dataset into a benchmark format."""
|
|
130
|
+
from agentpack.eval.benchmarks import slice_financebench, slice_tatqa, slice_qasper
|
|
131
|
+
|
|
132
|
+
out_dir = f"benchmarks/{dataset}_sample"
|
|
133
|
+
typer.echo(f"Preparing {dataset} into {out_dir} with {sample_size} samples...")
|
|
134
|
+
|
|
135
|
+
if dataset == "financebench":
|
|
136
|
+
slice_financebench(out_dir, sample_size=sample_size)
|
|
137
|
+
elif dataset == "tatqa":
|
|
138
|
+
slice_tatqa(out_dir, sample_size=sample_size)
|
|
139
|
+
elif dataset == "qasper":
|
|
140
|
+
slice_qasper(out_dir, sample_size=sample_size)
|
|
141
|
+
else:
|
|
142
|
+
typer.secho(f"Unsupported dataset: {dataset}", fg=typer.colors.RED)
|
|
143
|
+
raise typer.Exit(code=1)
|
|
144
|
+
|
|
145
|
+
typer.secho("Preparation complete.", fg=typer.colors.GREEN)
|
|
146
|
+
|
|
147
|
+
if __name__ == "__main__":
|
|
148
|
+
app()
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Evaluation module for benchmarking AgentPack."""
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import sqlite3
|
|
2
|
+
import os
|
|
3
|
+
import tiktoken
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from typing import List, Dict
|
|
6
|
+
|
|
7
|
+
def _extract_text(file_path: Path) -> str:
|
|
8
|
+
if file_path.suffix.lower() == ".pdf":
|
|
9
|
+
try:
|
|
10
|
+
import fitz
|
|
11
|
+
doc = fitz.open(file_path)
|
|
12
|
+
text = []
|
|
13
|
+
for page in doc:
|
|
14
|
+
text.append(page.get_text("text"))
|
|
15
|
+
return "\n".join(text)
|
|
16
|
+
except Exception:
|
|
17
|
+
return ""
|
|
18
|
+
else:
|
|
19
|
+
try:
|
|
20
|
+
with open(file_path, "r", encoding="utf-8", errors="replace") as f:
|
|
21
|
+
return f.read()
|
|
22
|
+
except Exception:
|
|
23
|
+
return ""
|
|
24
|
+
|
|
25
|
+
_raw_conn_cache = None
|
|
26
|
+
_naive_conn_cache = None
|
|
27
|
+
_last_corpus_dir = None
|
|
28
|
+
|
|
29
|
+
def _get_raw_conn(corpus_dir: Path):
|
|
30
|
+
global _raw_conn_cache, _last_corpus_dir
|
|
31
|
+
if _raw_conn_cache is not None and _last_corpus_dir == corpus_dir:
|
|
32
|
+
return _raw_conn_cache
|
|
33
|
+
|
|
34
|
+
conn = sqlite3.connect(":memory:")
|
|
35
|
+
cur = conn.cursor()
|
|
36
|
+
cur.execute('CREATE VIRTUAL TABLE files_fts USING fts5(path UNINDEXED, content)')
|
|
37
|
+
|
|
38
|
+
from tqdm import tqdm
|
|
39
|
+
all_files = []
|
|
40
|
+
for root, _, files in os.walk(corpus_dir):
|
|
41
|
+
for file in files:
|
|
42
|
+
p = Path(root) / file
|
|
43
|
+
if p.suffix in [".txt", ".md", ".csv", ".pdf"]:
|
|
44
|
+
all_files.append(p)
|
|
45
|
+
|
|
46
|
+
for p in tqdm(all_files, desc="Building Raw File Baseline Index"):
|
|
47
|
+
content = _extract_text(p)
|
|
48
|
+
cur.execute("INSERT INTO files_fts (path, content) VALUES (?, ?)", (p.name, content))
|
|
49
|
+
|
|
50
|
+
_raw_conn_cache = conn
|
|
51
|
+
_last_corpus_dir = corpus_dir
|
|
52
|
+
return conn
|
|
53
|
+
|
|
54
|
+
def _get_naive_conn(corpus_dir: Path, chunk_size: int = 4000):
|
|
55
|
+
global _naive_conn_cache
|
|
56
|
+
if _naive_conn_cache is not None and _last_corpus_dir == corpus_dir:
|
|
57
|
+
return _naive_conn_cache
|
|
58
|
+
|
|
59
|
+
conn = sqlite3.connect(":memory:")
|
|
60
|
+
cur = conn.cursor()
|
|
61
|
+
cur.execute('CREATE VIRTUAL TABLE chunks_fts USING fts5(path UNINDEXED, chunk_id UNINDEXED, content)')
|
|
62
|
+
|
|
63
|
+
chunk_id = 0
|
|
64
|
+
|
|
65
|
+
from tqdm import tqdm
|
|
66
|
+
all_files = []
|
|
67
|
+
for root, _, files in os.walk(corpus_dir):
|
|
68
|
+
for file in files:
|
|
69
|
+
p = Path(root) / file
|
|
70
|
+
if p.suffix in [".txt", ".md", ".csv", ".pdf"]:
|
|
71
|
+
all_files.append(p)
|
|
72
|
+
|
|
73
|
+
for p in tqdm(all_files, desc="Building Naive Chunk Baseline Index"):
|
|
74
|
+
content = _extract_text(p)
|
|
75
|
+
for i in range(0, len(content), chunk_size):
|
|
76
|
+
chunk_text = content[i:i+chunk_size]
|
|
77
|
+
cur.execute("INSERT INTO chunks_fts (path, chunk_id, content) VALUES (?, ?, ?)", (p.name, chunk_id, chunk_text))
|
|
78
|
+
chunk_id += 1
|
|
79
|
+
|
|
80
|
+
_naive_conn_cache = conn
|
|
81
|
+
return conn
|
|
82
|
+
|
|
83
|
+
def raw_file_search(corpus_dir: Path, query: str, top_k: int = 5) -> List[Dict]:
|
|
84
|
+
conn = _get_raw_conn(corpus_dir)
|
|
85
|
+
cur = conn.cursor()
|
|
86
|
+
|
|
87
|
+
clean_query = " OR ".join([f'"{w}"' for w in query.replace('"', '').split() if w])
|
|
88
|
+
if not clean_query: return []
|
|
89
|
+
|
|
90
|
+
try:
|
|
91
|
+
cur.execute('SELECT path, rank, content FROM files_fts WHERE files_fts MATCH ? ORDER BY rank LIMIT ?', (clean_query, top_k))
|
|
92
|
+
except sqlite3.OperationalError:
|
|
93
|
+
try:
|
|
94
|
+
cur.execute('SELECT path, rank, content FROM files_fts WHERE files_fts MATCH ? ORDER BY rank LIMIT ?', (query, top_k))
|
|
95
|
+
except sqlite3.OperationalError:
|
|
96
|
+
return []
|
|
97
|
+
|
|
98
|
+
encoder = tiktoken.get_encoding("cl100k_base")
|
|
99
|
+
results = []
|
|
100
|
+
for path, rank, content in cur.fetchall():
|
|
101
|
+
results.append({
|
|
102
|
+
"path": path,
|
|
103
|
+
"token_count": len(encoder.encode(content)),
|
|
104
|
+
"score": rank,
|
|
105
|
+
"content": content
|
|
106
|
+
})
|
|
107
|
+
return results
|
|
108
|
+
|
|
109
|
+
def naive_chunk_search(corpus_dir: Path, query: str, top_k: int = 5, chunk_size: int = 4000) -> List[Dict]:
|
|
110
|
+
conn = _get_naive_conn(corpus_dir, chunk_size)
|
|
111
|
+
cur = conn.cursor()
|
|
112
|
+
|
|
113
|
+
clean_query = " OR ".join([f'"{w}"' for w in query.replace('"', '').split() if w])
|
|
114
|
+
if not clean_query: return []
|
|
115
|
+
|
|
116
|
+
try:
|
|
117
|
+
cur.execute('SELECT path, chunk_id, rank, content FROM chunks_fts WHERE chunks_fts MATCH ? ORDER BY rank LIMIT ?', (clean_query, top_k))
|
|
118
|
+
except sqlite3.OperationalError:
|
|
119
|
+
try:
|
|
120
|
+
cur.execute('SELECT path, chunk_id, rank, content FROM chunks_fts WHERE chunks_fts MATCH ? ORDER BY rank LIMIT ?', (query, top_k))
|
|
121
|
+
except sqlite3.OperationalError:
|
|
122
|
+
return []
|
|
123
|
+
|
|
124
|
+
encoder = tiktoken.get_encoding("cl100k_base")
|
|
125
|
+
results = []
|
|
126
|
+
for path, chunk_id, rank, content in cur.fetchall():
|
|
127
|
+
results.append({
|
|
128
|
+
"chunk_id": chunk_id,
|
|
129
|
+
"path": path,
|
|
130
|
+
"token_count": len(encoder.encode(content)),
|
|
131
|
+
"score": rank,
|
|
132
|
+
"content": content
|
|
133
|
+
})
|
|
134
|
+
return results
|