> For the complete documentation index, see [llms.txt](https://docs.tensorx.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.tensorx.ai/developer-sdks/llamaindex.md).

# LlamaIndex

[LlamaIndex](https://www.llamaindex.ai/) is a data framework for LLM applications. Connect it to TensorX using the OpenAI-compatible configuration.

## Prerequisites

* Python 3.8+
* LlamaIndex installed
* TensorX API key from [app.tensorx.ai](https://app.tensorx.ai)

## Installation

```bash
pip install llama-index llama-index-llms-openai-like llama-index-embeddings-openai-like
```

## Configuration

LlamaIndex's `OpenAI` and `OpenAIEmbedding` classes check the model name against OpenAI's own model list and reject TensorX model IDs. For an OpenAI-compatible endpoint, use the `OpenAILike` and `OpenAILikeEmbedding` classes instead. They accept any model name and send the same requests. Pass `is_chat_model=True` so LlamaIndex uses the chat completions endpoint.

### Basic Usage

```python
from llama_index.llms.openai_like import OpenAILike

# Create LLM instance. Use any chat model from /v1/models.
llm = OpenAILike(
    model="z-ai/glm-5.3",
    api_key="your-tensorx-api-key",
    api_base="https://api.tensorx.ai/v1",
    is_chat_model=True
)

# Simple completion
response = llm.complete("Explain quantum computing in simple terms")
print(response)
```

### Chat Interface

```python
from llama_index.core.llms import ChatMessage
from llama_index.llms.openai_like import OpenAILike

llm = OpenAILike(
    model="z-ai/glm-5.3",
    api_key="your-tensorx-api-key",
    api_base="https://api.tensorx.ai/v1",
    is_chat_model=True
)

messages = [
    ChatMessage(role="system", content="You are a helpful coding assistant"),
    ChatMessage(role="user", content="Write a Python function to calculate fibonacci numbers")
]

response = llm.chat(messages)
print(response)
```

### Streaming Responses

```python
from llama_index.llms.openai_like import OpenAILike

llm = OpenAILike(
    model="z-ai/glm-5.3-flash",
    api_key="your-tensorx-api-key",
    api_base="https://api.tensorx.ai/v1",
    is_chat_model=True
)

# Streaming completion
response = llm.stream_complete("Write a short story about AI")
for chunk in response:
    print(chunk.delta, end="")
```

## RAG Applications

Build retrieval-augmented generation (RAG) systems with TensorX:

```python
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
from llama_index.llms.openai_like import OpenAILike
from llama_index.embeddings.openai_like import OpenAILikeEmbedding

# Configure the chat model. Use any chat model from /v1/models.
Settings.llm = OpenAILike(
    model="z-ai/glm-5.3",
    api_key="your-tensorx-api-key",
    api_base="https://api.tensorx.ai/v1",
    is_chat_model=True
)

# Configure TensorX embeddings. `dimensions` is optional and accepts
# 32 to 4096. Omit it for the full 4096-dimensional vector.
Settings.embed_model = OpenAILikeEmbedding(
    model_name="qwen/qwen3-embedding-8b",
    api_key="your-tensorx-api-key",
    api_base="https://api.tensorx.ai/v1",
    dimensions=1024,
    embed_batch_size=10
)

# Load documents
documents = SimpleDirectoryReader("./data").load_data()

# Create index
index = VectorStoreIndex.from_documents(documents)

# Query
query_engine = index.as_query_engine()
response = query_engine.query("What are the key points in these documents?")
print(response)
```

## Agent Applications

Create agents with TensorX:

```python
import asyncio
from llama_index.core.agent import ReActAgent
from llama_index.core.tools import FunctionTool
from llama_index.llms.openai_like import OpenAILike

# Define tools
def multiply(a: int, b: int) -> int:
    """Multiply two numbers."""
    return a * b

def add(a: int, b: int) -> int:
    """Add two numbers."""
    return a + b

multiply_tool = FunctionTool.from_defaults(fn=multiply)
add_tool = FunctionTool.from_defaults(fn=add)

# Create agent
llm = OpenAILike(
    model="z-ai/glm-5.3-flash",
    api_key="your-tensorx-api-key",
    api_base="https://api.tensorx.ai/v1",
    is_chat_model=True
)

agent = ReActAgent(
    tools=[multiply_tool, add_tool],
    llm=llm
)

async def main():
    response = await agent.run("What is 20 plus 30, then multiplied by 2?")
    print(response)

asyncio.run(main())
```

## Configuration with Settings

For global configuration across your application:

```python
from llama_index.core import Settings
from llama_index.llms.openai_like import OpenAILike

# Set global LLM
Settings.llm = OpenAILike(
    model="z-ai/glm-5.3",
    api_key="your-tensorx-api-key",
    api_base="https://api.tensorx.ai/v1",
    is_chat_model=True,
    temperature=0.7
)
```

## Model selection

Call `GET /v1/models` with your TensorX API key and use one of the returned IDs. If the integration depends on a capability or token limit, query `GET /v1/model/info` before selecting the model. See [Models](/api-reference/models.md) for examples.

***

## Troubleshooting

### Connection errors

Ensure `api_base` ends with `/v1`:

```python
api_base="https://api.tensorx.ai/v1"  # Correct
api_base="https://api.tensorx.ai"     # Wrong
```

### Token limits

Adjust `max_tokens` based on your needs:

```python
llm = OpenAILike(
    model="z-ai/glm-5.3",
    api_key="your-tensorx-api-key",
    api_base="https://api.tensorx.ai/v1",
    is_chat_model=True,
    max_tokens=1024
)
```

## Resources

* [LlamaIndex Documentation](https://docs.llamaindex.ai/)
* [LlamaIndex GitHub](https://github.com/run-llama/llama_index)
* [TensorX API Reference](/api-reference/overview.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.tensorx.ai/developer-sdks/llamaindex.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
