Skip to content
View as Markdown

LangChain

LangChain's ChatOpenAI talks to any OpenAI-compatible API, so it works with FreeTheAI by setting the base URL, your key, and an fta/ model ID.

Python ​

bash
pip install langchain-openai
python
import os
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="fta/zai/glm-5.3",
    base_url="https://api.freetheai.org/v1",
    api_key=os.environ["FTAI_KEY"],
    max_retries=2,
)

reply = llm.invoke("Give me three names for a bakery.")
print(reply.content)

for chunk in llm.stream("Write a two-line poem about coffee."):
    print(chunk.content, end="", flush=True)

Tools ​

python
from langchain_core.tools import tool

@tool
def get_weather(city: str) -> str:
    """Get the current weather for a city."""
    return f"It is 21°C and sunny in {city}."

llm_with_tools = llm.bind_tools([get_weather])
reply = llm_with_tools.invoke("What's the weather in Madrid?")
print(reply.tool_calls)

Use a model that can call tools, such as fta/zai/glm-5.3 or fta/kimi/k3. The fta/dog/ and fta/lion/ models cannot.

JavaScript and TypeScript ​

bash
npm install @langchain/openai @langchain/core
ts
import { ChatOpenAI } from '@langchain/openai'

const llm = new ChatOpenAI({
  model: 'fta/zai/glm-5.3',
  apiKey: process.env.FTAI_KEY,
  configuration: { baseURL: 'https://api.freetheai.org/v1' },
})

const reply = await llm.invoke('Give me three names for a bakery.')
console.log(reply.content)

Notes ​

  • Free accounts run one request at a time. Chains that fan out in parallel (batch, RunnableParallel, agents that call tools in parallel) will see 429 on the extra requests; set max_concurrency=1 in the run config, or call one at a time.
  • LangChain may print a warning that it does not know the model's context size. That is expected for fta/ models and harmless.
  • Errors are raised as the OpenAI SDK's exceptions; see Errors.

FreeTheAI documentation. Paid models and plans are coming soon; the free tier has daily limits.