在 LangChain 中使用 BigModel.io

用 ChatOpenAI(base_url 为 https://bigmodel.io/v1)或 ChatAnthropic(https://bigmodel.io)把 LangChain 接入 BigModel.io,含流式用量、工具绑定和 reasoning_content 限制。

本页目录
  1. 安装
  2. ChatOpenAI
  3. ChatAnthropic
  4. 流式输出
  5. 工具调用
  6. reasoning_content 的限制
  7. 验证配置
  8. 常见问题排查
  9. 相关文档

LangChain 通过 OpenAI 或 Anthropic 集成调用 BigModel.io。任何 Key 都可以用 ChatOpenAI;只有 Key 所在分组提供 /v1/messages 时才能用 ChatAnthropic。

类 Base URL 参数 值
ChatOpenAI(Python) base_url https://bigmodel.io/v1
ChatOpenAI(JS) configuration.baseURL https://bigmodel.io/v1
ChatAnthropic(Python) anthropic_api_url https://bigmodel.io(不带 /v1)
ChatAnthropic(JS) anthropicApiUrl https://bigmodel.io(不带 /v1)

模型 ID 从模型目录复制,能否调用取决于 Key 所在分组。

bash
export BIGMODEL_API_KEY="sk-..."

#安装

Python
pip install langchain-openai langchain-anthropic
TypeScript
npm install @langchain/openai @langchain/anthropic @langchain/core zod

#ChatOpenAI

Python
import os
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="glm-5.3",
    base_url="https://bigmodel.io/v1",
    api_key=os.environ["BIGMODEL_API_KEY"],
    stream_usage=True,
)
print(llm.invoke("列出上线前要检查的五件事。").content)
TypeScript
import { ChatOpenAI } from "@langchain/openai";

const llm = new ChatOpenAI({
  model: "glm-5.3",
  apiKey: process.env.BIGMODEL_API_KEY,
  configuration: { baseURL: "https://bigmodel.io/v1" },
  streamUsage: true,
});
const result = await llm.invoke("列出上线前要检查的五件事。");
console.log(result.content);

Python 版在设置自定义 Base URL 后默认关闭 stream_usage,流式响应不会带 usage_metadata,需要像上面一样传 stream_usage=True。

传 reasoning=... 或 use_responses_api=True 会让 ChatOpenAI 改走 /v1/responses,GLM、DeepSeek 分组不提供该接口(返回 404)。这类 Key 不要设置这两个参数。

#ChatAnthropic

Python
import os
from langchain_anthropic import ChatAnthropic

llm = ChatAnthropic(
    model="glm-5.3",
    anthropic_api_url="https://bigmodel.io",
    api_key=os.environ["BIGMODEL_API_KEY"],
    max_tokens=1024,
)
print(llm.invoke("列出上线前要检查的五件事。").content)
TypeScript
import { ChatAnthropic } from "@langchain/anthropic";

const llm = new ChatAnthropic({
  model: "glm-5.3",
  anthropicApiUrl: "https://bigmodel.io",
  apiKey: process.env.BIGMODEL_API_KEY,
  maxTokens: 1024,
});
const result = await llm.invoke("列出上线前要检查的五件事。");
console.log(result.content);

ChatAnthropic 也会读取环境变量 ANTHROPIC_API_URL / ANTHROPIC_BASE_URL,显式传参优先。

#流式输出

Python
for chunk in llm.stream("写一首关于延迟的俳句。"):
    print(chunk.content, end="", flush=True)
TypeScript
const stream = await llm.stream("写一首关于延迟的俳句。");
for await (const chunk of stream) {
  process.stdout.write(typeof chunk.content === "string" ? chunk.content : "");
}

#工具调用

bind_tools 发送标准的 OpenAI 或 Anthropic 工具定义,网关原样转发。是否支持工具取决于模型。

Python
from langchain_core.tools import tool

@tool
def get_weather(city: str) -> str:
    """查询城市当前天气。"""
    return f"{city}晴"

llm_with_tools = llm.bind_tools([get_weather])
msg = llm_with_tools.invoke("杭州现在天气怎么样?")
print(msg.tool_calls)
TypeScript
import { tool } from "@langchain/core/tools";
import { z } from "zod";

const getWeather = tool(async ({ city }) => `${city}晴`, {
  name: "get_weather",
  description: "查询城市当前天气。",
  schema: z.object({ city: z.string() }),
});

const msg = await llm.bindTools([getWeather]).invoke("杭州现在天气怎么样?");
console.log(msg.tool_calls);

#reasoning_content 的限制

GLM、DeepSeek 分组上的思考模型会在 reasoning_content 字段返回推理过程。网关会透传这个字段,但 ChatOpenAI 不会把它暴露出来,你只能拿到最终回答。需要推理文本时,改用 OpenAI SDK 直接读取 reasoning_content。见推理与思考。

#验证配置

Python
msg = llm.invoke("ping")
print(msg.content, msg.usage_metadata)
TypeScript
const msg = await llm.invoke("ping");
console.log(msg.content, msg.usage_metadata);

能返回回答和 token 数就说明 Base URL 和 Key 正确,请求也应出现在用量页面。

#常见问题排查

ChatOpenAI 所有请求都 404。 Base URL 少了 /v1。JS 中必须写在 configuration 里,不能写成顶层 baseURL。

ChatAnthropic 所有请求都 404。 URL 末尾带了 /v1,改成 https://bigmodel.io。

404 ... only supports /v1/chat/completions。 要么是用只支持 Chat Completions 的分组调用了 ChatAnthropic,要么是 reasoning / use_responses_api 让 ChatOpenAI 改走了 Responses API。改用普通的 ChatOpenAI。

流式响应没有 token 用量。 设置 stream_usage=True(Python)或 streamUsage: true(JS)。

401 INVALID_API_KEY。 Key 没传到客户端,环境里的 OPENAI_API_KEY 或 ANTHROPIC_API_KEY 可能被优先使用。显式传 api_key。

503 No available accounts: no available accounts supporting model: ...。 模型 ID 写错或不在 Key 的分组里。从模型目录复制 ID。

403 INSUFFICIENT_BALANCE 或 429。 到充值页面充值;429 见限额与速率。

#相关文档

更新于 · 以 Markdown 查看