跳到主要内容
Google Cloud PlatformAI Studio 相关的功能

聊天模型

Gemini 模型

通过 ChatGoogleGenerativeAI 访问 Gemini 模型,如 gemini-1.5-progemini-2.0-flex,如果使用 VertexAI,则通过 ChatVertexAI 类访问。
有关安装 LangChain 软件包的一般说明,请参阅此部分
  • GenAI
  • VertexAI
npm
npm install @langchain/google-genai @langchain/core
配置您的 API 密钥。
export GOOGLE_API_KEY=your-api-key
import { ChatGoogleGenerativeAI } from "@langchain/google-genai";

const model = new ChatGoogleGenerativeAI({
  model: "gemini-pro",
  maxOutputTokens: 2048,
});

// Batch and stream are also supported
const res = await model.invoke([
  [
    "human",
    "What would be a good company name for a company that makes colorful socks?",
  ],
]);
更新的 Gemini 模型支持图像输入
const visionModel = new ChatGoogleGenerativeAI({
  model: "gemini-2.5-flash-lite",
  maxOutputTokens: 2048,
});
const image = fs.readFileSync("./hotdog.jpg").toString("base64");
const input2 = [
  new HumanMessage({
    content: [
      {
        type: "text",
        text: "Describe the following image.",
      },
      {
        type: "image_url",
        image_url: `data:image/png;base64,${image}`,
      },
    ],
  }),
];

const res = await visionModel.invoke(input2);
点击此处查看 @langchain/google-genai 特定集成文档
image_url 的值必须是 base64 编码的图像(例如,data:image/png;base64,abcd124)。

Gemma

通过 AI Studio 使用 ChatGoogle 类访问 gemma-3-27b-it 模型。(此类是 ChatVertexAI 类的超类,可与 Vertex AI 和 AI Studio API 配合使用。)
由于 Gemma 是一个开放模型,它也可能在其他平台(包括 Ollama)上可用。
npm
npm install @langchain/google-gauth @langchain/core
配置您的 API 密钥。
export GOOGLE_API_KEY=your-api-key
import { ChatGoogle } from "@langchain/google-gauth";

const model = new ChatGoogle({
  model: "gemma-3-27b-it",
});

const res = await model.invoke([
  {
    role: "user",
    content:
      "What would be a good company name for a company that makes colorful socks?",
  },
]);

第三方模型

请参阅上文,了解如何通过 Vertex AI 设置身份验证以使用这些模型。 Anthropic Claude 模型也可通过 Vertex AI 平台获得。有关启用模型访问权限和要使用的模型名称的更多信息,请参见此处 PaLM 模型不再受支持。

向量存储

Vertex AI Vector Search,以前称为 Vertex AI Matching Engine,提供了业界领先的高规模低延迟向量数据库。这些向量数据库通常被称为向量相似度匹配或近似最近邻 (ANN) 服务。
import { MatchingEngine } from "@langchain/community/vectorstores/googlevertexai";

Postgres 向量存储

来自 @langchain/google-cloud-sql-pg 包的 PostgresVectorStore 模块提供了一种使用 CloudSQL for PostgresSQL 存储向量嵌入的方式。
$ yarn add @langchain/google-cloud-sql-pg
设置您的环境变量
PROJECT_ID="your-project-id"
REGION="your-project-region"
INSTANCE_NAME="your-instance"
DB_NAME="your-database-name"
DB_USER="your-database-user"
PASSWORD="your-database-password"
通过 PostgresEngine 类创建数据库连接
const engine: PostgresEngine = await PostgresEngine.fromInstance(
  process.env.PROJECT_ID ?? "",
  process.env.REGION ?? "",
  process.env.INSTANCE_NAME ?? "",
  process.env.DB_NAME ?? "",
  peArgs
);
初始化向量存储表
await engine.initVectorstoreTable(
  "my_vector_store_table",
  768,
  vectorStoreArgs
);
创建向量存储实例
const vectorStore = await PostgresVectorStore.initialize(
  engine,
  embeddingService,
  "my_vector_store_table",
  pvectorArgs
);

工具

  • 按照这些说明设置自定义搜索引擎
  • 从上一步获取 API 密钥和自定义搜索引擎 ID,并将它们分别设置为环境变量 GOOGLE_API_KEYGOOGLE_CSE_ID
存在一个包装此 API 的 GoogleCustomSearch 实用程序。要导入此实用程序
import { GoogleCustomSearch } from "@langchain/community/tools/google_custom_search";
我们可以轻松地将此包装器作为工具(与 Agent 一起使用)加载。我们可以通过以下方式完成此操作
const tools = [new GoogleCustomSearch({})];
// Pass this variable into your agent.

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.