Mastra 是一个用于 AI 智能体和工作流的 TypeScript 框架。除了内置服务商之外,智能体的 model 还可以是任意 OpenAI 兼容端点,写成一个包含 id 和 url 的对象即可。BroadNet Inference 在 https://inference.broadnet.ai/v1 上提供标准的 chat completions,所以可以这样接入,只是模型 ID 有一个坑。BroadRouter 是 BroadNet 自己的网站;本文依据 Mastra 的模型文档和 @mastra/core 1.83.0。
安装
npm install @mastra/core zod
export BROADNET_API_KEY='your-broadnet-key'
连接一个智能体
import { Agent } from "@mastra/core/agent";
const broadnet = (model: string) => ({
id: `broadnet/${model}`, // Mastra strips the first segment
url: "https://inference.broadnet.ai/v1",
apiKey: process.env.BROADNET_API_KEY,
});
export const assistant = new Agent({
id: "assistant",
name: "Assistant",
instructions: "You are a concise assistant.",
model: broadnet("broadnet/glm-5.3"), // sends broadnet/glm-5.3
});
const res = await assistant.generate("Give me three names for a Go linting tool.");
console.log(res.text);
为什么要写两遍前缀
Mastra 的文档说,写成 provider/model 的 id 会向远端发送不带服务商的模型名,而 gateway/provider/model 会在名称中保留服务商。BroadNet 的 ID 本身就含有斜杠,所以需要用更长的形式。我们的测试结果:
Mastra id |
BroadNet 收到的模型 ID |
|---|---|
broadnet/glm-5.3 |
glm-5.3(错误) |
broadnet/broadnet/glm-5.3 |
broadnet/glm-5.3(正确) |
上面的辅助函数会自动帮你加上多出来的那一段。
工具
import { createTool } from "@mastra/core/tools";
import { z } from "zod";
const weather = createTool({
id: "get_weather",
description: "Get the weather for a city",
inputSchema: z.object({ city: z.string() }),
execute: async (input) => ({ city: input.city, tempC: 21 }),
});
const agent = new Agent({
id: "weather",
name: "Weather",
instructions: "Use the weather tool to answer.",
tools: { weather },
model: broadnet("broadnet/glm-5.3"),
});
带工具的智能体请使用 broadnet/glm-5.3 或 broadnet/qwen3.8-27b。BroadNet 的文档只为这两个模型列出了工具和结构化输出。
流式输出
const stream = await assistant.stream("Explain vector search in two sentences.");
for await (const chunk of stream.textStream) process.stdout.write(chunk);
需要注意的设置
url是 base URL,以/v1结尾。Mastra 会拼上/chat/completions。- 不要加
api: "responses"。 Mastra 的文档说,这会切换到/v1/responses。BroadNet 没有列出 Responses API。 - 输出上限。 DeepSeek V4.1 Flash、GLM 5.3 和 Qwen3.8 27B 会先思考再回答,思考内容计入输出上限。如果你限制了输出 token,至少留出 2,000。
选哪个模型
| 智能体 | 模型 |
|---|---|
| 工具、结构化输出、工作流 | broadnet/glm-5.3 |
| 工具加图片输入或阿拉伯语文本 | broadnet/qwen3.8-27b |
| 固定价格套餐上的纯文本步骤 | broadnet/deepseek-v4.1-flash |
所有模型的价格。如果不用 Mastra、直接用 Vercel AI SDK,请看 AI SDK 指南。
我们是怎么测试的
2026-11-15,我们在 Node 24 下安装了 @mastra/core 1.83.0,把 generate、stream 和一个工具智能体指向一个本地 mock 服务器运行。该服务器模拟 BroadNet 的 chat completions 格式,并返回工具调用。我们查看了记录下来的请求:每次调用都发到了 /v1/chat/completions,并带有来自 apiKey 的 Bearer 密钥。上表中的前缀规则就是这样发现的。我们没有 BroadNet 密钥,因此没有测试真实的模型输出。
