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’sgemini and gemini-vision models, as well as other
generative models in LangChain through ChatGoogleGenerativeAI class in the
@langchain/google-genai integration package.
凭证
Get an API key here: https://ai.google.dev/tutorials/setup Then set theGOOGLE_API_KEY environment variable:
安装
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:Code execution
Google Generative AI also supports code execution. Using the built inCodeExecutionTool, you can make the model generate code, execute it, and use the results in a final completion:
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 aCachedContent object using GoogleAICacheManager class and then pass the CachedContent object to your ChatGoogleGenerativeAIModel with enableCachedContent() method.
- 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:- 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)
- System messages are not natively supported, and will be merged with the first human message if present.
- 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.
- 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 参考。
将这些文档连接到 Claude、VSCode 等工具,通过 MCP 获取实时答案。

