26382a7ac6
CI / Clippy (push) Failing after 15m13s
CI / Test (ubuntu-latest) (push) Failing after 16m1s
CI / Test (macos-latest) (push) Has been cancelled
CI / Test (windows-latest) (push) Has been cancelled
CI / Build (no embeddings / no ORT) (push) Has been cancelled
CI / Format (push) Has been cancelled
CI / Cookbook (Node) (push) Has been cancelled
CI / Pi Extension (Node) (push) Has been cancelled
CI / Rust SDK (lean-ctx-client) (push) Has been cancelled
CI / Embed SDK (lean-ctx-sdk) (push) Has been cancelled
CI / Python SDK (leanctx) (push) Has been cancelled
CI / Hermes Plugin (Python) (push) Has been cancelled
CI / SDK Conformance Matrix (push) Has been cancelled
CI / Coverage (push) Has been cancelled
CI / cargo-deny (push) Has been cancelled
CI / Adversarial Safety (push) Has been cancelled
CI / Benchmarks (push) Has been cancelled
CI / Output-Quality Gate (eval A/B) (push) Has been cancelled
CI / Documentation (push) Has been cancelled
CI / CI Green (push) Has been cancelled
JetBrains Plugin / Actionlint (push) Has been cancelled
CodeQL / Analyze (actions) (push) Has been cancelled
CodeQL / Analyze (javascript-typescript) (push) Has been cancelled
CodeQL / Analyze (rust) (push) Has been cancelled
JetBrains Plugin / Validation (push) Has been cancelled
JetBrains Plugin / Build (push) Has been cancelled
JetBrains Plugin / Test (push) Has been cancelled
Security Check / Security Scan (push) Has been cancelled
48 lines
1.7 KiB
Python
48 lines
1.7 KiB
Python
"""LlamaIndex integration for lean-ctx."""
|
|
|
|
from typing import Optional
|
|
|
|
from lean_ctx.client import LeanCtxClient
|
|
|
|
try:
|
|
from llama_index.core.node_parser import NodeParser
|
|
from llama_index.core.schema import BaseNode, TextNode, Document
|
|
|
|
class LeanCtxNodeParser(NodeParser):
|
|
"""LlamaIndex node parser using lean-ctx compression modes."""
|
|
|
|
client: LeanCtxClient = None
|
|
mode: str = "map"
|
|
|
|
def __init__(self, project_root: Optional[str] = None, mode: str = "map", **kwargs):
|
|
super().__init__(**kwargs)
|
|
self.client = LeanCtxClient(project_root=project_root)
|
|
self.mode = mode
|
|
|
|
def _parse_nodes(self, nodes: list[BaseNode], **kwargs) -> list[BaseNode]:
|
|
result_nodes = []
|
|
for node in nodes:
|
|
if isinstance(node, Document) and hasattr(node, "metadata"):
|
|
file_path = node.metadata.get("file_path", "")
|
|
if file_path:
|
|
compressed = self.client.read(file_path, mode=self.mode)
|
|
result_nodes.append(
|
|
TextNode(
|
|
text=compressed,
|
|
metadata={**node.metadata, "compression_mode": self.mode},
|
|
)
|
|
)
|
|
continue
|
|
result_nodes.append(node)
|
|
return result_nodes
|
|
|
|
except ImportError:
|
|
|
|
class LeanCtxNodeParser:
|
|
"""Stub: install llama-index-core for full integration."""
|
|
|
|
def __init__(self, **kwargs):
|
|
raise ImportError(
|
|
"llama-index-core is required: pip install llama-index-core"
|
|
)
|