Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1625713

Browse filesBrowse files
authored
Merge pull request #1 from TheoKanning/main
update from main
2 parents 7dc5b5b + 7141e4c commit 1625713
Copy full SHA for 1625713
Expand file treeCollapse file tree

29 files changed

+318
-248
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+19-4Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
> ⚠️OpenAI has deprecated all Engine-based APIs. See [Deprecated Endpoints](https://github.com/TheoKanning/openai-java#deprecated-endpoints) below for more info.
55
66
# OpenAI-Java
7-
Java libraries for using OpenAI's GPT-3 api.
7+
Java libraries for using OpenAI's GPT apis. Supports GPT-3, ChatGPT, and GPT-4.
88

99
Includes the following artifacts:
10-
- `api` : request/response POJOs for the GPT-3 APIs.
11-
- `client` : a basic retrofit client for the GPT-3 endpoints, includes the `api` module
10+
- `api` : request/response POJOs for the GPT APIs.
11+
- `client` : a basic retrofit client for the GPT endpoints, includes the `api` module
1212
- `service` : A basic service class that creates and calls the client. This is the easiest way to get started.
1313

1414
as well as an example project using the service.
@@ -95,7 +95,9 @@ OpenAiApi api = retrofit.create(OpenAiApi.class);
9595
OpenAiService service = new OpenAiService(api);
9696
```
9797

98-
98+
### Streaming thread shutdown
99+
If you want to shut down your process immediately after streaming responses, call `OpenAiService.shutdown()`.
100+
This is not necessary for non-streaming calls.
99101

100102
## Running the example project
101103
All the [example](example/src/main/java/example/OpenAiApiExample.java) project requires is your OpenAI api token
@@ -104,6 +106,19 @@ export OPENAI_TOKEN="sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
104106
./gradlew example:run
105107
```
106108

109+
## FAQ
110+
### Does this support GPT-4?
111+
Yes! GPT-4 uses the ChatCompletion Api, and you can see the latest model options [here](https://platform.openai.com/docs/models/gpt-4).
112+
GPT-4 is currently in a limited beta (as of 4/1/23), so make sure you have access before trying to use it.
113+
114+
### Why am I getting connection timeouts?
115+
Make sure that OpenAI is available in your country.
116+
117+
### Why doesn't OpenAiService support x configuration option?
118+
Many projects use OpenAiService, and in order to support them best I've kept it extremely simple.
119+
You can create your own OpenAiApi instance to customize headers, timeouts, base urls etc.
120+
If you want features like retry logic and async calls, you'll have to make an `OpenAiApi` instance and call it directly instead of using `OpenAiService`
121+
107122
## Deprecated Endpoints
108123
OpenAI has deprecated engine-based endpoints in favor of model-based endpoints.
109124
For example, instead of using `v1/engines/{engine_id}/completions`, switch to `v1/completions` and specify the model in the `CompletionRequest`.

‎api/gradle.properties

Copy file name to clipboard
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
POM_ARTIFACT_ID=api
22
POM_NAME=api
3-
POM_DESCRIPTION=Basic java objects for the OpenAI GPT-3 API
3+
POM_DESCRIPTION=Basic java objects for the OpenAI GPT APIs

‎api/src/main/java/com/theokanning/openai/completion/CompletionChoice.java

Copy file name to clipboardExpand all lines: api/src/main/java/com/theokanning/openai/completion/CompletionChoice.java
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import lombok.Data;
44

55
/**
6-
* A completion generated by GPT-3
6+
* A completion generated by OpenAI
77
*
88
* https://beta.openai.com/docs/api-reference/completions/create
99
*/
@@ -25,7 +25,7 @@ public class CompletionChoice {
2525
LogProbResult logprobs;
2626

2727
/**
28-
* The reason why GPT-3 stopped generating, for example "length".
28+
* The reason why GPT stopped generating, for example "length".
2929
*/
3030
String finish_reason;
3131
}

‎api/src/main/java/com/theokanning/openai/completion/CompletionChunk.java

Copy file name to clipboardExpand all lines: api/src/main/java/com/theokanning/openai/completion/CompletionChunk.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class CompletionChunk {
2626
long created;
2727

2828
/**
29-
* The GPT-3 model used.
29+
* The model used.
3030
*/
3131
String model;
3232

‎api/src/main/java/com/theokanning/openai/completion/CompletionResult.java

Copy file name to clipboardExpand all lines: api/src/main/java/com/theokanning/openai/completion/CompletionResult.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class CompletionResult {
2929
long created;
3030

3131
/**
32-
* The GPT-3 model used.
32+
* The GPT model used.
3333
*/
3434
String model;
3535

‎api/src/main/java/com/theokanning/openai/completion/chat/ChatCompletionChoice.java

Copy file name to clipboardExpand all lines: api/src/main/java/com/theokanning/openai/completion/chat/ChatCompletionChoice.java
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import lombok.Data;
55

66
/**
7-
* A chat completion generated by GPT-3.5
7+
* A chat completion generated by OpenAI
88
*/
99
@Data
1010
public class ChatCompletionChoice {
@@ -21,7 +21,7 @@ public class ChatCompletionChoice {
2121
ChatMessage message;
2222

2323
/**
24-
* The reason why GPT-3 stopped generating, for example "length".
24+
* The reason why GPT stopped generating, for example "length".
2525
*/
2626
@JsonProperty("finish_reason")
2727
String finishReason;

‎api/src/main/java/com/theokanning/openai/completion/chat/ChatCompletionChunk.java

Copy file name to clipboardExpand all lines: api/src/main/java/com/theokanning/openai/completion/chat/ChatCompletionChunk.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class ChatCompletionChunk {
2424
long created;
2525

2626
/**
27-
* The GPT-3.5 model used.
27+
* The model used.
2828
*/
2929
String model;
3030

‎api/src/main/java/com/theokanning/openai/completion/chat/ChatCompletionRequest.java

Copy file name to clipboardExpand all lines: api/src/main/java/com/theokanning/openai/completion/chat/ChatCompletionRequest.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
public class ChatCompletionRequest {
1717

1818
/**
19-
* ID of the model to use. Currently, only gpt-3.5-turbo and gpt-3.5-turbo-0301 are supported.
19+
* ID of the model to use.
2020
*/
2121
String model;
2222

‎api/src/main/java/com/theokanning/openai/completion/chat/ChatCompletionResult.java

Copy file name to clipboardExpand all lines: api/src/main/java/com/theokanning/openai/completion/chat/ChatCompletionResult.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class ChatCompletionResult {
2626
long created;
2727

2828
/**
29-
* The GPT-3.5 model used.
29+
* The GPT model used.
3030
*/
3131
String model;
3232

‎api/src/main/java/com/theokanning/openai/edit/EditChoice.java

Copy file name to clipboardExpand all lines: api/src/main/java/com/theokanning/openai/edit/EditChoice.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import lombok.Data;
44

55
/**
6-
* An edit generated by GPT-3
6+
* An edit generated by OpenAi
77
*
88
* https://beta.openai.com/docs/api-reference/edits/create
99
*/

‎api/src/main/java/com/theokanning/openai/edit/EditResult.java

Copy file name to clipboardExpand all lines: api/src/main/java/com/theokanning/openai/edit/EditResult.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import java.util.List;
77

88
/**
9-
* A list of edits generated by GPT-3
9+
* A list of edits generated by OpenAI
1010
*
1111
* https://beta.openai.com/docs/api-reference/edits/create
1212
*/

‎api/src/main/java/com/theokanning/openai/embedding/EmbeddingResult.java

Copy file name to clipboardExpand all lines: api/src/main/java/com/theokanning/openai/embedding/EmbeddingResult.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
public class EmbeddingResult {
1515

1616
/**
17-
* The GPT-3 model used for generating embeddings
17+
* The GPTmodel used for generating embeddings
1818
*/
1919
String model;
2020

‎api/src/main/java/com/theokanning/openai/model/Model.java

Copy file name to clipboardExpand all lines: api/src/main/java/com/theokanning/openai/model/Model.java
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import java.util.List;
77

88
/**
9-
* GPT-3 model details
9+
* GPT model details
1010
*
1111
* https://beta.openai.com/docs/api-reference/models
1212
*/
@@ -23,7 +23,7 @@ public class Model {
2323
public String object;
2424

2525
/**
26-
* The owner of the GPT-3 model, typically "openai"
26+
* The owner of the model, typically "openai"
2727
*/
2828
@JsonProperty("owned_by")
2929
public String ownedBy;

‎api/src/main/java/com/theokanning/openai/model/Permission.java

Copy file name to clipboardExpand all lines: api/src/main/java/com/theokanning/openai/model/Permission.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import lombok.Data;
55

66
/**
7-
* GPT-3 model permissions
7+
* Model permissions
88
* I couldn't find documentation for the specific permissions, and I've elected to leave them undocumented rather than
99
* write something incorrect.
1010
*

‎api/src/main/java/com/theokanning/openai/moderation/ModerationResult.java

Copy file name to clipboardExpand all lines: api/src/main/java/com/theokanning/openai/moderation/ModerationResult.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class ModerationResult {
1717
public String id;
1818

1919
/**
20-
* The GPT-3 model used.
20+
* The model used.
2121
*/
2222
public String model;
2323

‎client/build.gradle

Copy file name to clipboardExpand all lines: client/build.gradle
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ apply plugin: "com.vanniktech.maven.publish"
44
dependencies {
55
api project(":api")
66
api 'com.squareup.retrofit2:retrofit:2.9.0'
7-
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.9.0'
7+
api 'com.squareup.retrofit2:adapter-rxjava2:2.9.0'
88
implementation 'com.squareup.retrofit2:converter-jackson:2.9.0'
99

1010
testImplementation(platform('org.junit:junit-bom:5.8.2'))

‎client/gradle.properties

Copy file name to clipboard
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
POM_ARTIFACT_ID=client
22
POM_NAME=client
3-
POM_DESCRIPTION=Basic retrofit client for OpenAI's GPT-3 API
3+
POM_DESCRIPTION=Basic retrofit client for OpenAI's GPT APIs

‎example/src/main/java/example/OpenAiApiExample.java

Copy file name to clipboardExpand all lines: example/src/main/java/example/OpenAiApiExample.java
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
package example;
22

3+
import com.theokanning.openai.completion.chat.ChatCompletionRequest;
4+
import com.theokanning.openai.completion.chat.ChatMessage;
5+
import com.theokanning.openai.completion.chat.ChatMessageRole;
36
import com.theokanning.openai.service.OpenAiService;
47
import com.theokanning.openai.completion.CompletionRequest;
58
import com.theokanning.openai.image.CreateImageRequest;
69

10+
import java.util.ArrayList;
11+
import java.util.HashMap;
12+
import java.util.List;
13+
714
class OpenAiApiExample {
815
public static void main(String... args) {
916
String token = System.getenv("OPENAI_TOKEN");
@@ -26,5 +33,24 @@ public static void main(String... args) {
2633

2734
System.out.println("\nImage is located at:");
2835
System.out.println(service.createImage(request).getData().get(0).getUrl());
36+
37+
System.out.println("Streaming chat completion...");
38+
final List<ChatMessage> messages = new ArrayList<>();
39+
final ChatMessage systemMessage = new ChatMessage(ChatMessageRole.SYSTEM.value(), "You are a dog and will speak as such.");
40+
messages.add(systemMessage);
41+
ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest
42+
.builder()
43+
.model("gpt-3.5-turbo")
44+
.messages(messages)
45+
.n(1)
46+
.maxTokens(50)
47+
.logitBias(new HashMap<>())
48+
.build();
49+
50+
service.streamChatCompletion(chatCompletionRequest)
51+
.doOnError(Throwable::printStackTrace)
52+
.blockingForEach(System.out::println);
53+
54+
service.shutdownExecutor();
2955
}
3056
}

‎example/src/main/java/example/OpenAiApiStreamExample.java

Copy file name to clipboardExpand all lines: example/src/main/java/example/OpenAiApiStreamExample.java
-79Lines changed: 0 additions & 79 deletions
This file was deleted.

‎gradle.properties

Copy file name to clipboardExpand all lines: gradle.properties
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
GROUP=com.theokanning.openai-gpt3-java
2-
VERSION_NAME=0.11.1
2+
VERSION_NAME=0.12.0
33

44
POM_URL=https://github.com/theokanning/openai-java
55
POM_SCM_URL=https://github.com/theokanning/openai-java

‎service/build.gradle

Copy file name to clipboardExpand all lines: service/build.gradle
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ dependencies {
88
implementation 'com.squareup.retrofit2:converter-jackson:2.9.0'
99

1010
testImplementation(platform('org.junit:junit-bom:5.8.2'))
11-
testImplementation('org.junit.jupiter:junit-jupiter')
11+
testImplementation 'org.junit.jupiter:junit-jupiter'
12+
testImplementation 'com.squareup.retrofit2:retrofit-mock:2.9.0'
1213
}
1314

1415
compileJava {

‎service/gradle.properties

Copy file name to clipboard
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
POM_ARTIFACT_ID=service
22
POM_NAME=service
3-
POM_DESCRIPTION=Basic service to create and use a GPT-3 retrofit client
3+
POM_DESCRIPTION=Basic service to create and use an OpenAI retrofit client

‎service/src/main/java/com/theokanning/openai/service/AuthenticationInterceptor.java

Copy file name to clipboardExpand all lines: service/src/main/java/com/theokanning/openai/service/AuthenticationInterceptor.java
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import okhttp3.Response;
66

77
import java.io.IOException;
8+
import java.util.Objects;
89

910
/**
1011
* OkHttp Interceptor that adds an authorization token header
@@ -14,6 +15,7 @@ public class AuthenticationInterceptor implements Interceptor {
1415
private final String token;
1516

1617
AuthenticationInterceptor(String token) {
18+
Objects.requireNonNull(token, "OpenAI token required");
1719
this.token = token;
1820
}
1921

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.