Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/cjcrafter/chatgpt-java-api

Unofficial Java API supporting Chat, Assistants, and more!
https://github.com/cjcrafter/chatgpt-java-api

api assistant-chat-bots azure-openai chatbot chatgpt chatgpt-api chatgpt-bot chatgpt-java gpt gpt-35-turbo gpt-4 gpts java kotlin openai openai-api openai-java openai-kotlin

Last synced: 5 days ago
JSON representation

Unofficial Java API supporting Chat, Assistants, and more!

Awesome Lists containing this project

README

        

# ChatGPT Java API
[![Maven Central](https://img.shields.io/maven-central/v/com.cjcrafter/openai?color=blue&label=Download)](https://central.sonatype.com/artifact/com.cjcrafter/openai)
[![](https://img.shields.io/badge/-docs%20-blueviolet?logo=Kotlin&colorA=gray)](https://openai.cjcrafter.com/)
[![](https://img.shields.io/badge/-examples%20-orange?logo=Read+The+Docs&colorA=gray)](https://github.com/CJCrafter/ChatGPT-Java-API/tree/master/examples/src/main)
[![](https://img.shields.io/github/discussions/CJCrafter/ChatGPT-Java-API)](https://github.com/CJCrafter/ChatGPT-Java-API/discussions)
[![License](https://img.shields.io/github/license/CJCrafter/ChatGPT-Java-API)](https://github.com/CJCrafter/ChatGPT-Java-API/blob/master/LICENSE)

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

## Features
* [Completions](https://github.com/CJCrafter/ChatGPT-Java-API/blob/master/src/main/kotlin/com/cjcrafter/openai/OpenAI.kt#L44-L57)
* Streaming support via `OpenAI#streamCompletion`
* [Chat Completions](https://github.com/CJCrafter/ChatGPT-Java-API/blob/master/src/main/kotlin/com/cjcrafter/openai/OpenAI.kt#L84-L92)
* Streaming support via `OpenAI#streamChatCompletion`
* Functions support, check out the [java examples](https://github.com/CJCrafter/ChatGPT-Java-API/blob/master/examples/src/main/java/chat/StreamChatCompletionFunction.java#L49) and [kotlin examples](https://github.com/CJCrafter/ChatGPT-Java-API/blob/master/examples/src/main/kotlin/chat/StreamChatCompletionFunction.kt#L37)
* [Azure Support](https://github.com/CJCrafter/ChatGPT-Java-API/blob/master/src/main/kotlin/com/cjcrafter/openai/OpenAI.kt#L271-L276)
* [Embeddings](https://github.com/CJCrafter/ChatGPT-Java-API/blob/master/src/main/kotlin/com/cjcrafter/openai/OpenAI.kt#L113-L123) (New!)
* [Files](https://github.com/CJCrafter/ChatGPT-Java-API/blob/master/src/main/kotlin/com/cjcrafter/openai/files/FileHandler.kt) (New!)
* [Assistants](https://github.com/CJCrafter/ChatGPT-Java-API/blob/master/src/main/kotlin/com/cjcrafter/openai/assistants/AssistantHandler.kt) (New!)
* Which includes [Threads](), [Runs](), and everything else you'll need to run your assistants!

## Installation
For Kotlin DSL (`build.gradle.kts`), add this to your dependencies block:
```kotlin
dependencies {
implementation("com.cjcrafter:openai:2.1.0")
}
```
For Maven projects, add this to your `pom.xml` file in the `` block:
```xml

com.cjcrafter
openai
2.1.0

```
See the [maven repository](https://central.sonatype.com/artifact/com.cjcrafter/openai/2.1.0) for gradle/ant/etc.

## Working Example
This is a simple working example of the ChatGPT API in Java:
```java

import com.cjcrafter.openai.OpenAI;
import com.cjcrafter.openai.chat.ChatMessage;
import com.cjcrafter.openai.chat.ChatRequest;
import com.cjcrafter.openai.chat.ChatResponse;
import io.github.cdimascio.dotenv.Dotenv;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
* In this Java example, we will be using the Chat API to create a simple chatbot.
*/
public class ChatCompletion {

public static void main(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.
String key = Dotenv.load().get("OPENAI_TOKEN");
OpenAI openai = OpenAI.builder()
.apiKey(key)
.build();

List messages = new ArrayList<>();
messages.add(ChatMessage.toSystemMessage("Help the user with their problem."));

// Here you can change the model's settings, add tools, and more.
ChatRequest request = ChatRequest.builder()
.model("gpt-3.5-turbo")
.messages(messages)
.build();

Scanner scan = new Scanner(System.in);
while (true) {
System.out.println("What are you having trouble with?");
String input = scan.nextLine();

messages.add(ChatMessage.toUserMessage(input));
ChatResponse response = 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](https://github.com/CJCrafter/ChatGPT-Java-API/tree/master/examples/src/main).

> **Note**: OpenAI recommends using environment variables for your API token
([Read more](https://help.openai.com/en/articles/5112595-best-practices-for-api-key-safety)).

## Logging
We use [SLF4J](http://www.slf4j.org/) 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:
```kotlin
implementation("ch.qos.logback:logback-classic:$version")
```

Add a `logback.xml` file to your resources folder:
```xml


debug.log
false

%date %level [%thread] %logger{10} %msg%n



```

## Support
If I have saved you time, please consider [sponsoring me](https://github.com/sponsors/CJCrafter).

## License
ChatGPT-Java-API is an open-sourced software licensed under the [MIT License](https://github.com/CJCrafter/ChatGPT-Java-API/blob/master/LICENSE).
**This is an unofficial library, and is not affiliated with OpenAI**.