> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-zh.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# Google 集成

> 使用 LangChain JavaScript 集成 Google Gemini 工具。

`@langchain/google` 包支持 Gemini 的内置工具，这些工具提供了诸如网络搜索增强、代码执行、URL 上下文检索等功能。这些工具作为 Gemini 原生对象，通过 `bindTools()` 或 `tools` 调用选项传递给 `ChatGoogle`。

<Warning>
  在同一请求中，不能混合使用 Gemini 原生工具（Google 搜索、代码执行等）和标准的 LangChain 工具（基于 Zod 的函数工具）。关于标准工具调用的用法，请参阅 [ChatGoogle](/oss/javascript/integrations/chat/google) 页面。
</Warning>

### Google 搜索

`googleSearch` 工具使用实时的 Google 搜索结果来增强模型的响应。这对于关于当前事件或特定事实的问题非常有用。

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { ChatGoogle } from "@langchain/google";

const llm = new ChatGoogle("gemini-2.5-flash")
  .bindTools([
    {
      googleSearch: {},
    },
  ]);

const res = await llm.invoke("谁赢得了最近的世界大赛？");
console.log(res.text);
```

你可以选择性地将搜索结果过滤到特定的时间范围：

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
const llm = new ChatGoogle("gemini-2.5-flash")
  .bindTools([
  {
    googleSearch: {
      timeRangeFilter: {
        startTime: "2025-01-01T00:00:00Z",
        endTime: "2025-12-31T23:59:59Z",
      },
    },
  },
]);
```

<Note>
  `googleSearchRetrieval` 工具为向后兼容而保留，但推荐使用 `googleSearch`。
</Note>

更多信息，请参阅 [Google 的“使用 Google 搜索进行增强”文档](https://ai.google.dev/gemini-api/docs/grounding)。

### 代码执行

`codeExecution` 工具允许 Gemini 生成并运行 Python 代码来解决复杂问题。模型编写代码、执行代码并返回结果。

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { ChatGoogle } from "@langchain/google";

const llm = new ChatGoogle("gemini-2.5-flash")
  .bindTools([
    {
      codeExecution: {},
    },
  ]);

const res = await llm.invoke("计算第 100 个斐波那契数。");
console.log(res.contentBlocks);
```

响应在 `contentBlocks` 字段中同时包含生成的代码及其执行结果：

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
for (const block of res.contentBlocks) {
  if (block.type === "tool_code") {
    console.log("代码:", block.toolCode);
  } else if (block.type === "tool_result") {
    console.log("结果:", block.toolResult);
  }
}
```

更多信息，请参阅 [Google 的代码执行文档](https://ai.google.dev/gemini-api/docs/code-execution)。

### URL 上下文

`urlContext` 工具允许 Gemini 从 URL 获取内容并使用这些内容来增强其响应。

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { ChatGoogle } from "@langchain/google";

const llm = new ChatGoogle("gemini-2.5-flash")
  .bindTools([
    {
      urlContext: {},
    },
  ]);

const res = await llm.invoke("总结这个页面：https://js.langchain.com/");
console.log(res.text);
```

更多信息，请参阅 [Google 的 URL 上下文文档](https://ai.google.dev/gemini-api/docs/url-context)。

### Google 地图

`googleMaps` 工具使用来自 Google 地图的地理空间上下文来增强响应。这对于与地点相关的查询非常有用。

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { ChatGoogle } from "@langchain/google";

const llm = new ChatGoogle("gemini-2.5-flash")
  .bindTools([
    {
      googleMaps: {},
    },
  ]);

const res = await llm.invoke("时代广场附近最好的咖啡店有哪些？");
console.log(res.text);
```

你可以启用小部件上下文令牌来渲染 Google 地图小部件：

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
const llm = new ChatGoogle("gemini-2.5-flash")
  .bindTools([
    {
      googleMaps: {
        enableWidget: true,
    },
  },
]);

const res = await llm.invoke("找到芝加哥市中心的意大利餐厅");

// 从增强元数据中访问小部件上下文令牌
const groundingMetadata = res.response_metadata?.groundingMetadata;
console.log(groundingMetadata?.googleMapsWidgetContextToken);
```

更多信息，请参阅 [Google 的“使用 Google 地图进行增强”文档](https://ai.google.dev/gemini-api/docs/grounding/google-maps)。

### 文件搜索

`fileSearch` 工具从文件搜索存储中执行语义检索。文件必须首先使用 Gemini 文件 API 导入。

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { ChatGoogle } from "@langchain/google";

const llm = new ChatGoogle("gemini-2.5-flash")
  .bindTools([
    {
      fileSearch: {
      fileSearchStoreNames: ["fileSearchStores/my-store-123"],
    },
  },
]);

const res = await llm.invoke("报告关于第四季度收入说了什么？");
console.log(res.text);
```

配置选项：

* `fileSearchStoreNames` (必需) -- 要从中检索的文件搜索存储的名称
* `metadataFilter` (可选) -- 应用于检索的元数据过滤器
* `topK` (可选) -- 要返回的语义检索块的数量

更多信息，请参阅 [Google 的文件搜索文档](https://ai.google.dev/gemini-api/docs/file-search)。

### 计算机使用

`computerUse` 工具使 Gemini 能够与浏览器环境交互。模型可以查看屏幕截图并执行点击、输入、滚动等操作。

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { ChatGoogle } from "@langchain/google";

const llm = new ChatGoogle("gemini-2.5-flash")
  .bindTools([
    {
      computerUse: {
      environment: "ENVIRONMENT_BROWSER",
    },
  },
]);
```

配置选项：

* `environment` (必需) -- 正在操作的环境（例如 `"ENVIRONMENT_BROWSER"`）
* `excludedPredefinedFunctions` (可选) -- 要从操作空间中排除的预定义函数

更多信息，请参阅 [Google 的计算机使用文档](https://ai.google.dev/gemini-api/docs/computer-use)。

### MCP 服务器

`mcpServers` 字段允许 Gemini 连接到远程 MCP（模型上下文协议）服务器。与其他原生工具不同，MCP 服务器在工具对象上指定为一个数组。

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { ChatGoogle } from "@langchain/google";

const llm = new ChatGoogle("gemini-2.5-flash")
  .bindTools([
    {
      mcpServers: [
      {
        name: "my-mcp-server",
        streamableHttpTransport: {
          url: "https://my-mcp-server.example.com/mcp",
        },
      },
    ],
  },
]);

const res = await llm.invoke("使用 MCP 服务器中的工具来帮助我。");
console.log(res.text);
```

更多信息，请参阅 [Google 的 MCP 文档](https://ai.google.dev/gemini-api/docs/mcp)。

### Vertex AI Search 数据存储

如果你正在使用 Vertex AI (`platformType: "gcp"`)，你可以使用 Vertex AI Search 数据存储来增强响应。

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { ChatGoogle } from "@langchain/google";

const projectId = "YOUR_PROJECT_ID";
const datastoreId = "YOUR_DATASTORE_ID";

const llm = new ChatGoogle({
  model: "gemini-2.5-pro",
  platformType: "gcp",
}).bindTools([
  {
    retrieval: {
      vertexAiSearch: {
        datastore: `projects/${projectId}/locations/global/collections/default_collection/dataStores/${datastoreId}`,
      },
      disableAttribution: false,
    },
  },
]);

const res = await llm.invoke(
  "阿根廷对玻利维亚足球比赛的比分是多少？"
);
console.log(res.text);
```

更多信息，请参阅 [Google 的“使用 Vertex AI Search 进行增强”文档](https://cloud.google.com/vertex-ai/generative-ai/docs/grounding/ground-with-vertex-ai-search)。

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\oss\javascript\integrations\tools\google.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
