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

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.

Pass this embeddings object to a vector store such as QdrantVectorStore or PGVector. See Qdrant and pgvector.

Notes

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