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

# LlamaIndex

> Python auto-instrumentation for LlamaIndex

Python auto-instrumentation library for LlamaIndex.

These traces are fully OpenTelemetry compatible and can be sent to an OpenTelemetry collector for viewing, such as [Arize Phoenix](https://github.com/Arize-ai/phoenix).

## Installation

```bash theme={null}
pip install openinference-instrumentation-llama-index
```

## Compatibility

| llama-index version | openinference-instrumentation-llama-index version |
| ------------------- | ------------------------------------------------- |
| 0.12.3+             | 4.0+                                              |
| 0.11.0+             | 3.0+                                              |
| 0.10.43+            | 2.0 - 3.0                                         |
| 0.10.0 - 0.10.43    | 1.0 - 0.2                                         |
| 0.9.14 - 0.10.0     | 0.1.3                                             |

## Quickstart

Install packages needed for this demonstration:

```bash theme={null}
python -m pip install --upgrade \
    openinference-instrumentation-llama-index \
    opentelemetry-sdk \
    opentelemetry-exporter-otlp \
    "opentelemetry-proto>=1.12.0" \
    arize-phoenix
```

### Start Phoenix server

Start the Phoenix app in the background as a collector. By default, it listens on `http://localhost:6006`:

```bash theme={null}
python -m phoenix.server.main serve
```

The Phoenix app does not send data over the internet. It only operates locally on your machine.

### Setup instrumentation

```python theme={null}
from openinference.instrumentation.llama_index import LlamaIndexInstrumentor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.sdk.trace.export import SimpleSpanProcessor

endpoint = "http://127.0.0.1:6006/v1/traces"
tracer_provider = trace_sdk.TracerProvider()
tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint)))

LlamaIndexInstrumentor().instrument(tracer_provider=tracer_provider)
```

### Download a text file

```python theme={null}
import tempfile
from urllib.request import urlretrieve
from llama_index.core import SimpleDirectoryReader

url = "https://raw.githubusercontent.com/Arize-ai/phoenix-assets/main/data/paul_graham/paul_graham_essay.txt"
with tempfile.NamedTemporaryFile() as tf:
    urlretrieve(url, tf.name)
    documents = SimpleDirectoryReader(input_files=[tf.name]).load_data()
```

### Configure OpenAI credentials

```python theme={null}
import os

os.environ["OPENAI_API_KEY"] = "<your openai key>"
```

### Query the indexed documents

```python theme={null}
from llama_index.core import VectorStoreIndex

query_engine = VectorStoreIndex.from_documents(documents).as_query_engine()
print(query_engine.query("What did the author do growing up?"))
```

Visit the Phoenix app at `http://localhost:6006` to see the traces.

## More Info

* [OpenInference and Phoenix documentation](https://docs.arize.com/phoenix)
* [How to customize spans to track sessions, metadata, etc.](https://github.com/Arize-ai/openinference/tree/main/python/openinference-instrumentation#customizing-spans)
* [How to account for private information and span payload customization](https://github.com/Arize-ai/openinference/tree/main/python/openinference-instrumentation#tracing-configuration)
* [PyPI package](https://pypi.org/project/openinference-instrumentation-llama-index/)
