LangChain OpenTutorial
  • 🦜️🔗 The LangChain Open Tutorial for Everyone
  • 01-Basic
    • Getting Started on Windows
    • 02-Getting-Started-Mac
    • OpenAI API Key Generation and Testing Guide
    • LangSmith Tracking Setup
    • Using the OpenAI API (GPT-4o Multimodal)
    • Basic Example: Prompt+Model+OutputParser
    • LCEL Interface
    • Runnable
  • 02-Prompt
    • Prompt Template
    • Few-Shot Templates
    • LangChain Hub
    • Personal Prompts for LangChain
    • Prompt Caching
  • 03-OutputParser
    • PydanticOutputParser
    • PydanticOutputParser
    • CommaSeparatedListOutputParser
    • Structured Output Parser
    • JsonOutputParser
    • PandasDataFrameOutputParser
    • DatetimeOutputParser
    • EnumOutputParser
    • Output Fixing Parser
  • 04-Model
    • Using Various LLM Models
    • Chat Models
    • Caching
    • Caching VLLM
    • Model Serialization
    • Check Token Usage
    • Google Generative AI
    • Huggingface Endpoints
    • HuggingFace Local
    • HuggingFace Pipeline
    • ChatOllama
    • GPT4ALL
    • Video Q&A LLM (Gemini)
  • 05-Memory
    • ConversationBufferMemory
    • ConversationBufferWindowMemory
    • ConversationTokenBufferMemory
    • ConversationEntityMemory
    • ConversationKGMemory
    • ConversationSummaryMemory
    • VectorStoreRetrieverMemory
    • LCEL (Remembering Conversation History): Adding Memory
    • Memory Using SQLite
    • Conversation With History
  • 06-DocumentLoader
    • Document & Document Loader
    • PDF Loader
    • WebBaseLoader
    • CSV Loader
    • Excel File Loading in LangChain
    • Microsoft Word(doc, docx) With Langchain
    • Microsoft PowerPoint
    • TXT Loader
    • JSON
    • Arxiv Loader
    • UpstageDocumentParseLoader
    • LlamaParse
    • HWP (Hangeul) Loader
  • 07-TextSplitter
    • Character Text Splitter
    • 02. RecursiveCharacterTextSplitter
    • Text Splitting Methods in NLP
    • TokenTextSplitter
    • SemanticChunker
    • Split code with Langchain
    • MarkdownHeaderTextSplitter
    • HTMLHeaderTextSplitter
    • RecursiveJsonSplitter
  • 08-Embedding
    • OpenAI Embeddings
    • CacheBackedEmbeddings
    • HuggingFace Embeddings
    • Upstage
    • Ollama Embeddings With Langchain
    • LlamaCpp Embeddings With Langchain
    • GPT4ALL
    • Multimodal Embeddings With Langchain
  • 09-VectorStore
    • Vector Stores
    • Chroma
    • Faiss
    • Pinecone
    • Qdrant
    • Elasticsearch
    • MongoDB Atlas
    • PGVector
    • Neo4j
    • Weaviate
    • Faiss
    • {VectorStore Name}
  • 10-Retriever
    • VectorStore-backed Retriever
    • Contextual Compression Retriever
    • Ensemble Retriever
    • Long Context Reorder
    • Parent Document Retriever
    • MultiQueryRetriever
    • MultiVectorRetriever
    • Self-querying
    • TimeWeightedVectorStoreRetriever
    • TimeWeightedVectorStoreRetriever
    • Kiwi BM25 Retriever
    • Ensemble Retriever with Convex Combination (CC)
  • 11-Reranker
    • Cross Encoder Reranker
    • JinaReranker
    • FlashRank Reranker
  • 12-RAG
    • Understanding the basic structure of RAG
    • RAG Basic WebBaseLoader
    • Exploring RAG in LangChain
    • RAPTOR: Recursive Abstractive Processing for Tree-Organized Retrieval
    • Conversation-With-History
    • Translation
    • Multi Modal RAG
  • 13-LangChain-Expression-Language
    • RunnablePassthrough
    • Inspect Runnables
    • RunnableLambda
    • Routing
    • Runnable Parallel
    • Configure-Runtime-Chain-Components
    • Creating Runnable objects with chain decorator
    • RunnableWithMessageHistory
    • Generator
    • Binding
    • Fallbacks
    • RunnableRetry
    • WithListeners
    • How to stream runnables
  • 14-Chains
    • Summarization
    • SQL
    • Structured Output Chain
    • StructuredDataChat
  • 15-Agent
    • Tools
    • Bind Tools
    • Tool Calling Agent
    • Tool Calling Agent with More LLM Models
    • Iteration-human-in-the-loop
    • Agentic RAG
    • CSV/Excel Analysis Agent
    • Agent-with-Toolkits-File-Management
    • Make Report Using RAG, Web searching, Image generation Agent
    • TwoAgentDebateWithTools
    • React Agent
  • 16-Evaluations
    • Generate synthetic test dataset (with RAGAS)
    • Evaluation using RAGAS
    • HF-Upload
    • LangSmith-Dataset
    • LLM-as-Judge
    • Embedding-based Evaluator(embedding_distance)
    • LangSmith Custom LLM Evaluation
    • Heuristic Evaluation
    • Compare experiment evaluations
    • Summary Evaluators
    • Groundedness Evaluation
    • Pairwise Evaluation
    • LangSmith Repeat Evaluation
    • LangSmith Online Evaluation
    • LangFuse Online Evaluation
  • 17-LangGraph
    • 01-Core-Features
      • Understanding Common Python Syntax Used in LangGraph
      • Title
      • Building a Basic Chatbot with LangGraph
      • Building an Agent with LangGraph
      • Agent with Memory
      • LangGraph Streaming Outputs
      • Human-in-the-loop
      • LangGraph Manual State Update
      • Asking Humans for Help: Customizing State in LangGraph
      • DeleteMessages
      • DeleteMessages
      • LangGraph ToolNode
      • LangGraph ToolNode
      • Branch Creation for Parallel Node Execution
      • Conversation Summaries with LangGraph
      • Conversation Summaries with LangGraph
      • LangGrpah Subgraph
      • How to transform the input and output of a subgraph
      • LangGraph Streaming Mode
      • Errors
      • A Long-Term Memory Agent
    • 02-Structures
      • LangGraph-Building-Graphs
      • Naive RAG
      • Add Groundedness Check
      • Adding a Web Search Module
      • LangGraph-Add-Query-Rewrite
      • Agentic RAG
      • Adaptive RAG
      • Multi-Agent Structures (1)
      • Multi Agent Structures (2)
    • 03-Use-Cases
      • LangGraph Agent Simulation
      • Meta Prompt Generator based on User Requirements
      • CRAG: Corrective RAG
      • Plan-and-Execute
      • Multi Agent Collaboration Network
      • Multi Agent Collaboration Network
      • Multi-Agent Supervisor
      • 08-LangGraph-Hierarchical-Multi-Agent-Teams
      • 08-LangGraph-Hierarchical-Multi-Agent-Teams
      • SQL-Agent
      • 10-LangGraph-Research-Assistant
      • LangGraph Code Assistant
      • Deploy on LangGraph Cloud
      • Tree of Thoughts (ToT)
      • Ollama Deep Researcher (Deepseek-R1)
      • Functional API
      • Reflection in LangGraph
  • 19-Cookbook
    • 01-SQL
      • TextToSQL
      • SpeechToSQL
    • 02-RecommendationSystem
      • ResumeRecommendationReview
    • 03-GraphDB
      • Movie QA System with Graph Database
      • 05-TitanicQASystem
      • Real-Time GraphRAG QA
    • 04-GraphRAG
      • Academic Search System
      • Academic QA System with GraphRAG
    • 05-AIMemoryManagementSystem
      • ConversationMemoryManagementSystem
    • 06-Multimodal
      • Multimodal RAG
      • Shopping QnA
    • 07-Agent
      • 14-MoARAG
      • CoT Based Smart Web Search
      • 16-MultiAgentShoppingMallSystem
      • Agent-Based Dynamic Slot Filling
      • Code Debugging System
      • New Employee Onboarding Chatbot
      • 20-LangGraphStudio-MultiAgent
      • Multi-Agent Scheduler System
    • 08-Serving
      • FastAPI Serving
      • Sending Requests to Remote Graph Server
      • Building a Agent API with LangServe: Integrating Currency Exchange and Trip Planning
    • 08-SyntheticDataset
      • Synthetic Dataset Generation using RAG
    • 09-Monitoring
      • Langfuse Selfhosting
Powered by GitBook
On this page
  • Overview
  • Table of Contents
  • References
  • Environment Setup
  • Create API Key
  • ChatGoogleGenerativeAI
  • Safety Settings
  • Streaming and Batching
  • Multimodeal Model
  1. 04-Model

Google Generative AI

PreviousCheck Token UsageNextHuggingface Endpoints

Last updated 28 days ago

  • Author:

  • Peer Review :

  • Proofread :

  • This is a part of

Overview

You can use the ChatGoogleGenerativeAI class from the 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

[Note]

  • langchain-opentutorial is a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.

%%capture --no-stderr
!pip install langchain-opentutorial
# Install required packages
from langchain_opentutorial import package

package.install(
    [
        "langchain_core",
        "langchain_google_genai",
    ],
    verbose=False,
    upgrade=False,
)
# Set environment variables
from langchain_opentutorial import set_env

set_env(
    {
        "GOOGLE_API_KEY": "",
        "LANGCHAIN_API_KEY": "",
        "LANGCHAIN_TRACING_V2": "true",
        "LANGCHAIN_ENDPOINT": "https://api.smith.langchain.com",
        "LANGCHAIN_PROJECT": "05-GoogleGenerativeAI",
    }
)
Environment variables have been set successfully.

Create API Key

  • 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.

from dotenv import load_dotenv

load_dotenv(override=True)
True

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

from langchain_google_genai import ChatGoogleGenerativeAI

# Create an instance of ChatGoogleGenerativeAI
llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash")

# Pass the prompt to generate an output
answer = llm.stream("Please explain about Langchain(in three lines)")

# Print the result
for token in answer:
    print(token.content, end="", flush=True)
LangChain is a framework for developing applications powered by large language models (LLMs).  It simplifies building applications by connecting LLMs to other sources of data and computation.  This enables creation of sophisticated chains of prompts and actions, going beyond single LLM calls.
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.prompts import PromptTemplate

# Create an instance of ChatGoogleGenerativeAI
model = ChatGoogleGenerativeAI(model="gemini-1.5-flash")

# Create a prompt
prompt = PromptTemplate.from_template("Answer with Yes or No. Is {question} a fruit?")

# Create the chain
chain = prompt | model

# Print the result
response = chain.invoke({"question": "Apple"})
print(response.content)
Yes
    

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:

from langchain_google_genai import (
    ChatGoogleGenerativeAI,
    HarmBlockThreshold,
    HarmCategory,
)

# Create an instance of ChatGoogleGenerativeAI
llm = ChatGoogleGenerativeAI(
    model="gemini-1.5-flash",
    safety_settings={
        # Set threshold levels for blocking harmful content.
        # In this case, the settings indicate not to block harmful content. (However, there could still be basic blocking.)
        HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
        HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
        HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
        HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
    },
)

response = llm.invoke("Please explain about Gemini model")
print(response.content)
Gemini is Google's large multimodal AI model, designed to handle various types of information, including text, code, audio, and images.  It's positioned as a competitor to OpenAI's GPT models and aims to be a more versatile and powerful tool.  Here's a breakdown of key aspects:
    
    **Key Features and Capabilities:**
    
    * **Multimodality:** This is a crucial differentiator.  Gemini can understand and generate responses across different modalities, unlike models primarily focused on text.  This means it can process and create text, translate languages, write different kinds of creative content, and answer your questions in an informative way, even if they are open ended, challenging, or strange.  It can also analyze images and potentially other forms of data in the future.
    
    * **Reasoning and Problem-Solving:** Google emphasizes Gemini's improved reasoning abilities compared to previous models.  This allows it to tackle more complex tasks and provide more accurate and nuanced answers.
    
    * **Scalability and Efficiency:**  Gemini is built to be highly scalable, meaning it can be adapted and deployed for various applications and tasks.  Google also focuses on its efficiency, aiming for optimal performance with lower computational costs.
    
    * **Integration with Google Ecosystem:**  Gemini is designed to integrate seamlessly with other Google services and products, potentially enhancing functionalities across various platforms.
    
    
    **Different Versions/Sizes:**
    
    Google has announced different sizes or versions of Gemini, each with varying capabilities and computational resources needed to run them.  This is similar to how different GPT models (like GPT-3.5, GPT-4) exist with different strengths and weaknesses.  Larger models generally possess more capabilities but require more computational power.
    
    **Applications:**
    
    The potential applications of Gemini are vast and span various fields:
    
    * **Chatbots and Conversational AI:**  Creating more natural and engaging conversational experiences.
    * **Code Generation and Assistance:**  Helping developers write and debug code more efficiently.
    * **Content Creation:**  Assisting in writing, translating, and summarizing text.
    * **Image Understanding and Analysis:**  Analyzing images and extracting information from them.
    * **Scientific Research:**  Potentially accelerating research in various fields by analyzing large datasets.
    
    
    **Limitations:**
    
    Despite its advancements, Gemini, like all large language models, has limitations:
    
    * **Bias and Safety Concerns:**  Large language models can inherit biases present in the data they are trained on.  Addressing these biases and ensuring safe and responsible use is crucial.
    * **Hallucinations:**  The model might sometimes generate incorrect or nonsensical information, a phenomenon known as "hallucination."
    * **Computational Cost:**  Running large models like Gemini requires significant computational resources, making it expensive to deploy and maintain.
    
    
    **In summary:**
    
    Gemini represents a significant advancement in AI technology, offering a multimodal approach with improved reasoning capabilities.  Its potential applications are vast, but it's important to be aware of its limitations and the ongoing challenges in developing and deploying responsible AI.  Google's ongoing development and refinements of Gemini will likely further expand its capabilities and address its limitations over time.
    

Streaming and Batching

ChatGoogleGenerativeAI natively supports streaming and batching. Below is an example.

from langchain_google_genai import ChatGoogleGenerativeAI

# Create an instance of ChatGoogleGenerativeAI
llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash")

# Use the llm.batch() method to request multiple queries at once
for chunk in llm.stream("Can you recommend 5 travel destinations in California?"):
    print(chunk.content)
    print("---")
California
    ---
     offers a diverse range of experiences. Here are 5 travel destinations, each offering
    ---
     something different:
    
    1. **Yosemite National Park (Nature & Hiking
    ---
    ):** Iconic granite cliffs, giant sequoia trees, waterfalls, and challenging hikes make this a classic California experience.  Best for those who enjoy outdoor activities and
    ---
     stunning natural beauty.
    
    2. **San Francisco (City & Culture):** A vibrant city with iconic landmarks like the Golden Gate Bridge, Alcatraz Island,
    ---
     and cable cars. Offers a blend of history, culture, diverse food scenes, and a bustling atmosphere.
    
    3. **Santa Barbara (Beach & Relaxation):**  A charming coastal city with beautiful beaches, Spanish architecture, and a relaxed
    ---
     atmosphere. Perfect for those seeking a more laid-back vacation with opportunities for swimming, sunbathing, and exploring a quaint town.
    
    4. **Joshua Tree National Park (Desert & Stargazing):** Unique desert landscape with bizarre rock
    ---
     formations, Joshua trees, and incredible stargazing opportunities. Ideal for those who appreciate unusual scenery and enjoy hiking and photography.
    
    5. **Napa Valley (Wine Country & Gastronomy):** World-renowned wine region with rolling vineyards, charming towns, and luxurious resorts. Perfect for wine enthusiasts, foodies,
    ---
     and those seeking a relaxing getaway with opportunities for wine tasting and gourmet dining.
    
    
    These are just suggestions, and the best destination for you will depend on your interests and travel style.
    
    ---
from langchain_google_genai import ChatGoogleGenerativeAI

# Create an instance of ChatGoogleGenerativeAI
llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash")

# Use the llm.batch() method to request multiple queries at once
results = llm.batch(
    [
        "What is the capital of the United States?",
        "What is the capital of South Korea?",
    ]
)

for res in results:
    print(res.content)
The capital of the United States is **Washington, D.C.**
    
    The capital of South Korea is **Seoul**.
    

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

# CASE - A pulbic image URL
import requests
from IPython.display import Image

image_url = "https://picsum.photos/seed/picsum/300/300"
content = requests.get(image_url).content
Image(content)
from langchain_core.messages import HumanMessage
from langchain_google_genai import ChatGoogleGenerativeAI

# Create an instance of ChatGoogleGenerativeAI
llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash")

# The content is provided as a list, which can include both text and an image URL object
message = HumanMessage(
    content=[
        {"type": "text", "text": "What's in this image?"},
        {"type": "image_url", "image_url": image_url},
    ]
)

response = llm.invoke([message])
print(response.content)
That's a picture of the Matterhorn mountain in Switzerland.  The image shows the iconic pyramidal peak covered in snow, set against a dramatic, softly colored sunset or sunrise sky.  The foreground features a gently sloping snow-covered landscape.
    

Set up the environment. You may refer to for more details.

You can checkout the for more details.

Please create an API KEY from .

LangChain ChatGoogleGenerativeAI Reference
Environment Setup
langchain-opentutorial
link
HyeonJong Moon
effort-type
Two-Jay
LangChain Open Tutorial
langchain-google-genai
Overview
Create API key
ChatGoogleGenerativeAI
Safety Settings
Streaming and Batching
Multimodeal Model
jpeg