# Connect LlamaIndex

https://modelportal.ai/docs/integrations/llamaindex

> Python framework for retrieval-augmented apps, connected through OpenAILike.

## Use cases

Good for Python projects doing retrieval-augmented generation or document Q&A with LlamaIndex. OpenAILike speaks the OpenAI-compatible API and does not validate model names, so the platform's model ID works as is.

## Tips

Set is_chat_model=True so requests go to the chat endpoint. For long documents, also set context_window to the model's context length so LlamaIndex splits text to fit.

## If it cannot connect

1. Use `https://modelportal.ai/v1` as the API host, without `/chat/completions`.
2. The key starts with `mp-` (older keys: `key_`) and is not paused or revoked; `invalid_api_key` means the key cannot be used.
3. The model ID matches the [catalog](/catalog) and is in the key's allowed models.
4. `insufficient_balance` means the balance is too low; see [common errors](/docs/errors?lang=en) for more.

## Setup

1. Install the dependency: pip install llama-index-llms-openai-like.
2. Create OpenAILike with api_base set to the API host below, your own key as api_key, and is_chat_model=True.
3. Assign it to Settings.llm so indexes and query engines use it.

## Settings

- API host：`https://modelportal.ai/v1`
- API key：`YOUR_API_KEY`
- Model：`MODEL_ID`

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

llm = OpenAILike(
    model="MODEL_ID",
    api_base="https://modelportal.ai/v1",
    api_key="YOUR_API_KEY",
    is_chat_model=True,
)
print(llm.complete("你好"))
```

## FAQ

### Can I connect embedding models the same way?

Use OpenAILikeEmbedding from llama-index-embeddings-openai-like with the same api_base https://modelportal.ai/v1, and pick a model with the embedding capability.

