Why Does a Java Developer Need ChatGPT? Or How to Stop Fearing and Love Neural Networks
\nHello, colleague! If you're reading this, it means you, like me, are tired of writing cookie-cutter support responses, generating boring product descriptions, or manually analyzing reviews. What if I told you that in just one evening you can breathe life into your Java application by connecting real artificial intelligence to it? Yes, I mean that very ChatGPT that can tell jokes, write code, and even explain the Pythagorean theorem to a cat.
\nIn this article, we won't be analyzing probability theory or teaching transformers. We'll simply take Java, add a pinch of HTTP requests, and make GPT-4 (or 3.5 if the budget is tight) work for us. Sounds like magic? It's actually pure engineering. Let's go!
\n\nWhat Do We Need? A Minimal Gentleman's Kit
\nBefore writing code, make sure you have:
\n- \n
- JDK 11 or higher (we'll use the new
HttpClientthat appeared in version 11); \n - OpenAI API key — you can get it on the OpenAI platform. Don't forget to top up your balance (literally a couple of dollars is enough for testing); \n
- Any IDE — IntelliJ IDEA, Eclipse, or even a notepad if you're hardcore; \n
- Maven or Gradle — for convenient dependency management (but we'll keep it minimal). \n
By the way, if you think that integrating ChatGPT in Java is something complex requiring machine learning, I'm happy to reassure you: it's all solved with a simple HTTP request. No neural networks at runtime, just REST API.
\n\nProject Setup: Maven and Dependencies
\nCreate a new Maven project and add just one library to pom.xml — Jackson for working with JSON. You can't do without it because ChatGPT communicates with the world exclusively in this language.
<dependency>\n <groupId>com.fasterxml.jackson.core</groupId>\n <artifactId>jackson-databind</artifactId>\n <version>2.15.2</version>\n</dependency>\n\nThat's it! No SDKs, no wrappers — just pure Java and our ingenuity. If you use Gradle, simply add implementation 'com.fasterxml.jackson.core:jackson-databind:2.15.2'.
Request Structure for the OpenAI API
\nBefore writing code, let's understand what the server expects from us. The ChatGPT API accepts a POST request to the endpoint https://api.openai.com/v1/chat/completions. The request body is a JSON with the following structure:
{\n "model": "gpt-3.5-turbo",\n "messages": [\n {"role": "system", "content": "You are a helpful assistant."},\n {"role": "user", "content": "Tell me a joke about Java developers."}\n ],\n "temperature": 0.7\n}\n\nLet's break down the fields:\n
- \n
- model — which model responds. For cost savings, use
gpt-3.5-turbo; for impressive answers, usegpt-4; \n - messages — an array of messages. The system message sets the tone, and user is our request; \n
- temperature — from 0 to 2. The higher it is, the more creative the response (and the greater the chance the neural network will start hallucinating). \n
The response also comes in JSON, and we are interested in the choices[0].message.content field. That's where the text we will display to the user is hidden.
Writing the Code: Java Client for ChatGPT
\nLet's create a class ChatGPTClient. It will be responsible for sending requests and processing responses. All the code is in one file to avoid creating unnecessary entities.
import java.net.URI;\nimport java.net.http.HttpClient;\nimport java.net.http.HttpRequest;\nimport java.net.http.HttpRes