# LlamaIndex

LlamaIndex serves as a data framework that connects LLMs with private data sources. The library handles ingestion from various formats including APIs, PDFs, documents, and SQL databases through flexible connectors. Data gets organized into indices and graph structures optimized for LLM consumption, with a query interface that enriches model inputs with relevant context.

This guide covers capturing telemetry from LlamaIndex applications using InteractiveAI.

### Prerequisites

* InteractiveAI account with API credentials
* LLM provider credentials (OpenAI, Ollama, or other supported provider)

***

### Installation

```bash
pip install interactiveai openinference-instrumentation-llama-index llama-index-llms-openai llama-index
```

***

### Configuration

Set your API credentials as environment variables:

```python
import os

# InteractiveAI credentials
os.environ["INTERACTIVEAI_PUBLIC_KEY"] = "pk-..."
os.environ["INTERACTIVEAI_SECRET_KEY"] = "sk-..."

# Model provider credentials
os.environ["OPENAI_API_KEY"] = "sk-proj-..."
```

Initialize the client and confirm connectivity:

```python
from interactiveai import Interactive

interactiveai = Interactive(
    public_key=os.environ["INTERACTIVEAI_PUBLIC_KEY"],
    secret_key=os.environ["INTERACTIVEAI_SECRET_KEY"],
)

if interactiveai.auth_check():
    print("Connection established")
else:
    print("Authentication failed - verify credentials")
```

***

### Enabling Trace Capture

LlamaIndex has a dedicated OpenInference instrumentor that captures framework operations automatically:

```python
from openinference.instrumentation.llama_index import LlamaIndexInstrumentor

LlamaIndexInstrumentor().instrument()
```

Once activated, LLM calls, retrieval operations, and query processing generate spans that route to InteractiveAI.

***

### Running a LlamaIndex Application

Here's a working example with a simple LLM query:

```python
from llama_index.llms.openai import OpenAI
from interactiveai import Interactive

interactiveai = Interactive(
    public_key=os.environ["INTERACTIVEAI_PUBLIC_KEY"],
    secret_key=os.environ["INTERACTIVEAI_SECRET_KEY"],
)

llm = OpenAI(model="gpt-4o")

with interactiveai.start_as_current_span(name="llamaindex-query"):
    response = llm.complete("Explain what a vector database is in two sentences.")
    print(response)

interactiveai.flush()
```

***

### Enriching Traces with Context

Combine OpenInference instrumentation with the InteractiveAI SDK to attach identifiers and metadata:

<pre class="language-python"><code class="lang-python">from interactiveai import Interactive
from llama_index.llms.openai import OpenAI

interactiveai = Interactive(
    public_key=os.environ["INTERACTIVEAI_PUBLIC_KEY"],
    secret_key=os.environ["INTERACTIVEAI_SECRET_KEY"],
)

with interactiveai.start_as_current_span(name="llamaindex-rag-query") as span:
    
<strong>    interactiveai.update_current_trace(
</strong><strong>        user_id="user_11",
</strong><strong>        session_id="session_llamaindex",
</strong><strong>        tags=["llamaindex", "retrieval"],
</strong><strong>        metadata={"experiment": "baseline", "environment": "production"}
</strong><strong>    )
</strong>    
    llm = OpenAI(model="gpt-4o")
    
    query = "What are the benefits of retrieval-augmented generation?"
    response = llm.complete(query)
    
    interactiveai.update_current_trace(
        input=query,
        output=str(response)
    )

interactiveai.flush()
</code></pre>

***

### Trace Visibility

The InteractiveAI dashboard displays:

* LLM calls with prompts and completions
* Document retrieval operations and results
* Query processing steps and timing
* Token consumption and latency metrics
