> ## Documentation Index
> Fetch the complete documentation index at: https://docs.flexorch.com/llms.txt
> Use this file to discover all available pages before exploring further.

# LlamaIndex Integration

> Use FlexOrchReader to load FlexOrch dataset chunks into a LlamaIndex VectorStoreIndex.

<Note>
  This guide covers the **platform SDK** (`flexorch-sdk`). For local file auditing with the zero-dependency OSS library, see [flexorch-audit LlamaIndex](/open-source/llamaindex).
</Note>

## Install

```bash theme={null}
pip install flexorch-sdk llama-index-core
```

***

## FlexOrchReader

`FlexOrchReader` fetches all chunks from a built FlexOrch dataset with automatic pagination and returns `RAGDocument` objects that are duck-type compatible with LlamaIndex's `Document` (`.text` property, `.metadata` dict).

```python theme={null}
from flexorch_sdk import FlexOrch, FlexOrchReader

client = FlexOrch()  # reads FLEXORCH_API_KEY from env

reader = FlexOrchReader(client)

documents = reader.load_data(
    dataset_id=89,
    min_quality="B",           # exclude C and D grade chunks
    pii_masked_only=True,      # only load PII-safe chunks
    page_size=100,             # chunks fetched per request
)

for doc in documents:
    print(doc.text[:100])
    print(doc.metadata["quality_grade"])
```

### Parameters

| Parameter         | Type   | Default  | Description                                          |
| ----------------- | ------ | -------- | ---------------------------------------------------- |
| `dataset_id`      | `int`  | required | ID of the built FlexOrch dataset                     |
| `min_quality`     | `str`  | `"B"`    | Minimum grade to include: `"A"`, `"B"`, `"C"`, `"D"` |
| `pii_masked_only` | `bool` | `False`  | When `True`, only return chunks where PII was masked |
| `page_size`       | `int`  | `100`    | Chunks fetched per API request (max 100)             |

<Note>
  Chunk access via `GET /v1/datasets/{id}/chunks` requires a **Pro or Enterprise** plan.
</Note>

***

## VectorStoreIndex

```python theme={null}
from flexorch_sdk import FlexOrch, FlexOrchReader
from llama_index.core import VectorStoreIndex

client = FlexOrch()
reader = FlexOrchReader(client)

documents = reader.load_data(dataset_id=89, min_quality="B")
index = VectorStoreIndex.from_documents(documents)

query_engine = index.as_query_engine()
response = query_engine.query("What are the payment terms in the contracts?")
print(response)
```

***

## Persist and reload

```python theme={null}
from llama_index.core import StorageContext, load_index_from_storage

# Persist
index.storage_context.persist("./storage")

# Reload (no re-fetch needed)
storage_context = StorageContext.from_defaults(persist_dir="./storage")
index = load_index_from_storage(storage_context)
```

***

## Streaming query

```python theme={null}
query_engine = index.as_query_engine(streaming=True)
response = query_engine.query("Summarise the Q1 invoices.")

for token in response.response_gen:
    print(token, end="", flush=True)
```

***

## Filter by quality at query time

```python theme={null}
from llama_index.core.vector_stores import MetadataFilter, MetadataFilters

filters = MetadataFilters(filters=[
    MetadataFilter(key="quality_grade", value="A"),
])

query_engine = index.as_query_engine(filters=filters)
response = query_engine.query("High-quality contract clauses only")
```

***

## Multi-dataset index

Load chunks from several datasets into a single index:

```python theme={null}
all_documents = []
for dataset_id in [89, 90, 91]:
    docs = reader.load_data(dataset_id=dataset_id, min_quality="B")
    all_documents.extend(docs)

index = VectorStoreIndex.from_documents(all_documents)
```

***

## RAGDocument fields

`FlexOrchReader.load_data()` returns a list of `RAGDocument` objects:

| Field          | Type   | Description                             |
| -------------- | ------ | --------------------------------------- |
| `text`         | `str`  | Chunk text (LlamaIndex compatible)      |
| `page_content` | `str`  | Alias for `text` (LangChain compatible) |
| `metadata`     | `dict` | Chunk metadata                          |

Metadata keys:

| Key             | Description                 |
| --------------- | --------------------------- |
| `chunk_id`      | Unique chunk identifier     |
| `chunk_index`   | Position within the dataset |
| `dataset_id`    | Source dataset              |
| `quality_grade` | `A` \| `B` \| `C` \| `D`    |
| `quality_score` | 0.0 – 1.0                   |
| `doc_type`      | Document classification     |
| `language`      | ISO 639-1 language code     |
| `pii_masked`    | `true` if PII was masked    |
| `token_count`   | Estimated token count       |

<CardGroup cols={2}>
  <Card title="RAG pipeline overview" icon="route" href="/guides/rag-pipeline">
    End-to-end guide: export vs. managed search
  </Card>

  <Card title="LangChain integration" icon="link" href="/guides/langchain">
    FlexOrchRetriever for chain-based retrieval
  </Card>
</CardGroup>
