Skip to content

Commit

Permalink
Add new service module (TheoKanning#100)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
TheoKanning authored Feb 13, 2023
1 parent 32b4c98 commit be915d6
Show file tree
Hide file tree
Showing 24 changed files with 848 additions and 4 deletions.
39 changes: 39 additions & 0 deletions api/src/main/java/com/theokanning/openai/OpenAiError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.theokanning.openai;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* Represents the error body when an OpenAI request fails
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class OpenAiError {

public OpenAiErrorDetails error;

@Data
@NoArgsConstructor
@AllArgsConstructor
public static class OpenAiErrorDetails {
/**
* Human-readable error message
*/
String message;

/**
* OpenAI error type, for example "invalid_request_error"
* https://platform.openai.com/docs/guides/error-codes/python-library-error-types
*/
String type;

String param;

/**
* OpenAI error code, for example "invalid_api_key"
*/
String code;
}
}
32 changes: 32 additions & 0 deletions api/src/main/java/com/theokanning/openai/OpenAiHttpException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.theokanning.openai;

public class OpenAiHttpException extends RuntimeException {

/**
* HTTP status code
*/
public final int statusCode;

/**
* OpenAI error code, for example "invalid_api_key"
*/
public final String code;


public final String param;

/**
* OpenAI error type, for example "invalid_request_error"
* https://platform.openai.com/docs/guides/error-codes/python-library-error-types
*/
public final String type;

public OpenAiHttpException(OpenAiError error, Exception parent, int statusCode) {
super(error.error.message, parent);
// todo error.error looks dumb
this.statusCode = statusCode;
this.code = error.error.code;
this.param = error.error.param;
this.type = error.error.type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@

import static java.time.Duration.ofSeconds;

/**
* Use the OpenAiService from the new 'service' library. See README for more details.
*/
@Deprecated
public class OpenAiService {

private static final String BASE_URL = "https://api.openai.com/";
Expand Down
3 changes: 1 addition & 2 deletions client/src/test/java/com/theokanning/openai/EditTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ void edit() {
.instruction("Fix the spelling mistakes")
.build();

EditResult result = service.createEdit( request);

EditResult result = service.createEdit(request);
assertNotNull(result.getChoices().get(0).getText());
}

Expand Down
2 changes: 1 addition & 1 deletion example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ application {
}

dependencies {
implementation project(":client")
implementation project(":service")
}
3 changes: 2 additions & 1 deletion example/src/main/java/example/OpenAiApiExample.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package example;

import com.theokanning.openai.OpenAiService;
import com.theokanning.openai.service.OpenAiService;
import com.theokanning.openai.completion.CompletionRequest;
import com.theokanning.openai.image.CreateImageRequest;

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

Expand Down
21 changes: 21 additions & 0 deletions service/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apply plugin: 'java-library'
apply plugin: "com.vanniktech.maven.publish"

dependencies {
api project(":client")
api 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.9.0'
implementation 'com.squareup.retrofit2:converter-jackson:2.9.0'

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

compileJava {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
}

test {
useJUnitPlatform()
}
3 changes: 3 additions & 0 deletions service/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
POM_ARTIFACT_ID=service
POM_NAME=service
POM_DESCRIPTION=Basic service to create and use a GPT-3 retrofit client
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.theokanning.openai.service;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

/**
* OkHttp Interceptor that adds an authorization token header
*/
public class AuthenticationInterceptor implements Interceptor {

private final String token;

AuthenticationInterceptor(String token) {
this.token = token;
}

@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request()
.newBuilder()
.header("Authorization", "Bearer " + token)
.build();
return chain.proceed(request);
}
}
Loading

0 comments on commit be915d6

Please sign in to comment.