forked from TheoKanning/openai-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new service module (TheoKanning#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
- Loading branch information
1 parent
32b4c98
commit be915d6
Showing
24 changed files
with
848 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
api/src/main/java/com/theokanning/openai/OpenAiHttpException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,5 @@ application { | |
} | ||
|
||
dependencies { | ||
implementation project(":client") | ||
implementation project(":service") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
28 changes: 28 additions & 0 deletions
28
service/src/main/java/com/theokanning/openai/service/AuthenticationInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.