Start a Ghost Writing Career for Halloween with OpenAI's GPT-3 Engine and Java

October 28, 2021
Written by
Diane Phan
Reviewed by

header - Start a Ghost Writing Career for Halloween with OpenAI's GPT-3 Engine and Java

Illustration by Caroline.

It can be tempting to stay inside with a cup of hot cocoa to read as the days get chillier. Why not challenge yourself by writing some code along with a new story this fall season?

In this article, you will learn how to navigate a Java IDE to build a ghost writing application in Java with the OpenAI GPT-3 engine.

Tutorial Requirements

Start a new Java project in IntelliJ IDEA

Open IntelliJ IDEA and click on Create New Project.

Choose Gradle on the left hand side, check Java in the box on the right hand side, and click Next.

choose gradle option for new java project

Give your project a name such as "GhostWriter" and click the Finish button.

After the project setup is complete, your project directory structure should look like the following image:

project directory for the build gradle file

Open the build.gradle file in the IDE and add the following lines inside the dependencies block:

    implementation 'com.theokanning.openai-gpt3-java:client:0.4.0'
    implementation group: "org.slf4j", name: "slf4j-simple", version: "1.7.32"

Build your ghost writer story app  

In the spirit of Halloween and ghosts, we'll have the prompts used in this article be related to a scary story.

The OpenAI playground allows users to explore GPT-3 (Generative Pre-trained Transformer 3), a highly advanced language model that is capable of generating written text that sounds like an actual human wrote it. This powerful model can also read a user's input and learn about the context of the prompt to determine how it should generate a response.

In this project, we will be feeding the GPT-3 engine with a sentence to create a complete scary story that will keep running on your WhatsApp device.

Create the Ghost Writer app's Java class

Expand the main subfolder under the src folder at the root of the project directory. Notice the empty java folder.

project directory for the java subfolder

Right click on the Java folder and click on the New option, then select Java Class as seen in the image below. Clicking this option will prompt you to give the class a name. Go ahead and name it "GhostWriter".

Create a new Java class in Intellij IDEA

Delete the existing template code in the newly created file and then copy and paste the following code:

import java.util.*;
import java.util.function.Supplier;
import java.util.ArrayList;
import com.theokanning.openai.OpenAiService;
import com.theokanning.openai.completion.CompletionRequest;
import com.theokanning.openai.engine.Engine;
import com.theokanning.openai.completion.CompletionChoice;

public class GhostWriter {
    public static void main(String... args) {
        String token = System.getenv("OPENAI_TOKEN");
        OpenAiService service = new OpenAiService(token);

It is important to know that the Java class' filename must reflect the class name within the file.

In order to use OpenAI in Java projects, developers must incorporate TheoKanning's openai-java community library. This library allows us to utilize the CompletionRequest and CompletionChoice classes to generate an appropriate response from the engine.

In the public GhostWriter class, a main method is invoked so that the code in the program starts once executed.

As seen above, a token variable retrieves the OPENAI_TOKEN that holds the API key configured in the project's environment variables. The token allows the Java project to create an OpenAiService object where all the magic happens. This token will be set in the upcoming section.

Set the OpenAI API Key in IntelliJ IDEA

As mentioned above, this project requires an API token from OpenAI. During the time of this article, the only way to obtain the API key is by being accepted into their private beta program.

If you have access to the Beta page, the API key can be found in the Authentication tab in the Documentation.

OpenAI Beta Documentation Authentication page with API key

The Java application will need to have access to this key, so environment variables need to be set to safely make a connection to the OpenAI GPT-3 engine.

Locate the Run tab at the top of the IntelliJ IDEA console and select Edit Configurations… in the dropdown as seen below:

screenshot to edit configurations for the java application

Another window displaying the "Run/Debug Configurations" will pop up with the details regarding the project.

In the Environment variables section, follow the sample format to set the relevant variables. In this example, the API key is named OPENAI_TOKEN.

add the OpenAI Token environment variable for the GhostWriter application

Click on the Apply button, and then OK once finished.

Establish a connection to the GPT-3 engine

There are different engines to choose from when working with GPT-3. In this article, the davinci model is used because it is the most capable model, but feel free to explore other GPT-3 models for your project.

After creating the OpenAiService object, copy and paste the highlighted code to the GhostWriter class:

public class GhostWriter {
    public static void main(String... args) {
        String token = System.getenv("OPENAI_TOKEN");
        OpenAiService service = new OpenAiService(token);
        Engine davinci = service.getEngine("davinci");
        ArrayList<CompletionChoice> storyArray = new ArrayList<CompletionChoice>();
        // …

The generated text results are returned in an ArrayList format consisting of values from TheoKanning's CompletionChoice class. Storing results in this ArrayList object is encouraged so that you have the option to expand on the story later on if desired.

Teach the ghost writer app how to write

In order to make a request to the OpenAI GPT-3 engine that is relevant to our interests, a prompt and various arguments must be supplied to teach the engine, or ghost writer, how to return the desired results.

Copy and paste the following code right beneath the ArrayList definition to create an instance of TheoKanning's CompletionRequest class:

        System.out.println("\nBrewing up a story...");
        CompletionRequest completionRequest = CompletionRequest.builder()
                .prompt("The following is a spooky story written for kids, just in time for Halloween. Everyone always talks about the old house at the end of the street, but I couldn't believe what happened when I went inside.")
        service.createCompletion("davinci", completionRequest).getChoices().forEach(line -> {storyArray.add(line);});

If you played around with the OpenAI playground introduced earlier in the article, you may notice that some of the arguments here look familiar.

After setting the value for prompt, it is recommended for the builder function to specify values, such as the temperature, to lessen the randomness in results. Another value that should be specified is maxTokens, which stands for either a word or punctuation mark to be generated. This was set to 96 so that the length of the caption will be appropriate - not too long and not too short to provide a more cohesive idea on what you can start writing about.

You can read more about the GPT-3 customization options in the Ultimate Guide to OpenAI-GPT3 Language Model or explore the OpenAI Playground for yourself.

Before the build method is executed, the echo function is added to echo back the prompt in addition to the newly created tokens.

Each line generated by the OpenAI service object is appended to the ArrayList defined earlier and printed onto the console.

Run the ghost writer Java app

We've nearly reached the end of the tutorial. If you need to check your code, here's my GitHub repo.

Right click on the GhostWriter file in the project directory and find the option to Run 'GhostWriter.main()'. Wait a few seconds for the project to build and compile. When running, the Run window in the IntelliJ IDEA console should look something like this:

4:33:24 PM: Executing task ':GhostWriter.main()'...

> Task :compileJava
> Task :processResources NO-SOURCE
> Task :classes

> Task :GhostWriter.main()

Brewing up a story...
The story array = [CompletionChoice(text=The following is a spooky story written for kids, just in time for Halloween. Everyone always talks about the old house at the end of the street, but I couldn't believe what happened when I went inside. I got up early one morning to watch the sunrise. When it was finally time, I left the house and walked up the street. It was dark outside, so I had to be careful where I stepped. As I approached the old house, I saw that the door was slightly open. I pulled on the doorknob and pushed the door open.

I was shocked when I saw what was inside. It was dark and dreary, with cobwebs hanging, index=0, logprobs=null, finish_reason=length)]

And just like that, your little ghost writer is ready to help you start some new spooky stories to fill the cold nights ahead! Go ahead and stop the app, then rerun it and see what other results you get.

Keep in mind that you can't control what your ghost writer decides to write so it's up to you whether you want to read what they have to say.

What’s next for OpenAI GPT-3 projects?

If you're eager to build more, try expanding on the project by feeding the generated results to the Ghost writer app so that it keeps adding onto the story for you. Otherwise, check out these ideas and incorporate Twilio:

Let me know how you are using code to be creative this holiday season by reaching out to me over email!

Diane Phan is a software developer on the Developer Voices team. She loves to help programmers tackle difficult challenges that might prevent them from bringing their projects to life. She can be reached at dphan [at] or LinkedIn.