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
spendkey. See API keys. - A model name from
GET /v1/models?kind=embedding. See Embeddings.
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
- Keep one model for one index. A change of model needs a new index. See Embeddings.
- The exchange sets a cap on each request. It is in the
x-liquid-cap-usdresponse header. To limit total spend, set a spending limit on the key. See API keys. - A request can have 2,048 texts at most. Keep
embed_batch_sizeat 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 for the error codes.
Next steps
- Embeddings: the request, the price and the limits.
- Qdrant: store the vectors in a collection.
- LangChain: the same task with LangChain.