Skip to main content

Google logo谷歌

🌐 Google logoGoogle

通过 Mastra 的模型路由访问 26 个 Google 模型。身份验证将自动使用 GOOGLE_GENERATIVE_AI_API_KEY 环境变量处理。

🌐 Access 26 Google models through Mastra's model router. Authentication is handled automatically using the GOOGLE_GENERATIVE_AI_API_KEY environment variable.

Google 文档中了解更多信息。

🌐 Learn more in the Google documentation.

.env
GOOGLE_GENERATIVE_AI_API_KEY=your-api-key
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-1.5-flash"
});

// Generate a response
const response = await agent.generate("Hello!");

// Stream a response
const stream = await agent.stream("Tell me a story");
for await (const chunk of stream) {
console.log(chunk);
}

模型
Direct link to 模型

🌐 Models

ModelContextToolsReasoningImageAudioVideoInput $/1MOutput $/1M
google/gemini-1.5-flash1.0M$0.07$0.30
google/gemini-1.5-flash-8b1.0M$0.04$0.15
google/gemini-1.5-pro1.0M$1$5
google/gemini-2.0-flash1.0M$0.10$0.40
google/gemini-2.0-flash-lite1.0M$0.07$0.30
google/gemini-2.5-flash1.0M$0.30$3
google/gemini-2.5-flash-image33K$0.30$30
google/gemini-2.5-flash-image-preview33K$0.30$30
google/gemini-2.5-flash-lite1.0M$0.10$0.40
google/gemini-2.5-flash-lite-preview-06-171.0M$0.10$0.40
google/gemini-2.5-flash-lite-preview-09-20251.0M$0.10$0.40
google/gemini-2.5-flash-preview-04-171.0M$0.15$0.60
google/gemini-2.5-flash-preview-05-201.0M$0.15$0.60
google/gemini-2.5-flash-preview-09-20251.0M$0.30$3
google/gemini-2.5-flash-preview-tts8K$0.50$10
google/gemini-2.5-pro1.0M$1$10
google/gemini-2.5-pro-preview-05-061.0M$1$10
google/gemini-2.5-pro-preview-06-051.0M$1$10
google/gemini-2.5-pro-preview-tts8K$1$20
google/gemini-3-flash-preview1.0M$0.50$3
google/gemini-3-pro-preview1.0M$2$12
google/gemini-embedding-0012K$0.15
google/gemini-flash-latest1.0M$0.30$3
google/gemini-flash-lite-latest1.0M$0.10$0.40
google/gemini-live-2.5-flash128K$0.50$2
google/gemini-live-2.5-flash-preview-native-audio131K$0.50$2
26 available models

高级配置
Direct link to 高级配置

🌐 Advanced Configuration

自定义头
Direct link to 自定义头

🌐 Custom Headers

src/mastra/agents/my-agent.ts
const agent = new Agent({
id: "custom-agent",
name: "custom-agent",
model: {
id: "google/gemini-1.5-flash",
apiKey: process.env.GOOGLE_GENERATIVE_AI_API_KEY,
headers: {
"X-Custom-Header": "value"
}
}
});

动态模型选择
Direct link to 动态模型选择

🌐 Dynamic Model Selection

src/mastra/agents/my-agent.ts
const agent = new Agent({
id: "dynamic-agent",
name: "Dynamic Agent",
model: ({ requestContext }) => {
const useAdvanced = requestContext.task === "complex";
return useAdvanced
? "google/gemini-live-2.5-flash-preview-native-audio"
: "google/gemini-1.5-flash";
}
});

提供商选项
Direct link to 提供商选项

🌐 Provider Options

Google 通过 providerOptions 参数支持以下特定提供商的选项:

🌐 Google supports the following provider-specific options via the providerOptions parameter:

const response = await agent.generate("Hello!", {
providerOptions: {
google: {
// See available options in the table below
}
}
});

可用选项
Direct link to 可用选项

🌐 Available Options

responseModalities?:

("TEXT" | "IMAGE")[] | undefined

thinkingConfig?:

{ thinkingBudget?: number | undefined; includeThoughts?: boolean | undefined; thinkingLevel?: "low" | "medium" | "high" | undefined; } | undefined

cachedContent?:

string | undefined

structuredOutputs?:

boolean | undefined

safetySettings?:

{ category: "HARM_CATEGORY_UNSPECIFIED" | "HARM_CATEGORY_HATE_SPEECH" | "HARM_CATEGORY_DANGEROUS_CONTENT" | "HARM_CATEGORY_HARASSMENT" | "HARM_CATEGORY_SEXUALLY_EXPLICIT" | "HARM_CATEGORY_CIVIC_INTEGRITY"; threshold: "HARM_BLOCK_THRESHOLD_UNSPECIFIED" | ... 4 more ... | "OFF"; }[] | undefined

threshold?:

"HARM_BLOCK_THRESHOLD_UNSPECIFIED" | "BLOCK_LOW_AND_ABOVE" | "BLOCK_MEDIUM_AND_ABOVE" | "BLOCK_ONLY_HIGH" | "BLOCK_NONE" | "OFF" | undefined

audioTimestamp?:

boolean | undefined

labels?:

Record<string, string> | undefined

mediaResolution?:

"MEDIA_RESOLUTION_UNSPECIFIED" | "MEDIA_RESOLUTION_LOW" | "MEDIA_RESOLUTION_MEDIUM" | "MEDIA_RESOLUTION_HIGH" | undefined

imageConfig?:

{ aspectRatio?: "1:1" | "2:3" | "3:2" | "3:4" | "4:3" | "4:5" | "5:4" | "9:16" | "16:9" | "21:9" | undefined; imageSize?: "1K" | "2K" | "4K" | undefined; } | undefined

直接提供商安装
Direct link to 直接提供商安装

🌐 Direct Provider Installation

这个提供程序也可以作为独立包直接安装,可以用来替代 Mastra 模型路由字符串。有关更多详细信息,请查看 包文档

🌐 This provider can also be installed directly as a standalone package, which can be used instead of the Mastra model router string. View the package documentation for more details.

npm install @ai-sdk/google