Skip to content

Commit 43e58d8

Browse files
authored
Merge pull request #1 from launchableinc/fix-some-config
fix indent and version
2 parents e7de81c + 32c9c44 commit 43e58d8

File tree

265 files changed

+7933
-7466
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

265 files changed

+7933
-7466
lines changed

.github/workflows/test.yml

+12-14
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,19 @@ name: Test
33
on:
44
push:
55
branches: [ main ]
6-
6+
pull_request:
7+
branches: [ main ]
78
jobs:
89
test:
910
runs-on: ubuntu-latest
10-
1111
steps:
12-
- uses: actions/checkout@v3
13-
14-
- name: Set up JDK 1.8
15-
uses: actions/setup-java@v3
16-
with:
17-
distribution: temurin
18-
java-version: 8
19-
20-
- name: Test
21-
run: ./gradlew test --stacktrace
22-
env:
23-
OPENAI_TOKEN: ${{ secrets.OPENAI_TOKEN }}
12+
- uses: actions/checkout@v4
13+
- name: Set up JDK 1.8
14+
uses: actions/setup-java@v4
15+
with:
16+
distribution: temurin
17+
java-version: 8
18+
- name: Test
19+
run: ./gradlew test --stacktrace
20+
#env:
21+
# OPENAI_TOKEN: ${{ secrets.OPENAI_TOKEN }}

README.md

+129-52
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
![Maven Central](https://img.shields.io/maven-central/v/com.theokanning.openai-gpt3-java/client?color=blue)
22

3-
> ⚠️OpenAI has deprecated all Engine-based APIs. See [Deprecated Endpoints](https://github.com/TheoKanning/openai-java#deprecated-endpoints) below for more info.
3+
> ⚠️OpenAI has deprecated all Engine-based APIs.
4+
> See [Deprecated Endpoints](https://github.com/TheoKanning/openai-java#deprecated-endpoints) below
5+
> for more info.
46
57
# OpenAI-Java
8+
69
Java libraries for using OpenAI's GPT apis. Supports GPT-3, ChatGPT, and GPT-4.
710

811
Includes the following artifacts:
12+
913
- `api` : request/response POJOs for the GPT APIs.
1014
- `client` : a basic retrofit client for the GPT endpoints, includes the `api` module
11-
- `service` : A basic service class that creates and calls the client. This is the easiest way to get started.
15+
- `service` : A basic service class that creates and calls the client. This is the easiest way to
16+
get started.
1217

1318
as well as an example project using the service.
1419

1520
## Supported APIs
21+
1622
- [Models](https://platform.openai.com/docs/api-reference/models)
1723
- [Completions](https://platform.openai.com/docs/api-reference/completions)
1824
- [Chat Completions](https://platform.openai.com/docs/api-reference/chat/create)
@@ -26,81 +32,108 @@ as well as an example project using the service.
2632
- [Assistants](https://platform.openai.com/docs/api-reference/assistants)
2733

2834
#### Deprecated by OpenAI
35+
2936
- [Engines](https://platform.openai.com/docs/api-reference/engines)
3037
- [Legacy Fine-Tunes](https://platform.openai.com/docs/guides/legacy-fine-tuning)
3138

3239
## Importing
3340

3441
### Gradle
42+
3543
`implementation 'com.theokanning.openai-gpt3-java:<api|client|service>:<version>'`
3644

3745
### Maven
46+
3847
```xml
39-
<dependency>
40-
<groupId>com.theokanning.openai-gpt3-java</groupId>
41-
<artifactId>{api|client|service}</artifactId>
42-
<version>version</version>
43-
</dependency>
48+
49+
<dependency>
50+
<groupId>com.launchableinc.openai-gpt3-java</groupId>
51+
<artifactId>{api|client|service}</artifactId>
52+
<version>version</version>
53+
</dependency>
4454
```
4555

4656
## Usage
57+
4758
### Data classes only
59+
4860
If you want to make your own client, just import the POJOs from the `api` module.
4961
Your client will need to use snake case to work with the OpenAI API.
5062

5163
### Retrofit client
52-
If you're using retrofit, you can import the `client` module and use the [OpenAiApi](client/src/main/java/com/theokanning/openai/OpenAiApi.java).
53-
You'll have to add your auth token as a header (see [AuthenticationInterceptor](client/src/main/java/com/theokanning/openai/AuthenticationInterceptor.java))
64+
65+
If you're using retrofit, you can import the `client` module and use
66+
the [OpenAiApi](client/src/main/java/com/theokanning/openai/OpenAiApi.java).
67+
You'll have to add your auth token as a header (
68+
see [AuthenticationInterceptor](client/src/main/java/com/theokanning/openai/AuthenticationInterceptor.java))
5469
and set your converter factory to use snake case and only include non-null fields.
5570

5671
### OpenAiService
57-
If you're looking for the fastest solution, import the `service` module and use [OpenAiService](service/src/main/java/com/theokanning/openai/service/OpenAiService.java).
5872

59-
> ⚠️The OpenAiService in the client module is deprecated, please switch to the new version in the service module.
73+
If you're looking for the fastest solution, import the `service` module and
74+
use [OpenAiService](service/src/main/java/com/theokanning/openai/service/OpenAiService.java).
75+
76+
> ⚠️The OpenAiService in the client module is deprecated, please switch to the new version in the
77+
> service module.
78+
6079
```java
6180
OpenAiService service = new OpenAiService("your_token");
6281
CompletionRequest completionRequest = CompletionRequest.builder()
63-
.prompt("Somebody once told me the world is gonna roll me")
64-
.model("babbage-002"")
65-
.echo(true)
66-
.build();
67-
service.createCompletion(completionRequest).getChoices().forEach(System.out::println);
82+
.prompt("Somebody once told me the world is gonna roll me")
83+
.model("babbage-002"")
84+
.echo(true)
85+
.build();
86+
service.
87+
88+
createCompletion(completionRequest).
89+
90+
getChoices().
91+
92+
forEach(System.out::println);
6893
```
6994
7095
### Customizing OpenAiService
71-
If you need to customize OpenAiService, create your own Retrofit client and pass it in to the constructor.
96+
97+
If you need to customize OpenAiService, create your own Retrofit client and pass it in to the
98+
constructor.
7299
For example, do the following to add request logging (after adding the logging gradle dependency):
73100
74101
```java
75102
ObjectMapper mapper = defaultObjectMapper();
76103
OkHttpClient client = defaultClient(token, timeout)
77-
.newBuilder()
78-
.interceptor(HttpLoggingInterceptor())
79-
.build();
104+
.newBuilder()
105+
.interceptor(HttpLoggingInterceptor())
106+
.build();
80107
Retrofit retrofit = defaultRetrofit(client, mapper);
81108
82109
OpenAiApi api = retrofit.create(OpenAiApi.class);
83110
OpenAiService service = new OpenAiService(api);
84111
```
85112
86113
### Adding a Proxy
114+
87115
To use a proxy, modify the OkHttp client as shown below:
116+
88117
```java
89118
ObjectMapper mapper = defaultObjectMapper();
90119
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port));
91120
OkHttpClient client = defaultClient(token, timeout)
92-
.newBuilder()
93-
.proxy(proxy)
94-
.build();
121+
.newBuilder()
122+
.proxy(proxy)
123+
.build();
95124
Retrofit retrofit = defaultRetrofit(client, mapper);
96125
OpenAiApi api = retrofit.create(OpenAiApi.class);
97126
OpenAiService service = new OpenAiService(api);
98127
```
99128
100129
### Functions
101-
You can create your functions and define their executors easily using the ChatFunction class, along with any of your custom classes that will serve to define their available parameters. You can also process the functions with ease, with the help of an executor called FunctionExecutor.
130+
131+
You can create your functions and define their executors easily using the ChatFunction class, along
132+
with any of your custom classes that will serve to define their available parameters. You can also
133+
process the functions with ease, with the help of an executor called FunctionExecutor.
102134
103135
First we declare our function parameters:
136+
104137
```java
105138
public class Weather {
106139
@JsonPropertyDescription("City and state, for example: León, Guanajuato")
@@ -122,7 +155,9 @@ public static class WeatherResponse {
122155
}
123156
```
124157
125-
Next, we declare the function itself and associate it with an executor, in this example we will fake a response from some API:
158+
Next, we declare the function itself and associate it with an executor, in this example we will fake
159+
a response from some API:
160+
126161
```java
127162
ChatFunction.builder()
128163
.name("get_weather")
@@ -131,82 +166,124 @@ ChatFunction.builder()
131166
.build()
132167
```
133168
134-
Then, we employ the FunctionExecutor object from the 'service' module to assist with execution and transformation into an object that is ready for the conversation:
169+
Then, we employ the FunctionExecutor object from the 'service' module to assist with execution and
170+
transformation into an object that is ready for the conversation:
171+
135172
```java
136173
List<ChatFunction> functionList = // list with functions
137-
FunctionExecutor functionExecutor = new FunctionExecutor(functionList);
174+
FunctionExecutor
175+
functionExecutor =new
176+
177+
FunctionExecutor(functionList);
138178
139179
List<ChatMessage> messages = new ArrayList<>();
140-
ChatMessage userMessage = new ChatMessage(ChatMessageRole.USER.value(), "Tell me the weather in Barcelona.");
141-
messages.add(userMessage);
180+
ChatMessage userMessage = new ChatMessage(ChatMessageRole.USER.value(),
181+
"Tell me the weather in Barcelona.");
182+
messages.
183+
184+
add(userMessage);
185+
142186
ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest
143-
.builder()
144-
.model("gpt-3.5-turbo-0613")
145-
.messages(messages)
146-
.functions(functionExecutor.getFunctions())
147-
.functionCall(new ChatCompletionRequestFunctionCall("auto"))
148-
.maxTokens(256)
149-
.build();
150-
151-
ChatMessage responseMessage = service.createChatCompletion(chatCompletionRequest).getChoices().get(0).getMessage();
187+
.builder()
188+
.model("gpt-3.5-turbo-0613")
189+
.messages(messages)
190+
.functions(functionExecutor.getFunctions())
191+
.functionCall(new ChatCompletionRequestFunctionCall("auto"))
192+
.maxTokens(256)
193+
.build();
194+
195+
ChatMessage responseMessage = service.createChatCompletion(chatCompletionRequest).getChoices()
196+
.get(0).getMessage();
152197
ChatFunctionCall functionCall = responseMessage.getFunctionCall(); // might be null, but in this case it is certainly a call to our 'get_weather' function.
153198
154-
ChatMessage functionResponseMessage = functionExecutor.executeAndConvertToMessageHandlingExceptions(functionCall);
155-
messages.add(response);
199+
ChatMessage functionResponseMessage = functionExecutor.executeAndConvertToMessageHandlingExceptions(
200+
functionCall);
201+
messages.
202+
203+
add(response);
156204
```
205+
157206
> **Note:** The `FunctionExecutor` class is part of the 'service' module.
158207
159-
You can also create your own function executor. The return object of `ChatFunctionCall.getArguments()` is a JsonNode for simplicity and should be able to help you with that.
208+
You can also create your own function executor. The return object
209+
of `ChatFunctionCall.getArguments()` is a JsonNode for simplicity and should be able to help you
210+
with that.
160211
161-
For a more in-depth look, refer to a conversational example that employs functions in: [OpenAiApiFunctionsExample.java](example/src/main/java/example/OpenAiApiFunctionsExample.java).
162-
Or for an example using functions and stream: [OpenAiApiFunctionsWithStreamExample.java](example/src/main/java/example/OpenAiApiFunctionsWithStreamExample.java)
212+
For a more in-depth look, refer to a conversational example that employs functions
213+
in: [OpenAiApiFunctionsExample.java](example/src/main/java/example/OpenAiApiFunctionsExample.java).
214+
Or for an example using functions and
215+
stream: [OpenAiApiFunctionsWithStreamExample.java](example/src/main/java/example/OpenAiApiFunctionsWithStreamExample.java)
163216
164217
### Streaming thread shutdown
165-
If you want to shut down your process immediately after streaming responses, call `OpenAiService.shutdownExecutor()`.
218+
219+
If you want to shut down your process immediately after streaming responses,
220+
call `OpenAiService.shutdownExecutor()`.
166221
This is not necessary for non-streaming calls.
167222
168223
## Running the example project
169-
All the [example](example/src/main/java/example/OpenAiApiExample.java) project requires is your OpenAI api token
224+
225+
All the [example](example/src/main/java/example/OpenAiApiExample.java) project requires is your
226+
OpenAI api token
227+
170228
```bash
171229
export OPENAI_TOKEN="sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
172230
```
231+
173232
You can try all the capabilities of this project using:
233+
174234
```bash
175235
./gradlew runExampleOne
176236
```
237+
177238
And you can also try the new capability of using functions:
239+
178240
```bash
179241
./gradlew runExampleTwo
180242
```
243+
181244
Or functions with 'stream' mode enabled:
245+
182246
```bash
183247
./gradlew runExampleThree
184248
```
185249
186250
## FAQ
251+
187252
### Does this support GPT-4?
188-
Yes! GPT-4 uses the ChatCompletion Api, and you can see the latest model options [here](https://platform.openai.com/docs/models/gpt-4).
189-
GPT-4 is currently in a limited beta (as of 4/1/23), so make sure you have access before trying to use it.
253+
254+
Yes! GPT-4 uses the ChatCompletion Api, and you can see the latest model
255+
options [here](https://platform.openai.com/docs/models/gpt-4).
256+
GPT-4 is currently in a limited beta (as of 4/1/23), so make sure you have access before trying to
257+
use it.
190258
191259
### Does this support functions?
260+
192261
Absolutely! It is very easy to use your own functions without worrying about doing the dirty work.
193-
As mentioned above, you can refer to [OpenAiApiFunctionsExample.java](example/src/main/java/example/OpenAiApiFunctionsExample.java) or
194-
[OpenAiApiFunctionsWithStreamExample.java](example/src/main/java/example/OpenAiApiFunctionsWithStreamExample.java) projects for an example.
262+
As mentioned above, you can refer
263+
to [OpenAiApiFunctionsExample.java](example/src/main/java/example/OpenAiApiFunctionsExample.java) or
264+
[OpenAiApiFunctionsWithStreamExample.java](example/src/main/java/example/OpenAiApiFunctionsWithStreamExample.java)
265+
projects for an example.
195266
196267
### Why am I getting connection timeouts?
268+
197269
Make sure that OpenAI is available in your country.
198270
199271
### Why doesn't OpenAiService support x configuration option?
272+
200273
Many projects use OpenAiService, and in order to support them best I've kept it extremely simple.
201274
You can create your own OpenAiApi instance to customize headers, timeouts, base urls etc.
202-
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`
275+
If you want features like retry logic and async calls, you'll have to make an `OpenAiApi` instance
276+
and call it directly instead of using `OpenAiService`
203277
204278
## Deprecated Endpoints
205-
OpenAI has deprecated engine-based endpoints in favor of model-based endpoints.
206-
For example, instead of using `v1/engines/{engine_id}/completions`, switch to `v1/completions` and specify the model in the `CompletionRequest`.
279+
280+
OpenAI has deprecated engine-based endpoints in favor of model-based endpoints.
281+
For example, instead of using `v1/engines/{engine_id}/completions`, switch to `v1/completions` and
282+
specify the model in the `CompletionRequest`.
207283
The code includes upgrade instructions for all deprecated endpoints.
208284
209285
I won't remove the old endpoints from this library until OpenAI shuts them down.
210286
211287
## License
288+
212289
Published under the MIT License

api/build.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ dependencies {
77
api libs.jtokkit
88
compileOnly libs.lombok
99
annotationProcessor libs.lombok
10+
testCompileOnly libs.lombok // Ensure Lombok is available for test compilation
11+
testAnnotationProcessor libs.lombok // Process Lombok annotations in test code
1012

1113
testImplementation libs.jacksonDatabind
1214
testImplementation(platform(libs.junitBom))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.launchableinc.openai;
2+
3+
import lombok.Data;
4+
5+
/**
6+
* A response when deleting an object
7+
*/
8+
@Data
9+
public class DeleteResult {
10+
11+
/**
12+
* The id of the object.
13+
*/
14+
String id;
15+
16+
/**
17+
* The type of object deleted, for example "file" or "model"
18+
*/
19+
String object;
20+
21+
/**
22+
* True if successfully deleted
23+
*/
24+
boolean deleted;
25+
}

0 commit comments

Comments
 (0)