Skip to main content
Google Cloud PlatformAI Studio 相关的功能

聊天模型

Gemini 模型

访问 Gemini 模型,例如 gemini-1.5-progemini-2.0-flex,可以通过 ChatGoogleGenerativeAI,或者如果使用 VertexAI,则通过 ChatVertexAI 类。
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

使用 ChatGoogle 类通过 AI Studio 访问 gemma-3-27b-it 模型。(该类是适用于 Vertex AI 和 AI Studio API 的 ChatVertexAI 类的超类。)
由于 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
);

工具

Google 搜索

  • 设置自定义搜索引擎,请遵循这些说明
  • 从上一步获取 API 密钥和自定义搜索引擎 ID,并分别将它们设置为环境变量 GOOGLE_API_KEYGOOGLE_CSE_ID
有一个 GoogleCustomSearch 工具,它封装了这个 API。要导入这个工具:
import { GoogleCustomSearch } from "@langchain/community/tools/google_custom_search";
我们可以轻松地将此包装器加载为工具(供智能体使用)。我们可以这样做:
const tools = [new GoogleCustomSearch({})];
// Pass this variable into your agent.

以编程方式连接这些文档 到 Claude、VSCode 等,通过 MCP 获取实时答案。