Add OpenAiService and OpenAiApiExample

Added the ability to get all available engines, and added a example class to test it.
This commit is contained in:
Theo Kanning 2020-09-14 19:22:46 -05:00
parent fe6372f376
commit b38f8681b1
12 changed files with 146 additions and 40 deletions

View File

@ -0,0 +1,4 @@
```
export OPENAI_TOKEN="sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
./gradlew example:run
```

View File

@ -1,29 +1,5 @@
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java Library project to get you started.
* For more details take a look at the Java Libraries chapter in the Gradle
* User Manual available at https://docs.gradle.org/6.5/userguide/java_library_plugin.html
*/
plugins {
// Apply the java-library plugin to add support for Java Library
id 'java-library'
}
repositories {
// Use jcenter for resolving dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:29.0-jre'
// Use JUnit test framework
testImplementation 'junit:junit:4.13'
}
allprojects {
repositories {
jcenter()
}
}

10
example/build.gradle Normal file
View File

@ -0,0 +1,10 @@
apply plugin: 'java'
apply plugin: 'application'
application {
mainClassName = 'example.OpenAiApiExample'
}
dependencies {
implementation project(":openai")
}

View File

@ -0,0 +1,14 @@
package example;
import openai.OpenAiService;
import openai.engine.Engine;
class OpenAiApiExample {
public static void main(String... args) {
String token = System.getenv("OPENAI_TOKEN");
OpenAiService service = new OpenAiService(token);
System.out.println("Getting available engines");
service.getEngines().forEach((Engine e) -> System.out.println(e.id));
}
}

9
openai/build.gradle Normal file
View File

@ -0,0 +1,9 @@
apply plugin: 'java-library'
dependencies {
api 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.5.0'
implementation 'com.squareup.retrofit2:converter-jackson:2.5.0'
testImplementation 'junit:junit:4.13'
}

View File

@ -0,0 +1,28 @@
package openai;
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);
}
}

View File

@ -0,0 +1,11 @@
package openai;
import io.reactivex.Single;
import openai.engine.Engine;
import retrofit2.http.GET;
public interface OpenAiApi {
@GET("v1/engines")
Single<OpenAiResponse<Engine>> getEngines();
}

View File

@ -0,0 +1,8 @@
package openai;
import java.util.List;
public class OpenAiResponse<T> {
public List<T> data;
public String object;
}

View File

@ -0,0 +1,45 @@
package openai;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import openai.engine.Engine;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.jackson.JacksonConverterFactory;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class OpenAiService {
OpenAiApi api;
public OpenAiService(String token) {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new AuthenticationInterceptor(token))
.connectionPool(new ConnectionPool(5, 1, TimeUnit.SECONDS))
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.openai.com/")
.client(client)
.addConverterFactory(JacksonConverterFactory.create(mapper))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
api = retrofit.create(OpenAiApi.class);
}
public List<Engine> getEngines() {
return api.getEngines().blockingGet().data;
}
}

View File

@ -0,0 +1,8 @@
package openai.engine;
public class Engine {
public String id;
public String object;
public String owner;
public boolean ready;
}

View File

@ -1,10 +1,5 @@
/*
* This file was generated by the Gradle 'init' task.
*
* The settings file is used to specify which projects to include in your build.
*
* Detailed information about configuring a multi-project build in Gradle can be found
* in the user manual at https://docs.gradle.org/6.5/userguide/multi_project_builds.html
*/
rootProject.name = 'openai-java'
include ':openai'
include ':example'

View File

@ -1,2 +0,0 @@
public interface OpenAiApi {
}