进阶 mastra.ai 2026-10-12 10:15:56 · 52 阅读
第1章 模型配置
模型供应商
Mastra 提供统一接口,让你在多个供应商之间切换 LLM,通过单一 API 即可访问来自 214 家供应商的 7828 个模型。
特性(直接链接到“特性”)
- 一个 API 覆盖任意模型:无需安装和管理额外的供应商依赖即可接入任何模型。
- 紧跟最新 AI:无论新模型来自哪家供应商,发布后你都能立即使用。借助 Mastra 的供应商无关(provider-agnostic)接口,避免被单一厂商锁定。
- 灵活组合不同模型:针对不同任务选用不同模型。例如,用 GPT-5-mini 处理大上下文任务,再切换到 Claude Opus 4.6 执行推理任务。
- 模型回退机制:当某个供应商服务中断时,Mastra 能在应用层自动切换到其他供应商,相比 API 网关,延迟更低。
基础用法(直接链接到“基础用法”)
无论你使用的是 OpenAI、Anthropic、Google,还是 OpenRouter 等网关,只需将模型写为 “provider/model-name”,Mastra 会处理其余细节。
Mastra 会读取相应的环境变量(例如 ANTHROPIC_API_KEY),并将请求路由到对应供应商。如果缺少 API key,系统会给出明确的运行时错误,指出具体需要设置哪个变量。
OpenAI
Anthropic
Google Gemini
xAI
OpenRouter
src/mastra/agents/my-agent.ts
import { Agent } from "@mastra/core/agent";
const agent = new Agent({
id: "my-agent",
name: "My Agent",
instructions: "You are a helpful assistant",
model: "openai/gpt-5.6-sol"
})
src/mastra/agents/my-agent.ts
import { Agent } from "@mastra/core/agent";
const agent = new Agent({
id: "my-agent",
name: "My Agent",
instructions: "You are a helpful assistant",
model: "anthropic/claude-sonnet-4-6"
})
src/mastra/agents/my-agent.ts
import { Agent } from "@mastra/core/agent";
const agent = new Agent({
id: "my-agent",
name: "My Agent",
instructions: "You are a helpful assistant",
model: "google/gemini-2.5-flash"
})
src/mastra/agents/my-agent.ts
import { Agent } from "@mastra/core/agent";
const agent = new Agent({
id: "my-agent",
name: "My Agent",
instructions: "You are a helpful assistant",
model: "xai/grok-4.3"
})
src/mastra/agents/my-agent.ts
import { Agent } from "@mastra/core/agent";
const agent = new Agent({
id: "my-agent",
name: "My Agent",
instructions: "You are a helpful assistant",
model: "openrouter/anthropic/claude-haiku-4.5"
})
Model directoryDirect link to Model directory 通过左侧导航浏览可用模型目录,或在下方直接探索。 GatewaysOpenRouterNetlifyVercel+ 4 moreProvidersOpenAIAnthropicGoogle+ 204 more 你也可以在编辑器中发现模型。Mastra 为 model 字段提供完整的自动补全——只需开始输入,IDE 就会展示可用选项。 或者,在 Studio UI 中浏览并测试模型。 note在开发环境中,我们每小时自动刷新一次本地模型列表,确保 TypeScript 自动补全和 Studio 始终与最新模型保持同步。如需禁用,请设置 MASTRA_AUTO_REFRESH_PROVIDERS=false。在生产环境中,默认禁用自动刷新。 Mix and match modelsDirect link to Mix and match models 有些模型速度更快但能力较弱,而有些模型则提供更大的上下文窗口或更强的推理能力。可以使用同一提供商的不同模型,或跨提供商组合搭配,以适配不同任务。 src/mastra/agents/reasoning-agent.tsimport { Agent } from "@mastra/core/agent";
// Use a cost-effective model for document processing
const documentProcessor = new Agent({
id: "document-processor",
name: "Document Processor",
instructions: "Extract and summarize key information from documents",
model: "openai/gpt-5.6-sol"
})
// Use a powerful reasoning model for complex analysis
const reasoningAgent = new Agent({
id: "reasoning-agent",
name: "Reasoning Agent",
instructions: "Analyze data and provide strategic recommendations",
model: "anthropic/claude-opus-4-7"
})
Dynamic model selectionDirect link to Dynamic model selection 由于模型只是字符串,你可以基于请求上下文、变量或其他任何逻辑动态选择模型。 src/mastra/agents/dynamic-assistant-agent.tsconst agent = new Agent({
id: "dynamic-assistant",
name: "Dynamic Assistant",
model: ({ requestContext }) => {
const provider = requestContext.get("provider-id");
const model = requestContext.get("model-id");
return `${provider}/${model}`;
},
});
这让一些强大的模式成为可能: A/B 测试——在生产环境中对比不同模型的表现。 用户自选模型——让用户在应用里选择自己喜欢的模型。 多租户应用——每个客户可以自带 API key 和模型偏好。 Provider 专属选项 不同的模型提供商有自己的配置项。比如 OpenAI 可以调整 reasoningEffort,Anthropic 可以调整 cacheControl。Mastra 允许你在 agent 级别或单条消息级别设置这些 providerOptions。 src/mastra/agents/planner-agent.ts // Agent 级别(对所有后续消息生效) const planner = new Agent({ id: "planner", name: "Planner", instructions: { role: "system", content: "You are a helpful assistant.", providerOptions: { openai: { reasoningEffort: "low" } } }, model: "openai/gpt-5.6-sol", }); const lowEffort = await planner.generate("Plan a simple 3 item dinner menu"); // 消息级别(只对这条消息生效) const highEffort = await planner.generate([ { role: "user", content: "Plan a simple 3 item dinner menu for a celiac", providerOptions: { openai: { reasoningEffort: "high" } } } ]); 自定义请求头 如果需要设置自定义请求头,比如组织 ID 或其他 provider 专属字段,可以使用如下写法。 src/mastra/agents/custom-agent.ts const agent = new Agent({ id: "custom-agent", name: "Custom Agent", model: { id: "openai/gpt-5.6-sol", apiKey: process.env.OPENAI_API_KEY, headers: { "OpenAI-Organization": "org-abc123" } } }); 注意:不同提供商的配置方式有差异。自定义请求头的详情请参阅左侧导航中的各提供商页面。 模型回退 只依赖单一模型会给应用带来单点故障风险。模型回退机制可以在模型和提供商之间自动切换:当主模型不可用时,请求会自动重试到下一个配置的回退模型,直到某个模型成功响应。 src/mastra/agents/resilient-assistant-agent.ts import { Agent } from '@mastra/core/agent'; const agent = new Agent({ id: 'resilient-assistant', name: 'Resilient Assistant', instructions: 'You are a helpful assistant.', model: [ { model: "openai/gpt-5.6-sol", maxRetries: 3, }, { model: "anthropic/claude-sonnet-4-6", maxRetries: 2, }, { model: "google/gemini-2.5-pro", maxRetries: 2, }, ], }); Mastra 会优先尝试你的主模型。若遇到 500 错误、速率限制或超时,系统会自动切换到第一个备选模型;若该模型也失败,则继续尝试下一个。每个模型都有独立的重试次数,用尽后才换下一个。 用户对此过程完全无感——返回的响应格式保持一致,只是由不同的模型生成。在切换模型的同时,系统会保留错误上下文,确保错误传播清晰且兼容流式输出。 各模型设置 每个备选模型条目都可配置专属的 modelSettings、providerOptions 和 headers——当链中的模型需要不同的 temperature 或厂商特定参数以产生可比输出时,此功能十分实用。 src/mastra/agents/tuned-resilient-agent.ts import { Agent } from '@mastra/core/agent'; const agent = new Agent({ id: 'tuned-resilient', name: 'Tuned Resilient Agent', instructions: 'You are a helpful assistant.', model: [ { model: 'google/gemini-2.5-flash', maxRetries: 2, modelSettings: { temperature: 0.3 }, providerOptions: { google: { thinkingConfig: { thinkingBudget: 0 } } }, }, { model: 'openai/gpt-5-mini', maxRetries: 2, modelSettings: { temperature: 0.7 }, providerOptions: { openai: { reasoningEffort: 'low' } }, }, ], }); 优先级: modelSettings 与 providerOptions:每个回退条目(per-fallback entry)的配置会覆盖调用时(call-time)传入的选项,而调用时选项又覆盖 agent 的默认 defaultOptions。modelSettings 按 key 进行浅层合并。providerOptions 递归地执行深层合并,因此嵌套的 provider 配置(例如 google.thinkingConfig)能够跨层保留同级 key。 headers:调用时传入的 modelSettings.headers 优先于每个回退条目的 headers,而后者优先于从 model-router 的 models 中提取的 headers。运行时生成的 headers(如 tracing、auth、tenancy)有意设置为高于 model 级别的 headers。 每个字段还可以接受一个以 requestContext 为参数的函数,动态模型(dynamic models)的解析也遵循同样的机制。 使用 Mastra 运行本地模型 Mastra 也支持像 gpt-oss、Qwen3、DeepSeek 等许多其他你可以在自己硬件上运行的本地模型。运行本地模型的应用程序需要提供一个 OpenAI 兼容的 API 服务器供 Mastra 连接。我们推荐使用 LMStudio(参见“运行 LMStudio 服务器”)。 对于自定义的 OpenAI 兼容端点,id 是 Mastra 通过 model router 发送的路由形式。 如果远程服务行为类似直接的 provider,且期望使用裸模型名(例如 llama3.2),请使用 provider/model 格式。 如果远程服务行为类似模型网关,且上游模型命名空间包含 provider,例如 mastra/google/gemini-2.5-flash 或 openrouter/google/gemini-2.5-flash,请使用 gateway/provider/model 格式。 对于 url,请务必使用 Mastra model 配置所指向的 OpenAI 兼容端点的 base URL,而不是单独的 chat 端点。 src/mastra/agents/my-agent.ts import { Agent } from "@mastra/core/agent"; const agent = new Agent({ id: "my-agent", name: "My Agent", instructions: "You are a helpful assistant", model: { id: "custom/my-qwen3-model", url: "http://your-custom-openai-compatible-endpoint.com/v1" } }) 如果远程服务行为类似模型网关,请在 id 中包含网关前缀: src/mastra/agents/my-agent.ts import { Agent } from "@mastra/core/agent"; const agent = new Agent({ id: "my-agent", name: "My Agent", instructions: "You are a helpful assistant", model: { id: "mastra/google/gemini-2.5-flash", url: "http://your-custom-openai-compatible-endpoint.com/v1" } }) 示例:LMStudio 启动 LMStudio 服务器后,本地服务地址为 http://localhost:1234,提供 /v1/models、/v1/chat/completions 等端点,因此 url 设为 http://localhost:1234/v1。id 可以使用 lmstudio/${modelId} 格式,其中 modelId 就是 LMStudio 界面中显示的模型 ID。 src/mastra/agents/my-agent.tsimport { Agent } from "@mastra/core/agent";
const agent = new Agent({
id: "my-agent",
name: "My Agent",
instructions: "You are a helpful assistant",
model: {
id: "lmstudio/qwen/qwen3-30b-a3b-2507",
url: "http://localhost:1234/v1"
}
})
选择 OpenAI Responses API 自定义 url 端点默认使用 OpenAI Chat Completions API。如果你的端点提供 OpenAI Responses API(/v1/responses)——比如需要在某些强制要求该 API 的网关上把函数工具与推理模型结合使用——可以设置 api: "responses"。 src/mastra/agents/my-agent.tsimport { Agent } from "@mastra/core/agent";
const agent = new Agent({
id: "my-agent",
name: "My Agent",
instructions: "You are a helpful assistant",
model: {
id: "custom/my-model",
url: "http://your-custom-openai-compatible-endpoint.com/v1",
api: "responses"
}
})
如果不设置 api,默认值为 "chat",现有配置不受影响。使用 Responses API 时,provider 选项从 openai 命名空间读取;而 Chat Completions 路径则读取 openai-compatible 命名空间。 在 Mastra 中使用 AI SDK Mastra 支持直接使用 AI SDK 的 provider 模块。 src/mastra/agents/my-agent.tsimport { groq } from '@ai-sdk/groq';
import { Agent } from "@mastra/core/agent";
const agent = new Agent({
id: "my-agent",
name: "My Agent",
model: groq('gemma2-9b-it')
})
你可以在任何接受 “provider/model” 字符串的地方使用 AI SDK 模型(例如 groq('gemma2-9b-it')),包括模型路由器的后备方案和评分器中。 所有提供商 所有提供商直接链接 下方列出的每个提供商都支持在模型路由器中使用 provider/model 格式。请参阅对应提供商的页面,了解其环境变量、配置方法及模型列表。 OpenAI Anthropic Google DeepSeek Groq Mistral xAI 302.AI Abacus abliteration.ai above.dev AgentRouter Agnes AI AI-ROUTER ai& AI21 Labs ainetcafe Aixy AKI.IO Alibaba Alibaba (China) Alibaba Coding Plan Alibaba Coding Plan (China) Alibaba Token Plan Alibaba Token Plan (China) Ambient AMD AnyAPI Arcee Atomic Chat Auriko Bailing Baseten Bee by HEOSSI Berget.AI Blue Claw Bothub Cerebras Charm Hyper Chutes Clarifai Claudinio ClinePass CloudFerro Sherlock Cloudflare Workers AI CoralBricks CoreWeave Cortecs CrofAI CrossModel Crusoe D.Run (China) DaoXE Databricks Deep Infra DevPass (LLM Gateway) DigitalOcean DInference EBCloud Echo Eden AI EmpirioLabs AI engy evroc FastRouter Fireworks AI FreeModel Friendli FrogBot GMI Cloud GreenPT Helicone Hetzner HPC-AI Hugging Face iFlow Impossibl Inception Inceptron Inco Infer by Flow7 Inference InferX Infomaniak IO.NET IteraCompute Jalapeno Cloud Jiekou.AI Kenari Kilo Gateway Kimi For Coding (kimi.ai) Kimi For Coding (kimi.com) klokintegration.se Kosmik Compute KUAE Cloud Coding Plan Lilac Llama LLM Gateway LLM Tech LLMTR LMStudio LongCat LowRouter LucidQuery Lynkr Meganova Melious Meta MiniMax (minimax.cn) MiniMax (minimax.io) MiniMax Token Plan (minimax.cn) MiniMax Token Plan (minimax.io) Mixlayer Moark Modal Model Oracle AI Modelis ModelScope Moonshot AI Moonshot AI (China) Morph NaN NanoGPT NEAR AI Cloud Nebius Token Factory NeoSmith Neuralwatt Nova Novita AI Nvidia OCI Generative AI Ofox Ollama Cloud OpenCode Go OpenCode Zen OpenReason Opper OrcaRouter OVHcloud AI Endpoints Pareto Inference Pendra Perplexity Perplexity Agent Pioneer Poe Poolside Privatemode AI QiHang Qiniu Regolo AI Requesty routing.run RunInfra Sakana AI Sarvam AI Scaleway SCNet Token Plan SCX.ai SenseNova (China) SiliconFlow SiliconFlow (China) Snowflake Cortex STACKIT Standard Compute StepFun (China) StepFun (Global) StepFun Step Plan (China) StepFun Step Plan (Global) Subconscious submodel Synthetic Tempr Gateway Tencent Coding Plan (China) Tencent Token Plan Tencent TokenHub TensorX The Grid AI Thinking Machines Tinfoil Together AI TokenGo TokenRouter TrustedRouter Umans AI Umans AI Coding Plan UnoRouter Upstage Vancine Vispark Vivgrid Volcengine Ark Volcengine Ark Coding Plan Vultr Wafer Wallaby Xiaomi Xiaomi Token Plan (China) Xiaomi Token Plan (Europe) Xiaomi Token Plan (Singapore) Xpersona Z.AI Z.AI Coding Plan Zeldoc Zenifra ZenMux Zhipu AI Zhipu AI Coding Plan
})
Model directoryDirect link to Model directory 通过左侧导航浏览可用模型目录,或在下方直接探索。 GatewaysOpenRouterNetlifyVercel+ 4 moreProvidersOpenAIAnthropicGoogle+ 204 more 你也可以在编辑器中发现模型。Mastra 为 model 字段提供完整的自动补全——只需开始输入,IDE 就会展示可用选项。 或者,在 Studio UI 中浏览并测试模型。 note在开发环境中,我们每小时自动刷新一次本地模型列表,确保 TypeScript 自动补全和 Studio 始终与最新模型保持同步。如需禁用,请设置 MASTRA_AUTO_REFRESH_PROVIDERS=false。在生产环境中,默认禁用自动刷新。 Mix and match modelsDirect link to Mix and match models 有些模型速度更快但能力较弱,而有些模型则提供更大的上下文窗口或更强的推理能力。可以使用同一提供商的不同模型,或跨提供商组合搭配,以适配不同任务。 src/mastra/agents/reasoning-agent.tsimport { Agent } from "@mastra/core/agent";
// Use a cost-effective model for document processing
const documentProcessor = new Agent({
id: "document-processor",
name: "Document Processor",
instructions: "Extract and summarize key information from documents",
model: "openai/gpt-5.6-sol"
})
// Use a powerful reasoning model for complex analysis
const reasoningAgent = new Agent({
id: "reasoning-agent",
name: "Reasoning Agent",
instructions: "Analyze data and provide strategic recommendations",
model: "anthropic/claude-opus-4-7"
})
Dynamic model selectionDirect link to Dynamic model selection 由于模型只是字符串,你可以基于请求上下文、变量或其他任何逻辑动态选择模型。 src/mastra/agents/dynamic-assistant-agent.tsconst agent = new Agent({
id: "dynamic-assistant",
name: "Dynamic Assistant",
model: ({ requestContext }) => {
const provider = requestContext.get("provider-id");
const model = requestContext.get("model-id");
return `${provider}/${model}`;
},
});
这让一些强大的模式成为可能: A/B 测试——在生产环境中对比不同模型的表现。 用户自选模型——让用户在应用里选择自己喜欢的模型。 多租户应用——每个客户可以自带 API key 和模型偏好。 Provider 专属选项 不同的模型提供商有自己的配置项。比如 OpenAI 可以调整 reasoningEffort,Anthropic 可以调整 cacheControl。Mastra 允许你在 agent 级别或单条消息级别设置这些 providerOptions。 src/mastra/agents/planner-agent.ts // Agent 级别(对所有后续消息生效) const planner = new Agent({ id: "planner", name: "Planner", instructions: { role: "system", content: "You are a helpful assistant.", providerOptions: { openai: { reasoningEffort: "low" } } }, model: "openai/gpt-5.6-sol", }); const lowEffort = await planner.generate("Plan a simple 3 item dinner menu"); // 消息级别(只对这条消息生效) const highEffort = await planner.generate([ { role: "user", content: "Plan a simple 3 item dinner menu for a celiac", providerOptions: { openai: { reasoningEffort: "high" } } } ]); 自定义请求头 如果需要设置自定义请求头,比如组织 ID 或其他 provider 专属字段,可以使用如下写法。 src/mastra/agents/custom-agent.ts const agent = new Agent({ id: "custom-agent", name: "Custom Agent", model: { id: "openai/gpt-5.6-sol", apiKey: process.env.OPENAI_API_KEY, headers: { "OpenAI-Organization": "org-abc123" } } }); 注意:不同提供商的配置方式有差异。自定义请求头的详情请参阅左侧导航中的各提供商页面。 模型回退 只依赖单一模型会给应用带来单点故障风险。模型回退机制可以在模型和提供商之间自动切换:当主模型不可用时,请求会自动重试到下一个配置的回退模型,直到某个模型成功响应。 src/mastra/agents/resilient-assistant-agent.ts import { Agent } from '@mastra/core/agent'; const agent = new Agent({ id: 'resilient-assistant', name: 'Resilient Assistant', instructions: 'You are a helpful assistant.', model: [ { model: "openai/gpt-5.6-sol", maxRetries: 3, }, { model: "anthropic/claude-sonnet-4-6", maxRetries: 2, }, { model: "google/gemini-2.5-pro", maxRetries: 2, }, ], }); Mastra 会优先尝试你的主模型。若遇到 500 错误、速率限制或超时,系统会自动切换到第一个备选模型;若该模型也失败,则继续尝试下一个。每个模型都有独立的重试次数,用尽后才换下一个。 用户对此过程完全无感——返回的响应格式保持一致,只是由不同的模型生成。在切换模型的同时,系统会保留错误上下文,确保错误传播清晰且兼容流式输出。 各模型设置 每个备选模型条目都可配置专属的 modelSettings、providerOptions 和 headers——当链中的模型需要不同的 temperature 或厂商特定参数以产生可比输出时,此功能十分实用。 src/mastra/agents/tuned-resilient-agent.ts import { Agent } from '@mastra/core/agent'; const agent = new Agent({ id: 'tuned-resilient', name: 'Tuned Resilient Agent', instructions: 'You are a helpful assistant.', model: [ { model: 'google/gemini-2.5-flash', maxRetries: 2, modelSettings: { temperature: 0.3 }, providerOptions: { google: { thinkingConfig: { thinkingBudget: 0 } } }, }, { model: 'openai/gpt-5-mini', maxRetries: 2, modelSettings: { temperature: 0.7 }, providerOptions: { openai: { reasoningEffort: 'low' } }, }, ], }); 优先级: modelSettings 与 providerOptions:每个回退条目(per-fallback entry)的配置会覆盖调用时(call-time)传入的选项,而调用时选项又覆盖 agent 的默认 defaultOptions。modelSettings 按 key 进行浅层合并。providerOptions 递归地执行深层合并,因此嵌套的 provider 配置(例如 google.thinkingConfig)能够跨层保留同级 key。 headers:调用时传入的 modelSettings.headers 优先于每个回退条目的 headers,而后者优先于从 model-router 的 models 中提取的 headers。运行时生成的 headers(如 tracing、auth、tenancy)有意设置为高于 model 级别的 headers。 每个字段还可以接受一个以 requestContext 为参数的函数,动态模型(dynamic models)的解析也遵循同样的机制。 使用 Mastra 运行本地模型 Mastra 也支持像 gpt-oss、Qwen3、DeepSeek 等许多其他你可以在自己硬件上运行的本地模型。运行本地模型的应用程序需要提供一个 OpenAI 兼容的 API 服务器供 Mastra 连接。我们推荐使用 LMStudio(参见“运行 LMStudio 服务器”)。 对于自定义的 OpenAI 兼容端点,id 是 Mastra 通过 model router 发送的路由形式。 如果远程服务行为类似直接的 provider,且期望使用裸模型名(例如 llama3.2),请使用 provider/model 格式。 如果远程服务行为类似模型网关,且上游模型命名空间包含 provider,例如 mastra/google/gemini-2.5-flash 或 openrouter/google/gemini-2.5-flash,请使用 gateway/provider/model 格式。 对于 url,请务必使用 Mastra model 配置所指向的 OpenAI 兼容端点的 base URL,而不是单独的 chat 端点。 src/mastra/agents/my-agent.ts import { Agent } from "@mastra/core/agent"; const agent = new Agent({ id: "my-agent", name: "My Agent", instructions: "You are a helpful assistant", model: { id: "custom/my-qwen3-model", url: "http://your-custom-openai-compatible-endpoint.com/v1" } }) 如果远程服务行为类似模型网关,请在 id 中包含网关前缀: src/mastra/agents/my-agent.ts import { Agent } from "@mastra/core/agent"; const agent = new Agent({ id: "my-agent", name: "My Agent", instructions: "You are a helpful assistant", model: { id: "mastra/google/gemini-2.5-flash", url: "http://your-custom-openai-compatible-endpoint.com/v1" } }) 示例:LMStudio 启动 LMStudio 服务器后,本地服务地址为 http://localhost:1234,提供 /v1/models、/v1/chat/completions 等端点,因此 url 设为 http://localhost:1234/v1。id 可以使用 lmstudio/${modelId} 格式,其中 modelId 就是 LMStudio 界面中显示的模型 ID。 src/mastra/agents/my-agent.tsimport { Agent } from "@mastra/core/agent";
const agent = new Agent({
id: "my-agent",
name: "My Agent",
instructions: "You are a helpful assistant",
model: {
id: "lmstudio/qwen/qwen3-30b-a3b-2507",
url: "http://localhost:1234/v1"
}
})
选择 OpenAI Responses API 自定义 url 端点默认使用 OpenAI Chat Completions API。如果你的端点提供 OpenAI Responses API(/v1/responses)——比如需要在某些强制要求该 API 的网关上把函数工具与推理模型结合使用——可以设置 api: "responses"。 src/mastra/agents/my-agent.tsimport { Agent } from "@mastra/core/agent";
const agent = new Agent({
id: "my-agent",
name: "My Agent",
instructions: "You are a helpful assistant",
model: {
id: "custom/my-model",
url: "http://your-custom-openai-compatible-endpoint.com/v1",
api: "responses"
}
})
如果不设置 api,默认值为 "chat",现有配置不受影响。使用 Responses API 时,provider 选项从 openai 命名空间读取;而 Chat Completions 路径则读取 openai-compatible 命名空间。 在 Mastra 中使用 AI SDK Mastra 支持直接使用 AI SDK 的 provider 模块。 src/mastra/agents/my-agent.tsimport { groq } from '@ai-sdk/groq';
import { Agent } from "@mastra/core/agent";
const agent = new Agent({
id: "my-agent",
name: "My Agent",
model: groq('gemma2-9b-it')
})
你可以在任何接受 “provider/model” 字符串的地方使用 AI SDK 模型(例如 groq('gemma2-9b-it')),包括模型路由器的后备方案和评分器中。 所有提供商 所有提供商直接链接 下方列出的每个提供商都支持在模型路由器中使用 provider/model 格式。请参阅对应提供商的页面,了解其环境变量、配置方法及模型列表。 OpenAI Anthropic Google DeepSeek Groq Mistral xAI 302.AI Abacus abliteration.ai above.dev AgentRouter Agnes AI AI-ROUTER ai& AI21 Labs ainetcafe Aixy AKI.IO Alibaba Alibaba (China) Alibaba Coding Plan Alibaba Coding Plan (China) Alibaba Token Plan Alibaba Token Plan (China) Ambient AMD AnyAPI Arcee Atomic Chat Auriko Bailing Baseten Bee by HEOSSI Berget.AI Blue Claw Bothub Cerebras Charm Hyper Chutes Clarifai Claudinio ClinePass CloudFerro Sherlock Cloudflare Workers AI CoralBricks CoreWeave Cortecs CrofAI CrossModel Crusoe D.Run (China) DaoXE Databricks Deep Infra DevPass (LLM Gateway) DigitalOcean DInference EBCloud Echo Eden AI EmpirioLabs AI engy evroc FastRouter Fireworks AI FreeModel Friendli FrogBot GMI Cloud GreenPT Helicone Hetzner HPC-AI Hugging Face iFlow Impossibl Inception Inceptron Inco Infer by Flow7 Inference InferX Infomaniak IO.NET IteraCompute Jalapeno Cloud Jiekou.AI Kenari Kilo Gateway Kimi For Coding (kimi.ai) Kimi For Coding (kimi.com) klokintegration.se Kosmik Compute KUAE Cloud Coding Plan Lilac Llama LLM Gateway LLM Tech LLMTR LMStudio LongCat LowRouter LucidQuery Lynkr Meganova Melious Meta MiniMax (minimax.cn) MiniMax (minimax.io) MiniMax Token Plan (minimax.cn) MiniMax Token Plan (minimax.io) Mixlayer Moark Modal Model Oracle AI Modelis ModelScope Moonshot AI Moonshot AI (China) Morph NaN NanoGPT NEAR AI Cloud Nebius Token Factory NeoSmith Neuralwatt Nova Novita AI Nvidia OCI Generative AI Ofox Ollama Cloud OpenCode Go OpenCode Zen OpenReason Opper OrcaRouter OVHcloud AI Endpoints Pareto Inference Pendra Perplexity Perplexity Agent Pioneer Poe Poolside Privatemode AI QiHang Qiniu Regolo AI Requesty routing.run RunInfra Sakana AI Sarvam AI Scaleway SCNet Token Plan SCX.ai SenseNova (China) SiliconFlow SiliconFlow (China) Snowflake Cortex STACKIT Standard Compute StepFun (China) StepFun (Global) StepFun Step Plan (China) StepFun Step Plan (Global) Subconscious submodel Synthetic Tempr Gateway Tencent Coding Plan (China) Tencent Token Plan Tencent TokenHub TensorX The Grid AI Thinking Machines Tinfoil Together AI TokenGo TokenRouter TrustedRouter Umans AI Umans AI Coding Plan UnoRouter Upstage Vancine Vispark Vivgrid Volcengine Ark Volcengine Ark Coding Plan Vultr Wafer Wallaby Xiaomi Xiaomi Token Plan (China) Xiaomi Token Plan (Europe) Xiaomi Token Plan (Singapore) Xpersona Z.AI Z.AI Coding Plan Zeldoc Zenifra ZenMux Zhipu AI Zhipu AI Coding Plan