Skip to content

kalana250/java_chatbot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

# 🤖 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.

![Java](https://img.shields.io/badge/Java-11+-orange.svg)
![Maven](https://img.shields.io/badge/Maven-3.6+-blue.svg)
![Groq](https://img.shields.io/badge/AI-Groq-purple.svg)
![License](https://img.shields.io/badge/License-MIT-green.svg)

## ✨ 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

Installation

1. Clone the Repository

git clone https://github.com/kalana250/java_chatbot.git
cd java_chatbot

2. Get Your FREE Groq API Key

  1. Visit https://console.groq.com/
  2. Sign up for a free account (GitHub login available)
  3. Go to "API Keys" section
  4. Click "Create API Key"
  5. Give it a name (e.g., "Java Chatbot")
  6. Copy your API key (starts with gsk_)

⚠️ IMPORTANT: Save this key! You won't be able to see it again.

3. Configure API Key

Create the configuration file:

# Copy the example file
cp config.properties.example config.properties

# On Windows:
copy config.properties.example config.properties

Edit config.properties and add your key:

groq.api.key=gsk_your_actual_key_here

Replace gsk_your_actual_key_here with your actual Groq API key.

4. Build and Run

# Install dependencies and compile
mvn clean compile

# Run the application
mvn exec:java

The chatbot window should appear! 🎉


📁 Project Structure

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 Descriptions

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

🎮 Usage

Running the Application

Method 1: Maven (Recommended)

mvn clean compile exec:java

Method 2: IntelliJ IDEA

  1. Open project in IntelliJ IDEA
  2. Wait for Maven to import dependencies
  3. Right-click on ChatbotUI.java
  4. Select "Run 'ChatbotUI.main()'"

Method 3: Eclipse

  1. Import as "Existing Maven Projects"
  2. Right-click project → "Run As""Java Application"
  3. Select ChatbotUI as 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

Using the Chatbot

  1. Type your message in the text field at the bottom
  2. Send by clicking "Send" button or pressing Enter
  3. Wait for AI response (usually 1-3 seconds)
  4. Continue chatting - the bot remembers context!
  5. Clear conversation using the "Clear Chat" button

Example Conversations

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...

⚙️ Configuration & Customization

Change AI Model

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");

Adjust Response Length

// 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)

Modify Creativity Level

// 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, unpredictable

Customize Bot Personality

Edit 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.");

UI Customization

Change Colors

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

Change Window Size

// 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

Change Fonts

// 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;"

🐛 Troubleshooting

❌ "Error loading config.properties"

Problem: Application can't find or read the configuration file.

Solutions:

  1. Make sure config.properties exists in project root directory
  2. Verify it contains: groq.api.key=your-key-here
  3. Check file location:
    # Should be here:
    C:\IdeaProjects\java_chatbot\config.properties
  4. Verify file encoding is UTF-8
  5. Make sure there are no spaces around the = sign

❌ "Incorrect API key provided"

Problem: Invalid or malformed API key.

Solutions:

  1. Check your key starts with gsk_
  2. 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
  3. Get a new key from https://console.groq.com/keys
  4. Make sure you copied the entire key

❌ "JSONObject['choices'] not found"

Problem: API returned an error instead of a response.

Solutions:

  1. Check your internet connection
  2. Verify API key is correct
  3. Check Groq service status: https://status.groq.com/
  4. Look at console output for actual error message
  5. Try again - might be temporary API issue

❌ Maven build fails

# 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 install

❌ "Could not find or load main class ChatbotUI"

Solutions:

  1. Make sure you're in the project root directory
  2. Run Maven compile first:
    mvn clean compile
  3. Check Java version:
    java -version  # Should be 11 or higher
  4. In IntelliJ: File → Invalidate Caches → Invalidate and Restart

❌ GUI window doesn't appear

Windows:

  • Make sure Java JDK (not JRE) is installed
  • Check JAVA_HOME environment variable

Mac:

# Install Java if needed
brew install openjdk@11

Linux:

# Install Java and GUI libraries
sudo apt-get update
sudo apt-get install openjdk-11-jdk
export DISPLAY=:0

⚠️ Slow responses

Solutions:

  1. Switch to faster model in OpenAIService.java:
    requestBody.put("model", "llama-3.1-8b-instant");
  2. Reduce max_tokens to 200-300
  3. Check your internet speed
  4. Check Groq API status

❌ "Push declined due to repository rule violations"

Problem: You accidentally committed your API key to Git.

Solutions:

  1. NEVER commit config.properties
  2. Make sure .gitignore includes config.properties
  3. Use config.properties.example instead (without real key)
  4. If already committed, see Security Best Practices

🔐 Security Best Practices

✅ DO:

  • ✅ Use config.properties for API keys
  • ✅ Add config.properties to .gitignore
  • ✅ Commit config.properties.example (template only)
  • ✅ Revoke API keys if accidentally exposed
  • ✅ Use different keys for different projects
  • ✅ Keep your API keys private

❌ DON'T:

  • ❌ Hardcode API keys in source code
  • ❌ Commit config.properties to Git
  • ❌ Share API keys in public repositories
  • ❌ Share your config.properties file
  • ❌ Use production keys for testing
  • ❌ Reuse exposed API keys

If You Accidentally Expose Your Key:

  1. Immediately revoke it:

  2. Create a new key:

    • Generate new API key
    • Update config.properties
  3. 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"

📦 Dependencies

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 framework
  • java.awt.* - Graphics and layout
  • java.io.* - File I/O for config loading
  • java.util.* - Data structures

🔄 Alternative AI APIs

Switch to OpenAI GPT (Paid)

  1. Get API key from https://platform.openai.com/api-keys
  2. 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
  1. Update config.properties:
openai.api.key=sk-proj-your-openai-key

Cost: ~$0.002 per 1K tokens (very cheap)

Switch to Google Gemini (Free Tier)

  1. Get API key from https://ai.google.dev/
  2. Use Google's Java SDK or REST API
  3. Update endpoint and request format

Switch to Anthropic Claude

  1. Get key from https://console.anthropic.com/
  2. Update OpenAIService.java:
private static final String API_URL = "https://api.anthropic.com/v1/messages";
// Update request headers and format for Claude API

📚 API Reference

Groq API Limits (Free Tier)

Limit Type Value
Requests per minute 30
Requests per day 14,400
Tokens per minute 14,000
Models available All models
Cost FREE

Available Groq Models

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


🧪 Testing

Manual Testing

  1. Run the application
  2. Send test messages:
    • "Hello, who are you?"
    • "What can you help me with?"
    • "Write a Python function to add two numbers"
  3. Verify conversation context:
    • Ask follow-up questions
    • Check if bot remembers previous messages
  4. Test error handling:
    • Disconnect internet
    • Try invalid input
  5. Test UI features:
    • Send with Enter key
    • Clear chat button
    • Scroll behavior

API Connection Test

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/*" TestConnection

🚀 Advanced Features (Future Enhancements)

Potential 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

🤝 Contributing

Contributions are welcome! Here's how you can help:

Reporting Bugs

  1. Check if the bug is already reported in Issues
  2. 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)

Suggesting Features

  1. Open an issue with the enhancement label
  2. Describe the feature and its benefits
  3. Provide examples or mockups if possible

Pull Requests

  1. Fork the repository
  2. Create a feature branch:
    git checkout -b feature/AmazingFeature
  3. Make your changes
  4. Test thoroughly
  5. Commit with clear messages:
    git commit -m "feat: Add amazing feature"
  6. Push to your fork:
    git push origin feature/AmazingFeature
  7. Open a Pull Request

Development Setup

# 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

Code Style

  • Use 4 spaces for indentation
  • Follow Java naming conventions
  • Add comments for complex logic
  • Keep methods focused and small
  • Write descriptive variable names

📄 License

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.

👨‍💻 Author

Kalana


🙏 Acknowledgments

  • 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

📞 Support

Need help? Here are your options:

  1. 📖 Check Documentation

  2. 🐛 Report Bugs

  3. 💬 Ask Questions

  4. 🌐 Community Resources


📊 Project Stats

GitHub repo size GitHub code size GitHub stars GitHub forks


⭐ Show Your Support

If this project helped you, please give it a ⭐ on GitHub!

Star on GitHub


📝 Changelog

Version 1.0.0 (2024)

  • ✨ 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

🎯 Quick Command Reference

# 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

🔗 Useful Links


📖 Additional Resources

Learning Resources

Related Projects


💡 Tips & Tricks

Improve Response Quality

// 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.");

Faster Responses

// Use faster model
requestBody.put("model", "llama-3.1-8b-instant");
requestBody.put("max_tokens", 300);

Better Context Handling

// 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));
}

🎉 Success Stories

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! 🚀


⚡ TL;DR (Too Long; Didn't Read)

# 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! 🚀

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages