This project demonstrates how to build a simple chatbot using LangChain with Google Gemini models.
It takes user input from the terminal, sends it to the Gemini API, and prints the AI’s response.
- Python 3.9+
- LangChain
- python-dotenv
- Google Gemini API key
Clone the repository and install dependencies:
git clone https://github.com/your-username/your-repo.git
cd your-repo
pip install -r requirements.txtAdd the following to your requirements.txt:
langchain
python-dotenv
Create a .env file in the project root and add your Gemini API key:
GEMINI_API_KEY=your_api_key_here
from langchain.chat_models import init_chat_model
from dotenv import load_dotenv
import os
# Load environment variables
load_dotenv()
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
# Initialize Gemini model
module = init_chat_model(
model="gemini-3-flash-preview",
model_provider="google-genai",
api_key=GEMINI_API_KEY
)
# Chat loop
userinput = input("User: ")
response = module.invoke(userinput)
print(f"Agent: {response.content[0]['text']}")Run the script:
python main.pyExample interaction:
User: Hello, how are you?
Agent: I'm doing great! How can I help you today?
flowchart TD
A[👤 User Input] --> B[LangChain init_chat_model]
B --> C[Google Gemini API]
C --> D[AI Response]
D --> E[💻 Printed to Console]
sequenceDiagram
participant U as 👤 User
participant L as LangChain (init_chat_model)
participant G as Google Gemini API
participant C as 💻 Console
U->>L: Enter message (input)
L->>G: Send request with API key
G-->>L: Return AI response
L->>C: Print response
C-->>U: Display "Agent: ..."
- Make sure your API key is valid and has access to Gemini models.
- You can extend this script into a full chatbot with conversation history, streaming responses, or integration into web/mobile apps.
Feel free to fork this repo, open issues, or submit pull requests to improve the project.