Build a Calculator Chatbot with Gemini and LangChain
Project Task
Your task for today is to build a calculator chatbot using LangChain and Gemini. This project combines the power of LLMs with custom system prompts to simulate tools — in this case, a simple command-line calculator.
The program should:
Load your Gemini API key from an
.envfile usingdotenv.Create a Gemini-based chatbot using the
ChatGoogleGenerativeAIclass from LangChain.Use a system prompt that instructs the model to act as a calculator.
Accept user input (e.g.,
45 + 12) repeatedly.Let the Gemini model respond with the answer.
Keep track of conversation history to maintain context.
Exit the loop when the user types
"exit".
This project introduces you to LangChain’s Gemini integration, environment variables, and basic conversational AI applications with a practical utility flavor.
📌 Expected Output
When you run the program, it should prompt the user to type a math expression. Each expression is passed to the Gemini API, and the response is printed as a "Calculator" reply.
Example interaction:
The assistant will behave like a calculator, returning results based on the prompt you’ve given.
▶️ Start Coding Now in Colab
Jump straight into this project’s interactive notebook with full instructions, hints, and full runnable solutions.
🔒 Click below to access:
Keep reading with a 7-day free trial
Subscribe to Daily Python Projects to keep reading this post and get 7 days of free access to the full post archives.



