A lightweight command-line chatbot application built with Go, leveraging the Google Gemini API (specifically the gemini-2.5-flash model) to provide interactive conversational experiences directly in your terminal. This application streams responses from Gemini and maintains the chat history within the current session.
- Interactive Chat: Engage in real-time conversations via your command line.
- Gemini Integration: Powered by the
gemini-2.5-flashmodel for fast and relevant responses. - Streaming Responses: See Gemini's replies appear character by character (or token by token) as they are generated.
- Session History: The chatbot remembers previous turns in the conversation, allowing for context-aware interactions.
- Simple Exit: Easily quit the chat by typing
exitorbye.
Before you begin, ensure you have the following installed and set up:
- Go: Version 1.18 or higher. You can download it from go.dev.
- Google Cloud Project: Access to a Google Cloud project with the Gemini API enabled.
- Gemini API Key: An API key for authenticating with the Gemini API. You can generate one from the Google AI Studio or Google Cloud Console.
-
Clone the Repository:
git clone https://github.com/arifseft/gemini-chat.git cd gemini-chat -
Set Your API Key:
The application reads the Gemini API key from an environment variable named
GEMINI_API_KEY.- Linux/macOS:
export GEMINI_API_KEY="YOUR_GEMINI_API_KEY"
- Windows (Command Prompt):
set GEMINI_API_KEY="YOUR_GEMINI_API_KEY"
- Windows (PowerShell):
$env:GEMINI_API_KEY="YOUR_GEMINI_API_KEY"
Replace
YOUR_GEMINI_API_KEYwith your actual API key. For persistent setup, consider adding this to your shell's profile file (e.g.,.bashrc,.zshrc,config.fish, etc.). - Linux/macOS:
-
Run the Application:
go run main.go
Once the application is running, you will see a You: prompt. Type your message and press Enter. Gemini's response will then be streamed back to you.
You: What is capital city of Indonesia?
*****************
Gemini: The capital city of Indonesia is **Jakarta**.
*****************
You: Tell me something interesting about that city.
*****************
Gemini: Jakarta is a fascinating and incredibly dynamic city! Here are a few interesting things about it:
1. **It's One of the Fastest-Sinking Cities in the World:** This is perhaps the most pressing and unique challenge Jakarta faces. Due to excessive groundwater extraction and the weight of its buildings on soft alluvial soil, parts of North Jakarta are sinking by as much as 25 cm (10 inches) per year. This makes it highly vulnerable to flooding and rising sea levels, leading to the radical decision to move the capital.
2. **The Capital is Being Moved (to Nusantara):** Directly related to the sinking and overpopulation issues, Indonesia is in the process of moving its capital from Jakarta to a newly planned city called **Nusantara** on the island of Borneo. This is a massive, ambitious, and controversial project, making Jakarta one of the very few capital cities in the world to be officially relocated.
3. **Infamous Traffic Congestion:** Jakarta is legendary for its epic traffic jams, locally known as "macet." It consistently ranks among the worst in the world, consuming hours of residents' daily lives. While there have been significant efforts to improve public transportation (like the new MRT system), the sheer number of vehicles and limited road infrastructure remain a huge challenge.
4. **A Massive Melting Pot:** As the capital and economic hub, Jakarta is a huge "melting pot" of cultures from across Indonesia's incredibly diverse archipelago. You'll find people from Sumatra, Java, Sulawesi, Papua, and countless other islands, each bringing their unique languages, food, and traditions. This makes for a vibrant, albeit crowded, cultural tapestry.
5. **Rich Colonial History (Batavia):** Before becoming Jakarta, the city was known as **Batavia** under Dutch colonial rule for centuries. You can still see remnants of this history, especially in the **Old Town (Kota Tua)**, with its colonial buildings, museums, and even canals reminiscent of Amsterdam.
Jakarta is a city of extremes – incredible energy, rapid development, and profound challenges, making it a truly unique place on the global stage.
*****************
You: exit
- If the
GEMINI_API_KEYenvironment variable is not set, the application will exit with an error. - Network issues or API errors during communication will be printed to the console.
The Go modules required are automatically managed by go run or go build:
github.com/google/generative-ai-go/genaigoogle.golang.org/api/optiongoogle.golang.org/api/iterator
Feel free to fork the repository, make improvements, and submit pull requests.
This project is open-source and available under the MIT License.