This project demonstrates how to build a ChatGPT-like AI assistant using Java 17, OpenAI API, and a clean, modular approach. It provides a simple way to interact with OpenAI's GPT models through a Java application with both a console interface and a graphical user interface (GUI) with text-to-speech capabilities.
- Connect to OpenAI's GPT API using Java 17
- Send single questions or maintain conversation context
- Secure API key management (environment variables or properties file)
- Clean, modular code structure
- Simple console-based example application
- Graphical user interface (GUI) using Java Swing
- Text-to-speech functionality using FreeTTS
- Model selection (GPT-3.5-turbo, GPT-4)
- System message customization
├── src/main/java/com/chatgpt/clone/
│ ├── config/
│ │ └── OpenAIConfig.java # Configuration for API key and URL
│ ├── model/
│ │ ├── Message.java # Message model for API requests
│ │ ├── ChatCompletionRequest.java # Request model
│ │ └── ChatCompletionResponse.java # Response model
│ ├── service/
│ │ └── GPTService.java # Main service for API communication
│ ├── ui/
│ │ ├── ChatGPTUI.java # Graphical user interface
│ │ ├── VoiceManager.java # Text-to-speech functionality
│ │ └── ChatGPTApp.java # Main application launcher
│ ├── example/
│ │ └── GPTExample.java # Console example usage
│ ├── util/
│ │ ├── Logger.java # Logging utility
│ │ └── OpenAIUtil.java # OpenAI utilities
│ └── exception/
│ └── OpenAIException.java # Custom exception handling
├── config.properties.example # Example configuration file
├── run-ui.bat # Batch file to run the UI
└── pom.xml # Maven dependencies
- Java 17 or higher
- Maven
- OpenAI API key
- Clone this repository
- Configure your OpenAI API key using one of these methods:
- Environment Variable: Set
OPENAI_API_KEYenvironment variable- Windows:
set OPENAI_API_KEY=your_api_key_here - Linux/macOS:
export OPENAI_API_KEY=your_api_key_here
- Windows:
- Properties File:
- Copy
config.properties.exampletoconfig.properties - Replace
YOUR_API_KEY_HEREwith your actual OpenAI API key
- Copy
- Environment Variable: Set
- Build the project:
mvn clean install
// Create a GPTService instance
GPTService gptService = new GPTService();
// Ask a simple question
String response = gptService.askQuestion("What is the capital of France?");
System.out.println(response);// Create a list to hold the conversation
List<Message> conversation = new ArrayList<>();
// Add a system message to set the context
conversation.add(Message.systemMessage("You are a helpful assistant."));
// Add a user message
conversation.add(Message.userMessage("What is the capital of France?"));
// Get the response
String response = gptService.sendConversation(conversation);
// Add the assistant's response to the conversation
Message assistantMessage = Message.builder()
.role("assistant")
.content(response)
.build();
conversation.add(assistantMessage);
// Continue the conversation
conversation.add(Message.userMessage("What is its population?"));
response = gptService.sendConversation(conversation);
System.out.println(response);The project includes a console example application that demonstrates both simple questions and interactive conversations:
mvn exec:java -Dexec.mainClass="com.chatgpt.clone.example.GPTExample"
To run the graphical user interface with text-to-speech capabilities:
mvn exec:java -Dexec.mainClass="com.chatgpt.clone.ui.ChatGPTApp"
Or simply run the provided batch file:
run-ui.bat
The UI provides the following features:
- Chat with GPT models (GPT-3.5-turbo or GPT-4)
- Customize system messages
- Enable/disable text-to-speech
- Clear conversation history
- Visual display of conversation
You can specify which GPT model to use:
// Use GPT-4 instead of the default GPT-3.5-turbo
String response = gptService.askQuestion("What is quantum computing?", "gpt-4");You can customize the API URL if needed:
// Create a custom configuration
OpenAIConfig config = new OpenAIConfig("https://your-custom-endpoint.com/v1/chat/completions");
GPTService gptService = new GPTService(config);This project is licensed under the MIT License - see the LICENSE file for details.
- OpenAI API Documentation
- OkHttp for HTTP requests
- Jackson for JSON processing
Saurabh Kushwaha
🔗 Portfolio
📧 [email protected]
🔗 LinkedIn
🔗 Instagram Dev Page
This project is licensed under the MIT License.
