Skip to content

Repository files navigation

ChatGPT Java API

Maven CentralLicense

An unofficial, easy-to-use Java/Kotlin OpenAI API for ChatGPT, Assistants, and more!

Features

Installation

For Kotlin DSL (build.gradle.kts), add this to your dependencies block:

dependencies {
implementation("com.cjcrafter:openai:2.1.0")
}

For Maven projects, add this to your pom.xml file in the <dependencies> block:

<dependency>
<groupId>com.cjcrafter</groupId>
<artifactId>openai</artifactId>
<version>2.1.0</version>
</dependency>

See the maven repository for gradle/ant/etc.

Working Example

This is a simple working example of the ChatGPT API in Java:

importcom.cjcrafter.openai.OpenAI;
importcom.cjcrafter.openai.chat.ChatMessage;
importcom.cjcrafter.openai.chat.ChatRequest;
importcom.cjcrafter.openai.chat.ChatResponse;
importio.github.cdimascio.dotenv.Dotenv;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Scanner;
/** * In this Java example, we will be using the Chat API to create a simple chatbot. */publicclassChatCompletion {
publicstaticvoidmain(String[] args) {
// To use dotenv, you need to add the "io.github.cdimascio:dotenv-kotlin:version"// dependency. Then you can add a .env file in your project directory.Stringkey = Dotenv.load().get("OPENAI_TOKEN");
OpenAIopenai = OpenAI.builder()
.apiKey(key)
.build();
List<ChatMessage> messages = newArrayList<>();
messages.add(ChatMessage.toSystemMessage("Help the user with their problem."));
// Here you can change the model's settings, add tools, and more.ChatRequestrequest = ChatRequest.builder()
.model("gpt-3.5-turbo")
.messages(messages)
.build();
Scannerscan = newScanner(System.in);
while (true) {
System.out.println("What are you having trouble with?");
Stringinput = scan.nextLine();
messages.add(ChatMessage.toUserMessage(input));
ChatResponseresponse = openai.createChatCompletion(request);
System.out.println("Generating Response...");
System.out.println(response.get(0).getMessage().getContent());
// Make sure to add the response to the messages list!messages.add(response.get(0).getMessage());
}
}
}

For more examples, check out examples.

Note: OpenAI recommends using environment variables for your API token (Read more).

Logging

We use SLF4J for logging. To enable logging, add a logging implementation to your project. If you encounter an issue with the JSON parsing, we will ask that you enable logging and send us the logs.

Adding a logging implementation:

implementation("ch.qos.logback:logback-classic:$version")

Add a logback.xml file to your resources folder:

<configuration>
<appendername="FILE"class="ch.qos.logback.core.FileAppender">
<file>debug.log</file>
<append>false</append>
<encoder>
<pattern>%date %level [%thread] %logger{10} %msg%n</pattern>
</encoder>
</appender>
<loggername="com.cjcrafter.openai"level="DEBUG"/> <!-- Change to OFF to disable our logging -->
<rootlevel="DEBUG">
<appender-refref="FILE"/>
</root>
</configuration>

Support

If I have saved you time, please consider sponsoring me.

License

ChatGPT-Java-API is an open-sourced software licensed under the MIT License. This is an unofficial library, and is not affiliated with OpenAI.

Releases

Sponsor this project

Used by

Contributors

Languages