# 🤖 AI Chatbot - Java Swing Application
A modern, feature-rich AI chatbot application built with Java Swing and Groq's free AI API.
Features a clean chat interface with real-time AI responses powered by LLaMA 3.3 70B.




## ✨ Features
- 🎨 **Modern Chat UI** - WhatsApp-style chat bubbles with timestamps
- 🤖 **AI-Powered** - Uses Groq's free LLaMA 3.3 70B model
- 💬 **Context Aware** - Maintains conversation history
- ⚡ **Fast Responses** - Lightning-fast AI replies (1-3 seconds)
- ⌨️ **Keyboard Shortcuts** - Press Enter to send messages
- 🧹 **Clear History** - Reset conversation anytime
- 🎯 **Error Handling** - Graceful error messages and recovery
- 🔐 **Secure** - API keys stored in local config file (not in code)
- 🆓 **100% Free** - No API costs, no credit card required
## 🚀 Quick Start
### Prerequisites
- **Java JDK 11 or higher** ([Download](https://www.oracle.com/java/technologies/downloads/))
```bash
java -version # Verify installation
-
Maven 3.6+ (Download)
mvn -version # Verify installation -
Groq API Key - Free, no credit card required
- Sign up at: https://console.groq.com/
git clone https://github.com/kalana250/java_chatbot.git
cd java_chatbot- Visit https://console.groq.com/
- Sign up for a free account (GitHub login available)
- Go to "API Keys" section
- Click "Create API Key"
- Give it a name (e.g., "Java Chatbot")
- Copy your API key (starts with
gsk_)
Create the configuration file:
# Copy the example file
cp config.properties.example config.properties
# On Windows:
copy config.properties.example config.propertiesEdit config.properties and add your key:
groq.api.key=gsk_your_actual_key_hereReplace gsk_your_actual_key_here with your actual Groq API key.
# Install dependencies and compile
mvn clean compile
# Run the application
mvn exec:javaThe chatbot window should appear! 🎉
java_chatbot/
│
├── .gitignore # Git ignore rules
├── config.properties # Your API key (NOT in Git)
├── config.properties.example # Template for API key
├── pom.xml # Maven configuration
├── README.md # This file
│
└── src/
└── main/
└── java/
├── ChatbotUI.java # Main GUI application
├── OpenAIService.java # API communication handler
└── Message.java # Message data model
| File | Purpose |
|---|---|
ChatbotUI.java |
Main Swing GUI with chat interface, bubbles, and event handling |
OpenAIService.java |
Handles Groq API requests and manages conversation context |
Message.java |
Data model for chat messages (content, sender, timestamp) |
config.properties |
Contains your API key (excluded from Git) |
config.properties.example |
Template for configuration (committed to Git) |
pom.xml |
Maven dependencies and build configuration |
Method 1: Maven (Recommended)
mvn clean compile exec:javaMethod 2: IntelliJ IDEA
- Open project in IntelliJ IDEA
- Wait for Maven to import dependencies
- Right-click on
ChatbotUI.java - Select "Run 'ChatbotUI.main()'"
Method 3: Eclipse
- Import as "Existing Maven Projects"
- Right-click project → "Run As" → "Java Application"
- Select
ChatbotUIas the main class
Method 4: Command Line (after building)
# Compile first
mvn package
# Run the JAR
java -jar target/ai-chatbot-1.0-SNAPSHOT.jar- Type your message in the text field at the bottom
- Send by clicking "Send" button or pressing Enter
- Wait for AI response (usually 1-3 seconds)
- Continue chatting - the bot remembers context!
- Clear conversation using the "Clear Chat" button
You: What is Java?
AI: Java is a high-level, object-oriented programming language...
You: Write a hello world program
AI: Here's a simple Hello World program in Java:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
You: Explain the code
AI: Let me break down the Hello World program...
Edit OpenAIService.java line ~40:
// Fast and capable (default - recommended)
requestBody.put("model", "llama-3.3-70b-versatile");
// Faster responses, less capable
requestBody.put("model", "llama-3.1-8b-instant");
// Alternative models
requestBody.put("model", "mixtral-8x7b-32768");
requestBody.put("model", "gemma2-9b-it");// In OpenAIService.java, line ~41
requestBody.put("max_tokens", 500);
// Options:
// 100 = Short responses
// 500 = Medium (default)
// 1000 = Long, detailed responses
// 2000 = Maximum (may be slower)// In OpenAIService.java, line ~42
requestBody.put("temperature", 0.7);
// Scale: 0.0 to 2.0
// 0.0 = Focused, deterministic, factual
// 0.7 = Balanced (default)
// 1.0 = Creative
// 2.0 = Very creative, unpredictableEdit the system message in OpenAIService.java constructor:
// Default
systemMessage.put("content", "You are a helpful and friendly AI assistant.");
// Examples:
systemMessage.put("content", "You are a professional Java programming tutor.");
systemMessage.put("content", "You are a funny and casual chatbot who uses emojis.");
systemMessage.put("content", "You are an expert at explaining complex topics simply.");
systemMessage.put("content", "You are a coding assistant specialized in debugging.");In ChatbotUI.java:
// Header background (line ~45)
headerPanel.setBackground(new Color(70, 130, 180)); // Steel Blue
// Try: new Color(76, 175, 80) for green
// Try: new Color(156, 39, 176) for purple
// Send button (line ~85)
sendButton.setBackground(new Color(70, 130, 180));
// User message bubble (line ~150, HTML)
"background-color: #007bff;" // Blue
// Try: #28a745 for green
// Try: #6f42c1 for purple
// AI message bubble (line ~165, HTML)
"background-color: #f1f1f1;" // Light gray
// Try: #e3f2fd for light blue
// Try: #fff3e0 for light orange// In ChatbotUI constructor (line ~32)
setSize(800, 600); // Width x Height
// Examples:
setSize(1000, 700); // Larger
setSize(600, 500); // Smaller
setSize(1200, 800); // Full screen on small monitors// Header title (line ~50)
titleLabel.setFont(new Font("Arial", Font.BOLD, 24));
// Try: "Segoe UI", "Roboto", "Helvetica"
// Input field (line ~78)
inputField.setFont(new Font("Arial", Font.PLAIN, 14));
// Chat messages (in HTML, line ~155)
"font-family: Arial; font-size: 14px;"Problem: Application can't find or read the configuration file.
Solutions:
- Make sure
config.propertiesexists in project root directory - Verify it contains:
groq.api.key=your-key-here - Check file location:
# Should be here: C:\IdeaProjects\java_chatbot\config.properties
- Verify file encoding is UTF-8
- Make sure there are no spaces around the
=sign
Problem: Invalid or malformed API key.
Solutions:
- Check your key starts with
gsk_ - No extra spaces or quotes in
config.properties:# ❌ Wrong groq.api.key = "gsk_abc123" groq.api.key= gsk_abc123 # ✅ Correct groq.api.key=gsk_abc123
- Get a new key from https://console.groq.com/keys
- Make sure you copied the entire key
Problem: API returned an error instead of a response.
Solutions:
- Check your internet connection
- Verify API key is correct
- Check Groq service status: https://status.groq.com/
- Look at console output for actual error message
- Try again - might be temporary API issue
# Clean and rebuild
mvn clean install -U
# If still fails, delete Maven cache
# Windows:
rmdir /s %USERPROFILE%\.m2\repository
# Mac/Linux:
rm -rf ~/.m2/repository
# Then rebuild
mvn clean installSolutions:
- Make sure you're in the project root directory
- Run Maven compile first:
mvn clean compile
- Check Java version:
java -version # Should be 11 or higher - In IntelliJ: File → Invalidate Caches → Invalidate and Restart
Windows:
- Make sure Java JDK (not JRE) is installed
- Check JAVA_HOME environment variable
Mac:
# Install Java if needed
brew install openjdk@11Linux:
# Install Java and GUI libraries
sudo apt-get update
sudo apt-get install openjdk-11-jdk
export DISPLAY=:0Solutions:
- Switch to faster model in
OpenAIService.java:requestBody.put("model", "llama-3.1-8b-instant");
- Reduce max_tokens to 200-300
- Check your internet speed
- Check Groq API status
Problem: You accidentally committed your API key to Git.
Solutions:
- NEVER commit
config.properties - Make sure
.gitignoreincludesconfig.properties - Use
config.properties.exampleinstead (without real key) - If already committed, see Security Best Practices
- ✅ Use
config.propertiesfor API keys - ✅ Add
config.propertiesto.gitignore - ✅ Commit
config.properties.example(template only) - ✅ Revoke API keys if accidentally exposed
- ✅ Use different keys for different projects
- ✅ Keep your API keys private
- ❌ Hardcode API keys in source code
- ❌ Commit
config.propertiesto Git - ❌ Share API keys in public repositories
- ❌ Share your
config.propertiesfile - ❌ Use production keys for testing
- ❌ Reuse exposed API keys
-
Immediately revoke it:
- Go to https://console.groq.com/keys
- Delete the exposed key
-
Create a new key:
- Generate new API key
- Update
config.properties
-
Clean Git history (if committed):
# Remove from Git tracking git rm --cached config.properties # Add to .gitignore echo "config.properties" >> .gitignore # Commit the change git add .gitignore git commit -m "Remove API key from tracking"
This project uses the following dependencies:
<dependencies>
<!-- JSON parsing library -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20230227</version>
</dependency>
</dependencies>Built-in Java libraries:
java.net.http.*- HTTP client for API requests (Java 11+)javax.swing.*- GUI frameworkjava.awt.*- Graphics and layoutjava.io.*- File I/O for config loadingjava.util.*- Data structures
- Get API key from https://platform.openai.com/api-keys
- Update
OpenAIService.java:
private static final String API_URL = "https://api.openai.com/v1/chat/completions";
// In sendMessage() method
requestBody.put("model", "gpt-3.5-turbo");
// or "gpt-4" for better quality- Update
config.properties:
openai.api.key=sk-proj-your-openai-keyCost: ~$0.002 per 1K tokens (very cheap)
- Get API key from https://ai.google.dev/
- Use Google's Java SDK or REST API
- Update endpoint and request format
- Get key from https://console.anthropic.com/
- Update
OpenAIService.java:
private static final String API_URL = "https://api.anthropic.com/v1/messages";
// Update request headers and format for Claude API| Limit Type | Value |
|---|---|
| Requests per minute | 30 |
| Requests per day | 14,400 |
| Tokens per minute | 14,000 |
| Models available | All models |
| Cost | FREE |
| Model | Speed | Quality | Context Window |
|---|---|---|---|
llama-3.3-70b-versatile |
Fast | Excellent | 128K tokens |
llama-3.1-8b-instant |
Very Fast | Good | 128K tokens |
mixtral-8x7b-32768 |
Fast | Excellent | 32K tokens |
gemma2-9b-it |
Fast | Good | 8K tokens |
Recommendation: Use llama-3.3-70b-versatile for best quality
- Run the application
- Send test messages:
- "Hello, who are you?"
- "What can you help me with?"
- "Write a Python function to add two numbers"
- Verify conversation context:
- Ask follow-up questions
- Check if bot remembers previous messages
- Test error handling:
- Disconnect internet
- Try invalid input
- Test UI features:
- Send with Enter key
- Clear chat button
- Scroll behavior
Create a test file TestConnection.java:
public class TestConnection {
public static void main(String[] args) {
System.out.println("Testing Groq API connection...");
OpenAIService service = new OpenAIService();
String response = service.sendMessage("Say 'Connection successful!' if you can read this.");
System.out.println("Response: " + response);
if (response.contains("Connection successful")) {
System.out.println("✅ API connection working!");
} else {
System.out.println("❌ API connection failed!");
}
}
}Run with:
javac -cp "target/classes:target/dependency/*" TestConnection.java
java -cp ".:target/classes:target/dependency/*" TestConnectionPotential features to add:
- Save/Load Conversations - Export chat history to files
- Dark Mode - Toggle between light and dark themes
- Voice Input - Speech-to-text using Java Speech API
- Text-to-Speech - AI reads responses aloud
- Multiple Tabs - Multiple conversation threads
- Code Highlighting - Syntax highlighting for code in responses
- Export to PDF - Save conversations as PDF
- Image Support - Send and receive images
- Streaming Responses - Show AI typing word-by-word
- Custom Themes - User-selectable color schemes
- Emoji Picker - Built-in emoji selector
- Search History - Search through past conversations
- Pin Messages - Mark important messages
- Multi-language - Support for different languages
Contributions are welcome! Here's how you can help:
- Check if the bug is already reported in Issues
- Create a new issue with:
- Clear title and description
- Steps to reproduce
- Expected vs actual behavior
- Screenshots if applicable
- Your environment (OS, Java version, Maven version)
- Open an issue with the
enhancementlabel - Describe the feature and its benefits
- Provide examples or mockups if possible
- Fork the repository
- Create a feature branch:
git checkout -b feature/AmazingFeature
- Make your changes
- Test thoroughly
- Commit with clear messages:
git commit -m "feat: Add amazing feature" - Push to your fork:
git push origin feature/AmazingFeature
- Open a Pull Request
# Clone your fork
git clone https://github.com/YOUR_USERNAME/java_chatbot.git
cd java_chatbot
# Create config.properties
cp config.properties.example config.properties
# Add your API key
# Install dependencies
mvn clean install
# Run tests (when available)
mvn test
# Run the application
mvn exec:java- Use 4 spaces for indentation
- Follow Java naming conventions
- Add comments for complex logic
- Keep methods focused and small
- Write descriptive variable names
This project is licensed under the MIT License.
MIT License
Copyright (c) 2024 Kalana
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Kalana
- GitHub: @kalana250
- Project: java_chatbot
- Groq - For providing free, ultra-fast AI API access
- Meta AI - For the LLaMA language models
- OpenAI - For establishing the chat completion API standard
- Java Swing Community - For excellent GUI framework documentation
- Maven Community - For the robust build tool
Need help? Here are your options:
-
📖 Check Documentation
- Read this README thoroughly
- Check the Troubleshooting section
-
🐛 Report Bugs
- Open an issue
- Include error messages and steps to reproduce
-
💬 Ask Questions
- Start a discussion
- Share your use case or question
-
🌐 Community Resources
- Groq Discord: https://discord.gg/groq
- Groq Documentation: https://console.groq.com/docs
If this project helped you, please give it a ⭐ on GitHub!
- ✨ Initial release
- 🤖 Groq API integration with LLaMA 3.3
- 🎨 Modern Swing GUI with chat bubbles
- 💬 Conversation history management
- 🧹 Clear chat functionality
- 🔐 Secure configuration file system
- 📖 Comprehensive documentation
# Setup
git clone https://github.com/kalana250/java_chatbot.git
cd java_chatbot
cp config.properties.example config.properties
# Edit config.properties with your API key
# Build
mvn clean compile
# Run
mvn exec:java
# Package
mvn package
# Run packaged JAR
java -jar target/ai-chatbot-1.0-SNAPSHOT.jar
# Clean
mvn clean
# Test (when tests are added)
mvn test- Groq Console: https://console.groq.com/
- Groq Documentation: https://console.groq.com/docs
- Java Documentation: https://docs.oracle.com/en/java/
- Maven Documentation: https://maven.apache.org/guides/
- Swing Tutorial: https://docs.oracle.com/javase/tutorial/uiswing/
- Java Swing Tutorial: https://www.javatpoint.com/java-swing
- Maven in 5 Minutes: https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
- Groq API Examples: https://console.groq.com/docs/examples
- LLaMA Model Info: https://ai.meta.com/llama/
- OpenAI Java Client: https://github.com/TheoKanning/openai-java
- LangChain4j: https://github.com/langchain4j/langchain4j
- Spring AI: https://spring.io/projects/spring-ai
// More detailed system message
systemMessage.put("content",
"You are an expert Java programming tutor. " +
"Explain concepts clearly with code examples. " +
"Format code blocks properly and use markdown.");// Use faster model
requestBody.put("model", "llama-3.1-8b-instant");
requestBody.put("max_tokens", 300);// Limit conversation history to last N messages
if (conversationHistory.size() > 20) {
// Keep system message + last 18 messages
conversationHistory = conversationHistory.subList(0, 1)
.addAll(conversationHistory.subList(conversationHistory.size() - 18));
}Have you built something cool with this chatbot? Let us know!
Share your projects:
- Open a discussion on GitHub
- Tag us on social media
- Submit a PR with your modifications
Made with ❤️ and ☕ using Java
Happy Coding! 🚀
# Quick setup in 4 steps:
# 1. Clone
git clone https://github.com/kalana250/java_chatbot.git
cd java_chatbot
# 2. Get free API key from https://console.groq.com/
# 3. Configure
cp config.properties.example config.properties
# Edit config.properties: groq.api.key=gsk_your_key
# 4. Run
mvn clean compile exec:java
# That's it! 🎉 Start chatting!Last Updated: 2026
Version: 1.0.0
Status: ✅ Active Development
Save this as `README.md` in your project root. This comprehensive README includes:
✅ Complete installation guide
✅ Detailed troubleshooting
✅ Security best practices
✅ Customization options
✅ API documentation
✅ Contributing guidelines
✅ Professional formatting
✅ All error solutions
✅ Tips and tricks
✅ Quick reference
✅ Badge support
You can now commit this to your new GitHub repository! 🚀