README
¶
Gemini Chat Example
An interactive chat application for conversing with Google's Gemini AI using Terminus.
Features
- 🤖 Real-time chat with Gemini AI
- 💬 Message history with timestamps
- 🎨 Color-coded messages (user vs AI)
- ⌨️ Keyboard shortcuts for common actions
- 📜 Scrollable message history
- 🔄 Automatic text wrapping for long messages
- ⚡ Asynchronous message handling
Prerequisites
1. Get a Gemini API Key
- Go to Google AI Studio
- Sign in with your Google account
- Click "Get API Key"
- Copy your API key
2. Set Environment Variable
export GEMINI_API_KEY="your-api-key-here"
Or add it to your shell profile (.bashrc
, .zshrc
, etc.):
echo 'export GEMINI_API_KEY="your-api-key-here"' >> ~/.zshrc
source ~/.zshrc
3. Install Go Dependencies
go get google.golang.org/genai
Running the Example
cd examples/gemini_chat
go run main.go
Then open http://localhost:8890 in your browser.
Usage
Keyboard Shortcuts
- Enter - Send message
- Ctrl+L - Clear chat history
- Ctrl+T - Toggle timestamps
- Ctrl+C or Esc - Exit
Chat Interface
The interface shows:
- 🤖 Title and connection status
- 📊 Message count
- 💬 Scrollable message history
- ✏️ Input field at the bottom
Messages are color-coded:
- Green - Your messages
- Blue - Gemini's responses
- Yellow - System messages
Code Structure
Components
- GeminiChatComponent - Main component managing the chat
- GeminiChatModel - Application state including messages and UI widgets
- messageListItem - Custom list item for rendering messages
Message Types
- GeminiConnectedMsg - Successful connection to Gemini
- GeminiResponseMsg - Response from Gemini AI
- GeminiErrorMsg - Error messages
Key Features
-
Async Communication
func sendToGemini(message string) terminus.Cmd { return func() terminus.Msg { // Send message to Gemini API res, err := chat.SendMessage(ctx, genai.Part{Text: message}) // Return response as message } }
-
Message History
- Messages stored with role, content, and timestamp
- Auto-scrolling to latest message
- Persistent within session
-
Error Handling
- Connection errors displayed in UI
- API key validation
- Graceful error messages
Customization Ideas
- Model Selection - Add ability to choose different Gemini models
- System Prompts - Configure AI behavior with system messages
- Export Chat - Save conversation history to file
- Markdown Rendering - Parse and style markdown in responses
- Code Highlighting - Detect and highlight code blocks
- Multi-turn Context - Maintain conversation context across sessions
- Streaming Responses - Show AI responses as they're generated
- File Uploads - Support for image analysis with Gemini
Troubleshooting
"GEMINI_API_KEY environment variable not set"
Make sure you've set the environment variable before running the app:
export GEMINI_API_KEY="your-key"
go run main.go
Connection Errors
- Check your internet connection
- Verify your API key is valid
- Ensure you have API access enabled
No Response from Gemini
- Check API quotas/limits
- Try a simpler message
- Check for service outages
API Rate Limits
Be aware of Gemini API rate limits:
- Free tier has usage limits
- Consider implementing rate limiting for production use
- Handle 429 (rate limit) errors gracefully
Documentation
¶
There is no documentation for this package.
Click to show internal directories.
Click to hide internal directories.