A command-line interface for chatting with Google's Gemini AI model, built in Rust.
- Interactive Chat: Real-time conversation with Gemini AI
- Streaming Responses: See responses as they're generated (default mode)
- Non-streaming Mode: Get complete responses at once
- Built-in Commands: Help, clear screen, quit, toggle streaming
- Error Handling: Robust error handling for API issues
- Environment Configuration: Secure API key management
-
Clone and build the project:
cargo build
-
Set up your API key:
cp .env.example .env # Edit .env and add your Gemini API key -
Get a Gemini API key:
- Go to Google AI Studio
- Create a new API key
- Copy it to your
.envfile
cargo run/helpor/h- Show help message/clearor/cls- Clear the screen/quit,/exit, or/q- Exit the chat/stream- Toggle between streaming and non-streaming mode
Gemini Chat CLI
===============
Type your message and press Enter to send.
Commands:
/help - Show this help
/clear - Clear the screen
/quit - Exit the chat
/stream - Toggle streaming mode (default: on)
You: Hello! Can you explain what Rust is?
Gemini: Rust is a systems programming language that focuses on safety, speed, and concurrency...
You: /stream
Streaming mode: OFF
You: What are the main benefits of Rust?
Gemini: The main benefits of Rust include memory safety, performance, and fearless concurrency...
You: /quit
Goodbye!
-
GeminiClient (
src/gemini.rs):- Handles API communication with Google's Gemini service
- Supports both streaming and non-streaming responses
- Implements proper error handling and response parsing
-
Main CLI (
src/main.rs):- Interactive command-line interface
- User input handling and command processing
- Response display and formatting
-
Additional Tools:
src/bin/rag.rs- RAG (Retrieval-Augmented Generation) implementationsrc/bin/sse_client.rs- Server-Sent Events client for testing
- Async/Await: Built on Tokio for efficient async operations
- Streaming Support: Real-time response streaming using Server-Sent Events
- Error Handling: Comprehensive error handling with anyhow
- Environment Config: Secure API key management with dotenv
tokio- Async runtimereqwest- HTTP client for API callsserde- JSON serialization/deserializationanyhow- Error handlingfutures- Stream processingdotenv- Environment variable managementcrossterm- Terminal manipulation
The application uses environment variables for configuration:
GEMINI_API_KEY=your-actual-api-key-herecargo buildcargo run# RAG demo
cargo run --bin rag
# SSE client test
cargo run --bin sse_clientThe chat CLI integrates with Google's Gemini API using:
- Model:
gemini-2.0-flash-exp(configurable) - Endpoint:
https://generativelanguage.googleapis.com/v1beta/models - Features: Streaming and non-streaming content generation
- Temperature: 0.7
- Top-P: 0.95
- Top-K: 40
- Max Output Tokens: 2048
-
API Key Error:
GEMINI_API_KEY environment variable not setSolution: Copy
.env.exampleto.envand add your API key. -
Build Errors: Make sure you have Rust installed and run
cargo build. -
Network Issues: Check your internet connection and API key validity.
This project is open source. Feel free to use and modify as needed.