Skip to content
This repository was archived by the owner on Jun 6, 2024. It is now read-only.

Commit be915d6

Browse files
authored
Add new service module (#100)
* Add new service module This contains a new OpenAiService, and the old service is now deprecated. Moving this to a separate library will allow people to use the retrofit client without adding too many transitive dependencies. Now reads error bodies and includes them in exceptions * Update example to use new service
1 parent 32b4c98 commit be915d6

File tree

24 files changed

+848
-4
lines changed

24 files changed

+848
-4
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.theokanning.openai;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
/**
8+
* Represents the error body when an OpenAI request fails
9+
*/
10+
@Data
11+
@NoArgsConstructor
12+
@AllArgsConstructor
13+
public class OpenAiError {
14+
15+
public OpenAiErrorDetails error;
16+
17+
@Data
18+
@NoArgsConstructor
19+
@AllArgsConstructor
20+
public static class OpenAiErrorDetails {
21+
/**
22+
* Human-readable error message
23+
*/
24+
String message;
25+
26+
/**
27+
* OpenAI error type, for example "invalid_request_error"
28+
* https://platform.openai.com/docs/guides/error-codes/python-library-error-types
29+
*/
30+
String type;
31+
32+
String param;
33+
34+
/**
35+
* OpenAI error code, for example "invalid_api_key"
36+
*/
37+
String code;
38+
}
39+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.theokanning.openai;
2+
3+
public class OpenAiHttpException extends RuntimeException {
4+
5+
/**
6+
* HTTP status code
7+
*/
8+
public final int statusCode;
9+
10+
/**
11+
* OpenAI error code, for example "invalid_api_key"
12+
*/
13+
public final String code;
14+
15+
16+
public final String param;
17+
18+
/**
19+
* OpenAI error type, for example "invalid_request_error"
20+
* https://platform.openai.com/docs/guides/error-codes/python-library-error-types
21+
*/
22+
public final String type;
23+
24+
public OpenAiHttpException(OpenAiError error, Exception parent, int statusCode) {
25+
super(error.error.message, parent);
26+
// todo error.error looks dumb
27+
this.statusCode = statusCode;
28+
this.code = error.error.code;
29+
this.param = error.error.param;
30+
this.type = error.error.type;
31+
}
32+
}

client/src/main/java/com/theokanning/openai/OpenAiService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030

3131
import static java.time.Duration.ofSeconds;
3232

33+
/**
34+
* Use the OpenAiService from the new 'service' library. See README for more details.
35+
*/
36+
@Deprecated
3337
public class OpenAiService {
3438

3539
private static final String BASE_URL = "https://api.openai.com/";

client/src/test/java/com/theokanning/openai/EditTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ void edit() {
1919
.instruction("Fix the spelling mistakes")
2020
.build();
2121

22-
EditResult result = service.createEdit( request);
23-
22+
EditResult result = service.createEdit(request);
2423
assertNotNull(result.getChoices().get(0).getText());
2524
}
2625

example/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ application {
66
}
77

88
dependencies {
9-
implementation project(":client")
9+
implementation project(":service")
1010
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package example;
22

3-
import com.theokanning.openai.OpenAiService;
3+
import com.theokanning.openai.service.OpenAiService;
44
import com.theokanning.openai.completion.CompletionRequest;
55
import com.theokanning.openai.image.CreateImageRequest;
66

@@ -15,6 +15,7 @@ public static void main(String... args) {
1515
.prompt("Somebody once told me the world is gonna roll me")
1616
.echo(true)
1717
.user("testing")
18+
.n(3)
1819
.build();
1920
service.createCompletion(completionRequest).getChoices().forEach(System.out::println);
2021

service/build.gradle

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
apply plugin: 'java-library'
2+
apply plugin: "com.vanniktech.maven.publish"
3+
4+
dependencies {
5+
api project(":client")
6+
api 'com.squareup.retrofit2:retrofit:2.9.0'
7+
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.9.0'
8+
implementation 'com.squareup.retrofit2:converter-jackson:2.9.0'
9+
10+
testImplementation(platform('org.junit:junit-bom:5.8.2'))
11+
testImplementation('org.junit.jupiter:junit-jupiter')
12+
}
13+
14+
compileJava {
15+
sourceCompatibility = '1.8'
16+
targetCompatibility = '1.8'
17+
}
18+
19+
test {
20+
useJUnitPlatform()
21+
}

service/gradle.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
POM_ARTIFACT_ID=service
2+
POM_NAME=service
3+
POM_DESCRIPTION=Basic service to create and use a GPT-3 retrofit client
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.theokanning.openai.service;
2+
3+
import okhttp3.Interceptor;
4+
import okhttp3.Request;
5+
import okhttp3.Response;
6+
7+
import java.io.IOException;
8+
9+
/**
10+
* OkHttp Interceptor that adds an authorization token header
11+
*/
12+
public class AuthenticationInterceptor implements Interceptor {
13+
14+
private final String token;
15+
16+
AuthenticationInterceptor(String token) {
17+
this.token = token;
18+
}
19+
20+
@Override
21+
public Response intercept(Chain chain) throws IOException {
22+
Request request = chain.request()
23+
.newBuilder()
24+
.header("Authorization", "Bearer " + token)
25+
.build();
26+
return chain.proceed(request);
27+
}
28+
}

0 commit comments

Comments
 (0)