# 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 `spend` key. See [API keys](https://inference.ai.exchange/docs/keys.md).

## 1. Export the key

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

## 2. Install the package

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

```python
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](https://inference.ai.exchange/docs/market.md).

## Embeddings

Use `OpenAIEmbeddings` with the same base URL and key to buy vectors. See [Embeddings](https://inference.ai.exchange/docs/embeddings.md).

```python
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](https://inference.ai.exchange/docs/connect-qdrant.md) and [pgvector](https://inference.ai.exchange/docs/connect-pgvector.md).

## Notes

- 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).
- To send purchasing rules, pass an `exchange` block in `extra_body`. See [Purchasing rules](https://inference.ai.exchange/docs/rules.md).

## 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](https://inference.ai.exchange/docs/errors.md) for the error codes.

## Next steps

- [LiteLLM](https://inference.ai.exchange/docs/connect-litellm.md): a Python library with one interface to many APIs.
- [OpenAI SDK](https://inference.ai.exchange/docs/connect-openai-sdk.md): streams and purchasing rules in code.
- [Purchasing rules](https://inference.ai.exchange/docs/rules.md): models, strategy and limits, and presets.
