LangChain
LangChain is a framework for applications that use language models. Create its OpenAI chat model with the exchange as the base URL. Use the same base URL and key for embeddings.
Before you start
- A workspace with a balance and a
spendkey. See API keys.
1. Export the key
export LIQUID_API_KEY=sk-liquid-...
2. Install the package
# Python 3.10 or later
pip install langchain-openai
3. Send a request
Create the OpenAI chat model with the exchange as its base URL. The X-Title header names the client on your receipts.
import os
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
base_url="https://router.inference.ai.exchange/v1",
api_key=os.environ["LIQUID_API_KEY"],
model="liquid.auto",
default_headers={"X-Title": "LangChain"},
)
To get the current model names, use GET /v1/models. See Market data.
Embeddings
Use OpenAIEmbeddings with the same base URL and key to buy vectors. See Embeddings.
import os
from langchain_openai import OpenAIEmbeddings
embeddings = OpenAIEmbeddings(
model="text-embedding-3-small",
base_url="https://router.inference.ai.exchange/v1",
api_key=os.environ["LIQUID_API_KEY"],
check_embedding_ctx_length=False,
)
vectors = embeddings.embed_documents(["a cat on a bicycle", "a dog in a car"])
Set check_embedding_ctx_length=False.
- If it is
True, the class splits each text into tokens and sends arrays of token numbers. The exchange does not support token arrays and refuses the request. - With
False, the class sends the text.
Pass this embeddings object to a vector store such as QdrantVectorStore or PGVector. See Qdrant and pgvector.
Notes
- 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. - To send purchasing rules, pass an
exchangeblock inextra_body. See Purchasing rules.
Troubleshooting
| Symptom | Cause and fix |
|---|---|
401 |
The key is wrong, revoked or expired. Export it again. |
404 |
Keep /v1 at the end of the base URL. |
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
- LiteLLM: a Python library with one interface to many APIs.
- OpenAI SDK: streams and purchasing rules in code.
- Purchasing rules: models, strategy and limits, and presets.