Google Generative AI
Author: HyeonJong Moon
Peer Review : effort-type
Proofread : Two-Jay
This is a part of LangChain Open Tutorial
Overview
You can use the ChatGoogleGenerativeAI class from the langchain-google-genai integration package to access not only Google AI’s gemini and gemini-vision models, but also other generative models.
Table of Contents
References
Environment Setup
Set up the environment. You may refer to Environment Setup for more details.
[Note]
langchain-opentutorialis a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.You can checkout the
langchain-opentutorialfor more details.
Create API Key
Please create an API KEY from link.
Set the user's Google API key as the environment variable
GOOGLE_API_KEY.
You can alternatively set GOOGLE_API_KEY in .env file and load it.
[Note] This is not necessary if you've already set GOOGLE_API_KEY in previous steps.
ChatGoogleGenerativeAI
Import the ChatGoogleGenerativeAI class from the langchain_google_genai package.
The ChatGoogleGenerativeAI class is used to implement conversational AI systems using Google’s Generative AI models. Through this class, users can interact with Google’s conversational AI model. Conversations with the model take place in a chat format, and the model generates appropriate responses based on user input.
Because the ChatGoogleGenerativeAI class is integrated with the LangChain framework, it can be used alongside other LangChain components.
For information about supported models, see: https://ai.google.dev/gemini-api/docs/models/gemini?hl=en
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 construct your LLM as follows:
Streaming and Batching
ChatGoogleGenerativeAI natively supports streaming and batching. Below is an example.
Multimodeal Model
To provide an image, pass a human message with contents of type List[dict], where each dict contains either an image value (type of image_url) or a text (type of text) value. The value of image_url can be any of the following:
A public image URL
An accessible gcs file (e.g., "gcs://path/to/file.png")
A local file path
A base64 encoded image (e.g.,
data:image/png;base64,abcd124)A PIL image

Last updated