Skip to main content

Using Gemini API in LangChain: Step-by-Step Tutorial

What is LangChain and Why Use It?

LangChain is an open-source framework that simplifies the use of Large Language Models (LLMs) like OpenAI, Gemini (Google), and others by adding structure, tools, and memory to help build real-world applications such as chatbots, assistants, agents, or AI-enhanced software.

Why Use LangChain for LLM Projects?

  • Chainable Components: Easily build pipelines combining prompts, LLMs, tools, and memory.
  • Multi-Model Support: Work with Gemini, OpenAI, Anthropic, Hugging Face, etc.
  • Built-in Templates: Manage prompts more effectively.
  • Supports Multi-Turn Chat: Manage complex interactions with memory and roles.
  • Tool and API Integration: Let the model interact with external APIs or functions.

Let's Walk Through the Code: Gemini + LangChain

I will break the code into 4 main parts, each showcasing different features of LangChain and Gemini API.

Part 1: Basic Gemini API Call Using LangChain

import os
from dotenv import load_dotenv
load_dotenv(".env")  # Load environment variables from .env file
api_key = os.getenv("GOOGLE_API_KEY")  # Get Google API Key securely

# Import Gemini LLM wrapper from LangChain
from langchain_google_genai import ChatGoogleGenerativeAI

# Initialize the Gemini model with specific configuration
llm = ChatGoogleGenerativeAI(model='gemini-2.0-flash', temperature=0.9)

# Send a prompt to the model and get a response
response = llm.invoke('Explain what ChatGoogleGenerativeAI function do in langchain_google_genai.')
print(response.content)

Function Explanation:

  • load_dotenv(".env"): Loads your .env file which stores your API key.
  • os.getenv(): Retrieves your Gemini API key without hardcoding it in the script.
  • ChatGoogleGenerativeAI(): Initializes the Gemini model with the name and temperature (creativity level).
  • invoke(): Sends a prompt to the LLM and returns a response object.

This is a basic setup that demonstrates how to call Gemini via LangChain and print the result.

Part 2: Using PromptTemplate and Chain Composition

from langchain.prompts import PromptTemplate

llm = ChatGoogleGenerativeAI(model='gemini-2.0-flash', temperature=0.9)

# Define a reusable prompt structure with a placeholder
prompt = PromptTemplate.from_template('You are python coding agent. Explain me how to use {message}.')

# Chain the prompt template with the model using the pipe operator
chain = prompt | llm

# Provide the actual input for the placeholder
message = 'heapq'
response = chain.invoke(input=message)
print(response.content)

Function Explanation:

  • PromptTemplate.from_template(): Creates a parameterized prompt where {message} is replaced dynamically.
  • | (pipe operator): Chains the prompt and the LLM, forming a pipeline: prompt → LLM.
  • invoke(input=message): Passes the actual user input ('heapq') to the chain and gets the final output.

This part shows how to modularize your prompts and chain components together, which is a powerful pattern in LangChain.

Part 3: Using System and Human Messages for Role-Based Input

from langchain_core.messages import HumanMessage, SystemMessage

llm_pro = ChatGoogleGenerativeAI(model='gemini-2.5-pro-exp-03-25', temperature=0.9)

# Create structured messages indicating who is speaking (system vs human)
output = llm_pro.invoke([
    SystemMessage(content='Answer within 100 charicter in Korean.'),
    HumanMessage(content='Forcasting Amazon stock price for next week.')
])
print(output.content)

Function Explanation:

  • SystemMessage(): Gives the model background behavior (e.g., how to answer).
  • HumanMessage(): Simulates the user’s actual question or prompt.
  • Passing a list of messages: Supports multi-turn conversation or giving context with different roles.

This part introduces the chat-based message structure—important for building assistants with clear roles and behavior instructions.

Part 4: Using ChatPromptTemplate with Role Keywords

from langchain_core.prompts import ChatPromptTemplate

llm_pro = ChatGoogleGenerativeAI(model='gemini-2.5-pro-exp-03-25', temperature=0.9)

# Define a chat-style prompt template with role-based content
prompt = ChatPromptTemplate([
    ('system', 'Answer within 100 charicter in Korean.'),
    ('user', 'Forcasting {message} stock price for next week.')
])

# Create a chain from the prompt to the model
chain = prompt | llm_pro

# Replace the variable in the prompt template
message = 'Amazon'
response = chain.invoke(input=message)
print(response.content)

Function Explanation:

  • ChatPromptTemplate(): Provides a flexible way to define role-specific messages using keywords like 'system' and 'user'.
  • Templates with variables: {message} is dynamically filled in at runtime.
  • Prompt Chaining: Works just like before—template flows into the LLM.

This version is cleaner and more scalable than manually defining SystemMessage and HumanMessage.

2025.04.23 - [AI] - Building a Simple LLM Chatbot Server Using Google Gemini 2.5 Pro and LangChain

References

Comments

Popular

Building an MCP Agent with UV, Python & mcp-use

Model Context Protocol (MCP) is an open protocol designed to enable AI agents to interact with external tools and data in a standardized way. MCP is composed of three components: server , client , and host . MCP host The MCP host acts as the interface between the user and the agent   (such as Claude Desktop or IDE) and plays the role of connecting to external tools or data through MCP clients and servers. Previously, Anthropic’s Claude Desktop was introduced as a host, but it required a separate desktop app, license, and API key management, leading to dependency on the Claude ecosystem.   mcp-use is an open-source Python/Node package that connects LangChain LLMs (e.g., GPT-4, Claude, Groq) to MCP servers in just six lines of code, eliminating dependencies and supporting multi-server and multi-model setups. MCP Client The MCP client manages the MCP protocol within the host and is responsible for connecting to MCP servers that provide the necessary functions for the ...

How to Save and Retrieve a Vector Database using LangChain, FAISS, and Gemini Embeddings

How to Save and Retrieve a Vector Database using LangChain, FAISS, and Gemini Embeddings Efficient storage and retrieval of vector databases is foundational for building intelligent retrieval-augmented generation (RAG) systems using large language models (LLMs). In this guide, we’ll walk through a professional-grade Python implementation that utilizes LangChain with FAISS and Google Gemini Embeddings to store document embeddings and retrieve similar information. This setup is highly suitable for advanced machine learning (ML) and deep learning (DL) engineers who work with semantic search and retrieval pipelines. Why Vector Databases Matter in LLM Applications Traditional keyword-based search systems fall short when it comes to understanding semantic meaning. Vector databases store high-dimensional embeddings of text data, allowing for approximate nearest-neighbor (ANN) searches based on semantic similarity. These capabilities are critical in applications like: Question Ans...

RF-DETR: Overcoming the Limitations of DETR in Object Detection

RF-DETR (Region-Focused DETR), proposed in April 2025, is an advanced object detection architecture designed to overcome fundamental drawbacks of the original DETR (DEtection TRansformer) . In this technical article, we explore RF-DETR's contributions, architecture, and how it compares with both DETR and the improved model D-FINE . We also provide experimental benchmarks and discuss its real-world applicability. RF-DETR Architecture diagram for object detection Limitations of DETR DETR revolutionized object detection by leveraging the Transformer architecture, enabling end-to-end learning without anchor boxes or NMS (Non-Maximum Suppression). However, DETR has notable limitations: Slow convergence, requiring heavy data augmentation and long training schedules Degraded performance on low-resolution objects and complex scenes Lack of locality due to global self-attention mechanisms Key Innovations in RF-DETR RF-DETR intr...