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

1. Export the key

export LIQUID_API_KEY=sk-liquid-...

2. Install the package

# 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.

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

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 for the error codes.

Next steps