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

How to Fine-Tune LLaMA 3.2-1B-Instruct for Korean Instruction Tasks with LoRA and Hugging Face

LLaMA 3.2-1B-Instruct is a lightweight instruction-tuned language model released by Meta. It is designed to handle a wide range of instruction-based tasks with relatively low computational resources. Although the model was trained with multilingual capabilities, its performance on languages not included in its training set—such as Korean—is limited. This tutorial demonstrates how to fine-tune this open-source model on a Korean dataset using Hugging Face Transformers and PEFT (specifically LoRA), enabling it to better respond to Korean instructions. 1. Prerequisites Before running the example code below, ensure you have the following libraries installed: pip install torch transformers datasets peft accelerate mlflow huggingface_hub To use the LLaMA model or KoAlpaca datasets, you'll need a Hugging Face token. Additionally, you may need to handle potential CUDA Out-Of-Memory (OOM) errors. The following code takes care of both: from huggingface_hub import login login("y...

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