Skip to main content
Google AI offers a number of different chat models, including the powerful Gemini series. For information on the latest models, their features, context windows, etc. head to the Google AI docs. This will help you getting started with ChatGoogleGenerativeAI chat models. For detailed documentation of all ChatGoogleGenerativeAI features and configurations head to the API reference.
This library will be deprecatedThis library is based on a deprecated library from Google and will be replaced by the ChatGoogle library. New implementations should use the ChatGoogle library instead and existing implementations should consider migrating.

概述

集成详情

模型功能

请参阅下表标题中的链接,了解如何使用特定功能。

设置

You can access Google’s gemini and gemini-vision models, as well as other generative models in LangChain through ChatGoogleGenerativeAI class in the @langchain/google-genai integration package.
You can also access Google’s gemini family of models via the LangChain VertexAI and VertexAI-web integrations. See the Vertex AI integration docs.

凭证

Get an API key here: https://ai.google.dev/tutorials/setup Then set the GOOGLE_API_KEY environment variable:
如果你想要自动追踪模型调用,还可以设置你的 LangSmith API 密钥,取消注释以下内容:

安装

LangChain 的 ChatGoogleGenerativeAI 集成位于 @langchain/google-genai 包中:

实例化

现在我们可以实例化模型对象并生成聊天补全:

调用

Safety settings

Gemini models have default safety settings that can be overridden. If you are receiving lots of “Safety Warnings” from your models, you can try tweaking the safety_settings attribute of the model. For example, to turn off safety blocking for dangerous content, you can import enums from the @google/generative-ai package, then construct your LLM as follows:

工具调用

Tool calling with Google AI is mostly the same as tool calling with other models, but has a few restrictions on schema. The Google AI API does not allow tool schemas to contain an object with unknown properties. For example, the following Zod schemas will throw an error: const invalidSchema = z.object({ properties: z.record(z.unknown()) }); and const invalidSchema2 = z.record(z.unknown()); Instead, you should explicitly define the properties of the object field. Here’s an example:

Built in Google search retrieval

Google also offers a built in search tool which you can use to ground content generation in real-world information. Here’s an example of how to use it:
The response also includes metadata about the search result:

Code execution

Google Generative AI also supports code execution. Using the built in CodeExecutionTool, you can make the model generate code, execute it, and use the results in a final completion:
You can also pass this generation back to the model as chat history:

Context caching

Context caching allows you to pass some content to the model once, cache the input tokens, and then refer to the cached tokens for subsequent requests to reduce cost. You can create a CachedContent object using GoogleAICacheManager class and then pass the CachedContent object to your ChatGoogleGenerativeAIModel with enableCachedContent() method.
Note
  • The minimum input token count for context caching is 32,768, and the maximum is the same as the maximum for the given model.

Gemini prompting FAQs

As of the time this doc was written (2023/12/12), Gemini has some restrictions on the types and structure of prompts it accepts. Specifically:
  1. When providing multimodal (image) inputs, you are restricted to at most 1 message of “human” (user) type. You cannot pass multiple messages (though the single human message may have multiple content entries)
  2. System messages are not natively supported, and will be merged with the first human message if present.
  3. For regular chat conversations, messages must follow the human/ai/human/ai alternating pattern. You may not provide 2 AI or human messages in sequence.
  4. Message may be blocked if they violate the safety checks of the LLM. In this case, the model will return an empty response.

API 参考

有关所有 ChatGoogleGenerativeAI 功能和配置的详细文档,请前往 API 参考