# LlamaIndex

LlamaIndex is a framework for applications that search your own data. Use its OpenAI-like embedding class with the exchange as the base URL to buy embeddings.

## Before you start

- A workspace with a balance and a `spend` key. See [API keys](https://inference.ai.exchange/docs/keys.md).
- A model name from `GET /v1/models?kind=embedding`. See [Embeddings](https://inference.ai.exchange/docs/embeddings.md).

## 1. Export the key

```bash
export LIQUID_API_KEY=sk-liquid-...
```

## 2. Install the package

```bash
# Python 3.9 or later
pip install llama-index-embeddings-openai-like
```

## 3. Embed text

Use `OpenAILikeEmbedding`. Do not use `OpenAIEmbedding`, because it accepts only the model names of OpenAI.

```python
import os
from llama_index.embeddings.openai_like import OpenAILikeEmbedding

embed_model = OpenAILikeEmbedding(
    model_name="text-embedding-3-small",
    api_base="https://router.inference.ai.exchange/v1",
    api_key=os.environ["LIQUID_API_KEY"],
    embed_batch_size=64,
)

vectors = embed_model.get_text_embedding_batch(["a cat on a bicycle", "a dog in a car"])
print(len(vectors), len(vectors[0]))

# To use it in every index and query engine:
from llama_index.core import Settings

Settings.embed_model = embed_model
```

The last lines set the model for every index and query engine.

## Notes

- Keep one model for one index. A change of model needs a new index. See [Embeddings](https://inference.ai.exchange/docs/embeddings.md).
- The exchange sets a cap on each request. It is in the `x-liquid-cap-usd` response header. To limit total spend, set a spending limit on the key. See [API keys](https://inference.ai.exchange/docs/keys.md).
- A request can have 2,048 texts at most. Keep `embed_batch_size` at or below that number.
- The exchange has no streaming form for embeddings. It sends each batch complete.

## Troubleshooting

| Symptom                                 | Cause and fix                                                                  |
| --------------------------------------- | ------------------------------------------------------------------------------ |
| `401`                                   | The key is wrong, revoked or expired. Export it again.                         |
| `404`                                   | Keep `/v1` at the end of `api_base`.                                           |
| `model_not_found`                       | The model is not on the book. Use an exact name from `GET /v1/models`.         |
| A request is refused for lack of funds. | The workspace balance cannot cover the cap. Add funds on the **Credits** page. |

See [Errors](https://inference.ai.exchange/docs/errors.md) for the error codes.

## Next steps

- [Embeddings](https://inference.ai.exchange/docs/embeddings.md): the request, the price and the limits.
- [Qdrant](https://inference.ai.exchange/docs/connect-qdrant.md): store the vectors in a collection.
- [LangChain](https://inference.ai.exchange/docs/connect-langchain.md): the same task with LangChain.
