Skip to content

Using 树新在线 in LangChain

树新在线 provides an OpenAI-compatible API gateway that supports integration with LangChain. Users can access its services by specifying the Base URL and API Key.

Python Usage

python
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="gpt-5.6-luna",
    base_url="https://api.treenew.online/v1",
    api_key="sk-xxx"  # Create from the token page at https://api.treenew.online
)
print(llm.invoke("你好").content)

Parameter names vary across LangChain versions. Newer versions use base_url and api_key, while older versions may require openai_api_base and openai_api_key.

Using Environment Variables

After setting the following environment variables, the code can connect without modification:

OPENAI_BASE_URL=https://api.treenew.online/v1
OPENAI_API_KEY=sk-xxx

Streaming

python
for chunk in llm.stream("介绍 LangChain"):
    print(chunk.content, end="")

Key Notes

This site does not provide an embeddings endpoint, so OpenAIEmbeddings cannot be used. Any chains that rely on vector retrieval (RAG, VectorStore) require a separate service that provides embeddings; pure LLM calls are unaffected.

Verification

bash
curl https://api.treenew.online/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-xxx" \
  -d '{"model":"gpt-5.6-luna","messages":[{"role":"user","content":"Hello"}]}'

See also

Home · Console