https://github.com/rainnny7/openai-java
A Java library to interact with the OpenAI API.
https://github.com/rainnny7/openai-java
api chatgpt java library maven openai
Last synced: about 2 months ago
JSON representation
A Java library to interact with the OpenAI API.
- Host: GitHub
- URL: https://github.com/rainnny7/openai-java
- Owner: Rainnny7
- Created: 2023-07-08T00:48:29.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2023-08-08T00:13:54.000Z (almost 3 years ago)
- Last Synced: 2025-10-31T11:38:04.661Z (8 months ago)
- Topics: api, chatgpt, java, library, maven, openai
- Language: Java
- Homepage: https://git.rainnny.club/Rainnny/OpenAI-Java
- Size: 24.4 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# OpenAI-Java
A Java library to interact with the OpenAI API.
## Summary
- [Dependency](#dependency)
- [Examples](#examples)
- [Environment Variables](#environment-variables)
- [Todo](#todo)
## Dependency
### Maven
```xml
rainnny-repo-public
https://maven.rainnny.club/public
me.braydon
OpenAI-Java
1.0.0
```
### Gradle
```
maven {
name = "rainnnyRepoPublic"
url = uri("https://maven.rainnny.club/public")
}
implementation("me.braydon:OpenAI-Java:1.0.0")
```
## Examples
### Create the client
```java
OpenAI openAi = new OpenAI(ApiCredentials.builder()
.apiKey("YOUR_API_KEY")
.organization("OPTIONAL_ORG")
.build());
```
### List Models
```java
for (Model model : openAi.models()) {
System.out.println(String.format("Model %s is owned by %s", model.getId(), model.getOwnedBy()));
// Model gpt-3.5-turbo is owned by openai
}
```
### Get Specific Model
```java
Model model = openAi.getModel(ModelEnum.GPT_3_5_TURBO);
System.out.println("Found " + model.getId());
// Found gpt-3.5-turbo
```
### Chat Completion
```java
ChatCompletion completion = openAi.chatCompletion(ModelEnum.GPT_3_5_TURBO,
new ChatMessage("user", "Do a dice roll")
);
System.out.println("Got " + completion.choiceCount() + " choice(s):");
for (ChatCompletion.Choice choice : completion) {
ChatMessage message = choice.getMessage();
System.out.println(String.format(" %s (%s): %s", choice.getIndex(), message.getRole(), message.getContent()));
}
/**
* Got 1 choice(s):
* 0 (assistant): Sure! Here's a dice roll for you:
* You rolled a 4.
*/
```
## Environment Variables
*Environment variables are required for running tests.*
| Variable | Description |
| :------------: | :---------------------------------: |
| `TEST_API_KEY` | **Required**. Your OpenAI API key |
## TODO
- [ ] SSE implementation for streaming completions
- [ ] A better interface approach for the API
- [ ] Support for other routes
- [ ] More in depth multithreading capabilities